Divide and Conquer Chapter 6
Divide and Conquer Paradigm Divide the problem into sub-problems of smaller sizes Conquer by recursively doing the same divisions on each part until small manageable (solvable) size is reached. Combine the sub-solutions to form a solution to the original problem. Read section 6.4 for more details.
Examples of divide and Conquer Read the Recursive versions of Min-Max algorithm in Section 6.1 Binary Search algorithm in Section 6.2
Section 6.3: Top-Down MergeSort Alg. Input: A[1..n] Output: A[1..n] sorted non-decreasingly mergesort(A,1,n)
Procedure mergesort(A, low, high) If low < high then mid (low+high)/2 mergesort(A, low, mid) mergesort(A, mid+1, high) Merge(A, low, mid, high) % seen in Ch 1 End if
View
Top-down vs Bottom-up Bottom-up: iterative, merging is done level by level from bottom to up. Top-down. Recursive, merging is done in DFS (post-order) traversal.
Analysis Theorem: Let n be a power of 2, and C(n) be the number of comparisons in top- down MergeSort on A[1..n]. Then n/2 log n C(n) nlog n –n +1 Proof: assume n = 2 k
Best Case C(1)=0, and C(n) = 2 C(n/2) + n/2 = 2 2 C(n/2 2 ) + 2 x n/2 2 + n/2 = 2 2 C(n/2 2 ) + 2 x n /2 = 2 3 C(n/2 3 ) x n/ x n/2 = 2 3 C(n/2 3 ) + 3 x n/2 = 2 k C(n/2 k ) + k x n/2 = 2 k C(1) + k x n/2 = n/2 log n
Worst Case C(1)=0, and C(n) = 2 C(n/2) + n-1 = 2 2 C(n/2 2 ) + n n-2 0 = 2 3 C(n/2 3 ) + n n n-2 0 = 2 k C(n/2 k ) + n-2 k n n-2 0 = k n - 2 k +1 = n log n – n + 1
Theorem Both types of MergeSort use (n log n) They have same performance if n is a power of 2 But different if n is not a power of 2.