You are on page 1of 31

Lec‐05 & 06

Analysis of Algorithms

Umar Manzoor
1
ƒ The expression:

⎧ c n =1
⎪⎪
T ( n) = ⎨
⎪2T ⎜ ⎞⎟ + cn n > 1
⎛ n
⎪⎩ ⎝ 2 ⎠
is a recurrence.
ƒ Recurrence: an equation that describes a function 
in terms of its value on smaller functions
2
⎧ 0 n=0 ⎧ 0 n=0
s ( n) = ⎨ s ( n) = ⎨
⎩c + s (n − 1) n > 0 ⎩n + s (n − 1) n > 0

n =1 ⎧
⎧ c ⎪ c n =1
⎪⎪ ⎪
T ( n) = ⎨ T ( n) = ⎨
⎪2T ⎜ ⎞⎟ + c n > 1
⎛ n ⎪ ⎛n⎞
⎪⎩ ⎝ 2 ⎠ ⎪aT ⎜ ⎟ + cn n > 1
⎩ ⎝b⎠
3
ƒ Substitution method
ƒ Iteration method
ƒ Recursion tree method
ƒ Master Theorem

4
ƒ The substitution method 
ƒ the “making a good guess method”
ƒ Guess the form of the answer, then use induction 
to find the constants and show that solution 
works

ƒ Comes with experience

5
ƒ Another option is what the book calls the 
“iteration method”
ƒ Expand the recurrence 
ƒ Work some algebra to express as a summation
ƒ Evaluate the summation
ƒ We will show several examples 

6
⎧ 0 n=0
s ( n) = ⎨
⎩c + s (n − 1) n > 0
ƒ s(n) = 
c + s(n‐1)
c + c + s(n‐2)
2c + s(n‐2)
2c + c + s(n‐3)
3c + s(n‐3)

kc + s(n‐k) = ck + s(n‐k)
7
⎧ 0 n=0
s ( n) = ⎨
⎩c + s (n − 1) n > 0

ƒ So far for n >= k we have 
ƒ s(n) = ck + s(n‐k)
ƒ What if k = n?
ƒ s(n) = cn + s(0) = cn

8
⎧ 0 n=0
s ( n) = ⎨
ƒ So far for n >= k we have  ⎩c + s (n − 1) n > 0
ƒ s(n) = ck + s(n‐k)
ƒ What if k = n?
ƒ s(n) = cn + s(0) = cn
ƒ So ⎧ 0 n=0
s ( n) = ⎨
⎩c + s (n − 1) n > 0
ƒ Thus in general 
ƒ s(n) = cn
9
⎧ 0 n=0
s ( n) = ⎨
⎩n + s (n − 1) n > 0

ƒ s(n) 
= n + s(n‐1) 
= n + n‐1 + s(n‐2)
= n + n‐1 + n‐2 + s(n‐3)
= n + n‐1 + n‐2 + n‐3 + s(n‐4)
= …
=  n + n‐1 + n‐2 + n‐3 + … + n‐(k‐1) + s(n‐k)

10
ƒ s(n) 
= n + s(n‐1)  ⎧ 0 n=0
= n + n‐1 + s(n‐2) s ( n) = ⎨
= n + n‐1 + n‐2 + s(n‐3) ⎩n + s (n − 1) n>0
= n + n‐1 + n‐2 + n‐3 + s(n‐4)
= …
=  n + n‐1 + n‐2 + n‐3 + … + n‐(k‐1) + s(n‐k)

∑i
i = n − k +1
+ s(n − k )
11
ƒ So far for n >= k we have ⎧ 0 n=0
s ( n) = ⎨
⎩n + s (n − 1) n > 0
n

∑i
i = n − k +1
+ s(n − k )

12
ƒ So far for n >= k we have

⎧ 0 n=0
s ( n) = ⎨
n

∑i
i = n − k +1
+ s(n − k ) ⎩n + s (n − 1) n > 0

ƒ What if k = n?

13
ƒ So far for n >= k we have ⎧ 0 n=0
s ( n) = ⎨
n ⎩n + s (n − 1) n > 0
∑i
i = n − k +1
+ s(n − k )

ƒ What if k = n?
n
n +1
n


i =1
i + s ( 0) = ∑ i + 0 = n
i =1 2

14
n
ƒ So far for n >= k we have ∑i
i = n − k +1
+ s(n − k )

ƒ What if k = n?

ƒ Thus in general 
n n
n +1

i =1
i + s ( 0) = ∑ i + 0 = n
i =1 2

