Alg Wk4a

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

T(n)

More examples
next
1
You may use change variable method/For divide and
conquer.
2𝑘
T(n) = 4T( ) + n = T(2𝑘 ) = 4T(2𝑘−1 ) + 2𝑘 . let n=2𝑘
2

tk = 4 tk-1 + 2𝑘 .
tk - 4 tk-1 = 2𝑘 𝑘 0
𝑥 1 − 4 (𝑥 − 2)0+1 =0, r1 = 4, r2 =2.
tk = c1 4𝑘 + c2 2𝑘 .
T(2𝑘 )= c1 4𝑘 + c2 2𝑘 .
T(n) = c1 4𝑘 + c2 2𝑘 .
T(n) = c1 4𝑙𝑔𝑛 + c2 𝑛.
= c1 𝑛𝑙𝑔4 + c2 𝑛.
𝑘 2
= c1 𝑛2 + c2 𝑛. Or: 4𝑘 = (2)2 = (2)𝑘 = 𝑛2 = (n2)
2
example

Binary search
• Binary search based on homogenous
characteristic equation with change variables:

𝑛
𝑇 + 𝑐, 𝑛 > 1.
• T(n) = ቐ 2
𝜃 1 , 𝑛 = 1.

3
example

Binary search
𝑛
T(n) = 𝑇 + 𝑐
2
T(2𝑘 ) = T(2𝑘−1 ) + c . 𝑛0 .
tk - tk-1 =1𝑘 . 𝑐. 𝑘 0 .
𝑥 − 1 (𝑥 − 1)1 = 0.
tk = c1 1𝑘 + c2 . 𝑘 . 1𝑘
= c1 + c2 k.
T(n) = c1 + c2 lgn.
= Ɵ(lgn).

4
Merge sort: characteristic with change
example

variables.
𝑛
T(n) =2 𝑇 + c1 n+ c2 , n>1.
2
Let n=2𝑘 , then:
T(2𝑘 ) = 2 𝑇 2𝑘−1 + c1 2𝑘 + c2
tk = 2 tk-1+ c2 2𝑘 + c2 .
tk - 2 tk-1 = c2 2𝑘 + c2 .
= tk - 2 tk-1 = c2 2𝑘 . 𝑘 0
(x-2)(x-2) =0, r1 = r2 = 2. so, T(n) = Ɵ (n lgn).
5
example

𝑇 𝑛 − 1 + 𝑛, 𝑛 > 1.
Solve T(n) = ቊ
tn = tn-1+ n. 1, 𝑛 = 1. 𝑎𝑛𝑑 0, 𝑛 = 0
tn - tn-1 = n.1𝑛 .
𝑥 − 1 (𝑥 − 1)2 , r1 = r2 = r3 =1.
tn = c1 1𝑛 + c2 .n. 1𝑛 + c3 .𝑛2 . 1𝑛 . = 𝜃(𝑛2 ).

t1 = c1 + c2 + c3 =1.
t2 = t1 +n = 1+2 = 3 = c1 + 2c2 + 4c3.
t3= t2 +n = 3+3 = c1 + 3c2 + 9c3
find c1 , c2 , c3 {not needed}
𝑛(𝑛+1)
so 1+2+3+………+n = σ𝑛𝑖=1 𝑖 = = 𝜃(𝑛2 ).
2
 See next slide

6
𝑇 𝑛 − 1 + 𝑛, 𝑛 > 1.
Trace: T(n) = ቊ
1, 𝑛 = 1 𝑎𝑛𝑑 0, 𝑛 = 0.

7
example

Back to Master Theorem

Let T(n)=aT(n/b)+f(n)
T(1)=c
a>=1, b>1. c>0, iff f(n)=𝜃 𝑛𝑖

