# Chapter 4: Divide-and-Conquer Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide: divide instance of problem into two or more smaller instances, ideally of about equal size 2. Conquer: Solve smaller instances recursively 3. Combine: Obtain solution to original (larger) instance by combining these solutions A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 1 Divide-and-Conquer Technique (cont.) a problem of size n subproblem 1 of size n/2 subproblem 2 of size n/2

a solution to subproblem 1 a solution to subproblem 2 a solution to the original problem A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2 Divide-and-Conquer Examples Sorting: mergesort and quicksort Binary tree traversals Multiplication of large integers

Matrix multiplication: Strassens algorithm Closest-pair and convex-hull algorithms Binary search: decrease-by-half (or degenerate divide&conq.) A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 3 General Divide-and-Conquer Recurrence T(n) = aT(n/b) + f (n) where f(n) (nd), d 0 Master Theorem: If a < bd, T(n) (nd) If a = bd, T(n) (nd log n) If a > bd, T(n) (nlog b a ) Examples: T(n) = 4T(n/2) + n T(n) = 4T(n/2) + n2 T(n) = 4T(n/2) + n3

T(n) ? T(n) ? T(n) ? A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 4 Mergesort Divide into two equal parts Sort each part using merge-sort (recursion!!!) Merge two sorted subsequences A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 5 Pseudocode of Mergesort

A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 6 Mergesort Example 8 3 2 9 7 1 5 4 8 3 2 9 8 3 8 7 1 5 4 2 9 3 2 3 8 71

9 2 9 7 5 4 1 5 1 7 2 3 8 9 4 4 5 1 4 5 7 1 2 3 4 5 7 8 9 A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 7

Merge Merge sorted arrays B and C into array A as follows: Repeat the following until no elements remain in one of the arrays: compare the first elements in the remaining unprocessed portions of the arrays copy the smaller of the two into A, while incrementing the index indicating the unprocessed portion of that array Once all elements in one of the arrays are processed, copy the remaining unprocessed elements from the other array into A. A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 8 Merge A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 9

Merge A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10 Pseudocode of Merge A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 11 Analysis of Mergesort Recurrence relation: C(n) = 2C(n/2) + Cmerge(n) for n > 1, C(1) = 0 All cases have same efficiency: (n log n) Space requirement: (n) (not in-place)

Can be implemented without recursion (bottom-up) A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 12 Quick Sort Divide: divide into two parts such that elements of left part < elements of right part Conquer: recursively solve for each part separately Combine: trivial - do not do anything Quicksort(A[lr]) if l

//conquer left Quicksort(A[s+1r]) //conquer right A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 13 Quicksort Select a pivot (partitioning element) here, the first element Rearrange the list so that all the elements in the first s positions are smaller than or equal to the pivot and all the elements in the remaining n-s positions are larger than or equal to the pivot (see next slide for an algorithm) p A[i]p A[i]p Exchange the pivot with the last element in the first (i.e., )

subarray the pivot is now in its final position Sort the two subarrays recursively A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 14 Hoares Partitioning Algorithm A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 15 Quicksort Example l,i 5 5 r 3 1 9 i

i i 3 1 9 8 8 2 2 i 5 5 5 3 3 3 1

1 1 4 4 4 4 7 j j 4 7 j j 8 2

i j 8 2 i j 2 8 j i 9 7 9

7 9 7 5 3 1 4 2 8 9 7 2 3

1 4 5 8 9 7 A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 4 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 16 Analysis of Quicksort Best case: split in the middle Cbest (n) = 2Cbest (n/2) + n (n log n) Worst case: sorted array! (n2)

Average case: random arrays (n log n) Improvements: better pivot selection: random or median-of-three switch to insertion sort on small subarrays elimination of recursion Considered the method of choice for internal sorting of large files (n 10000) A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 17 Binary Tree Algorithms Binary tree is a divide-and-conquer ready structure! Ex. 1: Classic traversals (preorder, inorder, postorder) Algorithm Inorder(T) a if T Inorder(Tleft)

print(root of T) Inorder(Tright) b a c d b e c d e Efficiency: (n) A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 18 Binary Tree Algorithms (cont.) Ex. 2: Computing the height of a binary tree

TL TR h(T) = max{h(TL), h(TR)} + 1 if T and h() = -1 Efficiency: (n) A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 19