Lec 5
Lec 5
Based on: Thomas H. Cormen, Charles E. Leiserson, and Ronald L. Rivest, “Introduction to Algorithms”, 3rd Edition, The MIT
Press, 2009.
Last Lecture
◼ Review for Recurrence Relations
◼ Divide and Conquer Design Approach
◼ Binary Search
◼ Merge Sort
◼ Recurrence Relation for Merge Sort
◼ Analysis of Merge Sort Recurrence
Page 2
Contents
◼ Recurrence Tree for Solving Recurrences
◼ Master Method for Solving Recurrences
◼ Iteration Method for Solving Recurrences
◼ Matrix Multiplication: Simple Divide and Conquer
◼ Strassen’s Algorithm for Matrix Multiplication
Page 3
Recurrence Tree for Solving
Recurrences
• The number of nodes at depth d in
a perfect binary tree = 2d
4
Tree Termonology
L1.7
Recursion tree
T( n )
L1.8
Recursion tree
T(n/2) T(n/2)
L1.9
Recursion tree
cn/2 cn/2
L1.10
Recursion tree
cn/2 cn/2
Q(1)
L1.11
Recursion tree
cn/2 cn/2
Q(1)
L1.12
Recursion tree
cn/2 cn/2
Q(1)
L1.13
Recursion tree
cn/2 cn/2 cn
Q(1)
L1.14
Recursion tree
cn/2 cn/2 cn
Q(1)
L1.15
Recursion tree
cn/2 cn/2 cn
L1.16
Recursion tree
Solve T(n) = 2T(n/2) + c n, where c > 0 is constant.
cn cn
cn/2 cn/2 cn
c Number of leaves = n cn
Note that the tree here is not balanced: the longest path is
the rightmost one, and its length is log3/2 n 18
4.5 P. 93- The Master Method for Solving Recurrences
◼ Theorem 4.1. (The Master Theorem): Let a ≥ 1 and b > 1
be constants, let f(n) be a function, and
- T(n) be defined on the nonnegative integers by the recurrence
T(n) = aT(n/b) + f(n)
where we interpret n/b as ⌊n/b⌋ or ⌈n/b⌉.
Then T(n) can be bounded asymptotically as follows:
19
The Master Theorem
T(n) = ( n lg n )
Then Case 1 and Case 2 can not be applied
Try Case 3
Case 3
24
The Master Theorem
Example:
The Master Theorem
T(n) = aT(n/b) + f (n) where f(n) (nd), d 0
26
Solving Recurrence Relations: Iteration Method
◼ Convert the recursive relation into a summation
and try to bound it using known series
◼ Example:
27
Iteration Method
Matrix Multiplication
◼ Compute the matrix product C = A x B, for two n x n
matrices
31
Summary
◼ Recurrence Tree for Solving Recurrences
◼ Master Method for Solving Recurrences
◼ Iteration Method for Solving Recurrences
◼ Matrix Multiplication
Page 32
Next Lecture
◼ Matrix Multiplication: Simple Divide and Conquer
◼ Strassen’s Algorithm for Matrix Multiplication
◼ Heap Sort Algorithms
Page 33