0% found this document useful (0 votes)
12 views15 pages

Recurrence Relations - (Divide and Conquer)

1) Binary search is a divide and conquer recursive algorithm that runs in O(log n) time. It divides the search space in half at each recursive call. 2) The recurrence relation for binary search is T(n) = T(n/2) + 1, and solving this yields a runtime of O(log n). 3) Merge sort is another divide and conquer recursive algorithm. It divides the input array into two halves, recursively sorts each half, and then merges the sorted halves together. This results in a recurrence relation of T(n) = 2T(n/2) + n, yielding an overall runtime of O(n log n).

Uploaded by

f2021266018
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views15 pages

Recurrence Relations - (Divide and Conquer)

1) Binary search is a divide and conquer recursive algorithm that runs in O(log n) time. It divides the search space in half at each recursive call. 2) The recurrence relation for binary search is T(n) = T(n/2) + 1, and solving this yields a runtime of O(log n). 3) Merge sort is another divide and conquer recursive algorithm. It divides the input array into two halves, recursively sorts each half, and then merges the sorted halves together. This results in a recurrence relation of T(n) = 2T(n/2) + n, yielding an overall runtime of O(n log n).

Uploaded by

f2021266018
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Time Complexity of Recursive Algorithm – Divide and Conquer

Recurrence Relation
Dividing Functions
Time Complexity of Recursive Algorithm Example 1

Consider the following algorithm


Recurrence Relation:
BinarySearch ( A, start, end, x) {
T(n) = T(n/2) +
if (start < end) {
mid = (start +end)/2
if (x = = A[mid])
return mid;
else if (x > A[mid]) n/2
return BinarySearch ( A, mid +1, end , x)
else or
return BinarySearch ( A, start, mid -1 , x)
}
Single call at each activation
}
Time Complexity of Recursive Algorithm Example 1

Consider the following algorithm


Recurrence Relation: for n>1
BinarySearch ( A, start, end, x) {
T(n) = T(n/2) + 1
if (start < end) {
mid = (start +end)/2
if (x = = A[mid]) Constant
return mid; Only Dividing Cost
else if (x > A[mid]) No Combing here.
return BinarySearch ( A, mid +1, end , x)
else
return BinarySearch ( A, start, mid -1 , x)
}
} f(n) denotes cost of the work done outside the recursive call, which includes:
 cost of dividing the problem
 cost of combining/merging the solutions
Time Complexity of Recursive Algorithm Example 1

Consider the following algorithm


Recurrence Relation: for n>1
BinarySearch ( A, start, end, x) {
T(n) = T(n/2) + 1
if (start < end) {
mid = (start +end)/2 T(n) = 1 for n=1
if (x = = A[mid])
return mid;
else if (x > A[mid])
return BinarySearch ( A, mid +1, end , x)
else
return BinarySearch ( A, start, mid -1 , x)
}
}
Time Complexity of Recursive Algorithm Example 1

T(1) = 1 for n=1 𝑛


T(n)¿ 𝑇 ( 2 )+ 1
𝑛
T(n)¿ 𝑇 ( )
2 +1
( )
1 𝑛
¿𝑇 +1
22
] +1
𝑛
¿𝑇 ( 𝑛
22 )+ 2 2 T(n)¿ 𝑇 (
2
)+ 1

] 𝑛
2 ¿𝑇 ( )
23 +
¿𝑇 ( +3 ) 𝑛
23 3

¿𝑇 (
2𝑘)
𝑛
k
Time Complexity of Recursive Algorithm Example 1

T(1) = 1 for n=1 𝑛


T(n)¿ 𝑇 ( 2 )+ 1
𝑛
T(n) ¿ 𝑇 ( ) +1
( )
1 𝑛
2 ¿𝑇 +1
22
] +1
𝑛
¿𝑇 ( 𝑛
22 )+ 2 2 T(n)¿ 𝑇 (
2
)+ 1

] 𝑛
2 ¿𝑇 ( )
23 +
¿𝑇 ( )+3 𝑛
23
3

¿𝑇 (
2 𝑘 )+ k
𝑛
k
Time Complexity of Recursive Algorithm Example 1

T(1) = 1 for n=1


Assume
𝑛
T(n) ¿ 𝑇 ( ) +1 1
2 𝑛=2 𝑘
¿𝑇 ( )+ 2
𝑛
22 2
𝑛=2 𝑘
¿𝑇 (
2 3 )+3
𝑛
3 𝑙𝑜𝑔 𝑛=𝑙𝑜𝑔 2𝑘

¿𝑇 (
2 𝑘 )+ k
𝑛 𝑙𝑜𝑔 𝑛=k 𝑙𝑜𝑔 2 2
k
k
¿𝑇 ( +)log
𝑛
𝑛 n =1

+ log
¿ 1 n O (log n)
Time Complexity of Recursive Algorithm Example 2