n +1
s ( n) = n
2
15
The divide‐and‐conquer paradigm
Aside: What is a paradigm? One that serves as a pattern or model.
ƒ Divide the problem into a number of 
subproblems
ƒ Conquer the subproblems by solving them 
recursively. If small enough, just solve directly 
without recursion
ƒ Combine the solutions of the subproblems
into the solution for the original problem
16
ƒ Divide the n‐element sequence to be sorted 
in half (or just about, if n is odd)
ƒ Conquer – sort the two subsequences
ƒ Combine – merge the two sorted 
subsequences

17
Let T(n) be the running time on a problem of 
size n.
If problem is small enough, then solution takes 
constant time (this is a boundary condition, 
which will be assumed for all analyses)
It takes time to divide the problem into sub‐
problems at the beginning. Denote this work 
by D(n).

18
ƒ It takes time to combine the solutions at the end. 
Denote this by C(n).
ƒ If we divide the problem into ‘a’ problems, each of 
which is ‘1/b’ times the original size (n), and since 
the time to solve a problem with input size ‘n/b’ is 
T(n/b), and this is done ‘a’ times, the total time is:
T(n) = Θ(1), n small (or n ≤ c )
T(n) = aT(n/b) + D(n) + C(n)

19
Merge‐Sort(A, p, r)
ƒ if p < r
ƒ then q Å ⎣ ( p + r ) / 2 ⎦
ƒ Merge‐Sort(A, p, q)
ƒ Merge‐Sort(A, q+1, r)
ƒ Merge(A, p, q, r)

It follows that
T(n) = 2 T(n/2)+  cn
work done
dividing and
combining

number of sub- Sub-problem size


problems
20
Merge Sort (ctd) where A is an array and p, q, and r are indices numbering
elements of the array such that p ≤ q < r. The procedure
assumes that the subarrays A[p ‥ q] and A[q + 1 ‥ r] are
Merge( A, p, q, r) in sorted order. It merges them to form a single sorted
subarray that replaces the current subarray A[p ‥ r].
1. n1 Å q – p + 1
2. n2 Å r – q
3. create arrays L[1..n1+1], R[1..n2+1]
4. for i Å 1 to n1
5. do L[i] Å A[p + i – 1]
6. for j Å 1 to n2
7. do R[j] Å A[q + j] 
8. L[n1 + 1] = INF
9. R[n2 + 1] = INF
10. i Å 1
11. j Å 1
12. for k Å p to r
13. do if L[i] <= R[j]
14. then A[k] Å L[i]
15. i Å i + 1
16. else A[k] Å R[j]
17. j Å j + 1

21
22
cn

T(n/2) T(n/2)

23
cn

cn/2 cn/2

T(n/4) T(n/4) T(n/4) T(n/4)

24
cn

cn/2 cn/2

T(n/4) T(n/4) T(n/4) T(n/4)

Eventually, the input size (the argument of T) goes to 1, so...

25
cn

cn/2 cn/2

T(n/4) T(n/4) T(n/4) T(n/4)

T(1) T(1) T(1) T(1) ............................. T(1) T(1)

n sub problems of size 1, but T(1) = c


by boundary condition
26
cn

cn/2 cn/2

T(n/4) T(n/4) T(n/4) T(n/4)

c c c c ............................. c c

n subproblems of size 1

27
level nodes/     cost/
level  level
0 20 = 1 cn
1 21 = 2 cn
2 22 = 4 cn
. .
. .
. .
N‐1 2N‐1=n cn
Since 2N‐1 = n, lg(2N‐1) = lg(n)
levels = N = 1+lg(n)
T(n) = total cost = 
(levels)(cost/level)
T(n) = cn [1+lg(n)] = O( n lg(n)) n nodes at level N-1
28
ƒ Given: a divide and conquer algorithm
ƒ An algorithm that divides the problem of 
size n into a subproblems, each of size n/b
ƒ Let the cost of each stage (i.e., the work to 
divide the problem + combine solved 
subproblems) be described by the function 
f(n)
ƒ Then, the Master Theorem gives us a 
cookbook for the algorithm’s running time:
29
ƒ if  T(n) = aT(n/b) + f(n) then
⎧ ⎫

⎪ Θ (
n )
log b a
(
f (n) = O nlogb a −ε) ⎪

⎪ ⎪
⎪⎪ ⎪ε > 0
T (n) = ⎨Θ n ( log b a
log n ) (
f (n) = Θ n )
log b a

⎪ ⎪ c <1
⎪ ⎪
⎪Θ( f (n) ) ( )
f (n) = Ω nlogb a +ε AND ⎪
⎪ ⎪
⎪⎩ af (n / b) < cf (n) for large n⎭
30

You might also like