Divide and ConquerSyllabus
General Method
Defective chessboard
Binary Search
Finding the maximum and minimum
Merge sort
Quick sort
4.
Divide & ConquerStrategy:
• One of the most widely used algorithm design techniques is the Divide and Conquer
Strategy. This can be applicable to problems that are divisible.
• In this approach, a complex problem can be divided into smaller subproblems. The
subproblem is also divisible and can be further divided into sub-subproblems. The
same process continues until the problems are indivisible.
• The subproblems are recursively solved and combined into the final solution in
reverse fashion.
5.
• The Divideand Conquer Strategy follows these three steps:
1. Divide
In this phase, a complex problem is divided into smaller subproblems until
they become indivisible.
2. Conquer
All the subproblems are recursively solved.
3. Combine
All the solutions of the subproblems are combined into the final solution in
reverse fashion.
Control Abstraction forDivide and Conquer Algorithms – General Method
Algorithm DandC(P) {
if Small(P) then return S(P);
else {
Divide P into smaller instances P1,P2,…….,Pk, k≥1; Apply DandC to each of these
sub-problems;
Return Combine(DandC(P1),DandC(P2)...,DandC(Pk)),
}
8.
Divide and Conquer
Divideand Conquer for finding Counterfeit coin (exactly one coin):
Algorithm CounterfeitCoin(Bunch, numberofcoins)
{
if (numberofcoins = 2) { weigh the two coins;
if one of them weighs less that is counterfeit coin; else, no counterfeit coin in the Bunch
}
else {
Divide Bunch into two halves Bunch1 and Bunch2;
if Bunch1 weighs less than Bunch 2, call CounterfeitCoin(Bunch1, numberofcoins / 2);
else, call CounterfeitCoin(Bunch2, numberofcoins / 2); } }
9.
Finding Largest withDivide and Conquer:
Algorithm DandCLargest (a,k, j)
{
if (k = j) Return a[n]; else
{
mid := (k + j) /2;
x1= DandCLargest (a,k,mid); x2= DandCLargest (a,mid+1,j); if (x1 > x2) Return
x1;
else Return x2;
10.
}
}
First Call forRecursion:
DandCLargest (a,1,n);
Divide and Conquer algorithm design works on the principle of
dividing the given problem into smaller sub problems which are
similar to the original problem. The sub problems are ideally of the
same size.
11.
The Divide andConquer strategy can be viewed as one which has three steps.
The first step is called Divide which is nothing but dividing the given problems
into smaller sub problems which are identical to the original problem and also
these sub problems are of the same size. The second step is called Conquer
where in we solve these sub problems recursively. The third step is called
Combine where in we combine the solutions of the sub problems to get the
solution for the original problem.
12.
Binary Search
•Binary Searchtakes a sorted array as the input
•It works by comparing the target (search key) with the middle element of the
array and terminates if it equals, else it divides the array into two sub arrays and
continues the search in left (right) sub array if the target is less (greater) than the
middle element of the array.
Reducing the problem size by half is part of the Divide step in Binary Search and
searching in this reduced problem space is part of the Conquer step in Binary
Search. There is no Combine step in Binary Search.
13.
With Divide andConquer (recursion)
Algorithm BinSrch(a,i,l,x) {
// Given an array a[I:l] of elements in non decreasing order, 1≤ i ≤ l,
// determine whether x is //present and if so, return j such that x =a[j]; else return
0.
if (l=i) then { // if Small(P)
if (x=a(i)) then return i; else return 0;
}
14.
else {
// ReduceP into a smaller subproblem. mid :=[(i + l)/2 ];
if (x=a[mid]) then return mid; else if (x<a[mid] then
return BinSrch (a,i, mid -1,x);
else return BinSrch (a, mid +1,l,x);
}
}
Iterative Binary Search (non-recursive)
Algorithm BinSearch (a,n,x) { low :=1;high :=n;
15.
while (low ≤high) do { mid :=(low +high)/2;
if ( x<a[mid]) then high :=mid – 1;
else if (x>a[mid]) then low :=mid + 1;
else return mid;
}
return 0;
}
16.
Theorem Algorithm BinSearch(a,n,x)works correctly. Proof:
We assume that all statements work as expected and that comparisons such as
x>a[mid] are appropriately carried out.
Initially low = 1, high = n, n ≥ 0, and a[1]≤ a[2]≤..≤ a[n]. If n = 0, the while loop is
not entered and 0 is returned.
Otherwise we observe that each time through the loop the possible elements to be
checked for equality with x are a [low], a[low + 1]…, ..., a[mid],..., a[high).
If x = a[mid], then the algorithm terminates successfully.
Otherwise the range is narrowed by either increasing low to mid + 1 or decreasing
high to mid — 1. Clearly this narrowing of the range does not affect
17.
the outcome ofthe search.
If low becomes greater than high, then x is not present and hence the loop is exited.
Performance of Binary Search
18.
Merge Sort
Merge sortis yet another sorting algorithm which works on the Divide and
Conquer design principle.
• Merge sort works by dividing the given array into two sub arrays of equal size
• The sub arrays are sorted independently using recursion
• The sorted sub arrays are then merged to get the solution for the original array.
The breaking of the given input array into two sub arrays of equal size is part of
the Divide step. The recursive calls to sort the sub arrays are part of the Conquer
step. The merging of the sub arrays to get the solution for the original array is
part of the Combine step.
19.
•The basic operationin Merge sort is comparison and swapping. Merge
Sort Algorithm calls it self recursively. Merge Sort divides the array into
sub arrays based on the position of the elements whereas Quick Sort
divides the array into sub arrays based on the value of the elements.
Merge Sort requires an auxiliary array to do the merging (Combine step).
The merging of two sub arrays, which are already sorted, into an auxiliary
array can be done in O(n) where n is the total number of elements in both
the sub arrays. This is possible because both the sub arrays are sorted.
20.
Algorithm MergeSort(low, high){
// Small(P) is true if there is only one element to sort .
// In this case the list is already sorted. if (low < high) then {
//If there are more than one element
//Divide P into subproblems. mid := [(low + high)/2] ;
// Solve the subproblems. MergeSort(low, mid); MergeSort (mid + 1, high);
21.
// Combine thesolutions. Merge(low, mid, high);
}
}
Merging in Merge Sort
Algorithm Merge (low, mid, high) {
// a[low:high] is a global array containing two sorted subsets in
// a [low:mid] and in a [mid + 1 :high]. The goal is to merge these
// two sets into a single set residing in a [low:high]. B[] is an
// auxiliary global array. h:=low; i:=low ; j : =mid +1; while ((h≤ mid) and
22.
( j≤high)) do{
if (a[h]≤a[j] then {
b[i] := a[h];h :=h+1; }
else {
b[i] := a[j];j :=j+1;
}
i:= i+1;
}
if ( h>mid) then
23.
for k:=j tohigh do
{ b[i] : =a[k]; i:=i+1; }
else for k :=h to mid do
{ b[i] := a[k]; i:= i+1; }
for k: =low to high do a[k] :=b[k];
}
Complexity of Merge Sort is O(n log n) and binary search is O(log n). This can
be proved by repeated substitution in the recurrence relations.
Quicksort:
Quick sort isone of the most powerful sorting algorithms. Quick sort works by finding an
element, called the pivot, in the given input array and partitions the array into three sub
arrays such that the left sub array contains all elements which are less than or equal to the
pivot.
The quick sort mechanism will follow the following 3 Steps:
1. if a[i] < pivot then i++ ( i value moves from left to right or forward direction )
2. if a[i] > pivot then j-- ( j value moves from right to left or backward direction )
3. if i < j exchange a[i] with a[j] else exchange pivot with a[j].
28.
Algorithm QuickSort (p,q)
//Sorts the elements a[p],…, a[q] which reside in the global
// array a[1 ;n] into ascending order ; a[n+1] is considered to
// be defined and must be ≥ all the elements in a[1 :n]
{
if (p < q) then // if there are more than one element
{
// divide P into two sub problems.
j : = Partition (a,p,q +1);
29.
// j isthe position of the partitioning element.
// Solve the subproblems.
QuickSort (p,j -1),
QuickSort( j +1,q)
// there is no need for combining solutions.
}
}
30.
Algorithm for Sortingby partitioning.
Algorithm Partition (a,m,p)
//Within a[m],a[m+1],….a[p-1] the elements are
//rearranged in such a manner that if initially t = a[m],
// then after completion a[q] = t for some q between m
// and p-1, a[k]≤ t for m≤ k<q, and a [k] ≥ t
// for q< k<p. q is returned. Set a[p] = ∞.
{
v :=a[m], I :=m; j : = p;
Algorithm Interchange (a , i, j)
//Exchange a[i] with a [j]
{
p : = a[i];
a [i] : = a[j]; a[j] : p;
}
Algorithm Partition the array a[m : p-1] about a [m]
Defective Chess BoardProblem :
• One of the most popular problems is the Defective Chess Board Problem,
which can be solved only by using the Divide & Conquer strategy.
• This problem cannot be solved using any algorithm design technique except
Divide and Conquer.
• In this problem, we have an n × n chessboard, where only one tile is
defective. Here, n is a power of 2, i.e., n = 2^k. The total number of tiles in
the chessboard is:
n × n = 2 × 2^k = 2^(2k)
35.
• For anychessboard, irrespective of the value of k, we have only one defective
tile and (2^(2k) - 1) undefective tiles.
• For the Defective Chess Board Problem, an L-shaped three-tile
combination is called a "Triomino". The triomino may be in any of the
following orientations.