Lecture # 08 - New
Lecture # 08 - New
Algorithm
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)
T(n) = T(n-1) +
1
T(n-1) = T(n-2) +
1
T(n-2) = T(n-3) +
1
T(n) = T(n-1) + c …
T(n) = T(n-2) + c + c = T(n-2) +T(2)
2c = T(1 ) + 1
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 = log (n + 1)
2
Department of Computer Science
d = log2 (n + 1) – 1
Recursion Tree Method: UP-SHOT
Expanding the recurrence into a tree
In a recursion tree, each node represents
the cost of a single sub-problem
somewhere in the set of recursive function
invocations.
Summing the cost at each level
We sum the costs within each level of the
tree to obtain a set of per-level costs, and
then we sum all the per-level costs to
determine the total cost of all levels of the
Sn
a1 r n 1
recursion. S
a1
r 1 1 r
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
(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) ≤ cg(n), for some c > 0 and n ≥ n0 (strong induction)
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
Which one is
Brute Force Example
better?
Algorithms that find maximum in an array
Recursive 2
A[0..n-1]
Solution:
Iterativ
e
Recursive 1
T(1) = d
Department of Computer Science
T(n) = T(n/2) + c
General Recurrence Relation of Divide and Conq.