T(1) = 1 for n=1 𝑛


T(n)¿ 𝑇 ( 2 )+ n
𝑛
T(n)
¿𝑇 ( )+ n

]
2 ¿𝑇 ( 𝑛
22 )
+
+n
+n 𝑛
T(n)¿ 𝑇 ( )+ n
2
( +n 𝑛
¿𝑇 ( )+
] ( +n 23

( ) +n
¿𝑇 ( 𝑛
23 )n ( ) + n
Time Complexity of Recursive Algorithm Example 2

T(1) = 1 for n=1


𝑛
T(n)¿ 𝑇 ( )+ n 1
2
n( )+n 2

𝟏 𝟏
¿𝑇 ( 𝑛
+ n
23 ) (
𝟐
)𝟐 + n (
𝟐
)+ n 3

-------------------------------

¿𝑇 ( 𝑛
2𝑘 +) n( )+] k
Time Complexity of Recursive Algorithm Example 2

T(1) = 1 for n=1 Assume


𝑛
T(n)¿ 𝑇 ( ) 𝑛=2 𝑘
2 +n
𝒂(𝟏 −𝒓 𝒌)
¿𝑇 ( 𝑛
+)
2𝑘 n] =
𝟏 −𝒓
𝟏𝒌 𝟏 =1
𝟏(𝟏 − ) 𝟏− 𝟏
¿𝑇 ( ) 𝑛
𝑛+ n. 𝟐𝒌
𝟏
𝟏−( )
¿𝑇 ( 1+) . n
𝟏
𝒏 r =
𝟐
𝟐 𝟐
𝒏−𝟏
¿ 1+ 2. n 𝒏 ¿ 1
+ 2 + 2 𝒏− 2
( 𝒏−𝟏 )¿ 1
¿2 𝒏 −𝟏 O (n)
Time Complexity of Recursive Algorithm Example 3

Consider the Algorithm Merge(A, p, q, r)


n1 = q - p + 1; n2 = r – q;
Let L[1 … n1+1] and R = [1 … n2+1] be new arrays
MERGE-SORT(A, p, r) { for i = 1 to n1
if p < r Dividing Cost
L [i] = A [p+i-1]
for j = 1 to n2
q = [(p+r)/2] R [j] = A [q+j]
MERGE-SORT(A, p, q) Two Recursive calls L [n1+1] = ∞
R [n2+1] = ∞
MERGE-SORT(A, q+1, r) each of size n/2 i=1 O(n)
j=1
Merge(A, p, q, r) for k = p to r
} Combine Step if L [i] ≤ R [j]
A [k] = L [i]
i = i+1
𝑛
T(n)¿ 𝟐 𝑇 ( )+ n else A [k] = R [j]
2 j = j+1

T(1) = 1 for n=1 f(n) denotes cost of the work done outside the recursive call, which includes:
 cost of dividing the problem
 cost of combining/merging the solutions
Time Complexity of Recursive Algorithm Example 3

T(1) = 1 for n=1 𝑛


T(n)¿ 2 𝑇 ( 2 )+ n
𝑛
T(n)¿ 𝟐 𝑇 ( )
2 +n
( )
1 𝑛
¿2𝑇 +
] 22
+n
+n 𝑛
T(n)¿ 2 𝑇 ( )+ n
2
+2n 2 𝑛
¿2𝑇 ( )+
] 23
2n

2n
¿𝟐𝟑𝑇 ( +) 3 n
𝑛
23 3
Time Complexity of Recursive Algorithm Example 3

T(1) = 1 for n=1


𝑛
T(n)¿ 𝟐 𝑇 ( )
2 +n 1

2n 2

¿𝟐𝟑𝑇 ( 𝑛
23 )3 n 3

-------------------------------

¿𝟐𝒌𝑇 ( 𝑛
2𝑘 )k n k
Time Complexity of Recursive Algorithm Example 3

T(1) = 1 for n=1


Assume
𝑛
T(n) ¿ 𝟐 𝑇 ( )
2 +n
𝑛=2 𝑘
¿𝟐𝒌𝑇( )k n 𝑛
2𝑘
𝑛=2 𝑘
¿𝒏𝑇(
𝑛 )n
𝑛 𝑙𝑜𝑔 2 𝑛=𝑙𝑜𝑔2 2 𝑘
𝑙𝑜𝑔 2 𝑛=k 𝑙𝑜𝑔 2 2
¿ 𝒏n k
O (n log n)
Time Complexity of Recursive Algorithm

References:

1.Appendix B: Solving Recurrence Equations, “Foundations of Algorithms” By Neapolitan (p-555)

2.Chapter 8: Advanced Counting Techniques, “Discrete Mathematics and Its Applications” By Rosen (p-501)

YouTube Link
https://www.youtube.com/watch?v=pauQm5VxnO8&list=LL&index=13

You might also like