CH 3
CH 3
Algorithm
Divide and Conquer Technique
• In algorithmic methods, the design is to take a dispute on a huge input,
break the input into minor pieces, decide the problem on each of the small
pieces, and then merge the piecewise solutions into a global solution.
• This mechanism of solving the problem is called the Divide & Conquer
Strategy.
• Divide: Break the original problem into a set of sub-problems.
• Conquer: Solve every sub-problem individually, recursively.
• Combine: Put together the solutions of the sub-problems to get the solution
to the whole problem.
Divide and Conquer Technique
Algorithm DC(P)
{
if P is too small then
return solution of P
else
{ Divide(P) and obtain P1, P2, P3, …. Pn
where n >= 1
Apply DC to each subproblem
return combine (DC(P1), DC(P2)...(DC(Pn));
}
}
Divide and Conquer Technique
Recurrence relation:
T(n) = g(n) if n is small
T(n1) + T(n2) + …….T(nr) + F(n) when n is sufficiently large
The total time t(n) taken by this divide and conquer algorithm is something like
T(n) = a T(n / b) + g(n)
So Time complexity of Divide and Conquer can be identified by Master Theory.
Divide and Conquer Technique
Problem of size n
Solution to original
problem
Problem solving using Divide and Conquer
1. Binary Search
2. Quick Sort
3. Merge Sort
4. Min max problem
5. Matrix Multiplication
6. Exponential
Multiplying large integers problems
• General formula for multiplying large integer problems
c=a*b
c=+
Where n is total number of digits in the integer
=
=
= (+ (
Example
• Multiply 2101 and 1130
c=a*b
=2101*1130
Divide the numbers in equal halves.
=21, =01, =11, =30
= = 21*11 = 231
= = 01*30 = 30
= (+(
= (01+21) * (11+30) – (231+30) = (22*41) – 261 =641
a*b = +
= 231* + 641*+ 30
= 2374130
Binary search
• Binary search is an efficient method. While searching the elements using
this method the most essential thing is that the elements in the array should
be sorted one.
• An element which is to be searched from the list of elements stored in array
A[0…n-1] is called KEY element.
• Let A[m] be the mid element of array A. then there are three conditions that
needs to be tested while searching the array using this method.
1. If KEY= A[m] then desired element is present in the list.
2. Otherwise if KEY<A[m] then search the left sub list.
3. Otherwise if KEY>A[m] then search the right sub list.
Binary search
• This can be represented as,
A[0]… A[m-1] A[m] A[m+1]…A[n-1]
Low High
• m=(low+high)/2
= (0+6)/2 = 3
Check A[m]=KEY
A[3]=60 ??
A[3]=40 and 40<60
Now search right sub list.
Example
• Right sub list is
4 5 6
50 60 70
Low High
• m=(low+high)/2
= (4+6)/2 = 5
Check A[m]=KEY
A[5]=60 ??
A[5]=60
The number is present in the list.
Algorithm
Searching the element using binary search.
low0
highn-1
while(low<high) do
{
m(low+high)/2
if(KEY=A[m])then
return m
elseif(KEY<A[m])then
highm-1 // search the left sub list
else
lowm+1 // search the right sub list
}
return -1 // if element is not present in the list
Analysis
• Time complexity of binary search.
Best case Average case Worst case
Θ(1) Θ( Θ(
• Advantages:
• Optimal searching algorithm using which we can search the desired element very
efficiently.
• Disadvantages:
• Algorithm requires the list to be sorted.
• Applications:
• Search records from the database.
• For solving non linear equation with one unknown.
Sequential search vs. Binary Search
Sr Sequential Search Binary Search
no.
1 Simple technique of searching an Efficient technique of searching an
element. element.
2 This technique does not require the This technique requires the list to be
list to be sorted. sorted.
3 Every element of the list may get Only the mid element of the list is
compared with the key element. compared with the key element.
O(n).
4 Worst case time complexity is Worst case time complexity is O(
Max-Min Problem
• Problem : Find Minimum and Maximum number from the given list.
• Example
50 40 -5 -9 45 90 65 25 75
=2T(n/2) + cn
So,
a=2, b=2, k=1, p=0
so, as per master theorem
T(n) = Ɵ(nlogb a logp+1 n)
= Ɵ(n logn)
Quick Sort
Quick sort algorithm is a sorting algorithm that uses the divide and conquer
strategy.
i<j
Here i>j
Quick Sort example
i<j
Now
condition
false
i>j
Quick Sort example
Now
condition
false
i>j
Quick Sort algorithm
Algorithm Quick(A[0 . . .n-1] , low , high)
{
if (low < high)
then m partition(A[low. . . high])
Quick(A[low . . .m-1])
Quick(A[m+1 . . .high])
}
Quick Sort algorithm
Algorithm Partition (A[low . . . high])
{
pivot A[low]
i low
j high
while(i < j) do
{ while(A[i] < pivot) do // Move left to right
ii+1
while(A[j] >= pivot) do // Move right to left
jj–1
if( i < j) then
swap(A[i] , A[j])
}
swap(A[low] , A[j])
return j
}
Quick Sort analysis
• As per master method
T(n) = aT(n/b) + f(n)
in Quick sort every time there will be two parts so a=2, b=2.
T(n) = 2T(n/2) + n
So, a = 2, b=2, k=1, p=0
Hence,
T(n) = Ɵ(nlogb a logp+1 n)
= Ɵ(n logn)
Matrix Multiplication
• Suppose we want to multiply two matrices of size N x N: for example
C= A x B
• C11=S1+S4-S5+S7
• C12=S3+S5
• C21=S2+S4
• C22=S1+S3-S2+S6
Continue . . .
• Now
For example
C11 = S1+ S4 - S5 + S7
C11 = (A11 + A22)(B11+B22) + A22 (B21 – B11) – (A11 + A12) B22 + (A12 – A22) (B21 + B22)
C11=A11B11 + A11B22 + A22B11 + A22B22 + A22B21 – A22B11 – A11B22 – A12B22 + A12B21
+ A12B22 – A22B21 – A22B22
C11=A11B11 + A12B21
Continue . . .
• It is therefore possible to multiply two 2x2 matrices using only seven scalar
multiplications.
• Let, t(n) be the time needed to multiply two nxn matrices.
• Here matrices can be added and subtracted in a time in Ө(n2).
So, t(n) = 7 t(n/2) + g(n)
When g(n) Є Ө(n2)
a= 7 , b= 2, P =0, k=2
Case-1 is true. So,
T(n) = Ө(nlog27)
= Ө(n2.81).
Exponential
• Suppose : an
• Here a is any constant value and n is power of a.
Example :
a5 = a * a * a * a *a
Here total 4 multiplications are required.
• Which involves only three multiplications and four squaring instead of the 28
multiplications required. The above recurrence gives rise to the following algorithm.
• ALGORITHM
To find number of multiplication required
Example : a15
So, N(15) = ?
Example:
Example:
Algorithm Exponentiation(x,n)
{
if(n=0) then
return 1;
else
r= Exponentiation(x,n/2);
if(n%2==0) then
return (r*r) //when exponentiation value is even
else
return (r*r*x) //when exponentiation value is odd
}
Thank You