Professional Documents
Culture Documents
PART ONE
2. Recurrences
http://aofa.cs.princeton.edu
A N A LY T I C C O M B I N AT O R I C S
PART ONE
2. Recurrences
• Computing values
• Telescoping
• Types of recurrences
OF
• Mergesort
• Master Theorem
http://aofa.cs.princeton.edu
2a.Recur.Values
What is a recurrence?
recurrence
= − + − ≥ = =
MUST specify
for all N with
sequence
initial conditions
0, 1, 1, 2, 3, 5, 8, 13, 21, ...
3
What is a recurrence?
recurrence program
public class Quick
= + + ( + ) {
private static int partition(Comparable[] a, int lo, int hi)
{
int i = lo, j = hi+1;
while (true)
≥ = {
while (less(a[++i], a[lo])) if (i == hi) break;
while (less(a[lo], a[--j])) if (j == lo) break;
if (i >= j) break;
sequence exch(a, i, j);
}
exch(a, lo, j);
0, 2, 5, 8 2/3, 12 5/6, 17 2/5,... return j;
}
F(49) F(48)
NO, NO, NO: Takes exponential time! F(48) F(47) F(47) F(46)
5
Common-sense starting point for solving any recurrence
First step: Download "standard model" from Algorithms, 4th edition booksite.
http://algs4.cs.princeton.edu
6
Common-sense starting point for solving any recurrence
Ex. 2: Quicksort =( + ) +
QuickSeq.java
public class QuickSeq implements Sequence
{
private final double[] c; % java QuickSeq 15
0.000000
public QuickSeq(int maxN) 2.000000
{ 5.000000
c = new double[maxN+1]; 8.666667
c[0] = 0; 12.833333
for (int N = 1; N <= maxN; N++) 17.400000
c[N] = (N+1)*c[N-1]/N + 2; 22.300000
} 27.485714
32.921429
public double eval(int N) 38.579365
{ return c[N]; } 44.437302
50.477056
public static void main(String[] args) 56.683478
{ 63.043745
// Similar to Fib.java. 69.546870
}
}
8
Common-sense starting point for solving any recurrence
9
A N A LY T I C C O M B I N AT O R I C S
PART ONE
2. Recurrences
• Computing values
• Telescoping
• Types of recurrences
OF
• Mergesort
• Master Theorem
http://aofa.cs.princeton.edu
2a.Recur.Values
A N A LY T I C C O M B I N AT O R I C S
PART ONE
2. Recurrences
• Computing values
• Telescoping
• Types of recurrences
OF
• Mergesort
• Master Theorem
http://aofa.cs.princeton.edu
2b.Recur.Telescope
Telescoping a (linear first-order) recurrence
Linear first-order recurrences telescope to a sum.
Example 1.
= + =
Do it again = +( )+( )+
( + )
Evaluate sum = Check.
( + ) ( )
= +
geometric series =
<
( )
arithmetic series = =
<
+
binomial (upper) =
+
see Knuth volume 1
for many more
binomial theorem =( + )
Harmonic numbers =
+
Vandermonde convolution =
13
Telescoping a (linear first-order) recurrence (continued)
Example 2.
= + =
Divide by 2n = +
Telescope to a sum = =
=
Check.
= ( ) +
Challenge: How do we find the summation factor?
14
Telescoping a (linear first-order) recurrence (continued)
= + + > =
summation factor:
+
... = +
Divide by n+1 = +
+ +
2
Telescope = = +
+
= ( + )( + )
= + + > = = ( + )( + )
= + = = ( )=
= + = = ( )=
= ( )
= + = /
= ( / + / + / )
= /
Proof
+ ( )+ = ( + )( )+
= ( + )( + )
16
In-class exercise 2.
=( ) + > =
Hard way:
Easy way: = =
=
WHY?
17
A N A LY T I C C O M B I N AT O R I C S
PART ONE
Recurrences
• Computing values
• Telescoping
• Types of recurrences
OF
• Mergesort
• Master Theorem
http://aofa.cs.princeton.edu
2b.Recur.Telescope
A N A LY T I C C O M B I N AT O R I C S
PART ONE
Recurrences
• Computing values
• Telescoping
• Types of recurrences
OF
• Mergesort
• Master Theorem
http://aofa.cs.princeton.edu
2c.Recur.Types
Types of recurrences
linear an = nan 1 1
first
order
nonlinear an = 1/(1 + an 1)
linear an = an 1 + 2an 2
p
second order nonlinear an = an 1 an 2+ an 2
variable
coefficients an = nan 1 + (n 1)an 2 +1
20
Nonlinear first-order recurrences
! "
Example. (Newton’s method ) = + [Typical in scientific computing]
−
−
SqrtTwo.java
public class SqrtTwo implements Sequence
{
private final double[] c;
% java SqrtTwo 10
1.0
public SqrtTwo(int maxN)
{
1.5
c = new double[maxN+1]; 1.4166666666666665
c[0] = 1; quadratic convergence:
number of significant 1.4142156862745097
for (int N = 1; N <= maxN; N++)
c[N] = (c[N-1] + 2/c[N-1])/2; digits doubles for 1.4142135623746899
} each iteration 1.414213562373095
public double eval(int N) 1.414213562373095
{ return c[N]; } 1.414213562373095
public static void main(String[] args) 1.414213562373095
{ 1.414213562373095
int maxN = Integer.parseInt(args[0]);
SqrtTwo test = new SqrtTwo(maxN);
for (int i = 0; i < maxN; i++)
StdOut.println(test.eval(i));
}
}
21
Higher-order linear recurrences
[ Stay tuned for systematic solution using generating functions (next lecture) ]
Example 4.
= = =
Postulate that an = xn =
Divide by xn−2 + =
Factor ( )( )=
Solution is c0 = 1 and c1 = −1 =
22
Higher-order linear recurrences
[ Stay tuned for systematic solution using generating functions (next lecture) ]
Postulate that an = xn = +
Divide by xn−2 =
+
ˆ) = =
Factor ( )(
ˆ=
Form of solution must be = + ˆ
23
Higher-order linear recurrences (continued)
Complex roots? Stay tuned for systematic solution using GFs (next lecture)
24
Divide-and-conquer recurrences
Classic examples:
• Binary search
• Mergesort
• Batcher network
• Karatsuba multiplication
• Strassen matrix multiplication
25
A N A LY T I C C O M B I N AT O R I C S
PART ONE
Recurrences
• Computing values
• Telescoping
• Types of recurrences
OF
• Mergesort
• Master Theorem
http://aofa.cs.princeton.edu
2c.Recur.Types
A N A LY T I C C O M B I N AT O R I C S
PART ONE
Recurrences
• Computing values
• Telescoping
• Types of recurrences
OF
• Mergesort
• Master Theorem
http://aofa.cs.princeton.edu
2d.Recur.Mergesort
Warmup: binary search
Everyone’s first divide-and-conquer algorithm
28
Analysis of binary search (easy case)
= / + > =
= + > =
a0 +
+1
Telescope to a sum = =
+1
=
Check. = ( / )+
29
Analysis of binary search (general case)
• B1 = 1. 1101011 110101 1
Theorem. BN = lg N +1 +
= + <
Proof. Immediate by definition of . < + = =
N 1 2 3 4 5 6 7 8 9
binary 1 10 11 100 101 110 111 1000 1001
lg N 0 1.0 1.58... 2.0 2.32... 2.58... 2.80... 3 3.16...
lg N 0 1 1 2 2 2 2 3 3
lg N +1 1 2 2 3 3 3 3 4 4
30
Mergesort
Everyone’s second divide-and-conquer algorithm
31
Analysis of mergesort (easy case)
Solution: =
32
Analysis of mergesort (general case)
Solution: =
A. NO !
33
Coefficient of the linear term for mergesort
34
Analysis of mergesort (general case)
/ = ( + )/
Same formula for N+1. + = ( + )/ + ( + )/ + +
= / + / + + + 1 0 1 1 1
2 1 1 1 2
3 1 2 2 2
Subtract. + = / + / + 4 2 2 2 3
5 2 3 3 3
6 3 3 3 4
Define DN = CN+1 - CN. = / + 7 3 4 4 4
different 8 4 4 4 5
initial 9 4 5 5 5
Solve as for binary search. = lg + value
/ + = ( + )/
Telescope.
= + ( lg + )
<
35
Combinatorial correspondence
SN = number of bits in the binary rep. of all numbers < N
/2 /2 1
1 1 1 1
10 10 10 10
11 11 11 11
100 100 100 100
101 101 101 101
110 110 110 110
111 111 111 111
1000 = 1000 + 1000 + 1000
1001 1001 1001 1001
1010 1010 1010 1010
1011 1011 1011 1011
1100 1100 1100 1100
1101 1101 1101 1101
1110 1110 1110 1110
= /2 + /2 + 1
+1 37
Analysis of mergesort (summary)
Solution: =
+1
Theorem. Number of compares for mergesort is 2 +2
Notation: lg = lg {lg }
( )
{lg }
+
{lg }
=
( ) {lg } {lg }
✓
38
A N A LY T I C C O M B I N AT O R I C S
PART ONE
Recurrences
• Computing values
• Telescoping
• Types of recurrences
OF
• Mergesort
• Master Theorem
http://aofa.cs.princeton.edu
2d.Recur.Mergesort
A N A LY T I C C O M B I N AT O R I C S
PART ONE
Recurrences
• Computing values
• Telescoping
• Types of recurrences
OF
• Mergesort
• Master Theorem
http://aofa.cs.princeton.edu
2e.Recur.Master
Divide-and-conquer algorithms
Example 1 (mergesort): α = 2, β = 2, γ = 1, δ = 0 = / +
41
“Master Theorem” for divide-and-conquer algorithms
Suppose that an algorithm attacks a problem of size n by
dividing into α parts of size about n/β with extra cost Θ(nγ(log n)δ)
ANALYSIS
TO THE
ALGORITHMS
OF
S E C O N D E D I T I O N
Example: α = 3
PHILIPPE FLAJOLET
β=2
β=3
β=4
42
Typical “Master Theorem” applications
Master Theorem
Suppose that an algorithm attacks a problem of size N by
= ( (log ) < log
• Dividing into α parts of size about N/β.
+
= ( (log ) = log
• Solving recursively.
log
• Combining solutions with extra cost Θ(N γ(log N )δ)
= ( ) > log
43
Versions of the “Master Theorem”
ANALYSIS
TO THE
S E C O N D E D I T I O N
ROBERT SEDGEWICK
PHILIPPE FLAJOLET
Recurrences
• Computing values
• Telescoping
• Types of recurrences
OF
• Mergesort
• Master Theorem
http://aofa.cs.princeton.edu
2e.Recur.Master
Exercise 2.17
AN INTRODUCTION
ANALYSIS
TO THE
ALGORITHMS
OF
S E C O N D E D I T I O N
ROBERT SEDGEWICK
PHILIPPE FLAJOLET
46
Exercise 2.69
AN INTRODUCTION
ANALYSIS
TO THE
ALGORITHMS
OF
S E C O N D E D I T I O N
ROBERT SEDGEWICK
PHILIPPE FLAJOLET
47
Assignments for next lecture
AN INTRODUCTION
ANALYSIS
TO THE
ALGORITHMS
OF
S E C O N D E D I T I O N
4. Do Exercise 2.69.
48
A N A LY T I C C O M B I N AT O R I C S
PART ONE
2. Recurrences
http://aofa.cs.princeton.edu