Reputation: 77
Here is the link to my homework.
I just want help with the first problem for merge and will do the second part myself. I understand the first part of induction is proving the algorithm is correct for the smallest case(s), which is if X is empty and the other being if Y is empty, but I don't fully understand how to prove the second step of induction: showing merge is correct with input sizes k + 1.
I've done induction before on equations, never on an algorithm.
Thanks!
Upvotes: 1
Views: 15202
Reputation: 15141
First assumption: the merge routine you use merges two sorted arrays into a sorted array. Second assumption: the merge routine terminates
Now we need to prove the inductive step is correct.
Merge sort splits the array into two subarrays L = [1,n/2]
and R = [n/2 + 1, n]
. See that ceil(n/2)
is smaller than k
based on the facts above. By our inductive hypothesis both results of merge sort for L and R are correctly sorted (as they are within [1,k]
range). Furthermore from our assumption merge routine merges them into a sorted array which contains all elements because size(L) + size(R) = n
so this means that it correctly sorted an array of size n = k+1
.
@Edit: sorry, missread. For the merge part:
Here we will have a multidimensional induction.
Assumption: input arrays X,Y are already sorted
For the first induction step over X we have 2 cases, besides the base case:
X[1] ⊕ merge(tail(X), Y)
=> this is true as merge(k, size(Y))
is true according to our hypothesis over X and we are putting the smaller element at the front so we are keeping the orderY[1] ⊕ merge(X, tail(Y))
=> here we have two options:
size(tail(Y)) = 0
=> we hit a base case thus this case is proven to be correctsize(tail(Y)) > 0
=> we recurse further finally hitting the base case or merge(tail(X), subarray(Y))
where size(tail(X)) = k
=> is proven by our inductive hypothesisSimilarly for induction step over Y:
Y[1] ⊕ merge(X, tail(Y)) => this is true by our hypothesis
merge(size(X), l)` is true and we are putting the smaller element at the frontX[1] ⊕ merge(tail(X), Y)
=> here we have two options:
size(tail(X)) = 0
=> we hit a base case thus this case is proven to be correctsize(tail(X)) > 0
=> we recurse further finally hitting the base case or merge(subarray(X), tail(Y))
where size(tail(Y)) = l
=> is proven by our inductive hypothesisThe algorithm terminates as at each step we are making one of the arrays smaller by 1 element, thus one of them will eventually hit our base case
Upvotes: 6