CS 473: Algorithms: Chandra Chekuri Chekuri@cs - Illinois.edu 3228 Siebel Center
CS 473: Algorithms: Chandra Chekuri Chekuri@cs - Illinois.edu 3228 Siebel Center
Chandra Chekuri
chekuri@cs.illinois.edu
3228 Siebel Center
University of Illinois, Urbana-Champaign
Fall 2010
Chekuri CS473 1
Reductions
Recursion
Part I
Chekuri CS473 2
Reductions
Recursion
Reduction
Chekuri CS473 3
Reductions
Recursion
Chekuri CS473 4
Reductions
Recursion
Naive algorithm:
for i = 1 to n − 1 do
for j = i + 1 to n do
if (A[i] = A[j])
return YES
endfor
endfor
return NO
Chekuri CS473 4
Reductions
Recursion
Naive algorithm:
for i = 1 to n − 1 do
for j = i + 1 to n do
if (A[i] = A[j])
return YES
endfor
endfor
return NO
Running time:
Chekuri CS473 4
Reductions
Recursion
Naive algorithm:
for i = 1 to n − 1 do
for j = i + 1 to n do
if (A[i] = A[j])
return YES
endfor
endfor
return NO
Chekuri CS473 4
Reductions
Recursion
Reduction to Sorting
Sort A
for i = 1 to n − 1 do
if (A[i] = A[i + 1]) then
return YES
endfor
return NO
Chekuri CS473 5
Reductions
Recursion
Reduction to Sorting
Sort A
for i = 1 to n − 1 do
if (A[i] = A[i + 1]) then
return YES
endfor
return NO
Chekuri CS473 5
Reductions
Recursion
Chekuri CS473 6
Reductions
Recursion
Chekuri CS473 6
Reductions
Recursion
Recursion
Chekuri CS473 7
Reductions
Recursion
Recursion
Chekuri CS473 7
Reductions
Recursion
Recursion
Chekuri CS473 8
Reductions
Recursion
Selection Sort
SelectSort(A[1..n]):
If (n = 1) return
Find smallest number in A. Let A[i] be smallest number
Swap A[1] and A[i]
SelectSort(A[2..n])
Chekuri CS473 9
Reductions
Recursion
Selection Sort
SelectSort(A[1..n]):
If (n = 1) return
Find smallest number in A. Let A[i] be smallest number
Swap A[1] and A[i]
SelectSort(A[2..n])
T (n) =
Chekuri CS473 9
Reductions
Recursion
Selection Sort
SelectSort(A[1..n]):
If (n = 1) return
Find smallest number in A. Let A[i] be smallest number
Swap A[1] and A[i]
SelectSort(A[2..n])
T (n) = Θ(n2 ).
Chekuri CS473 9
one of the other needles, tower, temple, and Brahmins alike will crumble into dust, and with a
Reductions
thunderclap the world will vanish. Recursion
Of course, being good computer scientists, we read this story and immediately substitute n for the
Tower of Hanoi
hardwired constant sixty-four.4 How can we move a tower of n disks from one needle to another,
using a third needles as an occasional placeholder, never placing any disk on top of a smaller disk?
The trick to solving this puzzle is to think recursively. Instead of trying to solve the entire puzzle
all at once, let’s concentrate on moving just the largest disk. We can’t move it at the beginning,
Move stack of n disks from peg 0 to peg 2, one disk at a time.
because all the other disks are covering it; we have to move those n − 1 disks to the third needle
before Rule:
we can cannot
move theput a larger
nth disk. disk after
And then on awesmaller
move the disk.
nth disk, we have to move those
n − 1 disks back on top of it. So now all we have to
Question: what is a strategy and how many moves figure out is how to. .does
. it take?
3
This English translation is from W. W. Rouse Ball and H. S. M.
Chekuri Coxeter’s book Mathematical Recreations and Essays.
CS473 10
Reductions
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, which we can gleefully hand off to the
Recursion Fairy (or, to carry the original story further, to the junior monks at the temple).
recursion
recursion
The Tower of Hanoi algorithm; ignore everything but the bottom disk
Our algorithm does make one subtle but important assumption: there is a largest disk. In other
words, our recursive algorithm works for any n ≥ 1, but it breaks down when n = 0. We must
handle that base case directly. Fortunately, the monks at Benares, being good Buddhists, are quite
adept at moving zero disks from one needle to another.
Chekuri CS473 11
Reductions
Recursion
Recursive Algorithm
Chekuri CS473 12
Reductions
Recursion
Recursive Algorithm
Chekuri CS473 12
Reductions
Recursion
Recursive Algorithm
Chekuri CS473 12
Reductions
Recursion
Analysis
T (n) = 2T (n − 1) + 1
= 22 T (n − 2) + 2 + 1
= ...
= 2i T (n − i) + 2i−1 + 2i−2 + . . . + 1
= ...
= 2n−1 T (1) + 2n−2 + . . . + 1
= 2n−1 + 2n−2 + . . . + 1
= (2n − 1)/(2 − 1) = 2n − 1
Chekuri CS473 13
Reductions
Recursion
Non-recursive Algorithm 1:
Always move smallest disk forward if n is even, backward if n
is odd.
Never move the same disk twice in a row.
Done when no legal move.
Chekuri CS473 14
Reductions
Recursion
Non-recursive Algorithm 1:
Always move smallest disk forward if n is even, backward if n
is odd.
Never move the same disk twice in a row.
Done when no legal move.
Non-recursive Algorithm 2:
Let ρ(n) be the smallest integer k such that n/2k is not an
integer. Example: ρ(40) = 4, ρ(18) = 2.
In step i move disk ρ(i) forward if n − i is even and backward
if n − i is odd.
Chekuri CS473 14
Reductions
Recursion
Non-recursive Algorithm 1:
Always move smallest disk forward if n is even, backward if n
is odd.
Never move the same disk twice in a row.
Done when no legal move.
Non-recursive Algorithm 2:
Let ρ(n) be the smallest integer k such that n/2k is not an
integer. Example: ρ(40) = 4, ρ(18) = 2.
In step i move disk ρ(i) forward if n − i is even and backward
if n − i is odd.
Moves are exactly same as those of recursive algorithm. Prove by
induction.
Chekuri CS473 14
Merge Sort
Quick Sort
Part II
Chekuri CS473 15
Merge Sort
Quick Sort
Chekuri CS473 16
Merge Sort
Quick Sort
Chekuri CS473 16
Merge Sort
Quick Sort
Chekuri CS473 16
Merge Sort
Quick Sort
Chekuri CS473 16
Merge Sort
Quick Sort
Chekuri CS473 16
Merge Sort
Quick Sort
Chekuri CS473 16
Merge Sort
Quick Sort
Chekuri CS473 16
Merge Sort
Quick Sort
Sorting
Chekuri CS473 17
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Chekuri CS473 18
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Chekuri CS473 18
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Chekuri CS473 18
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Chekuri CS473 18
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Chekuri CS473 18
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
AG LO R H I M S T
A
Chekuri CS473 19
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
AG LO R H I M S T
AG
Chekuri CS473 19
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
AG LO R H I M S T
AG H
Chekuri CS473 19
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
AG LO R H I M S T
AG H I
Chekuri CS473 19
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
AG LO R H I M S T
AG H I LM O R S T
Chekuri CS473 19
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
AG LO R H I M S T
AG H I LM O R S T
Chekuri CS473 19
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Running Time
Chekuri CS473 20
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Running Time
Chekuri CS473 20
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Running Time
Chekuri CS473 20
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Chekuri CS473 21
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Chekuri CS473 21
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Recursion Trees
MergeSort: n is a power of 2
Chekuri CS473 22
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Recursion Trees
MergeSort: n is a power of 2
2 Identify a pattern.
Chekuri CS473 22
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Recursion Trees
MergeSort: n is a power of 2
Chekuri CS473 22
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Recursion Trees
MergeSort: n is a power of 2
Chekuri CS473 22
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Recursion Trees
MergeSort: n is a power of 2
Chekuri CS473 22
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Mergesort Analysis
When n is not a power of 2
Chekuri CS473 23
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Mergesort Analysis
When n is not a power of 2
Chekuri CS473 23
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Mergesort Analysis
When n is not a power of 2
Chekuri CS473 23
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Mergesort Analysis
When n is not a power of 2
Chekuri CS473 23
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Recursion Trees
MergeSort: n is not a power of 2
Chekuri CS473 24
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Chekuri CS473 25
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Chekuri CS473 25
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Induction Step
We have
Chekuri CS473 26
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Chekuri CS473 27
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Typically you do the algebra with α and then show at the end that
α can be chosen to be sufficiently large constant.
Chekuri CS473 27
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
But we want to show that T (n) ≤ αcn! So guess does not work
for any constant α. Suggests that our guess is incorrect.
Chekuri CS473 28
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Chekuri CS473 29
Merge Sort
Merge Sort
Analysis
Quick Sort
Solving Recurrences
Chekuri CS473 29
Merge Sort
Quick Sort
Quick Sort
Quick Sort[Hoare]
1 Pick a pivot element from array
2 Split array into 3 subarrays: those smaller than pivot, those
larger than pivot, and the pivot itself.
Chekuri CS473 30
Merge Sort
Quick Sort
Quick Sort
Quick Sort[Hoare]
1 Pick a pivot element from array
2 Split array into 3 subarrays: those smaller than pivot, those
larger than pivot, and the pivot itself.
Chekuri CS473 30
Merge Sort
Quick Sort
Quick Sort
Quick Sort[Hoare]
1 Pick a pivot element from array
2 Split array into 3 subarrays: those smaller than pivot, those
larger than pivot, and the pivot itself. Linear scan of array
does it. Time is O(n)
3 Recursively sort the subarrays, and concatenate them.
Example:
array: 16, 12, 14, 20, 5, 3, 18, 19, 1
pivot: 16
split into 12, 14, 5, 3, 1 and 20, 19, 18 and recursively sort
put them together with pivot in middle
Chekuri CS473 30
Merge Sort
Quick Sort
Quick Sort
Quick Sort[Hoare]
1 Pick a pivot element from array
2 Split array into 3 subarrays: those smaller than pivot, those
larger than pivot, and the pivot itself. Linear scan of array
does it. Time is O(n)
3 Recursively sort the subarrays, and concatenate them.
Example:
array: 16, 12, 14, 20, 5, 3, 18, 19, 1
pivot: 16
split into 12, 14, 5, 3, 1 and 20, 19, 18 and recursively sort
put them together with pivot in middle
Chekuri CS473 30
Merge Sort
Quick Sort
Time Analysis
Chekuri CS473 31
Merge Sort
Quick Sort
Time Analysis
Chekuri CS473 31
Merge Sort
Quick Sort
Time Analysis
Chekuri CS473 31
Merge Sort
Quick Sort
Time Analysis
Chekuri CS473 31
The Problem
Algorithmic Solution
Part III
Fast Multiplication
Chekuri CS473 32
The Problem
Algorithmic Solution
Multiplying Numbers
Chekuri CS473 33
Grade School Multiplication
The Problem
Divide and Conquer Solution
Algorithmic Solution
Karatsuba’s Algorithm
Chekuri CS473 34
Grade School Multiplication
The Problem
Divide and Conquer Solution
Algorithmic Solution
Karatsuba’s Algorithm
A Trick of Gauss
Chekuri CS473 35
Grade School Multiplication
The Problem
Divide and Conquer Solution
Algorithmic Solution
Karatsuba’s Algorithm
A Trick of Gauss
Chekuri CS473 35
Grade School Multiplication
The Problem
Divide and Conquer Solution
Algorithmic Solution
Karatsuba’s Algorithm
A Trick of Gauss
Chekuri CS473 35
Grade School Multiplication
The Problem
Divide and Conquer Solution
Algorithmic Solution
Karatsuba’s Algorithm
Chekuri CS473 36
Grade School Multiplication
The Problem
Divide and Conquer Solution
Algorithmic Solution
Karatsuba’s Algorithm
Example
Chekuri CS473 37
Grade School Multiplication
The Problem
Divide and Conquer Solution
Algorithmic Solution
Karatsuba’s Algorithm
Time Analysis
Chekuri CS473 38
Grade School Multiplication
The Problem
Divide and Conquer Solution
Algorithmic Solution
Karatsuba’s Algorithm
Time Analysis
Chekuri CS473 38
Grade School Multiplication
The Problem
Divide and Conquer Solution
Algorithmic Solution
Karatsuba’s Algorithm
Time Analysis
Chekuri CS473 38
Grade School Multiplication
The Problem
Divide and Conquer Solution
Algorithmic Solution
Karatsuba’s Algorithm
Time Analysis
Chekuri CS473 38
Grade School Multiplication
The Problem
Divide and Conquer Solution
Algorithmic Solution
Karatsuba’s Algorithm
Chekuri CS473 39
Grade School Multiplication
The Problem
Divide and Conquer Solution
Algorithmic Solution
Karatsuba’s Algorithm
Chekuri CS473 39
Grade School Multiplication
The Problem
Divide and Conquer Solution
Algorithmic Solution
Karatsuba’s Algorithm
which means
Chekuri CS473 39
Grade School Multiplication
The Problem
Divide and Conquer Solution
Algorithmic Solution
Karatsuba’s Algorithm
Chekuri CS473 39
Grade School Multiplication
The Problem
Divide and Conquer Solution
Algorithmic Solution
Karatsuba’s Algorithm
Chekuri CS473 40
Grade School Multiplication
The Problem
Divide and Conquer Solution
Algorithmic Solution
Karatsuba’s Algorithm
Chekuri CS473 41
Grade School Multiplication
The Problem
Divide and Conquer Solution
Algorithmic Solution
Karatsuba’s Algorithm
Chekuri CS473 41
Grade School Multiplication
The Problem
Divide and Conquer Solution
Algorithmic Solution
Karatsuba’s Algorithm
Chekuri CS473 42