2.divide and Conquer
2.divide and Conquer
General Method:
1. An instance of the given problem is divided into smaller instances of same type and
equal size.
2. All the smaller instances of the problem are solved.
3. The solution of all the smaller instances are combined together to get a solution to
the original problem.
Problem of size N
solution to solution to
problem A problem B
else
Divide P into P1,P2,P3----- PK, k>=1; //divide P into several instances
ALGORITHM Sum(low,high,a)
1. if (low = high ) return a[low]
2. mid = (low+high)/2
3. return sum(low,mid,a)+sum(mid+1,high,a)
Tracing :
Sum(0,5,a)
Sum(0,2,a) Sum(3,5,a)
Analysis:
The recurrence relation for this algorithm
T(n)= 0 if n=0
T(n/2) +T(n/2) +1 otherwise
Time required to add 2Nos
Time required to add the items in the right part of the array.
.
Time required to add the items in the left part of the array.
Analysis:
Best case: The best case occurs when all the elements are in the increasing order. The
number of comparisons will be (n-1).
Worst case: The worst case occurs when all the elements are in the decreasing order. The
number of comparisons will be 2 (n-1).
Average case: The number of comparisons will be 3n/2 -1
Step 1: if (i = = j)
max = min = a[i]
else
{
Step 2: if (i = =j-1)
{
Step 3: if(a[i] > a[j])
{
max = a [ i ]
min = a [ j ]
}
else
{
max=a [ j ]
min = a [ i ]
}
}
Step 4: else
{
mid=( i + j)/2
DandMaxMin (i , mid, max1, min1)
DandMaxMin (mid + 1, j, max2, min2)
if(max1 > max2)
max=max1
else
max=max2
if(min1 > min2)
min=min1
else
min=min2
}
}
Tracing:
Consider an array with elements 23, 9, 8, 72, 66, 34wheren=6.
In the first call the array is broken into two halves. The list is again broken down as
long as the size of the array is 2 or 1.
23 9 8 72 66 34 i=1
j=6
23 9 8 i=1 72 66 34 i= 4
j=3 j=6
The maximum and minimum values are chosen from the smallest list and these values are
returned to the preceding step where the size is slightly big. This is continued until the entire
list is searched.
1, 6 max= 72
min= 8
Analysis:
T(N)= 0 if n = 1
1 if n=2
T (n/2)+T (n/2) + 2 if n > 2
Let n = is the size of items in an array and T (n) = time required to apply the
algorithm on an array of size n. Here we divide the terms as T(n/2).
Addition with constant value 2 here tends to the comparison of the minimum with
minimum and maximum with maximum as in above example.
T(n)=T(n/2)+T(n/2)+2
=2.T(n/2)+2 --->eq.(1)
Let n=2k
In e.(2) substitute the value for T(2k-1) from eq.(3) obtained above
T(2k )=2.T(2k-1)+2
=2.[2.T(2k-2)+2]+2
=4T(2k-2)+4+2
=4.[2.T(2k -3)+2]+4+2
=8 T(2k -3)+8+4+2
As T(2)=1
and so on…
Generalizing it with3=k-1
=2k-1T(2(k-(k-1))+[2k-1+2k-2+………+21]
T(2k )=2k-1T(2)[2k-1+2k-2+………+21]
As T(2)=1,
=2k-1 [21+22+23+……………+2k-2+2k-1]
=2k-1+21. (2k-1-1)
(2-1)
Using geometric progression, a(rn-1) where a=2,n=k-1 and r=2
r-1
=2k-1+21. (2k-1-1)
(2-1)
=2k-1+2k-1+1-2 =2k-1+2k-2
2-1 1
T(2k)= 2k +2k-2
2
T(n)=n +n-2
2
T(n)=3n - 2
2
Therefore the number of comparisons required applying the divide and conquer
algorithm on n element =3n - 2
2
2.5 Searching
Searching Algorithms are designed to check for an search item or retrieve an item from
any given set of elements.
2.5.1 Linear Search or Sequential search
The most straight forward method of finding a particular element in an unordered list
is linear search. This technique simply involves the comparison of each element of the
list in a sequential manner until the desired element is found.
i=i;
Step 5:if(loc != -1) then
write ”item found at“ loc
else
write “item not found”
Step 6:exit
Tracing:
Case I: Element found in the list
Consider the elements 22 11 66 44 where n=4 and the item=66
i=0 ,LOC = -1
A[i]=22
22 != 66 incremnt i, and i=1
A[i]=11
11!= 66 incremnt i, and i=2
A[2] = =66
LOC=2
Therefore 66 is found in position 3 (LOC+1)
Case II: Element not found in the list
Consider the item 99 in the above list 22 11 66 44
i=0 ,LOC= -1
A[i]=22
22 != 99 increment i, and i=1
A[i]=11
11!= 99 increment i, and i=2
A[2] =66
66!=99 increment i, and i=3
A[3] =44
44!=99 increment i, and i=4
i is not less than n and LOC=-1 itself. Therefore display item not found in the list.
Analysis
Best case: It occurs if the item being searched is in the first location. Therefore Ω (1)
Worst case: It occurs if the item being searched is in the last position. Therefore O (n)
Average case:
Number of indexes=n
i.e)values are1,2,…n
Probability of finding an element at any position is same and it is 1/n
Number of comparisons = 1. 1 +2. 1 +3. 1 +………..+n.1
n n n n
Example:
Consider the elements in array A 11 22 33 44 where n=4 and search element ITEM is 11
low = 0(lower bound)
high = 3(upper bound)
mid = (0+3)/2 =1
ITEM<A[mid] so go to the lower half segment.
Therefore reset the value of high to mid-1
low=0 high=0
mid=0
ITEM= =A[0] and search element 11 is found at position 1 (mid+1).
Step 1. low←0
Step 2. high←n-1
Step 3. while(low<=high)
mid ←(low+ high)/2
if(item = =a[mid])
return mid+1
if(item < a[mid])
high←mid -1
else
low←mid+1
endif
endwhile
Step 4. return -1
Analysis:
The recurrence relation of Binary search in worst case is
T(n)= a if n=1
T(n/2) + b otherwise
Best case:
This occurs when the search element is the mid element itself.Best case complexity is
Ω(1)
Worst case (successful and unsuccessful case):
T(n) = a if n=1
T(n/2)+b otherwise
Assume that the size of the array is always an integral power of 2.If n is the number of
elements in the array , let n = 2k. Now consider the recurrence relation:
T(n)=T(n/2)+b -----eq.(1)
Let n=2k
In e.(2) substitute the value for T(2k-1) from eq.(3) obtained above
T(2k )=T(2k-1)+b
=T(2k-2)+b]+b
=T(2k-2)+2b
=T(2k -3)+b]+=2b
=T(2k -3)+3b
and so on…
=T(20)+kb
As n=2k , k=log2n
T(n) = a+log2n.b
Ignoring the constant T(n) =O(log2n)
Therefore time complexity of binary search in worst case is given by
T (n) = O (log2n)
Average case:
The search element is found somewhere in between the recursive calls but not at the
end.
c=n
T(n) = 1 ∑ c.2c-1
N c=1
c=k
T(n) = 1 ∑ c [ 2c - 2c-1 ]
n c=1
c=k
T(n) = 1 ∑ c .2c - c.2c-1
n c=1
c=k c=k
T(n) = 1 ∑ c .2c - ∑ c.2c-1
n c=1 c=1
c=k c=k-1
c=k c=k-1
T(n) = 1 ∑ c .2c - ∑ (c+1).2c
n c=1 c=1
As c=0 to k-1 only k term remains when the first two summations are equated
= 1 k.2k - [1(2k-1)]
n 1
= 1 [ k.2k - 2k + 1]
n
= 1 [2k(k-1)+1]
n
= 1 [n(k-1)+1]
n
= n(k-1) + 1
n n
=(k-1)+ 1 = nk - n + 1
n n
As n=2k ,
k=log2n
T(n)=n log2n - n + 1
2.6 SORTING
A Sorting mechanism is used to rearrange a given array or list elements according to a
comparison operator on the elements. The comparison operator is used to decide the
new order of element in the respective data structure - ascending or descending order.
ALGORITHM Simplemerge(A,low,mid,high)
// Merge two sorted arrays
//input A is a sorted arrary from the index position low to mid
// A is a sorted array from index position mid+1 to high
//output A is a sorted from index low to high
end while
Step 4: while(i<=mid)
c[k]=a[i]
k=k+1
i=i+1
endwhile
Step 5: while(j<=high)
c[k]=a[j]
k=k+1
j=j+1
endwhile
Step 6: for i=low to high
a[i]=c[i]
endfor
Step 7: return
T(n)= a if n=1
T(n/2) +T(n/2) +c.n otherwise
Time required to merge n elements
Time required to sort right sub array and right subarray
Let n = 2k
T(2k)=T(2k) +T(2k) +c.2k
2 2
and so on…
= 2k 2T(20) + k.c.2k
60,50,25,10 35,25,75,30
Divide
60,50 25,10 35,25 75,30
60 50 25 10 35 25 75 30
---------------------------------------------------------------------------
10,25,50,65 25,30,35,75
//input A is an unsorted array with low and high as lower and upper bound
//output A is a sorted array
Algorithm partition(A,low,high)
//purpose: Partition the array into parts such that elements towards left than
the key element and elements towards right of key are greater
than key element.
key←a[low]
i←low;
j←high+1
while ( i<=j)
do
i←i+1
while(key >=a[i])
do
j←j-1
while(key <=a[j])
if(i<j) exhange(a[i],a[j])
endwhile
exchange(a[j],a[low])
return j
Best Case:
Occurs when the key element is placed in middle and there are 2 subarrays (half
size of the original on both the sides)
T(n)= a if n=1
T(n/2) +T(n/2) +cn otherwise
Time required to merge n elements
Time required to sort right sub array
Let n = 2k
T(2k)=T(2k) +T(2k) +c.2k
2 2
= 2k 2T(20) + k.c.2k
and so on…
T(n)=O(n2)
Average case :
=
=2 1∫N 1/K dk
= 2 log e N – loge 1
T(N)/ N+1 = 2 loge N
T(N) = (N+1) .2 loge N
=2 N loge N > loge N
T(N) = θ (N log N)
low i j high
→42 37 11 98 36 72 65 10 88 78 (42>37 so, increment i)
key
i
42 37 11 98 36 72 65 10 88 78 (42>11 so, increment i)
i
42 37 11 98 36 72 65 10 88 78 (42<98 stop incrementing i
and compare 42 with a[j]
which is 78)
i j
42 37 11 98 36 72 65 10 88 78 (42<78 so, increment i)
i j
42 37 11 98 36 72 65 10 88 78 (42<88 so, decrement j)
i j
42 37 11 98 36 72 65 10 88 78 (since 42 < 10 is false
stop decrementing j)
since i<j exchange a[i] with a[j] and repeat the process
i j
42 37 11 10 36 72 65 98 88 78 (42>10 ,increment i)
j i
42 37 11 10 36 72 65 98 88 78(42 > 36,increment i)
i j
42 37 11 10 36 72 65 98 88 78(42 > 72,is false so stop
incrementing i and compare
42 with a[j] i.e.,98)
i j
42 37 11 10 36 72 65 98 88 78(42 < 98 decrement j)
i j
42 37 11 10 36 72 65 98 88 78(42 < 65,decrement j)
ij
42 37 11 10 36 72 65 98 88 78(42 <72,decrement j)
j i
42 37 11 10 36 72 65 98 88 78(42 <36,is false)
Since i exceeds j ,exchange a[low] and a[j] , now the array will be
36 37 11 10 42 72 65 98 88 78
Repeatedly sort the left sub array and sort the right sub array.
Disadvantages:
1. Recursions are slow with divide and conquer.
Questions:
1. Write the control abstraction for divide and conquer method.
2. Write an algorithm for finding maximum and minimum using straight forward
approach and find the time complexity.
3. Write an algorithm for finding maximum and minimum using divide and
conquer approach and find the time complexity.
4. Explain the process of binary search on a set of elements{23,56,64,73,82,88,90}
with the search element=208
5. Write an algorithm for merge sort using divide and conquer method and find its
time complexity.
6. Trace the following set of elements using merge sort.
4, 17, 8, 44, 109, 435, 16
7. Write an algorithm for quick sort using divide and conquer method and find its
time complexity.
8. Write a recursive binary search algorithm and analyse its time complexity.
9. Write the advantages of divide and conquer technique.
10. Trace the following set of elements using quick sort.
42, 37, 88, 22, 13, 45, 6
******************