Week 03 (Reccurence)
Week 03 (Reccurence)
Lecturer:
Recurrences
&
Master Method
Lecture Outline
T( n ) = Sub-
problem
Size (n/2)
2 * T( n / 2) + O( n)
Number Of Time required for Work Dividing
+
Subproblems * each Subproblem and Combining
The divide- and- conquer
design paradigm
Binary search
Find an element in a sorted array:
1. Divide: Check middle element.
2. Conquer: Recursively search 1 subarray.
3. Combine: Trivial.
Recurrence for binary search
T( n) =
1 * T( n / 2) + Θ(1)
Number Of Time required for Work Dividing
+
Subproblems * each Subproblem and Combining
Recurrences
Running times of algorithms with recursive calls can be
described using recurrences.
A recurrence is an equation or inequality that describes a
function in terms of its value on smaller inputs.
For divide and conquer algorithms:
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
T( n ) = 2iT(n/2i) + in
= n T(1) + n lg n T(n) = O(nlgn)
= n + n lg n
Repeated Substitution
Method
The procedure is straightforward:
Substitute, Expand, Substitute, Expand, …
Observe a pattern and determine the expression after
the i-th substitution.
Find out what the highest value of i (number of
iterations, e.g., lg n) should be to get to the base case
of the recurrence (e.g., T(1)).
Insert the value of T(1) and the expression of i into
your expression.
Another Example…
2 if n 1
T (n)
2T ( n / 2) 2n 3 if n 1
T(n) = 2T(n/2) + 2n + 3 substitute
= 2(2T(n/4) + n + 3)
3 + 2n +3 expand
= 22T(n/4) + 4n + 2x3 + 3 substitute
= 22(2T(n/8) + n/2 + 3)
3 + 4n + 2x3 + 3
expand
= 23T(n/23) + 2x3n + 3x(22+21+20) observe
pattern
T(n) = 2iT(n/2i) + 2ni+ 3
= 2iT(1) + 2ni + 3 (2i-1)
= n + 2n lg n + 3(n - 1) T(n) = O(nlgn)
= n+2n lg n + 3n - 3
Substitution Method
T(n) = 4T(n/2) + n
4c(n/2)2 + n
= cn2 + n
cn2
...Substitution Method
T(n) = 4T(n/2) + n
4(c(n/2)2 – d(n/2)) + n
= cn2 – 2dn + n
= (cn2 – dn) – (dn – n)
cn2 – dn if d
Don’t miss the next
class !!!!!!!!!!!!!!!!!!