𝜽 𝒏𝒊 , 𝒂 < 𝒃𝒊
T(n) = 𝜽 𝒏𝒊 𝒍𝒈𝒏 , 𝒂 = 𝒃𝒊.
𝜽 𝒏𝒍𝒈𝒃𝒂 , 𝒂 > 𝒃𝒊

8
example

Examples
T(n)=4T(n/2)+n
a=4, b=2, i=1 so lgba = lg 24 = 2
lg 24 >i ------------------------- case 2

so, 𝜃(n2)

9
example

Examples
T(n)=4T(n/2)+n2
a=4, b=2, i=2 so lgba = lg 24 = 2
lg 24 =2 =i
------------------------- case 1
𝑇 𝑛 = 𝜃 𝑛𝑖 𝑙𝑔𝑛 = 𝜃 𝑛2 𝑙𝑔𝑛

10
example

Examples
T(n)=4T(n/2)+n3
a=4, b=2, i=3
lg 24 =2 <i
------------------------- case 3
So, T(n)= 𝜃 𝑛𝑖 =𝜃 𝑛3

11
Example
and more
analysis Master theorem (Proof)
Provides bound for recurrence of the form T(n) = a T(n/b) + c.𝑛𝑖 , n>1.

T(1) = 1 Let n= 𝑏 𝑘 , so k=lgbn.


T(𝑏 𝑘 ) = aT(𝑏 𝑘−1 ) + (𝑏 𝑘𝑖 )
tk - a tk-1 = c.(𝑏 𝑖 )𝑘 .
(x-a)(x-bi)=0
r1 = a, r2 = 𝑏 𝑖 ,
tn = (c1r1n+c2r2n)

12
Cont..
𝑘 𝑖 𝑘 𝑖
tk = ൝𝑎 𝑐1 + 𝑐2 (𝑏 ) , a ≠ 𝑏 . Roots are distinct: case 2,3
𝑐1 𝑎𝑘 + 𝑐2 𝑘 (𝑏 𝑖 )𝑘 , 𝑎 = 𝑏 𝑖 . ∶ 𝑐𝑎𝑠𝑒 1

T(𝑏 𝑘 ) = c1 𝑎𝑘 + c2 (𝑏 𝑘 )𝑖
log 𝑛
Distinct → T(𝑛) = c1 𝑎 𝑏 + c2 (𝑏 𝑘 )𝑖
log_ 𝑛
= c1 𝑎 𝑏 + c2 . 𝑛 𝑖
log _𝑏 𝑎
= c1 𝑛 + c2 . 𝑛 𝑖

Which is greater? log 𝑏 𝑎 or i if i>lg_ba it is case 3 else case 2


𝑎
= c1 𝑛log_ 𝑏 + c2 . 𝑛𝑖 .
(lgba > 𝑖) → Ɵ(𝑛lg_ 𝑏𝑎 ) [𝑐𝑎𝑠𝑒2] otherwise→ Ɵ(ni) [case3]

13
Cont..
If a = 𝑏 𝑖 → 𝑐1 𝑎𝑘 + 𝑐2 𝑘 (𝑏 𝑖 )𝑘

𝑘 lg 𝑏 𝑛
T(𝑏 ) = c1 𝑎 + c2 lgbn . 𝑛𝑖
lg 𝑏 𝑎
= c1 𝑛 + c2 lg 𝑏 𝑛 . 𝑛𝑖
𝑏𝑖
= c1 𝑛lg 𝑏 + c2 lg 𝑏 𝑛 . 𝑛𝑖
= c1 𝑛𝑖 + c2 lg 𝑏 𝑛 . 𝑛𝑖 = Ɵ(lg 𝑏 𝑛 . 𝑛𝑖 )

14
Merge Sort
2𝑇 𝑛/2 + 𝑎𝑛1 + 𝑐, 𝑛 𝑙𝑎𝑟𝑔𝑒, 𝑛 > 1.
T(n) = ቊ
Ɵ 1 , 𝑛 𝑠𝑚𝑎𝑙𝑙, 𝑛 = 1.
i=1, a=2, b=2. lgba= 1 = I → which case?

