Download presentation
Presentation is loading. Please wait.
1
Ch 4: Recurrences Ming-Te Chi
Algorithms Ch 4: Recurrences Ming-Te Chi Ch4 Recurrences
2
Recurrences When an algorithm contains a recursive call to itself, its running time can often described by a recurrence. A recurrence is an equation or inequality that describes a function in terms of its value of smaller inputs. Ch4 Recurrences
3
Recurrences─examples
Summation Factorial Ch4 Recurrences
4
Recurrences─examples
Summation Factorial Ch4 Recurrences
5
Termination conditions (boundary conditions)
Summation Factorial Ch4 Recurrences
6
Recurrences─CS oriented examples
Fibonacci number Merge sort Ch4 Recurrences
7
Influence of the boundary conditions
Fibonacci number Merge sort Ch4 Recurrences
8
Recurrences Substitution method Recursion-tree method Master method
Ch4 Recurrences
9
Technicalities Neglect certain technical details when stating and solving recurrences. A good example of a detail that is often glossed over is the assumption of integer arguments to functions. Boundary conditions is ignored. Omit floors, ceilings. Ch4 Recurrences
10
The maximum-subarray problem
Ch4 Recurrences
11
Ch4 Recurrences
12
Ch4 Recurrences
13
Matrix multiplication
Ch4 Recurrences
14
A simple divide-and-conquer algorithm
Ch4 Recurrences
15
* Partition problem: Index calculation vs copy entries
Ch4 Recurrences
16
Strassen’s method Ch4 Recurrences
17
Ch4 Recurrences
18
The substitution method: Mathematical induction
The substitution method for solving recurrence has two steps: 1. Guess the form of the solution. 2. Use mathematical induction to find the constants and show that the solution works. Powerful but can be applied only in cases when it is easy to guess the form of solution. Ch4 Recurrences
19
Can be used to establish either upper bound or lower bound on a recurrence.
Ch4 Recurrences
20
General principle of the Mathematical Induction
True for the trivial case, problem size =1. If it is true for the case of problem at step k, we can show that it is true for the case of problem at step k+1. Ch4 Recurrences
21
Example Determine the upper bound on the recurrence
(We may omit the initial condition later.) Guess Prove for some c > 0. Ch4 Recurrences
22
Inductive hypothesis: assume this bound holds for
The recurrence implies Ch4 Recurrences
23
Initial condition However Ch4 Recurrences
24
Making a good guess We guess
Making guess provides loose upper bound and lower bound. Then improve the gap. Ch4 Recurrences
25
Ch4 Recurrences
26
Ch4 Recurrences
27
Subtleties Guess Assume However, assume Ch4 Recurrences
28
Avoiding pitfalls Assume Hence (WRONG!) You cannot find such a c.
(WRONG!) You cannot find such a c. Ch4 Recurrences
29
Changing variables Ch4 Recurrences
30
4.4 the Recursion-tree method
Ch4 Recurrences
31
Ch4 Recurrences
32
Subproblem size for a node at depth i Total level of tree
Number of nodes at depth i Cost of each node at depth i Total cost at depth i Last level, depth , has nodes Ch4 Recurrences
33
Prove of We conclude, Ch4 Recurrences
34
The cost of the entire tree
Ch4 Recurrences
35
Ch4 Recurrences
36
Substitution method We want to Show that T(n) ≤ dn2 for some constant d > 0. Using the same constant c > 0 as before, we have Where the last step holds as long as d (16/13)c. Ch4 Recurrences
37
Ch4 Recurrences
38
Subproblem size for a node at depth i Total level of tree
Ch4 Recurrences
39
substitution method As long as d c/(lg3 – (2/3)). Ch4 Recurrences
40
4.5 Master method Master Theorem Ch4 Recurrences
41
Remarks 1: In the first case, f(n) must be polynomailly smaller than
That is, f(n) must be asymptotically smaller than by a factor of Similarly, in the third case, f(n) must be polynomailly larger than Also, the condition must be hold. Ch4 Recurrences
42
Remarks 2: The three cases in the master theorem do not cover all the possibilities. There is a gap between cases 1 and 2 when f(n) is smaller than but not polynomailly smaller. Similarly, there is a gap between cases 2 and 3 when f(n) is larger than but not polynomailly larger (or the additional condition does not hold.) Ch4 Recurrences
43
Example 1: Ch4 Recurrences
44
Example 2: Ch4 Recurrences
45
Example 3: Ch4 Recurrences
46
Example 4: Ch4 Recurrences
47
Example 5: Ch4 Recurrences
48
Remarks 3: The master theory does not apply to the recurrence
even though it has the proper form: a = 2, b=2, f(n)= n lgn, and It might seem that case 3 should apply, since f(n)= n lgn is asymptotically larger than But it is not polynomially larger. Ch4 Recurrences
49
Remarks 3 (continue) However, the master method augments the conditions Case 2 can be applied. Ch4 Recurrences
50
Example 6: Ch4 Recurrences
51
Example 7: Ch4 Recurrences
52
Ch4 Recurrences
53
Ch4 Recurrences
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.