Lecture # 21 - New
Lecture # 21 - New
Lecture # 21 - New
Lecture No 7
Lecture No 8
(Analysis Framework.)
T(n) = d+ aT(m) + c
T(n/2)
T(1) = d
T(n) = T(n/2) + c
T(n-1)
T(n-1)
T(n/2)
T(n/2)
T(n/2)
T(n/2)
Merge(A, p, q, r)
MergeSort (A, p, r) // sort A[p..r] by divide &
1 n1 q – p + 1
conquer
2 n2 r – q
1 if p < r
3 for i 1 to n1
2 then q (p+r)/2
4 do L[i] A[p + i – 1]
3 MergeSort (A, p, q) 5 for j 1 to n2
4 MergeSort (A, q+1, r) 6 do R[j] A[q + j]
5 Merge (A, p, q, r) // merges A[p..q] with7 L[n1+1]
A[q+1..r] 8 R[n2+1]
9 i1
10 j1
11 for k p to r
12 do if L[i] R[j]
13 then A[k] L[i]
14 ii+1
15 else A[k] R[j]
16 jj+1
Decrease-by-One:
T (n) = T (n − 1) + f (n)
Decrease-by-a-Constant-Factor:
T (n) = T (n/b) + f (n),
Divide-and-Conquer
T (n) = aT (n/b) + f (n)
T(n) = T(n-1) + 1
T(n-1) = T(n-2) + 1
T(n-2) = T(n-3) + 1
…
T(2) = T(1 ) + 1
T(n) = T(n-1) + c
T(n) = T(n-2) + c + c = T(n-2) + 2c
T (n) = T (n − 1) + f (n)
T(n) = T(n/2) +c
T(n/2) = T(n/4) +c
T(n) = T(n/2) + c T(n/4) = T(n/8) +c
= T(n/4)+ c + c = T(n/22)+ 2c
= T(n/8) +c + c + c = T(n/23)+ 3c
After ith substitution
T(n) = T(n/2i)+ ic
Suppose after ith substitution, we reached at base case,
therefore
n = 2i i = log2n
T(n) =T(1) +d log2n = Θ(log2n)
T (n) = T (n − 1) + f (n)
Expanding the recurrence into a tree
The root of the recursion tree is a box (node) containing the
value f (n),
the root has only one child, which is the root of (recursively
defined) recursion tree for the function T (n-1).
The recursion stops when we get to the base case(s) of the
recurrence.
Summing the cost at each level
T (n) is just the sum of all values stored in the recursion tree.
For each i, the ith level of the tree contain 1 node, each with
value f(n-i), Thus,
T (n) = T (n − 1) + n
T (n) = T (n/2) + n
d + 1 = log2 (n + 1)
d = log2 (n + 1) – 1
Department of Computer Science
Recursion Tree Method: UP-SHOT
Sn
a1 r n 1 S
a1
r 1 1 r
i 0
2
i
2 W (1) n
2
2
i 0
2
nn 2
i 0
O(n) n
1 1
O ( n) 2n 2
2
W(n) = O(n2)
Department of Computer Science 48
Example
2
3 2 3 1
T (n) cn n log4 3
cn 2 n log4 3 cn 2 n log4 3 O(n 2 )
i 0 16 i 0 16
3
1
T(n) =ofO(n
Department 2
) Science
Computer 50 16
Master
Theorem
Here, a = ?, b = ?, d = ? a = 2, b = 2, d = 0
Which of the 3 cases holds ?a = 2 > bd = 20, case 3
Department of Computer Science
T(n) = aT(n/b)+f(n), a ≥ 1, b > 1
If f(n) є Θ(nd) where d ≥ 0, then
Θ(nd) if a < bd
T(n) є Θ(ndlogn) if a = bd
T(n) = 2T(n/2)+6n-1?
T(n) = 3 T(n/2) + n a = 3, b = 2, f(n) є Θ(n1), so d = 1
a=3 > bd=21
1. Guess a solution
Guess a solution
T(n) = O(g(n))
Induction goal: apply the definition of the asymptotic notation
T(n) = T(n-1) + n
Guess: T(n) = O(n2)
Induction goal: T(n) ≤ c n2, for some c and n ≥ n0
Induction hypothesis: T(n-1) ≤ c(n-1)2 for all k < n
Proof of induction goal:
T(n) = T(n-1) + n ≤ c (n-1)2 + n
= cn2 – (2cn – c - n) ≤ cn2
if: 2cn – c – n ≥ 0 c ≥ n/(2n-1) c ≥ 1/(2 –
1/n)
For n ≥ 1 2 – 1/n ≥ 1 any c ≥ 1 will work
Department of Computer Science 58
T (n) = T (n/b) + f (n)
T(n) = T(n/2) + c
Guess: T(n) = O(lgn)
Induction goal: T(n) ≤ d lgn, for some d and n ≥ n0
Induction hypothesis: T(n/2) ≤ d lg(n/2)
Proof of induction goal:
T(n) = T(n/2) + c ≤ d lg(n/2) + c
= d lgn – d + c ≤ d lgn
if: – d + c ≤ 0, d ≥ c
Department of Computer Science 60
T (n) = aT (n/b) + f (n)
T(n) = 2T(n/2) + n
Guess: T(n) = O(nlgn)
Induction goal: T(n) ≤ cn lgn, for some c and n ≥ n0
Induction hypothesis: T(n/2) ≤ cn/2 lg(n/2)
Proof of induction goal:
T(n) = 2T(n/2) + n ≤ 2c (n/2)lg(n/2) + n
= cn lgn – cn + n ≤ cn lgn
if: - cn + n ≤ 0 c ≥ 1
Department of Computer Science 62
Brute ForceWhich one is better?
Example
Algorithms that find maximum in an array
Recursive 2
A[0..n-1]
Solution:
Iterative
Recursive 1
T(1) = d
Department of Computer Science
T(n) = T(n/2) + c
General Recurrence Relation of Divide and Conq.