Professional Documents
Culture Documents
Divide and Conquer : General method, Binary search, Recurrence equation for divide and
conquer, Finding the maximum and minimum, Merge sort, Quick sort, Strassen’s matrix
multiplication, Advantages and Disadvantages of divide and conquer. Decrease and
Conquer Approach : Topological sort.
1.nagratna
2. maitre
3.arshiya afreen
4.sushma
5.mamtha patil
6.karishma
7.rekha.b
8.aishwarya
9.prajwal
1o.raju
Guided by:
Tejeshwini C SB.E., M.Tech
Assistant Professor
CSE,DEPT Page 1
MODULE-2
NOTE:
The base case for the recursion is sub-problem of constant size.
GENERAL METHOD
Sub problem 2
Set problem1 of Of size n/2
size n/2
CSE,DEPT Page 2
MODULE-2
{ if small (p) then s(p);
Else
{
Divided p into smaller instance p1,p2,p3….Pk,,,,, k>1
Apply DA and C tom each of these sub problems;
Return combine(D And C(P1),D And C(P2),,,,,,,,,,,,,,,D And(p3))
}
}
The complexity of many divide &conquer algorithms is given by recurrence of the form
T(n)={G(N)
T(N1)+T(N2)+…..+T(NK)+F(N)
N is small
STEP 1: A problem instance is divided into several smaller instance of the same kind, identically of the same size.
STEP 2: A smaller instance of problem are solved recursively.
STEP 3: If necessary the solution obtained for the smaller instances combined to get solution for instance.
NOTE :
1. D AND C TOP DOWN APPORACH LIKE FUNCTIONS.
2. IN GENERAL D&C TECHNIQUE IS A SEQUENTIOL.
Binary Search
Description:
* Binary search is a divide and conquer technique for searching a key in any set (numbers or characters)
* Binary search can applied only for sorted or ordered set (ascending order)
* Here we divide the entire array into two equal parts hence it is called binary search
Algorithm steps:-
Step 1: find the mid value by the formula
Mid= low + high /2
Step 2: It works by comparing a search tree k with array mid elements
A[mid]
Step 3: If they match the algorithms stop by returning the mid value
Step 4: otherwise the same operation is repeated recursively for the first half of the array if k>a[mid]
Step 5: else return-1 for the unsuccessful search
CSE,DEPT Page 3
MODULE-2
Algorithm:-
l→0
r→n-1
while l ≤ r do
m→( l + r) / 2
if key = = A[m]
return m
else
if key < A[m]
r→m-1
else
l→m+1
return -1
Analysis:-
• Input size: Array size, n
• Basic operation: key comparison
• Depend on:
Best – key matched with mid element
Worst – key not found or key sometimes in the list
• Let C(n) denotes the number of times basic operation is executed. Then
Cworst(n) = Worst case efficiency. Since after each comparison the algorithm
divides the problem into half the size, we have
Cworst(n) = Cworst(n/2) + 1 for n>1
C(1) = 1
• Solving the recurrence equation using master theorem, to give the number of
times the search key is compared with an element in the array, we have:
• C(n) = C(n/2) + 1
a=1
b=2
f(n) = n0 ; d = 0
case 2 holds:
C(n) = Θ (ndlog n)
= Θ (n0log n)
= Θ ( log n)
CSE,DEPT Page 4
MODULE-2
Example:-
20 32 43 49 52
n=5 k=32
1st iteration:- 2nd iteration :- 3rd iteration
l=0 r=4 l=0 r=1 l=1 r=1
0< =r 0<=1 1<=1
True True True
5 4 -1 -9
5 4 -1 -9
Max=5 max=-9 5
Min=4 min=-1
Divide the array into 2 parts until it becomes the array of 2 nos.
Apple conquer process by comparing 2 temp min & max hence we find out real min & max.
Max= 5 > -9
Min= 4< -1 =-1
ALGORITHM:
Max _ Min _Val(I, j, max, min)
{
if(i==j)then
{
maxA[i]
minA[j]
}
else if (i=j-1) then
{
if(A[i]<A[j]) then
{
maxA[j]
min A[i]
}
else
{
maxA[i]
minA[j]
}
}
else
mid (i+j)/2
max _ min_ val (i, mid, max ,min)
Max _ Min _Val(mid+1,max_new,min_new)
if(max<max_ new)then
maxmax_ new
if(min>min_ new)then
minmin_ new
}
MERGE SORT
Description:-
Merge sort is a divide and conquer technique, & it is a sort algorithm that splits the items to be sorted in to two
groups, recursively sorts each group, and merges them into final sorted sequence.
It sort a given array A[0……..n-1] by dividing into 2 equal i,e A[0…….n/2-1]and b[n/2……..n-1],sorting of
them recursively
Merge fn is a sub fn under a merge sort for merging 2 smaller sorted arrays into a single sorted one
Algorithm:
ALGORITHM Merge sort ( A[0… n-1] )
//sorts array A[0- -n-1] by recursive merge sort
//Input: An array A[0- -n-1]of orderable elements
//output: array A[0- -n-1] sorted in non decreasing order
if n > 1
copy A[0… (n/2 -1)] to B[0… (n/2 -1)]
copy A[n/2… n -1)] to C[0… (n/2 -1)]
Merge sort ( B[0… (n/2 -1)] )
Merge sort ( C[0… (n/2 -1)])
Merge ( B, C, A )
i→0
j→0
k→0
while i < p and j < q do
if B[i] ≤ C[j]
Dept of CSE Page 24
MODULE-2
A[k]→B[i]
i<--i+1
else
A[k] →C[j]
J<--j + 1
K<--k +
1 if i = p
copy C [ j… q-1 ] to A [ k… (p+q-1) ]
else
copy B [ i… p-1 ] to A [ k… (p+q-1) ]
Example:
Apply merge sort for the following list of elements: 6, 3, 7, 8, 2, 4, 5, 1
TRACKING:
2 3 8 9
Take this array as p=4
i=0,j=0,k=0
B[0]<=C[0]
2<=1[false]
A[0]<-C[0]
A[0]<-1
J<-j+1
J<-0+1
J<-1
K<-0+1
K<-1
I=0,j=1,k=1
B[0]<=C[1]
2<=4[true]
A[1]<-B[0]
A[1]<=2
I=i+1=0+1
I<-1
K<-1+1
K<-2
I=1,j=1,k=2
B[1]<=C[1]
3<=4[true]
A[2]<-B[1]
A[2]<-3
I<-i+1<-1+1
I<-2
K<-2+1
K<-3
I=2,j=1,k=3
Dept of CSE Page 25
MODULE-2
B[2]<=C[1]
8<=4[false]
A[3]<-4
J<-j=1<-1=1
J<-2
K<-3+1
K<-4
I=2,j=2,k=4
B[2]<=C[2]8
8<=5[false]
A[4]<-5
J<-j+1<-2+1
J<-3
K<-4+1
K<-5
I=2,j=3,k=5
B[2]<=C[3]
8<=7[false]
A[5]<-7
J<-3+1
J<-4
K<-5+1<-6
I=2,j=4,k=6
I<p &j<q
2<4 4<4 false
Come out of the while loop
And the check,
I=p
2==4 false
Then copy the last two elements 8&9
Now the sorted array is
1 2 3 4 5 6 7 8 9
Analysis:
• Input size: Array size, n
• Basic operation: key comparison
• Best, worst, average case exists:
Worst case: During key comparison, neither of the two arrays becomes empty
before the other one contains just one element.
• Let C(n) denotes the number of times basic operation is executed. Then
C(n) = 2C(n/2) + Cmerge(n) for n > 1
C(1) = 0
where, Cmerge(n) is the number of key comparison made during the merging stage.
In the worst case:
Cmerge(n) = 2 Cmerge(n/2) + n-1 for n > 1
Cmerge(1) = 0
• Solving the recurrence equation using master theorem:
C(n) = 2C(n/2) + n-1 for n > 1
C(1) = 0
Here a = 2
b=2
f(n) = n; d = 1
Therefore 2 = 21, case 2 holds
C(n) = Θ (ndlog n)
= Θ (n1log n)
//Input: A sub-array A[l..r] of A[0..n-1],defined by its left and right indices l and r
//Output: The sub-array A[l..r], sorted in non
decreasing order if l < r
S<--Partition (A[l..r]) // s is a split
position
Quick sort(A[l..s-1])
Quick sort(A[s+1..r]
swap(A[i], a[j])
until i >= j
//no need to scan
swap(A[i], A[j])
swap(A[l], A[j])
return j
Analysis:
• Input size: Array size, n
• Basic operation: key comparison
• Best, worst, average case exists:
Best case: when partition happens in the middle of the array each time.
Worst case: When input is already sorted. During key comparison, one half is
empty, while remaining n-1 elements are on the other partition.
• Let C(n) denotes the number of times basic operation is executed in worst case:
Then
C(n) = C(n-1) + (n+1) for n > 1 (2 sub-problems of size 0 and n-1 respectively
C(1) = 1
Best case:
C(n) = 2C(n/2) + Θ(n) (2 sub-problems of size n/2 each)
Solving the recurrence equation using backward substitution/ master theorem, we have:
C(n) = C(n-1) + (n+1) for n > 1; C(1) = 1
C(n) = Θ (n2)
NOTE:
The quick sort efficiency in average case is Θ( n log n) on random input.
Decrease by constant:
In the size of an instance is reduced by same constant on each iterations of an algorithm.
Ex: Increase sort is an best example.
The technique suggest reducing a problem instance by same constant factor on each iteration of an
algorithm, on or in most of constant factor is always is equal to 2.
Hence the technique works like D & C
TOPOLOGICAL SORTING:
Topological sorting is a sorting method to list the vertices of the graph in such an order that for
every edges in the graph , the vertex where the edges starts is listed before the vertex where the
edge end.
1. DFS METHOD:
* it is d& c technique to travel a graph & to find topological ordering,
* stack is used for the structure of DFS.
EXAMPLE:
Apply DFS method to find topological sorting for graph below
a b
nd
2 iteration =b is source get all Adjacency of nodes of source b.
Top=-1 stack
{a, c, d} D 1
1 0 0 C 2
B 3
{a, c, d} A 4
push pop
‘d’ be since there is unvisited .adjacency vertex of d becomes dead end hence pop d.
5th= c as source (c is stack source since stack stop is c).get all adjacency node are
{a, b, d}
1 1 1 C
B
A
1 1 1