Design and Analysis of Algorithms (CS3052)
Design and Analysis of Algorithms (CS3052)
Algorithms (CS3052)
Prof. G. G. Shingan
Computer science and Engineering
Department
RIT, Rajaramnagar.
Lecture No. 4
Content
• Divide and Conquer Strategy
• Application: Binary Search
▫ Time Complexity
▫ Space Complexity
Learning Outcomes
• Understand Divide and Conquer Strategy
• How to analyze performance of recursive algorithms
• Analysis the complexity of Binary Search
Divide and Conquer: (Divide, Conquer, Combine)
• The idea is to divide the problem into smaller but similar sub
problems (divide), solve it (conquer), and (combine) these solutions
to create a solution to the original problem.
Algorithm DandC(P)
{
if small(P) the return S(P)
else
{
Divide P into smaller instances P1,P2,P3……Pk, k>=1
Apply DandC to each subproblems
Return(Combine DAndC(P1), DAndC(P2),….. DAndC(Pk));
}
}
Recurrence Relation for divide and
conquer Algorithm
③
②
①
Binary Search
Approximate number of comparisons (worst case):
search
http://www.cs.armstrong.edu/liang/animation/web/BinarySearch.html
https://www.cs.usfca.edu/~galles/visualization/Search.html
https://blog.penjee.com/binary-vs-linear-search-animated-gifs/
BINARY SEARCH
Input: An array a[low….high] of elements sorted in non
decreasing order
Procedure T(n)
binarysearch(low,high)
1
if (low=high) then
1 { if x=A[i]then return 0 else return 1; }
else
mid (low high)/2
1
if x=A[mid] then return mid T(n/2)
1 else if x<A[mid] then return binarysearch(low,mid-1)
else return binarysearch(mid+1,high)
end if
T(n/2)
2-------------------- n=1
Recurrence relation T(n)
T(n/2)+2----------n>1
• T(n)=T(n/2)+2-------(I)
=T(n/4)+(2+2)
=T(n/4)+(2*2)
T(n/2)=T(n/4)+2----------(II)
=T(n/8)+(2+2+2)
=T(n/23)+(3*2) T(n/4)=T(n/8)+2----------(IV)
= T(n/16)+(2+2+2+2)
= T(n/24)+(4*2)
………….
= T(n/2k)+(k*2)
for simplicity assume that n=2k n=2k
T(n)= T(2k/2k)+(k*2) Apply log on both sides
Log n =k log 2
T(n)=T(1)+(k*2) K=log n/log 2
T(n)=2+2*log 2 n K=logn2
=O(log n)