Mergesort, Recurrences




CS146

Chris Pollett

Feb 10, 2014

Outline

Designing Algorithms

Divide-and-Conquer

Divide-and-Conquer -- MERGE-SORT

Pseudo-code for MERGE

The basic idea is that we image having two piles of items. We look at the top item in each pile and move the smaller to an output pile, continuing until all the items have been placed in the output pile.

MERGE(A, p, q, r)
 1 n1 = q - p + 1
 2 n2 = r - q
 3 let L[1..n1+1] and R[1..n2+1] be new arrays
 4 for i = 1 to n1
 5     L[i] = A[p + i - 1] //copy to first subarray to L
 6 for j = 1 to n2
 7     R[j] = A[q +j] //copy second subarray to R
 8 L[n1 + 1] = infinity // a value bigger than any value in the array 
 9 R[n2 + 1] = infinity // acts as a "sentinel" value so will never
                        // go off end of array
10 i = 1
11 j = 1
12 for k = p to r 
13     if L[i] <= R[j]
14         A[k] = L[i]
15         i = i + 1
16     else 
           A[k] = R[j]
17         j= j + 1

MERGE Example

Example of the merge operation

Quiz (Sec 5)

Which of the following statements is true?

  1. To show a loop in an algorithm has a loop invariant one needs to establish initialization, maintenance, and termination conditions for the invariant.
  2. The random access machine model we discussed last week was based on a quad-core Intel i7 chip.
  3. The best-case and worst-case run-times of insertion sort on an input of size `n` are the same.

Quiz (Sec 6)

Which of the following statements is true?

  1. The input size to a computational problem is always the number of bits it takes to write down an algorithm for the problem.
  2. We argued last week that in the average case setting, the run time of insertion sort was a linear function of the input size.
  3. The loop invariant we showed for insertion sort was:

    At the start of each iteration of the for loop of lines 1-8, `A[1, .. j-1]` consists of the elements originally in `A[1 .. j-1]`, but in sorted order.

A Loop Invariant for MERGE

Claim. MERGE satisfies the following loop invariant:

At the start of each iteration of the for loop of lines 12-17, the subarray `A[p..k-1]` contains the k - p smallest elements of `L[1..n1 + 1]` and `R[1..n2 + 1]` in sorted order. Moreover, `L[i]` and `R[j]` are the smallest elements of their arrays that have not been copied back to `A`.

Proof. We need to show the three properties of loop invariants:

Initialization
Prior to the first iteration of the loop, we have `k=p`, so `A[p..k-1]` is empty. The empty array contains the `k-p = 0` smallest elements of `L` and `R`. Since `i=j=1`, both `L[i]` and `R[j]` are the smallest elements of their arrays that have not been copied back into `A`.
Maintenance
Suppose `L[i] le R[j]`. Then `L[i]` is the smallest element not yet copied back to `A`. Because `A[p..k-1]` contains the `k-p` smallest elements, after line 14 copies `L[i]` into `A[k]`, the subarray `A[p..k]` will contain the `k-p +1` smallest elements. Incrementing `k` and `i` reestablishes the loop invariant for the next iteration. If instead `L[i] > R[j]`, then lines 16-17 perform the appropriate action to maintain the loop invariant.
Termination
When MERGE is done, `k=r+1`. By the loop invariant, the subarray `A[p..k-1] = A[p..r]` contains the `k-p = r - p + 1` smallest elements of `L[1..n1+1]` and `R[1..n2+1]`, in sorted order. The arrays `L` and `R` contain `n1 + n2 +2 = r -p + 3` elements. All but the two largest have not been copied back into A because these two largest elements are the sentinels (infinity).

Runtime of MERGE

MERGE-SORT

Given our MERGE subroutine we are now in a position to present the MERGE-SORT algorithm:

MERGE-SORT(A,p,r)
1 if p < r
2    q = floor((p+r)/2)
3    MERGE-SORT(A, p, q)
4    MERGE-SORT(A,q+1,r)
5    MERGE(A, p, q, r)

floor is the function which round down to the next lower integer.

Analyzing Divide-and-conquer Algorithms

Analysis of Merge Sort

Divide
The divide step just computes the middle of the subarray which takes constant time, so `D(n) = Theta(1)`.
Conquer
We recursively solve two subproblems, each of size `n/2` which contributes `2T(n/2)` to the running time.
Combine
This involves MERGE. So `C(n) = Theta(n)`

Recursion Tree for MERGE-SORT

A recursion tree for MERGE-SORT