0% found this document useful (0 votes)
55 views

Design and Analysis of Algorithms (CS3052)

This document discusses the divide and conquer algorithm design strategy and analyzes the binary search algorithm. It begins by defining the divide and conquer strategy as dividing a problem into smaller subproblems, solving those, and combining the solutions. It then analyzes the recurrence relation and time complexity of binary search, showing that it runs in O(log n) time.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Design and Analysis of Algorithms (CS3052)

This document discusses the divide and conquer algorithm design strategy and analyzes the binary search algorithm. It begins by defining the divide and conquer strategy as dividing a problem into smaller subproblems, solving those, and combining the solutions. It then analyzes the recurrence relation and time complexity of binary search, showing that it runs in O(log n) time.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Design and Analysis of

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

Time for DAndC on input size n


Time to compute answer directly for small input
Time for dividing P and combining solutions to sub
problems

a and b are the constants


n is a power of b (i.e. n=bk)
Example of recurrence Relation
Binary Search
• To search a list of n items, first look at the item in
location n/2
▫ Then search either the region from 0 to (n/2)-1
or the region from (n/2)+1 to (n-1)
• Example: searching for 57 in a sorted list of 15
numbers

start in the middle




Binary Search
Approximate number of comparisons (worst case):
search

N= 100 N = 1,000 N=10,000 N=1,00,000

search 100 1,000 10,000 1,00,000

bsearch 7 10 13.2877 16.60

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)

You might also like