Divide & Conquer Unit 1
Divide & Conquer Unit 1
Conquer/Solve
• This step receives a lot of smaller sub-problems to be solved. Generally, at this level, the problems
are considered 'solved' on their own.
Merge/Combine
• When the smaller sub-problems are solved, this stage recursively combines them until they
formulate a solution of the original problem.
• This algorithmic approach works recursively and conquer & merge steps works so close that they
appear as one.
Main Problem
Application of Divide and Conquer Approach
Following are some problems, which are solved using divide and
conquer approach.
Example 4
T(n)=2T(n/2)+n-------------------------------------------------------------1
T(1)=1
SOL: Find T(n/2)
T(n/2)=2T(n/4)+n/2
T(n)=2(2T(n/4)+n/2)+n➔4T(n/4)+2n
T(n/4)=2T(n/8)+n/4
T(n)=4(2T(n/8)+n/4)+2n➔8T(n/8)+3n→16T(n/16)+4n→32T(n/32)+5n➔2^5
T(n/2^5)+5n→2^kT(n/2^k)+kn
n/2^k=1➔n=2^k➔k=log2n
T(n)=2^lognT(1)+logn(n)➔n(1)+nlogn➔O(nlogn)
Example 5
T (n) =2T(n/2) +2 n>1
• T(1)=0
• Sol: T(n/2)=2T(n/4)+2
T(n)=2(2T(n/4)+2)+2➔4T(n/4)+4+2->8T(n/8)+8+4+2➔
16T(n/16)+16+8+4+2➔
2^kT(n/2^k)+2^k+2^k-1…………2➔2^k(T(1)+(2^K+1- 2)
→2^logn(0)+2^logn+1=>
Example 6
• T (n) =2T(n/2) +n 2 n>1 •
T(1)=0
Master Method
• Master Method is a direct way to get the solution of a recurrence
relation, provided that it is of the following type:
T(n)=2T(n/2)+cn
• 1. a=2 b=2 f(n)=cn
• 2. h(n)= ��(��)
��^ log ����=cn/n^(log 22)=cn/n=c=➔c(log
�� �������� 1
n)^0 • 3. u(n)=
1= ��(log �� )
log ��
• 4. T(n)=�� �� [T(1)+u(n)]=n[1+��(log�� )
]=��(nlog�� ) •
Then the three condition that needs to be tested while searching array
1. If key<a[mid] then search left subarray.
2. If key>a[mid] then search in right subarray.
3. if key=a[mid] then the desired element is present in list.
Example
• 10,20,30,40,50,60,70
Key is 30
10, 20, 30 40 50, 60 70
1 2 3 4 5 6 7
Mid=(low+high)/2=1+7=8/2=4 a[4]=40
Key 30 with 40
2. If key<a[mid] then search left subarray. Key=30
30<40
So search in left subtree
0+2=2/2=1
10, 20, 30
1 2 3
30>a[1] 30>20 so search in left subtree 10
30
2
10 20 30 40 50 60 70 80
a[1] a[2] a[3] a[8]
• Key is 70
• 10 20 30 40 50 60 70 80 mid=1+8=9/2=4 10 20 30
50 60 70 80
50 70 80
70 80
Low High Mid A[mid] compare
1 8 4 40 70>40
5 8 13/2=6 60 70>60
7 8 15/2=7 70 70=70
2,4,5,6,8,9 Key is 8
a[0]=2,a[1]=4 a[2]=5 a[3]=6 a[4]=8 a[5]=9
• a[6]
• 1. mid ➔(low+high)/2➔0+5/2=>2 a[2]=5
• 2. 8>5 check it sub array 2,4,5,6,8,9
• 2,4, 5 6,8,9
• 6,8,9
• Low=3 high=5 mid=(8/2)=4 a[4]=8 6 9 •
Key==mid
Example
• arr = [2, 3, 5, 7, 8, 10, 12, 15, 18,
20] target = 7
low=0
high=9
.ow= g=
mid=4
2. 7<8
low=0 high=3
mid=1 3. a[mid]=>a[1]=3 7>3
low=2 high=3 mid=2
A[]=1 2 5 6 7 10 24 56 84 100 115 120 131
150 key 150
Low High Mid A[mid] compare
1 14 1+14=15/2=7 24 150>24
8 14 11 115 150>115
12 14 13 131 150>113
14 14 14 150 found
• Alg(A,6,8)
•{ 8!=9
• low:=1 high=6 8<9
• while(1<=6) low=4 ; high=4
•{ while(4<=4)
• mid=1=6=7/2=3 {
• 8!=6 mid=8/2=4
• 8>6 key==a[mid]➔8==8 return mid;
• Low=4 high=6
Algorithm recursive Binary
search 1. Algorithm
Rbinsearch(A,key,low,high 11. if(key==a[mid]) then return mid;
) 2. { 12. else if(key<a[mid]) then return
3. if(low==high) then Rbinsearch(A,key,low,mid-1);
4. { 13. else return
5. if(key==a[low]) then return low; Rbinsearch(A,key,mid+1,high)
6. else return 0; ; } 150 8,14
7. } 150,12,14
8. else 150,14,14
10. mid:=(low+high)/2; }
9. {
3,6,9,12,13 key 6
• Alg binsearch(A,6,1,5) mid=3 a[3]=9 6<9
• Average Case: When key found between recursive calls. But not till
end.
Let say the iteration in Binary Search terminates after k iterations. In the above example, it
terminates after 3 iterations, so here k = 3
At each iteration, the array is divided by half. So let’s say the length of array at any iteration is
n At Iteration 1,
Length of array = n
At Iteration 2,
Length of array = n⁄2
At Iteration 3,
Length of array = (n⁄2)⁄2 = n⁄2(2) =n/4
Therefore, after Iteration k,
Length of array = n⁄2^k
Also, we know that after
After k divisions, the length of array becomes 1
Therefore
Length of array = n⁄2^k = 1
=> n = 2^k
Applying log function on both sides:
=> log2 (n) = log2(2k)
• => log2 (n) = k log2 (2)
• As (loga(a) = 1)
• Therefore,
• => k = log2 (n)
• Hence, the time complexity of Binary Search is
• log2(n)
K th iteration N/2^k=1
N=2^k➔k=log 2 N➔O(log N)
n=30
Algorithm
• Algorithm mergesort(l,h)
{
if(l<=h)
{
mid:=(l+h)/2
mergesort(l,mid);
mergesort(mid+1,h)
; merge(l,mid,h);
}
}
Merge(l,m,h) 1. 7. b[p]:= a[i]; i++;
8. else
Algorithm merge(l,m,h) 2. { 9. b[p]:=a[j];
3. i:=l; j:=mid+1 p:=l;
10. j++;
4. while((i<=mid))&&(j<=high)) 5. {
11. p++;
6. if(a[i]<=a[j]) then
12. }
13. if(i>mid) then
14. {
15. for q:=j to high
28.
1. Algorithm merge(l,mid,h)
18. p++; 2. {
3. i=l,p=l,j=mid+1;
19. }
4. while((i<=mid)&&(j<=h)) 5. {
20. }
6. if(a[i]<=a[j])
21. Else 7. {
22. for q:= I to mid 23. { 8. b[p]=a[i];
24. b[p]:=a[q]; 25. p++; 9. i++;
10. }
26. }
11. else
27. } 12. {
13. b[p]=a[j];
14. j:=j+1;
15. } 28. for k:=i to mid do 29. {
16. p++; 30. b[p]=a[k];
17. } 31. p++;
18. if(i>mid) 32. }
19. { 33. }
20. for k:=j to h do 21. { 34. for k:=1 to h do
22. b[p]:=a[k]; 23. p++; 35. a[k]:=b[k];
24. } 36. }
25. } 37.
26. else
27. {
Analysyis
Algorithm mergesort(l,h)
{
if(l<=h) then
else
{
mid:=(l+h)/2
mergesort(l,mid); T(n/2)
mergesort(mid+1,h); T(n/2)
merge(l,mid,h); n
}
}
T(n)=1 if n is small
T(n/2)+T(n/2)+n➔2T(n/2)+n
• T(n)=2T(n/2)+n
Substitution method
T(n/2)=2T(n/4)+n/2
T(n)= 2[2T(n/4)+n/2]+n➔4Tn/4+n+n➔4T(n/4)+2n
T(n/4)=2T(n/8)+n/4
T(n)=8T(n/8)+3n➔16T(n/16)+4n➔2^4 T(n/2^4)+4n
→ 2^k T(n/2^k)+kn
n/2^k=1➔n=2^k=>k=log n
2^(log n) T(1)+(logn )n➔n logn➔O(n
log n)
T(n)=1 if n is small
2T(n/2)+n
• Masters theorem
• a=2 b=2 f(n)=n
• h(n)=f(n)/n^(logb a)➔ f(n)=n/n^(log 2 2)➔n/n=1 (logn ^0) •
u(n)=Teta(log n)
• T(n)=n^(log 2 2)[1+teta(log n)]➔ n[teta(log n)]➔teta(n log n)
• Worst Case Time Complexity O(n*log n)
• Best Case Time Complexity O(n*log n)
• All the elements to the left side of pivot are smaller than pivot. • All the
elements to the right side of pivot are greater than pivot. • After dividing the
array into two sections, the pivot is set at its correct position.
• Then, sub arrays are sorted separately by applying quick sort algorithm
recursively.
Example- 1 3 6 9 1 5 2
• 1.Pivot =3
• 2. i=1 i++ a[i]<=pivot
• 3. j=high j- - a[j]>=pivot
• 4. i<j swap a[i]&a[j];
• 5. i>j swap a[j] & pivot
369152
a[1] a[2] a[3] a[4] a[5] a[6]
• Pivot 3;
• i=1 3<=3 i++ j=6 2>=3 false
• i=2 6<=3 false
• i<j 2<6 swap a[2]&a[6]
•329156
• a[1] a[2] a[3] a[4] a[5] a[6]
i=2 2<=3 i++ j=6 6>=3 j--
i=3 a[3]<=pivot 9<=3 false j=5 5>=3 j--
j=4 1>=3 false
321956
i=3 1<=3 i++ j=4 9>=3
i=4 9<=3 false j=3 1>=3 false swap a[j] & pivot
1 2 3 9 5 6 pivot
• 1 2 1st sublist
9 5 6 2nd sublist
• Step 1 - Consider the first element of the list as pivot (i.e.,
Element at first position in the list).
• Step 2 - Define two variables i and j. Set i and j to first and
last elements of the list respectively.
• Step 3 - Increment i until list[i] > pivot then stop.
• Step 4 - Decrement j until list[j] < pivot then
stop. • Step 5 - If i < j then exchange list[i] and
list[j].
• Step 6 - Repeat steps 3,4 & 5 until i > j.
• Step 7 - Exchange the pivot element with list[j] element.
Repeat until loop