Professional Documents
Culture Documents
Chapter 1: Divide and Conquer
Chapter 1: Divide and Conquer
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
Time required
Time required Time required to add left and
to add items on to add items on right parts
the left part of the right part of
array array
T(n)=θ(n)
Solve using repetitive substitution method
Or using masters theorem
MERGESORT
• ALGORITHM : Mergesort(A[0..n − 1])
//Sorts array A[0….n − 1] by recursive mergesort
//Input: An array A[0….n − 1] of orderable elements
//Output: Array A[0….n − 1] sorted in nondecreasing 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]
Mergesort(B[0..n/2 − 1])
Mergesort(C[0..n/2 − 1])
Merge(B, C, A)
• ALGORITHM: Merge(B[0..p − 1], C[0..q − 1], A[0..p + q − 1])
//Merges two sorted arrays into one sorted array
//Input: Arrays B[0..p − 1] and C[0..q − 1] both sorted
//Output: Sorted array A[0..p + q − 1] of the elements of B and C
i ←low; j ←mid+1; k←low
while i <=p and j <=q do
if B[i]≤ C[j ]
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]
ANALYSIS
0 if n=1
T(n)= 2T(n/2)+Tmerge(n) otherwise
DISADVANTAGES
• Uses more memory on stack because of recursion.
• Uses extra space ,is proportional to N. so algorithm is not in
place.
• Though merging can be done in place, the resulting algorithm
is quite complicated and has significantly lager multiplicative
constant.
QUICK SORT(partition exchange sort)
• Unlike mergesort, which divides its input
elements according to their position in the
array, quicksort divides them according to
their value.
p←A[l]
i ←l; j ←r + 1
repeat
repeat i ←i + 1 until A[i]≥ p
repeat j ←j − 1 until A[j ]≤ p
swap(A[i], A[j])
until i ≥ j
swap(A[i], A[j]) //undo last swap when i ≥ j
swap(A[l], A[j ])
return j
ANALYSIS
• BEST CASE: (pivot present exactly at center)
0 if n=1
T(n)= 2T(n/2)+n otherwise
T(n)= θ(nlog2n)
T(n)= O(n2)
• AVERAGE CASE: (pivot element can be placed
at any arbitrary position)
n-1
T(n)= θ(nlog2n)
ADVANTAGES AND DISADVANTAGES
ADVANTAGES
• It has an extremely short inner loop
• Time complexity O(nlog2n) in best and average case to sort n
items.
• The algorithm is in PLACE: since it uses a small auxiliary stack.
DISADVANTAGES
• It is not stable
• Time complexity is O(n2) in worst case
• It is fragile, i.e, simple mistake in implementation go
unnoticed and algorithm may not work .
BINARY SEARCH
ALGORITHM: BS( key, a , low , high)
//search for key element in the list//
//input: a-array //
// key: element to be searched in the list/
// low and high; index of first and last elements//
// output: return position of key if found ,-1 otherwise//
If( low>high) return 0;
mid=(low+high)/2
If (key=a[mid]) return mid;
If (key<a[mid]) return BS(key,a,low,mid-1)
return BS(key,a,mid+1,high)
ANALYSIS
• BEST CASE: T(n)=Ω(1)
T(n)=θ(log2n)
FINDING MINIMUM AND MAXIMUM ELEMENT
Algorithm: Maxmin(i,j,max,min)
If(i=j)then
Max=min=a[i];
If(i=j-1)then
If(a[i]<a[j])then
Max=a[j];
Min=a[i];
else
Max=a[i];
Min=a[j];
End if
End if
Else
Mid=(i+j)/2;
Maxmin(i,mid,max,min);
Maxmin(mid+1,j,max1,min1);
Max=max(max,max1);
min=min(min,min1);
endif
End maxmin
ANALYSIS
T(n/2)+T(n/2)+2 for n>2
• T(n)= 1 n=2
0 n=1
T(n)= θ(n)
STRASSEN’ MATRIX MULTILICATION
C00 C01 = A00 A01 * B00 B01
C10 C11 A10 A11 B10 B11
= m1+m4-m5+m7 m3+m5
m2+m4 m1+m3-m2+m6
T(n)= θ(n2.807)
GENERAL METHOD
Algorithm : DAndC(P){
if Small(P) then returnS(P);
else
{
divide P into smallerinstancesP1,P2,..Pk, k > 1;
Apply DAndC to each of these sub-problems;
Return Combine(DAndC(P1),DAndC(P2),DAndC(Pk));
}
}
DECREASE AND CONQUER
• The decrease-and-conquer technique is based on exploiting the
relationship between a solution to a given instance of a problem and a
solution to its smaller instance.
• f (n) = f (n − 1) . a if n > 0,
1 if n = 0,
decrease-by-a-constant-factor
• decrease-by-a-constant-factor technique suggests reducing a
problem instance by the same constant factor on each
iteration of the algorithm.
• In most applications, this constant factor is equal to two.
• Methods:
1. DFS method
2. Source removal method
DFS METHOD
Step 1: select any arbitrary vertex.
to stack.
Step 4: repeat step 2and 3 for all the vertices in the graph.
T(n)= O(|V|+|E|)
SOURCE REMOVAL METHOD
• In this method, the vertex with no incoming edges is
selected and deleted along with edges.