Professional Documents
Culture Documents
solving_trivial_problem if n 1
T (n)
num_pieces T (n / subproblem_size_factor) dividing combining if n 1
• Example: Merge Sort
(1) if n 1
T (n)
2T (n / 2) ( n) if n 1
Solving Recurrences
• Repeated (backward) substitution method
• Expanding the recurrence by substitution and noticing a pattern
• Substitution method
• guessing the solutions
• verifying the solution by the mathematical induction
• Recursion-trees
• Master method
• templates for different classes of recurrences
Repeated Substitution Method
• The procedure is straightforward:
• Substitute
• Expand
• Substitute
• Expand
• …
• Observe a pattern and write how your expression looks after the
i-th substitution
• Find out what the value of i (e.g., lgn) should be to get the base
case of the recurrence (say T(1))
• Insert the value of T(1) and the expression of i into your
expression
Repeated Substitution (Ex. 1)
• Let’s find the running time
1 if n 1
T ( n)
2T (n / 2) n if n 1
T (n) 2T n / 2 n substitute
2 2T n / 4 n / 2 n expand
2 2 T ( n / 4) 2 n substitute
2 2 (2T ( n / 8) n / 4) 2 n expand
23 T ( n / 8) 3n observe the pattern
T (n) 2i T ( n / 2i ) in
2 lg n T ( n / n ) n lg n n n lg n
j 0
2lg n T (n / n) 2n lg n 3(2lg n 1) 5n 2n lg n 3
Repeated Substitution (Ex. 3)
1 if n 1
T ( n)
4T (n 1) 1 if n 1
T (n) 4T n 1 1 substitute
4 4T n 2 1 1 expand
42 T n 2 4 1 substitute
42 4T n 3 1 4 1 expand
43T n 3 4 2 +41 +40 observe the pattern
i 1
T ( n) 4 T ( n i ) 4 j
i
j 0
n 1 4n1 1 4n 1
4 T (1)
4 1 3