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

Learn DAA: From B K Sharma

BCST-503: Design and


Analysis of Algorithms
Divide and Conquer: Binary
Search
Learn DAA: From B K Sharma

Unit I: Syllabus
• Introduction:
– Algorithm definition
– Algorithm Specification
• Performance Analysis-
– Space complexity
– Time complexity
Learn DAA: From B K Sharma

Unit I: Syllabus
• Randomized Algorithms.
• Divide and conquer- General method
– Applications:
• Binary search
• Merge sort Quick sort
• Strassen’s Matrix Multiplication.
Learn DAA: From B K Sharma
Different Algorithm Design Techniques

Divide Backtracking Greedy Dynamic Branch &


And Approach programming Bound
Conquer
Applications: Applications: Applications: Applications: Applications:

Binary Search The 8-queens Knapsack Chained matrix Travelling sales


Problem Problem multiplication person problem
Merge sort
Sum-of- Job All pairs
Quick sort Sequencing
subsets shortest
Strassen’s Problem with deadlines path problem
Matrix Minimum cost Optimal binary
Multiplication. Graph spanning trees search trees
Colouring
Single source 0/1 knapsack
Problem
shortest path problem,
Hamiltonian problem. Reliability design
Clycle
Travelling sales
Problem
person problem
Learn DAA: From B K Sharma
Divide and Conquer
• Divide the problem into a number of sub-problems
Similar sub-problems of smaller size
• Conquer the sub-problems
Solve the sub-problems recursively

• Combine the solutions of the sub-problems


Obtain the solution for the original problem
Learn DAA: From B K Sharma
Divide and Conquer

Problem

Divide
Sub-problem Sub-problem

Conquer Solve Sub-problem Conquer Solve Sub-problem

Solution to Sub-problem Solution to Sub-problem

Combine

Solution to Problem
Learn DAA: From B K Sharma
Divide and Conquer:- General Method

