Presentation is loading. Please wait.

Presentation is loading. Please wait.

Divide and Conquer Chapter 6. Divide and Conquer Paradigm Divide the problem into sub-problems of smaller sizes Conquer by recursively doing the same.

Similar presentations


Presentation on theme: "Divide and Conquer Chapter 6. Divide and Conquer Paradigm Divide the problem into sub-problems of smaller sizes Conquer by recursively doing the same."— Presentation transcript:

1 Divide and Conquer Chapter 6

2 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.

3 Examples of divide and Conquer Read the Recursive versions of Min-Max algorithm in Section 6.1 Binary Search algorithm in Section 6.2

4 Section 6.3: Top-Down MergeSort Alg. Input: A[1..n] Output: A[1..n] sorted non-decreasingly mergesort(A,1,n)

5 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

6 View

7 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.

8 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

9 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 ) + 2 2 x n/2 3 + 2 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

10 Worst Case C(1)=0, and C(n) = 2 C(n/2) + n-1 = 2 2 C(n/2 2 ) + n-2 1 + n-2 0 = 2 3 C(n/2 3 ) + n-2 2 + n-2 1 + n-2 0 = 2 k C(n/2 k ) + n-2 k-1 +...+n-2 1 + n-2 0 = k n - 2 k +1 = n log n – n + 1

11 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.


Download ppt "Divide and Conquer Chapter 6. Divide and Conquer Paradigm Divide the problem into sub-problems of smaller sizes Conquer by recursively doing the same."

Similar presentations


Ads by Google