You are on page 1of 97

CS 473: Fundamental Algorithms, Spring 2011

Reductions, Recursion and Divide and Conquer
Lecture 5
February 1, 2011

Sariel (UIUC)

CS473

1

Spring 2011

1 / 42

Part I Reductions and Recursion

Sariel (UIUC)

CS473

2

Spring 2011

2 / 42

Reduction
Reducing problem A to problem B: Algorithm for A uses algorithm for B as a black box

Sariel (UIUC)

CS473

3

Spring 2011

3 / 42

Distinct Elements Problem Problem Given an array A of n integers. are there any duplicates in A? Naive algorithm: for i = 1 to n − 1 do for j = i + 1 to n do if (A[i] = A[j]) return YES return NO Running time: O(n2 ) Sariel (UIUC) CS473 4 Spring 2011 4 / 42 .

are there any duplicates in A? Naive algorithm: for i = 1 to n − 1 do for j = i + 1 to n do if (A[i] = A[j]) return YES return NO Running time: O(n2 ) Sariel (UIUC) CS473 4 Spring 2011 4 / 42 .Distinct Elements Problem Problem Given an array A of n integers.

Distinct Elements Problem Problem Given an array A of n integers. are there any duplicates in A? Naive algorithm: for i = 1 to n − 1 do for j = i + 1 to n do if (A[i] = A[j]) return YES return NO Running time: O(n2 ) Sariel (UIUC) CS473 4 Spring 2011 4 / 42 .

are there any duplicates in A? Naive algorithm: for i = 1 to n − 1 do for j = i + 1 to n do if (A[i] = A[j]) return YES return NO Running time: O(n2 ) Sariel (UIUC) CS473 4 Spring 2011 4 / 42 .Distinct Elements Problem Problem Given an array A of n integers.

Reduction to Sorting Sort A for i = 1 to n − 1 do if (A[i] = A[i + 1]) then return YES return NO Running time: O(n) plus time to sort an array of n numbers Important point: algorithm uses sorting as a black box Sariel (UIUC) CS473 5 Spring 2011 5 / 42 .

Reduction to Sorting Sort A for i = 1 to n − 1 do if (A[i] = A[i + 1]) then return YES return NO Running time: O(n) plus time to sort an array of n numbers Important point: algorithm uses sorting as a black box Sariel (UIUC) CS473 5 Spring 2011 5 / 42 .

Two sides of Reductions Suppose problem A reduces to problem B Positive direction: Algorithm for B implies an algorithm for A Negative direction: Suppose there is no “efficient” algorithm for A then it implies no efficient algorithm for B (technical condition for reduction time necessary for this) Example: Distinct Elements reduces to Sorting in O(n) time An O(n log n) time algorithm for Sorting implies an O(n log n) time algorithm for Distinct Elements problem. If there is no o(n log n) time algorithm for Distinct Elements problem then there is no o(n log n) time algorithm for Sorting. Sariel (UIUC) CS473 6 Spring 2011 6 / 42 .

If there is no o(n log n) time algorithm for Distinct Elements problem then there is no o(n log n) time algorithm for Sorting.Two sides of Reductions Suppose problem A reduces to problem B Positive direction: Algorithm for B implies an algorithm for A Negative direction: Suppose there is no “efficient” algorithm for A then it implies no efficient algorithm for B (technical condition for reduction time necessary for this) Example: Distinct Elements reduces to Sorting in O(n) time An O(n log n) time algorithm for Sorting implies an O(n log n) time algorithm for Distinct Elements problem. Sariel (UIUC) CS473 6 Spring 2011 6 / 42 .

problem instances of small size are solved by some other method as base cases Sariel (UIUC) CS473 7 Spring 2011 7 / 42 .Recursion Reduction: reduce one problem to another Recursion: a special case of reduction reduce problem to a smaller instance of itself self-reduction Problem instance of size n is reduced to one or more instances of size n − 1 or less. For termination.

Recursion Reduction: reduce one problem to another Recursion: a special case of reduction reduce problem to a smaller instance of itself self-reduction Problem instance of size n is reduced to one or more instances of size n − 1 or less. For termination. problem instances of small size are solved by some other method as base cases Sariel (UIUC) CS473 7 Spring 2011 7 / 42 .

Recursion Recursion is a very powerful and fundamental technique Basis for several other methods Divide and conquer Dynamic programming Enumeration and branch and bound etc Some classes of greedy algorithms Makes proof of correctness easy (via induction) Recurrences arise in analysis Sariel (UIUC) CS473 8 Spring 2011 8 / 42 .