Algorithm GENDCM(P)
{
if SMALL (P) then
return S(P);
else
{
divide P into smaller instances p1, p2, …. Pk, k≥ 1;
apply GENDCM to each of these sub problems;
return (COMBINE (GENDCM (p1) ,
GENDCM(p2),…., GENDCM (pk));
}
}
Learn DAA: From B K Sharma
Divide and Conquer:- General Method
SMALL (P) is a Boolean valued function which
determines whether the input size is small enough so
that the answer can be computed without splitting.

If this is so function ‘S’ is invoked otherwise, the


problem ‘P’ is divided into smaller sub problems.

These sub problems p1, p2, . . . , pk are solved by


recursive application of GENDCM().
Learn DAA: From B K Sharma
Divide and Conquer
Recursive Definition of Factorial Function
If n=0, then n!=1
Else if n> 0, then, n!=n x (n-1)!
long fact(long n)
{
if (n==0) Base Condition
return 1;
else
return n * fact(n-1);
}
Learn DAA: From B K Sharma
Divide and Conquer
Recursive Definition of Factorial Function
Call fact(4)

long fact(long n) 0
{ 1
2
if (n==0)
3
return 1;
else
return n * fact(n-1);
}
Learn DAA: From B K Sharma
Divide and Conquer
Recursive Definition of Factorial Function
=24 6 2
(1) 4!=4 x 3! 1
(2) 3!= 3 x 2!
(3) 2!= 2 x 1! 1

(4) 1!=1 x 0!
(5) 0!=1
Learn DAA: From B K Sharma
Divide and Conquer
Recursive Definition of Factorial Function
=24
n=4 fact(4)=4 * fact(3)
3
6 6
n=3 fact(3)=3 * fact(2)
2
2 2
n=2 fact(2)=2 * fact(1)
1
1 1
n=1 fact(1)=1 * fact(0)
0
1 n=0 1
Learn DAA: From B K Sharma
Divide and Conquer
Recursive Definition of Fibonacci Number

if n = 0 or n = 1, then fibn=n
else if n > 1, then fibn=fibn - 2 + fibn - 1
int Fib(int n)
{
if(n==0) Base Condition
return 0;
else if ( n==1) Base Condition
return 1;
else
return Fib( n-2) + Fib(n-1);
}
Learn DAA: From B K Sharma
Divide and Conquer
Recursive Definition of Fibonacci Number
fib(4) =3
n=4

fib(3) n=3
+ n=2 fib(2)
=2 =1

fib(2) n=2
+ n=1 n=1 + n=0

=1
fib(1) fib(1) fib(0)
=1
n=1 + n=0
=1 =0
fib(1) fib(0)
=1 =0
Learn DAA: From B K Sharma

Binary Search Technique


int binary(int a[],int n,int key,int l,int u)
{
int mid,c=0;
if(l<u)
{
mid=(l+u)/2;
if(key==a[mid])
c=1;
else if(key<a[mid])
return binary(a,n,key,l,mid-1);
else
return binary(a,n,key,mid+1,u);
}
return c;
}
DAA: B K Sharma

Binary Search Technique: Example


Search for 45
1 2 3 4 5 6 7 8 9 10 11
12 15 21 23 27 45 76 77 90 95 98

mid
Found on first step, 45 equals the middle item
Search for 27
1 2 3 4 5 6 7 8 9 10 11
12 15 21 23 27 45 76 77 90 95 98

mid
Search for 27
1 2 3 4 5 6 7 8 9 10 11
12 15 21 23 27 45 76 77 90 95 98

mid
DAA: B K Sharma

Binary Search Technique: Example


Search for 27
1 2 3 4 5 6 7 8 9 10 11
12 15 21 23 27 45 76 77 90 95 98

mid
Found !
Search for 91
1 2 3 4 5 6 7 8 9 10 11
12 15 21 23 27 45 76 77 90 95 98

Search for 91 mid


1 2 3 4 5 6 7 8 9 10 11
12 15 21 23 27 45 76 77 90 95 98

91 < 95
Search Completed, No value found mid
DAA: B K Sharma

Binary Search Technique: Analysis

Binary Search

1 for n  1
T ( n) = 

T(n/2) + 1 otherwise
Learn DAA : From B K Sharma

The Master Theorem / Method


gives us
a cookbook method

for solving

running time of

Divide and Conquer Type of Algorithms

if
T(n) has the form

T(n) = a T(n/b) + f(n)


Learn DAA : From B K Sharma

The Master Theorem / Method

T(n) = a T(n/b) + f(n)

Where,
a ≥ 1 & b > 1
n is the size of the problem.

‘a’ is the number of sub-problems in the recursion


n/b is the size of each sub-problem. (Here it is assumed
that all sub-problems are essentially the same size.)
f(n) is the work to divide D(n) and combine C(n)
Learn DAA : From B K Sharma

Understanding the Master Theorem / Method


T(n) = a T(n/b) + f(n) a ≥ 1 & b > 1
For example
T(n) = 2T(n/2) + 1
T(n) = 9T(n/3) + n
T(n) = 16T(n/4)+n
T(n) = T(3n/7) + 1
T(n) = 3T(n/4) + n lg n
T(n) = 2T(n/2) + n2
T(n) = 4T(n/2) + n
T(n) = 4T(n/2) + n2
T(n) = 4T(n/2) + n3
Learn DAA : From B K Sharma

The Master Theorem / Method


T(n) = a T(n/b) + f(n) a ≥ 1 & b > 1

if

= if

if
Learn DAA : From B K Sharma

Understanding the Master Theorem / Method

Compare f(n) with nlogba


The solution to the recurrence is determined by
the larger of the two functions.
Case 1: If f(n) is smaller than nlogba then
the solution T(n) = Θ(nlogba).
Case 2: If f(n) and nlogba are of same size then
the solution is T(n) = Θ(nlogba lg n) = Θ(f(n) lg n).
Case 3: If f(n) is the larger than nlogba, and a f(n/b) ≤
c f(n) for large n then
the solution is T(n) = (f(n)).
Learn DAA : From B K Sharma

Application of Master Method to Recurrence of


Binary search

1 for n  1
T ( n) = 

T(n/2) + 1 otherwise

Here,
a=1, b=2, f(n)=1
Now,
Find nlogba
nlogba= nlog21= n0= 1
Since,
f(n)=nlogba=1
Master Method’s Case 2 Applies:
T(n) = Θ(nlogba lg n) = Θ(f(n) lg n)= Θ(lgn)
Learn DAA : From B K Sharma

END

For more examples of Master Method


/
Master Theorem,
Go to PPT named Master Theorem

You might also like