𝑏 𝑖 = 21 , a=2 → Ɵ (𝑛1 . lg 2 n).

15
Binary search
𝑛
T(n) = 𝑇 + 𝑐. 𝑛0
2
a=1, b=2, i=0 → 𝑎 = 𝑏 𝑖 = 1 = 20 =1.
Ɵ(𝑛0 log 2 n) = Ɵ(lg n).

16
Example
e.g: T(n) = 8T(n/8) + 𝑛 + n → Ɵ(?), let n = 8𝑘 .
a=8, b=8, i=1
lgba= lg88= 1 = i , so it is case 2
𝜃 𝑛𝑖 𝑙𝑔𝑛 , 𝑖 = lg _𝑏𝑎 i= lgba … … … . 𝑐𝑎𝑠𝑒 1 𝑜𝑟 2 𝑜𝑟 3?
So, T(n) = 𝜃(𝑛1 𝑙𝑔𝑛)

17
Example
e.g: T(n) =7T(n/8) + 𝑛 + n → Ɵ(?), let n = 8𝑘 .
a=7, b=8, i=1
lgba= lg87= 1-ɛ < i , so it is case 3
𝜃 𝑛𝑖 ,
So, T(n) = 𝜃(𝑛1 )

18
T(n) =7T(n/8) + 𝑛 + n
T(n) =7T(n/8) + 𝑛 + n
T(8k) =7T(8k/8) + 8k +8k
tk=7tk-1+ 8k +8k

tk-7tk-1 = 8k +8k → b1kp1(n)+ …

(x - 7)(x - 8)(x - 8) = 0

𝜃(n)
19
Back to divide and conquer

e.g: 13, 21, 92, 43, 65, 31, 57, 26, 75, 0.
Select pivot partition: 65

Quick Sort (A, p, q):


{ if (p<q) then,
m = partition (A, p, q) -------------- 𝜃 𝑛 ,
Quick sort (A, p, m) ---------------- T(m)
Quick sort (A, m+1, q) ---------------- T(n-m)

20
Quick sort
𝑇 𝑛 − 𝑚 + 𝑇 𝑚 + 𝜃 𝑛 ,𝑛 > 1
T(n) = ቊ
𝜃 1 ,𝑛 = 1

Partition:
Balanced partition: (best case complexity ) when elements
are unordered which means the pivot is in location n/2.
T(n)= T(n/2) + T(n/2) + Ɵ(n)
=2T(n/2) + Ɵ(n)
=2T(n/2) + a.n + c
Let n = 2𝑘 , then
tk - 2tk-1 = a.n + c → n lgn
tk - 2tk-1 = 2𝑘 𝑘 0 → (x-2) (x-2) = Ɵ(n lgn).
21
Un-balanced partition
Un-balanced partition: (worst case),
𝑇 𝑛 =𝑇 1 +𝑇 𝑛−1 +𝜃 𝑛
=T(n-1) + Ɵ(n).
tn = tn-1 + Ɵ(n).
tn - tn-1 = 1𝑛 . (𝑎𝑛 + 𝑏)
𝑥 − 1 (𝑥 − 1)2 = 0.
tn = c1 1𝑛 + c2 . 𝑛. 1𝑛 + c2 . 𝑛2 . 1𝑛 = 𝜃 𝑛2

22
Quicksort
W(n) = 𝜃 𝑛2
A(n) = 𝜃 𝑛𝑙𝑔𝑛
B(n) = 𝜃 𝑛𝑙𝑔𝑛

23
Quicksort
Average case: best case , worst case. To make it
balance we apply probability of partitioning.
# of partitions = n.

σ𝑛
𝑖=1 𝑇𝑖 𝑛 .1
T(n) =
𝑛

24
Quicksort
• Not Stable, but in place, internal sort

25
26

You might also like