..n] of integers. Swap A[1] and A[i] SelectSort(A[2. T(n) = T(n − 1) + n for n > 1 and T(1) = 1 for n = 1 T(n) = Θ(n2 ). Sariel (UIUC) CS473 9 Spring 2011 9 / 42 .n]) Let A[i] be smallest number T(n): time for SelectSort on an n element array. Recursive version of Selection sort. SelectSort(A[1..Selection Sort Sort a given array A[1.n]): if n = 1 return Find smallest number in A.

SelectSort(A[1. Sariel (UIUC) CS473 9 Spring 2011 9 / 42 . T(n) = T(n − 1) + n for n > 1 and T(1) = 1 for n = 1 T(n) = Θ(n2 ). Recursive version of Selection sort..Selection Sort Sort a given array A[1...n] of integers.n]) Let A[i] be smallest number T(n): time for SelectSort on an n element array. Swap A[1] and A[i] SelectSort(A[2.n]): if n = 1 return Find smallest number in A.

n]): if n = 1 return Find smallest number in A.n]) Let A[i] be smallest number T(n): time for SelectSort on an n element array.n] of integers. T(n) = T(n − 1) + n for n > 1 and T(1) = 1 for n = 1 T(n) = Θ(n2 ). Recursive version of Selection sort..Selection Sort Sort a given array A[1. Sariel (UIUC) CS473 9 Spring 2011 9 / 42 . SelectSort(A[1. Swap A[1] and A[i] SelectSort(A[2...

n − 1 disks back on top of it. . Rouse Ball and H. let’s concentrate on moving just the largest disk.thunderclap the world will vanish. we may also freely use ‘cookies’ and be Sariel (UIUC) CS473 Spring 2011 10 / 42 . M. . Of course. because allstack of disks are covering it. we 0 toto move those n disk at to the third needle before we cannot putnth larger disk on a we move the nth disk. Question: what is a strategy and how many moves does it take? 3 4 This English translation is from W. never placing any disk on top of a smaller disk? Tower of Hanoi The Tower of Hanoi puzzle The trick to solving this puzzle is to think recursively. Move the other n disks from peg have peg 2. we read this story and immediately substitute n for the hardwired constant sixty-four. And then after smaller disk. Coxeter’s book Mathematical Recreations and Essays.4 How can we move a tower of n disks from one needle to another. We can’t move it at the beginning. Instead of trying to solve the entire puzzle all at once. W. Recognizing that the underlying mathematical abstraction would10 unchanged. So now all we have to figure out is how to. using a third needles as an occasional placeholder. one − 1 disks a time. we have to move those Rule: can move the a disk. S. being good computer scientists.

recursion recursion The Tower of Hanoi algorithm. for more complicated problems. We must handle that base case directly. are quite adept at moving zero disks from one needle to another. In fact. being good Buddhists. ignore everything but the bottom disk Our algorithm does make one subtle but important assumption: there is a largest disk. The base case for the Tower of Hanoi algorithm. to carry the original story further. the monks at Benares. there is no bottom disk While it’s tempting to think about how all those smaller disks get moved—in other words. to the junior monks at the temple). but it breaks down when n = 0. our recursive algorithm works for any n ≥ 1. In other words. Our11 only task is to reduce the problem to one 11 / 42 Sariel opening Spring 2011 . (UIUC) up the recursion is a CS473 distraction. what happens when the recursion is unfolded—it’s not necessary. Fortunately. which we can gleefully hand off to the Recursion Fairy (or.Algorithms Lecture 1: Recursion Tower of Hanoi via Recursion STOP!! That’s it! We’re done! We’ve successfully reduced the n-disk Tower of Hanoi problem to two instances of the (n − 1)-disk Tower of Hanoi problem.

dest. src. dest) Move disk n from src to dest Hanoi(n − 1. src) T(n): time to move n disks via recursive strategy T(n) = 2T(n − 1) + 1 n>1 and T(1) = 1 Sariel (UIUC) CS473 12 Spring 2011 12 / 42 . tmp. tmp): If (n > 0) then Hanoi(n − 1. tmp. dest.Recursive Algorithm Hanoi(n. src.

dest. src) T(n): time to move n disks via recursive strategy T(n) = 2T(n − 1) + 1 n>1 and T(1) = 1 Sariel (UIUC) CS473 12 Spring 2011 12 / 42 . dest. tmp): If (n > 0) then Hanoi(n − 1.Recursive Algorithm Hanoi(n. tmp. src. tmp. dest) Move disk n from src to dest Hanoi(n − 1. src.

dest.Recursive Algorithm Hanoi(n. src. src) T(n): time to move n disks via recursive strategy T(n) = 2T(n − 1) + 1 n>1 and T(1) = 1 Sariel (UIUC) CS473 12 Spring 2011 12 / 42 . tmp. tmp): If (n > 0) then Hanoi(n − 1. dest. src. dest) Move disk n from src to dest Hanoi(n − 1. tmp.

. . .. 2i T(n − i) + 2i−1 + 2i−2 + ..Analysis T(n) = = = = = = = = 2T(n − 1) + 1 22 T(n − 2) + 2 + 1 . 2n−1 T(1) + 2n−2 + . . . + 1 2n−1 + 2n−2 + . .. + 1 .. + 1 (2n − 1)/(2 − 1) = 2n − 1 Sariel (UIUC) CS473 13 Spring 2011 13 / 42 .

Sariel (UIUC) CS473 14 Spring 2011 14 / 42 . Non-recursive Algorithm 2: Let ρ(n) be the smallest integer k such that n/2k is not an integer. Prove by induction. Never move the same disk twice in a row.Non-Recursive Algorithms for Tower of Hanoi Pegs numbered 0. ρ(18) = 2. Example: ρ(40) = 4. In step i move disk ρ(i) forward if n − i is even and backward if n − i is odd. 2 Non-recursive Algorithm 1: Always move smallest disk forward if n is even. Moves are exactly same as those of recursive algorithm. backward if n is odd. Done when no legal move. 1.

Non-Recursive Algorithms for Tower of Hanoi Pegs numbered 0. 2 Non-recursive Algorithm 1: Always move smallest disk forward if n is even. backward if n is odd. Prove by induction. Sariel (UIUC) CS473 14 Spring 2011 14 / 42 . In step i move disk ρ(i) forward if n − i is even and backward if n − i is odd. Example: ρ(40) = 4. Never move the same disk twice in a row. Done when no legal move. Moves are exactly same as those of recursive algorithm. 1. ρ(18) = 2. Non-recursive Algorithm 2: Let ρ(n) be the smallest integer k such that n/2k is not an integer.

In step i move disk ρ(i) forward if n − i is even and backward if n − i is odd. backward if n is odd. 2 Non-recursive Algorithm 1: Always move smallest disk forward if n is even. Non-recursive Algorithm 2: Let ρ(n) be the smallest integer k such that n/2k is not an integer. Done when no legal move. Moves are exactly same as those of recursive algorithm. Sariel (UIUC) CS473 14 Spring 2011 14 / 42 . Example: ρ(40) = 4. 1. Never move the same disk twice in a row. ρ(18) = 2.Non-Recursive Algorithms for Tower of Hanoi Pegs numbered 0. Prove by induction.

Part II Divide and Conquer Sariel (UIUC) CS473 15 Spring 2011 15 / 42 .

divide step Recursively solve problem on smaller instances Combine solutions to smaller instances to obtain a solution to the original instance . each smaller instance is typically at least a constant factor smaller than the original instance which leads to efficient running times. Sariel (UIUC) CS473 16 Spring 2011 16 / 42 . There are many examples of this particular type of recursion that it deserves its own treatment.conquer step Question: Why is this not plain recursion? In divide and conquer.Divide and Conquer Paradigm Divide and Conquer is a common and useful type of recursion Approach Break problem instance into smaller instances .

Divide and Conquer Paradigm Divide and Conquer is a common and useful type of recursion Approach Break problem instance into smaller instances .divide step Recursively solve problem on smaller instances Combine solutions to smaller instances to obtain a solution to the original instance . There are many examples of this particular type of recursion that it deserves its own treatment.conquer step Question: Why is this not plain recursion? In divide and conquer. Sariel (UIUC) CS473 16 Spring 2011 16 / 42 . each smaller instance is typically at least a constant factor smaller than the original instance which leads to efficient running times.

Divide and Conquer Paradigm Divide and Conquer is a common and useful type of recursion Approach Break problem instance into smaller instances . each smaller instance is typically at least a constant factor smaller than the original instance which leads to efficient running times. There are many examples of this particular type of recursion that it deserves its own treatment.conquer step Question: Why is this not plain recursion? In divide and conquer.divide step Recursively solve problem on smaller instances Combine solutions to smaller instances to obtain a solution to the original instance . Sariel (UIUC) CS473 16 Spring 2011 16 / 42 .

Sorting Input Given an array of n elements Goal Rearrange them in ascending order Sariel (UIUC) CS473 17 Spring 2011 17 / 42 .

n] ALGORITHMS 2 Divide into subarrays A[1 . . m] and A[m + 1 . . . . . . where m = n/2 ALGOR ITHMS Recursively MergeSort A[1 .Merge Sort [von Neumann] MergeSort 1 Input: Array A[1 . . m] and A[m + 1 . n] AGLOR HIMST 3 4 Merge the sorted arrays AGHILMORST Sariel (UIUC) CS473 18 Spring 2011 18 / 42 . n]. . . .

. where m = n/2 ALGOR ITHMS Recursively MergeSort A[1 . m] and A[m + 1 . n] AGLOR HIMST 3 4 Merge the sorted arrays AGHILMORST Sariel (UIUC) CS473 18 Spring 2011 18 / 42 . . .Merge Sort [von Neumann] MergeSort 1 Input: Array A[1 . . . . n] ALGORITHMS 2 Divide into subarrays A[1 . . . . m] and A[m + 1 . n]. .

m] and A[m + 1 . n] ALGORITHMS 2 Divide into subarrays A[1 . n] AGLOR HIMST 3 4 Merge the sorted arrays AGHILMORST Sariel (UIUC) CS473 18 Spring 2011 18 / 42 . where m = n/2 ALGOR ITHMS Recursively MergeSort A[1 . . . . . . . . . m] and A[m + 1 . n].Merge Sort [von Neumann] MergeSort 1 Input: Array A[1 . . .

. . . . where m = n/2 ALGOR ITHMS Recursively MergeSort A[1 . . . . .Merge Sort [von Neumann] MergeSort 1 Input: Array A[1 . m] and A[m + 1 . . . n] ALGORITHMS 2 Divide into subarrays A[1 . n]. n] AGLOR HIMST 3 4 Merge the sorted arrays AGHILMORST Sariel (UIUC) CS473 18 Spring 2011 18 / 42 . m] and A[m + 1 .

where m = n/2 ALGOR ITHMS Recursively MergeSort A[1 . . .Merge Sort [von Neumann] MergeSort 1 Input: Array A[1 . . n]. . m] and A[m + 1 . n] ALGORITHMS 2 Divide into subarrays A[1 . . . . . m] and A[m + 1 . . . n] AGLOR HIMST 3 4 Merge the sorted arrays AGHILMORST Sariel (UIUC) CS473 18 Spring 2011 18 / 42 .

storing elements in C in order AGLOR HIMST AGHILMORST Merge two arrays using only constantly more extra space (in-place merge sort): doable but complicated and typically impractical Sariel (UIUC) CS473 19 Spring 2011 19 / 42 .Merging Sorted Arrays Use a new array C to store the merged array Scan A and B from left-to-right.

storing elements in C in order AGLOR HIMST AGHILMORST Merge two arrays using only constantly more extra space (in-place merge sort): doable but complicated and typically impractical Sariel (UIUC) CS473 19 Spring 2011 19 / 42 .Merging Sorted Arrays Use a new array C to store the merged array Scan A and B from left-to-right.

Merging Sorted Arrays
Use a new array C to store the merged array Scan A and B from left-to-right, storing elements in C in order AGLOR HIMST AGHILMORST Merge two arrays using only constantly more extra space (in-place merge sort): doable but complicated and typically impractical

Sariel (UIUC)

CS473

19

Spring 2011

19 / 42

Merging Sorted Arrays
Use a new array C to store the merged array Scan A and B from left-to-right, storing elements in C in order AGLOR HIMST AGHILMORST Merge two arrays using only constantly more extra space (in-place merge sort): doable but complicated and typically impractical

Sariel (UIUC)

CS473

19

Spring 2011

19 / 42

Merging Sorted Arrays
Use a new array C to store the merged array Scan A and B from left-to-right, storing elements in C in order AGLOR HIMST AGHILMORST Merge two arrays using only constantly more extra space (in-place merge sort): doable but complicated and typically impractical

Sariel (UIUC)

CS473

19

Spring 2011

19 / 42

storing elements in C in order AGLOR HIMST AGHILMORST Merge two arrays using only constantly more extra space (in-place merge sort): doable but complicated and typically impractical Sariel (UIUC) CS473 19 Spring 2011 19 / 42 .Merging Sorted Arrays Use a new array C to store the merged array Scan A and B from left-to-right.

upper bound T(n) = Ω(f(n)) .Running Time T(n): time for merge sort to sort an n element array T(n) = T( n/2 ) + T( n/2 ) + cn What do we want as a solution to the recurrence? Almost always only an asymptotically tight bound.lower bound Sariel (UIUC) CS473 20 Spring 2011 20 / 42 . T(n) = O(f(n)) . That is we want to know f(n) such that T(n) = Θ(f(n)).

That is we want to know f(n) such that T(n) = Θ(f(n)). T(n) = O(f(n)) .lower bound Sariel (UIUC) CS473 20 Spring 2011 20 / 42 .upper bound T(n) = Ω(f(n)) .Running Time T(n): time for merge sort to sort an n element array T(n) = T( n/2 ) + T( n/2 ) + cn What do we want as a solution to the recurrence? Almost always only an asymptotically tight bound.

Running Time T(n): time for merge sort to sort an n element array T(n) = T( n/2 ) + T( n/2 ) + cn What do we want as a solution to the recurrence? Almost always only an asymptotically tight bound.upper bound T(n) = Ω(f(n)) .lower bound Sariel (UIUC) CS473 20 Spring 2011 20 / 42 . T(n) = O(f(n)) . That is we want to know f(n) such that T(n) = Θ(f(n)).

but not simpler.” Know where to be loose in analysis and where to be tight. practice! Sariel (UIUC) CS473 21 Spring 2011 21 / 42 . exponentials. logarithms. practice. Comes with practice.Solving Recurrences: Some Techniques Know some basic math: geometric series. elementary calculus Expand the recurrence and spot a pattern and use simple math Recursion tree method — imagine the computation as a tree Guess and verify — useful for proving upper and lower bounds even if not tight bounds Albert Einstein: “Everything should be made as simple as possible.

” Know where to be loose in analysis and where to be tight.Solving Recurrences: Some Techniques Know some basic math: geometric series. elementary calculus Expand the recurrence and spot a pattern and use simple math Recursion tree method — imagine the computation as a tree Guess and verify — useful for proving upper and lower bounds even if not tight bounds Albert Einstein: “Everything should be made as simple as possible. but not simpler. practice. logarithms. Comes with practice. exponentials. practice! Sariel (UIUC) CS473 21 Spring 2011 21 / 42 .

At the ith level total work is cn Sum over all levels. T(n) = 2T(n/2) + cn n cn n/2 cn/2 n/2 cn/2 n/4 cn/4 n/4 cn/4 n/4 cn/4 n/4 cn/4 2 3 Identify a pattern.Recursion Trees MergeSort: n is a power of 2 1 Unroll the recurrence. The number of levels is log n. So total is cn log n = O(n log n) Sariel (UIUC) CS473 22 Spring 2011 22 / 42 .

Recursion Trees MergeSort: n is a power of 2 1 Unroll the recurrence. At the ith level total work is cn Sum over all levels. The number of levels is log n. So total is cn log n = O(n log n) Sariel (UIUC) CS473 22 Spring 2011 22 / 42 . T(n) = 2T(n/2) + cn n cn n/2 cn/2 n/2 cn/2 n/4 cn/4 n/4 cn/4 n/4 cn/4 n/4 cn/4 2 3 Identify a pattern.

The number of levels is log n. So total is cn log n = O(n log n) Sariel (UIUC) CS473 22 Spring 2011 22 / 42 .Recursion Trees MergeSort: n is a power of 2 1 Unroll the recurrence. At the ith level total work is cn Sum over all levels. T(n) = 2T(n/2) + cn n cn n/2 cn/2 n/2 cn/2 n/4 cn/4 n/4 cn/4 n/4 cn/4 n/4 cn/4 2 3 Identify a pattern.

The number of levels is log n. So total is cn log n = O(n log n) Sariel (UIUC) CS473 22 Spring 2011 22 / 42 . At the ith level total work is cn Sum over all levels. T(n) = 2T(n/2) + cn n cn n/2 cn/2 n/2 cn/2 n/4 cn/4 n/4 cn/4 n/4 cn/4 n/4 cn/4 2 3 Identify a pattern.Recursion Trees MergeSort: n is a power of 2 1 Unroll the recurrence.

Recursion Trees MergeSort: n is a power of 2 1 Unroll the recurrence. T(n) = 2T(n/2) + cn n cn n/2 cn/2 n/2 cn/2 n/4 cn/4 n/4 cn/4 n/4 cn/4 n/4 cn/4 2 3 Identify a pattern. The number of levels is log n. So total is cn log n = O(n log n) Sariel (UIUC) CS473 22 Spring 2011 22 / 42 . At the ith level total work is cn Sum over all levels.

the running time of mergesort is expressed as T(n) = T( n/2 ) + T( n/2 ) + cn n1 = 2k−1 < n ≤ 2k = n2 (n1 . Sariel (UIUC) CS473 23 Spring 2011 23 / 42 . n2 powers of 2) T(n1 ) < T(n) ≤ T(n2 ) (Why?) T(n) = Θ(n log n) since n/2 ≤ n1 < n ≤ n2 ≤ 2n.MergeSort Analysis When n is not a power of 2 When n is not a power of 2.

Sariel (UIUC) CS473 23 Spring 2011 23 / 42 . the running time of mergesort is expressed as T(n) = T( n/2 ) + T( n/2 ) + cn n1 = 2k−1 < n ≤ 2k = n2 (n1 . n2 powers of 2) T(n1 ) < T(n) ≤ T(n2 ) (Why?) T(n) = Θ(n log n) since n/2 ≤ n1 < n ≤ n2 ≤ 2n.MergeSort Analysis When n is not a power of 2 When n is not a power of 2.

Sariel (UIUC) CS473 23 Spring 2011 23 / 42 .MergeSort Analysis When n is not a power of 2 When n is not a power of 2. the running time of mergesort is expressed as T(n) = T( n/2 ) + T( n/2 ) + cn n1 = 2k−1 < n ≤ 2k = n2 (n1 . n2 powers of 2) T(n1 ) < T(n) ≤ T(n2 ) (Why?) T(n) = Θ(n log n) since n/2 ≤ n1 < n ≤ n2 ≤ 2n.

the running time of mergesort is expressed as T(n) = T( n/2 ) + T( n/2 ) + cn n1 = 2k−1 < n ≤ 2k = n2 (n1 .MergeSort Analysis When n is not a power of 2 When n is not a power of 2. n2 powers of 2) T(n1 ) < T(n) ≤ T(n2 ) (Why?) T(n) = Θ(n log n) since n/2 ≤ n1 < n ≤ n2 ≤ 2n. Sariel (UIUC) CS473 23 Spring 2011 23 / 42 .

Sariel (UIUC) CS473 24 Spring 2011 24 / 42 . x/2 + x/2 = x.Recursion Trees MergeSort: n is not a power of 2 T(n) = T( n/2 ) + T( n/2 ) + cn Observation: For any number x.

Can guess that T(n) = Θ(n log n) for all n. Induction Step Assume T(k) ≤ 2ck log k for all k < n and prove it for k = n. Sariel (UIUC) CS473 25 Spring 2011 25 / 42 .When n is not a power of 2: Guess and Verify If n is power of 2 we saw that T(n) = Θ(n log n). Verify? proof by induction! Induction Hypothesis: T(n) ≤ 2cn log n for all n ≥ 1 Base Case: n = 1. T(1) = 0 since no need to do any work and 2cn log n = 0 for n = 1.

Verify? proof by induction! Induction Hypothesis: T(n) ≤ 2cn log n for all n ≥ 1 Base Case: n = 1. T(1) = 0 since no need to do any work and 2cn log n = 0 for n = 1. Can guess that T(n) = Θ(n log n) for all n. Sariel (UIUC) CS473 25 Spring 2011 25 / 42 . Induction Step Assume T(k) ≤ 2ck log k for all k < n and prove it for k = n.When n is not a power of 2: Guess and Verify If n is power of 2 we saw that T(n) = Θ(n log n).

Induction Step We have T(n) = ≤ ≤ ≤ ≤ ≤ ≤ ≤ ≤ T( n/2 ) + T( n/2 ) + cn 2c n/2 log n/2 + 2c n/2 log n/2 + cn (by induct 2c n/2 log n/2 + 2c n/2 log n/2 + cn 2c( n/2 + n/2 ) log n/2 + cn 2cn log n/2 + cn 2cn log(2n/3) + cn (since n/2 ≤ 2n/3 for all n ≥ 2) 2cn log n + cn(1 − 2 log 3/2) 2cn log n + cn(log 2 − log 9/4) 2cn log n Sariel (UIUC) CS473 26 Spring 2011 26 / 42 .

Instead we assume that T(n) ≤ αcn log n for some constant α that will be fixed later. Sariel (UIUC) CS473 27 Spring 2011 27 / 42 . All we need to prove that there is some sufficiently large constant α that will make the algebra go through.Guess and Verify The math worked out like magic! Why was 2cn log n chosen instead of say 4cn log n? Typically we don’t know upfront what constant to choose. We need to choose α such that α log 3/2 > 1. Typically you do the algebra with α and then show at the end that α can be chosen to be sufficiently large constant.

Instead we assume that T(n) ≤ αcn log n for some constant α that will be fixed later. We need to choose α such that α log 3/2 > 1. Sariel (UIUC) CS473 27 Spring 2011 27 / 42 .Guess and Verify The math worked out like magic! Why was 2cn log n chosen instead of say 4cn log n? Typically we don’t know upfront what constant to choose. All we need to prove that there is some sufficiently large constant α that will make the algebra go through. Typically you do the algebra with α and then show at the end that α can be chosen to be sufficiently large constant.

Suggests that our guess is incorrect. We try to prove by induction that T(n) ≤ αcn for some constant α. Sariel (UIUC) CS473 28 Spring 2011 28 / 42 .Guess and Verify: When is a guess incorrect? Suppose we guessed that the soln to the mergesort recurrent is T(n) = O(n). Induction Step: attempt T(n) = ≤ ≤ ≤ T( n/2 ) + T( n/2 ) + cn αc n/2 + αc n/2 + cn αcn + cn (α + 1)cn But we want to show that T(n) ≤ αcn! So guess does not work for any constant α.

Running time is O(n log n) Question: Merge Sort splits into 2 (roughly) equal sized arrays.Selection Sort vs Merge Sort Selection Sort spends O(n) work to reduce problem from n to n − 1 leading to O(n2 ) running time. Merge Sort spends O(n) time after reducing problem to two instances of size n/2 each. Can we do better by splitting into more than 2 arrays? Say k arrays of size n/k each? Sariel (UIUC) CS473 29 Spring 2011 29 / 42 .

Selection Sort vs Merge Sort Selection Sort spends O(n) work to reduce problem from n to n − 1 leading to O(n2 ) running time. Running time is O(n log n) Question: Merge Sort splits into 2 (roughly) equal sized arrays. Can we do better by splitting into more than 2 arrays? Say k arrays of size n/k each? Sariel (UIUC) CS473 29 Spring 2011 29 / 42 . Merge Sort spends O(n) time after reducing problem to two instances of size n/2 each.

1 pivot: 16 split into 12. 18 and recursively sort put them together with pivot in middle Sariel (UIUC) CS473 30 Spring 2011 30 / 42 . those larger than pivot. 18. and the pivot itself. 19. 1 and 20. and concatenate them. 12. 14. 5. Example: array: 16. 19. Linear scan of array does it. 3. 20. Time is O(n) Recursively sort the subarrays. 3. 5.Quick Sort Quick Sort[Hoare] 1 2 3 Pick a pivot element from array Split array into 3 subarrays: those smaller than pivot. 14.

3. 19. 18 and recursively sort put them together with pivot in middle Sariel (UIUC) CS473 30 Spring 2011 30 / 42 .Quick Sort Quick Sort[Hoare] 1 2 3 Pick a pivot element from array Split array into 3 subarrays: those smaller than pivot. 5. 3. 18. 12. 19. and the pivot itself. 5. 14. Linear scan of array does it. 1 pivot: 16 split into 12. those larger than pivot. and concatenate them. 1 and 20. Time is O(n) Recursively sort the subarrays. Example: array: 16. 14. 20.

19. 5. 18. 19. those larger than pivot. 14. 14. 18 and recursively sort put them together with pivot in middle Sariel (UIUC) CS473 30 Spring 2011 30 / 42 . Linear scan of array does it. and the pivot itself. 3. and concatenate them. 1 and 20. Time is O(n) Recursively sort the subarrays. Example: array: 16. 20.Quick Sort Quick Sort[Hoare] 1 2 3 Pick a pivot element from array Split array into 3 subarrays: those smaller than pivot. 12. 3. 5. 1 pivot: 16 split into 12.

14. 1 and 20. 19. Example: array: 16. those larger than pivot. 20. 5. 5. 14. 12. and concatenate them. 18 and recursively sort put them together with pivot in middle Sariel (UIUC) CS473 30 Spring 2011 30 / 42 . 1 pivot: 16 split into 12. 19.Quick Sort Quick Sort[Hoare] 1 2 3 Pick a pivot element from array Split array into 3 subarrays: those smaller than pivot. and the pivot itself. 18. Linear scan of array does it. 3. 3. Time is O(n) Recursively sort the subarrays.

T(n) = O(n log n). T(n) = max (T(k − 1) + T(n − k) + O(n)) 1≤k≤n In the worst case T(n) = T(n − 1) + O(n). Then. T(n) = T(k − 1) + T(n − k) + O(n) If k = n/2 then T(n) = T( n/2 −1)+T( n/2 )+O(n) ≤ 2T(n/2)+O(n). Sariel (UIUC) CS473 31 Spring 2011 31 / 42 .Time Analysis Let k be the rank of the chosen pivot. median can be found in linear time. pivot is the first or last element of array. Theoretically. Then. Happens if array is already sorted and pivot is always first element. Then. which means T(n) = O(n2 ). Typically.

Sariel (UIUC) CS473 31 Spring 2011 31 / 42 . T(n) = O(n log n).Time Analysis Let k be the rank of the chosen pivot. Typically. Then. pivot is the first or last element of array. median can be found in linear time. which means T(n) = O(n2 ). Happens if array is already sorted and pivot is always first element. T(n) = max (T(k − 1) + T(n − k) + O(n)) 1≤k≤n In the worst case T(n) = T(n − 1) + O(n). Theoretically. T(n) = T(k − 1) + T(n − k) + O(n) If k = n/2 then T(n) = T( n/2 −1)+T( n/2 )+O(n) ≤ 2T(n/2)+O(n). Then. Then.

pivot is the first or last element of array. median can be found in linear time. Then. Sariel (UIUC) CS473 31 Spring 2011 31 / 42 . which means T(n) = O(n2 ). Happens if array is already sorted and pivot is always first element.Time Analysis Let k be the rank of the chosen pivot. Then. Typically. Theoretically. T(n) = max (T(k − 1) + T(n − k) + O(n)) 1≤k≤n In the worst case T(n) = T(n − 1) + O(n). T(n) = O(n log n). Then. T(n) = T(k − 1) + T(n − k) + O(n) If k = n/2 then T(n) = T( n/2 −1)+T( n/2 )+O(n) ≤ 2T(n/2)+O(n).

T(n) = O(n log n). Theoretically. pivot is the first or last element of array. Sariel (UIUC) CS473 31 Spring 2011 31 / 42 . T(n) = max (T(k − 1) + T(n − k) + O(n)) 1≤k≤n In the worst case T(n) = T(n − 1) + O(n). Then. median can be found in linear time. Then. which means T(n) = O(n2 ). Then. Happens if array is already sorted and pivot is always first element. T(n) = T(k − 1) + T(n − k) + O(n) If k = n/2 then T(n) = T( n/2 −1)+T( n/2 )+O(n) ≤ 2T(n/2)+O(n).Time Analysis Let k be the rank of the chosen pivot. Typically.

Part III Fast Multiplication Sariel (UIUC) CS473 32 Spring 2011 32 / 42 .

3141 ×2718 25128 3141 21987 6282 8537238 Sariel (UIUC) CS473 33 Spring 2011 33 / 42 . compute their product.Multiplying Numbers Problem Given two n-digit numbers x and y. Grade School Multiplication Compute “partial product” by multiplying each digit of y with x and adding the partial products.

Time Analysis of Grade School Multiplication Each partial product: Θ(n) Number of partial products: Θ(n) Addition of partial products: Θ(n2 ) Total time: Θ(n2 ) Sariel (UIUC) CS473 34 Spring 2011 34 / 42 .

A Trick of Gauss Carl Fridrich Gauss: 1777–1855 “Prince of Mathematicians” Observation: Multiply two complex numbers: (a + bi) and (c + di) (a + bi)(c + di) = ac − bd + (ad + bc)i How many multiplications do we need? Only 3! If we do extra additions and subtractions. bd. (a + b)(c + d). Then (ad + bc) = (a + b)(c + d) − ac − bd Sariel (UIUC) CS473 35 Spring 2011 35 / 42 . Compute ac.

Compute ac. (a + b)(c + d).A Trick of Gauss Carl Fridrich Gauss: 1777–1855 “Prince of Mathematicians” Observation: Multiply two complex numbers: (a + bi) and (c + di) (a + bi)(c + di) = ac − bd + (ad + bc)i How many multiplications do we need? Only 3! If we do extra additions and subtractions. Then (ad + bc) = (a + b)(c + d) − ac − bd Sariel (UIUC) CS473 35 Spring 2011 35 / 42 . bd.

Compute ac.A Trick of Gauss Carl Fridrich Gauss: 1777–1855 “Prince of Mathematicians” Observation: Multiply two complex numbers: (a + bi) and (c + di) (a + bi)(c + di) = ac − bd + (ad + bc)i How many multiplications do we need? Only 3! If we do extra additions and subtractions. bd. (a + b)(c + d). Then (ad + bc) = (a + b)(c + d) − ac − bd Sariel (UIUC) CS473 35 Spring 2011 35 / 42 .

y0 Therefore xy = (10n/2 xL +xR )(10n/2 yL +yR ) = 10n xL yL +10n/2 (xL yR +xR yL )+xR Sariel (UIUC) CS473 36 Spring 2011 36 / 42 . . . y0 x = 10n/2 xL + xR where xL = xn−1 . . xn/2 and xR = xn/2−1 . . . yn/2 and yR = yn/2−1 . .Divide and Conquer Assume n is a power of 2 for simplicity and numbers are in decimal. . . . x0 y = 10n/2 yL + yR where yL = yn−1 . . . x = xn−1 xn−2 . . x0 and y = yn−1 yn−2 .

Example 1234 × 5678 = (100 × 12 + 34) × (100 × 56 + 78) = 10000 × 12 × 56 +100 × (12 × 78 + 34 × 56) +34 × 78 Sariel (UIUC) CS473 37 Spring 2011 37 / 42 .

No better than grade school multiplication! Can we invoke Gauss’s trick here? Sariel (UIUC) CS473 38 Spring 2011 38 / 42 .Time Analysis xy = (10n/2 xL +xR )(10n/2 yL +yR ) = 10n xL yL +10n/2 (xL yR +xR yL )+xR 4 recursive multiplications of number of size n/2 each plus 4 additions and left shifts (adding enough 0’s to the right) T(n) = 4T(n/2) + O(n) T(1) = O(1) T(n) = Θ(n2 ).

Time Analysis xy = (10n/2 xL +xR )(10n/2 yL +yR ) = 10n xL yL +10n/2 (xL yR +xR yL )+xR 4 recursive multiplications of number of size n/2 each plus 4 additions and left shifts (adding enough 0’s to the right) T(n) = 4T(n/2) + O(n) T(1) = O(1) T(n) = Θ(n2 ). No better than grade school multiplication! Can we invoke Gauss’s trick here? Sariel (UIUC) CS473 38 Spring 2011 38 / 42 .

Time Analysis xy = (10n/2 xL +xR )(10n/2 yL +yR ) = 10n xL yL +10n/2 (xL yR +xR yL )+xR 4 recursive multiplications of number of size n/2 each plus 4 additions and left shifts (adding enough 0’s to the right) T(n) = 4T(n/2) + O(n) T(1) = O(1) T(n) = Θ(n2 ). No better than grade school multiplication! Can we invoke Gauss’s trick here? Sariel (UIUC) CS473 38 Spring 2011 38 / 42 .

No better than grade school multiplication! Can we invoke Gauss’s trick here? Sariel (UIUC) CS473 38 Spring 2011 38 / 42 .Time Analysis xy = (10n/2 xL +xR )(10n/2 yL +yR ) = 10n xL yL +10n/2 (xL yR +xR yL )+xR 4 recursive multiplications of number of size n/2 each plus 4 additions and left shifts (adding enough 0’s to the right) T(n) = 4T(n/2) + O(n) T(1) = O(1) T(n) = Θ(n2 ).

Improving the Running Time xy = (10n/2 xL +xR )(10n/2 yL +yR ) = 10n xL yL +10n/2 (xL yR +xR yL )+xR Gauss trick: xL yR + xR yL = (xL + xR )(yL + yR ) − xL yL − xR yR Recursively compute only xL yL . (xL + xR )(yL + yR ).585 ) Sariel (UIUC) CS473 39 Spring 2011 39 / 42 T(1) = O(1) . xR yR . Time Analysis Running time is given by T(n) = 3T(n/2) + O(n) which means T(n) = O(nlog2 3 ) = O(n1.

Improving the Running Time xy = (10n/2 xL +xR )(10n/2 yL +yR ) = 10n xL yL +10n/2 (xL yR +xR yL )+xR Gauss trick: xL yR + xR yL = (xL + xR )(yL + yR ) − xL yL − xR yR Recursively compute only xL yL . (xL + xR )(yL + yR ). Time Analysis Running time is given by T(n) = 3T(n/2) + O(n) which means T(n) = O(nlog2 3 ) = O(n1.585 ) Sariel (UIUC) CS473 39 Spring 2011 39 / 42 T(1) = O(1) . xR yR .

(xL + xR )(yL + yR ). xR yR .Improving the Running Time xy = (10n/2 xL +xR )(10n/2 yL +yR ) = 10n xL yL +10n/2 (xL yR +xR yL )+xR Gauss trick: xL yR + xR yL = (xL + xR )(yL + yR ) − xL yL − xR yR Recursively compute only xL yL . Time Analysis Running time is given by T(n) = 3T(n/2) + O(n) which means T(n) = O(nlog2 3 ) = O(n1.585 ) Sariel (UIUC) CS473 39 Spring 2011 39 / 42 T(1) = O(1) .

xR yR .Improving the Running Time xy = (10n/2 xL +xR )(10n/2 yL +yR ) = 10n xL yL +10n/2 (xL yR +xR yL )+xR Gauss trick: xL yR + xR yL = (xL + xR )(yL + yR ) − xL yL − xR yR Recursively compute only xL yL . Time Analysis Running time is given by T(n) = 3T(n/2) + O(n) which means T(n) = O(nlog2 3 ) = O(n1. (xL + xR )(yL + yR ).585 ) Sariel (UIUC) CS473 39 Spring 2011 39 / 42 T(1) = O(1) .

State of the Art Sch¨nhage-Strassen 1971: O(n log n log log n) time using o Fast-Fourier-Transform (FFT) Martin F¨rer 2007: O(n log n2O(log u ∗ n) ) time Conjecture: there is an O(n log n) time algorithm Sariel (UIUC) CS473 40 Spring 2011 40 / 42 .

Saving a multiplication: T(n) = 3T(n/2) + O(n). Work at depth i is 4i n/2i and 3i n/2i respectively: number of children at depth i times the work at each child Total work is therefore n L 2i and n L (3/2)i respectively. Claim: T(n) = Θ(n2 ). T(1) = 1. depth of recursion L = log n. T(1) = 1.5 ) Use recursion tree method: In both cases. Claim: T(n) = Θ(n1+log 1. i=0 i=0 Sariel (UIUC) CS473 41 Spring 2011 41 / 42 .Analyzing the Recurrences Basic divide and conquer: T(n) = 4T(n/2) + O(n).

T(1) = 1. Work at depth i is 4i n/2i and 3i n/2i respectively: number of children at depth i times the work at each child Total work is therefore n L 2i and n L (3/2)i respectively. Claim: T(n) = Θ(n2 ). Claim: T(n) = Θ(n1+log 1.Analyzing the Recurrences Basic divide and conquer: T(n) = 4T(n/2) + O(n). i=0 i=0 Sariel (UIUC) CS473 41 Spring 2011 41 / 42 . T(1) = 1. depth of recursion L = log n. Saving a multiplication: T(n) = 3T(n/2) + O(n).5 ) Use recursion tree method: In both cases.

Recursion tree analysis Sariel (UIUC) CS473 42 Spring 2011 42 / 42 .

Notes

Sariel (UIUC)

CS473

43

Spring 2011

43 / 42

Notes

Sariel (UIUC)

CS473

44

Spring 2011

44 / 42

Notes

Sariel (UIUC)

CS473

45

Spring 2011

45 / 42

Notes Sariel (UIUC) CS473 46 Spring 2011 46 / 42 .