DESIGN and ANALYSIS of
Algorithms
III year II-sem(CSE )
V.Swathi
Asst.Prof
UNIT-II: Divide and Conquer: General Method,
Defective Chessboard, Binary Search, Finding
the Maximum and Minimum, Merge Sort, Quick
Sort, Performance Measurement, Randomized
Sorting Algorithms.
.
Learningoutcomes
1.Describe the divide-and-conquer paradigm and explain
when an algorithmic design situation calls for it.
2.Recite algorithms that employ this paradigm.
3.Synthesize divide-and-conquer algorithms.
4.Derive and solve recurrences describing the
performance of divide-and-conquer algorithms
Introduction
Divide-and-conquer is a top-down
technique for designing algorithms that
consists of dividing the problem into
smaller sub problems hoping that the
solutions of the sub problems are easier
to find and then composing the partial
solutions into the solution of the original
problem.
Divide-and-conquer
For Example: Assuming that each
divide step creates two sub-
problems
Divide-and-conquer
Example-2
Divide-and-conquer
For Example: If we can divide the
problem into more than two, it
looks like this
Divide-and-conquer Detail
Divide/Break
This step involves breaking the problem
into smaller sub-problems.
Sub-problems should represent a part of
the original problem.
This step generally takes a recursive
approach to divide the problem until no
sub-problem is further divisible.
At this stage, sub-problems become
atomic in nature but still represent some
part of the actual problem.
Divide-and-conquer Detail
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.
Divide-and-conquer Detail
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.
Standard Algorithms
based on D & C
The following algorithms are based on divide-and conquer
algorithm design paradigm.
Defective Chess Board
Binary Search
Finding MAX and MIN
Merge Sort
Quick Sort
Strassen's Matrix Multiplication(Not in Syllabus)
General method
Divide : Split the input with n sample points into k
subsets, 1 < k ≤ n, with no overlap between
subsets.
Conquer : Solve each of the k subproblems, possibly by
splitting recursively
If the subproblems are small enough, solve those using a
simple (non-recursive) method
Also known as the bottoming out of recursion, or arrival at
a base case
Combine : Combine the result of the k sub problems to
solve the original problem
Detecting a counterfeit coin
– Bag of 16 coins with one of them possibly counterfeit
– Counterfeit coins are lighter than genuine ones
– Determine if the bag contains a counterfeit coin
– Algorithm 1
∗ Compare a pair of coins; if one of them is counterfeit, it will be lighter
∗ Compare next pair, and so on
∗ You will find the counterfeit coin in at most 8 trials
– Algorithm 2: Divide and conquer
∗ Divide the 16 coin instance into two instances of 8 coins each.
∗ Compare the weight of two sets; if same, no counterfeit coin; else divide the lighter instance into two of half the
original size and repeat
∗ Presence of counterfeit coin is determined in 1 trial
∗ Identification of coin happens in four trials
Algorithm D And C(P)
{
if small(P) then return S(P);
else
{
divide P into smaller instances P1,P2… Pk, k>=1;
Apply D And C to each of these sub problems;
return combine (D And C(P1), D And (P2),…….,D And C(Pk));
}
}
Control abstraction for divide and conquer
• If the size of ‘p’ is n and the sizes of the ‘k’ sub problems are n1,
n2 ….nk, respectively, then the computing time of D And C is
described by the recurrence relation.
T(n)= g(n) n small
T(n1)+T(n2)+……………+T(nk)+f(n); otherwise
Where
T(n) is the time for D And C on any I/p of size ‘n’.
g(n)is the time of compute the answer directly for
small I/ps.
f(n) is the time for dividing P & combining the
solution to sub problems.
• The complexity of many divide-and-conquer
algorithms is given by recurrences of the
form
T(n) = T(1) n=1
aT(n/b)+f(n) n>1
Where a & b are known constants.
We assume that T(1) is known & ‘n’ is a power of b(i.e., n=b^k)
• One of the methods for solving any such recurrence relation is
called the substitution method.
• This method repeatedly makes substitution for each occurrence
of the function T in the Right-hand side until all such occurrences
disappear.
T(n) = aT(n/b) + f (n) where f(n) (nd), d 0
Master Theorem: If a < bd, T(n) (nd)
If a = bd, T(n) (nd log n)
If a > bd, T(n) (nlog b a )
Note: The same results hold with O instead of .
Example
1) Consider the case in which a=2 and b=2.
Let T(1)=2 & f(n)=n
DEFECTIVE CHESS
BOARD PROBLEM-
DIVIDE AND CONQUER
The Defective Chessboard
GIVEN CONDITIONS:-Problem
1.We have a chessboard of size n x n , where n =2k
2.Exactly one square is defective in the chessboard.
3.The tiles(trominoes) are in L-shape i.e. 3 squares.
OBJECTIVE
Cover all the chessboard with L-shape tiles(trominoes), except
the defective square.
Is it possible to solve this?
Absolutely, it is possible to cover all non-defective squares.
Let’s see how
•As the size of the chessboard is n x n and n=2k
•Therefore, Total no. of squares =2k x2k =22k
•No. of non-defective squares = 22k-1
•Now, for the value of K,22k-1 is divisible by 3.
•For E.g. K=1 , 22(1)-1 =3 is divisible by 3.
•K=2, 22(2) -1 =15 is divisible by 3.
Defective chessboards
1x1 2 x2 2 x2 2 x2 2 x2
As the Size of these chessboards displayed here, 1x1 and 2x2 we
don’t have to place any L-shape tile in the first and rest can be
filled by just using 1 L-shaped tile.
For 4x 4 chessboard
8X8 DEFECTIVE CHESS BOARD
Step-1 One of the Step- 2 We divide the
cell is defective chess board into equal
sub half's.
Step- 3 Trick to cover the Step -4 Again creation of
chess board with tiles defective boxes as we
divide the chess board
Creation of defective box DIVISION OF PROBLEM INTO
SUB PROBLEM
Step-6 The procedure will
Step-5 As we have finally
continue until all the sub board
divided the problem into 2x2
are covered with the tiles.
board we will put the tiles.
Step-7 The final chess board Step-7 Here we will cover the
covered with all the titles defectives which we have
and only left with the created as in the last, there
defectives which we created. should be only one defective
left.
COMBINIG OF ALL SUB
PROBLEMS
2x2
2x2
4x4
2x2
2x2
4x4 4x4
4x4 4x4
8x8
1x1 2x2 4x4 8x8
ALGORITHIM
1.For a 2x2 board with defective cell we
just need to add the single tile to it.
2.We will place a L shape tile in the
middle such that it does not cover the
sub square in which there is already
defective. All the cell have a defective
cell.
3.Repeat this process recursively till we
have a 2x2 board.
Time Complexity For Defective
Chess Board Problem
•Let n = 2k.
•Let t(k) be the time taken to tile a 2 k x 2k defective
chessboard.
•Recurrence equation for t()
t(k)= d k=0
4t(k-1) + c, when k > 0
Here c is a constant.
Substitution Method
• t(k)= 4t(k-1) + c
= 4[4t(k-2)+c] + c
= 42 t(k-2) + 4c + c
= 42[4t(k-3)+c] + 4c + c
= 43 t(k-3) + 42c + 4c + c
= …
= 4k t(0) + 4k-1c + 4k-2c + … + 42c + 4c + c
= 4k d + 4k-1c + 4k-2c + … + 42c + 4c + c
= Theta(4k)
= Theta(number of triominoes placed)
Binary Search
Binary Search is a search algorithm that finds
the position of a target value within a sorted
array.
Binary Search is also known as half- interval
search or logarithmic search.
Binary Search algorithm works on the principle
of Divide And Conquer.
Binary Search can be implemented on only
sorted list of elements.
Binary Search
Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
• Invariant. Algorithm maintains a[low] value a[high].
Binary search for 33
• Ex.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
low high
Binary Search
• Mid=(low+high)/2
• =0+14/2=7
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
low mid high
Binary Search
• Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
• Invariant. Algorithm maintains a[lo] value a[hi].
• Ex. Binary search for 33.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo hi
Binary Search
• Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
• Invariant. Algorithm maintains a[lo] value a[hi].
• Ex. Binary search for 33.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo mid hi
Binary Search
• Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
• Invariant. Algorithm maintains a[lo] value a[hi].
• Ex. Binary search for 33.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo hi
Binary Search
• Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
• Invariant. Algorithm maintains a[lo] value a[hi].
• Ex. Binary search for 33.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo mid hi
Binary Search
• Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
• Invariant. Algorithm maintains a[lo] value a[hi].
• Ex. Binary search for 33.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo
hi
Binary Search
• Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
• Invariant. Algorithm maintains a[lo] value a[hi].
• Ex. Binary search for 33.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo
hi
mid
Binary Search
• Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
• Invariant. Algorithm maintains a[lo] value a[hi].
• Ex. Binary search for 33.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo
hi
mid
Recursive Binary Search algorithm
Space complexity: For variables i,l,x use 3spaces and a requies n
spaces so for n we use i more space
Totally n+4 locations ,hence, Space complexity is O(n)
Analysis Of Binary Search
T(n)=T(n/2)+1
=T(n/4)+1+1
=T(n/8)+1+1+1
=.......(k times)
=T(n/2k)+k
Assume n=2k
==> k=log n
2
= T(2k/2k)+k
=T(1)+log2n
Therefore,Time complexity of Binary search is O(logn)
Modified Binary search
BinSearch1 is better when compared to BinSearch and the TimeComplexities
of best,average and worst case times for modified search is same for both
successful search and unsuccessful search is O(logn)
Quicksort
Quicksort is a divide-and-conquer algorithm.
It works by selecting a 'pivot' element from the
array and partitioning the other elements into
two sub-arrays, according to whether they are
less than or greater than the pivot.
The sub-arrays are then sorted recursively.
The problem of sorting a given list is reduced to
the problem of sorting two sub lists and process
continuous until the list is sorted.
Divide and Conquer Procedure for sorting the sub array A[p...r]
Divide: A[p...r] is divided into two halves
Partition the array into two sub-arrays
A[p . . q-1] and A[q+1 . . r] such that each element of A[p . .
q-1] is less than or equal to A[q], which in turn less than or
equal to each element of A[q+1 . . r]
Hence A[q] is the pivot.
A[p].......A[q-1] A[q]A[q+1]......A[r]
<A[q] Pivot element >A[q]
Conquer:Sort the two sub-arrays A[p . . q-1] and A[q+1 . .
r] by recursive calls to quick sort.
Combine:No need of this step,as all this leave sorted array
in-place.
Example:
Consider the list and sort the elements using
Quicksort
26,5,37,1,61,11,59,15,48,19
When quicksort always has the most unbalanced partitions possible, then the
original call takes cn, time for some constant c, the recursive call on n-1elements
takes c(n-1),the recursive call on n-2 elements takes c(n-2) and so on. Here's a
tree of the sub problem sizes with their partitioning times:
MERGE SORT
Given a sequence of elements A[1],….,A[n], the general idea is to
imagine them split into two sets A[1],….,A[n/2] and A[n/2 + 1],
…,A[n]. Each set is individually sorted, and the resulting sorted
sequences are merged to produce a single sorted sequence of ‘n’
elements.
3.1). Algorithm Mergesort(low, high)
// A[low..high] is a global array to be sorted. If there is only one element, itself can be
// treated as sorted list.
{
If (low < high) then // If there are more than one element.
{ // divide list into sub lists
mid = (low + high)/2 ; // Find where to
// Solve the sub problems split
Mergesort(low, mid) ;
Mergesort(mid+1, high) ;
// Combine the solutions
Merge(low, mid, high) ;
}
}
Finding Minimum and Maximum:
The problem is to find the ‘maximum’ and ‘minimum’ items in a set of ‘n’ elements.
. The Max-Min Problem in algorithm analysis: finding the maximum and minimum
value in an array. Solution : 2 methods :
1. naive method 2. divide and conquer approach
Algorithm MaxMin(A, n, max, min)// DIRECT APPROACH
// Set max to the maximum and min to the minimum of A[1..n]
{
max = min = A[1]; for( i = 2 to n ) do
{
if (A[i]>max) then max = A[i]; if (A[i]<min) then min = A[i];
}
} The above algorithm requires 2(n-1) element
comparisons in the best, average and worst cases.
o
Divide and Conquer Approach
In this approach, the array is divided into
two halves.
Then using recursive approach maximum
and minimum numbers in each halves are
found.
Later, return the maximum of two maxima
of each half and the minimum of two minima
of each half.
Algorithm MaxMin(i, j, max, min) // DIVIDE & CONQUER
// A[1..n] is a global array. Parameters i and j are integers.
// 1 ≤ i ≤ j ≤ n. This algorithm is to set max and min to the largest and smallest
// values in A[i..j] respectively.
{
if ( i = j ) then max=min=A[i]; // There is only one element in the
Else if ( i = j-1 ) then list.
{ // List contains TWO elements
if ( A[i] < A[j] ) then
max = A[ j ] and min = A[ i ] ;
else max = A[ i ] and min =
} A[ j ] ;
else // If list is not small, divide the list into sub
lists.
{
mid = (i + j ) /2 ; // Find where to split the list
// Solve the sub problems MaxMin(i, mid, max, min) ;
MaxMin(mid+1, j, max1, min1) ;
// Combining the solutions
if (max < max1) then max = max1;
if (min > min1) then min = min1;
}
}
The procedureis initially invokedby the statement
MaxMin(l,n,x,y)
Supposewe simulateMaxMin on the following nineelements:
a: [1] [2] [3] [4] [5] [6] [7] [8] [9]
22 13 -5 -8 15 60 17 31 47
Unit-2 Previous year questions
1.a) Discuss the working strategy of merge sort and illustrate the process of merge
sort algorithm for the given data: 43, 32, 22, 78, 63, 57, 91 and 13. [7M]
b) Describe binary search in detail and provide time complexity analysis with an
example. [7M]
2. a) Illustrate the tracing of quick sort algorithm for the following set of numbers:
25, 10, 72, 18, 40, 11, 64, 58, 32, 9. [7M]
b) With a suitable algorithm, explain the problem of finding the maximum and
minimum items in a set of n elements. [7M]
3. a) Apply Merge Sort to sort the list a[1:10]=(31,28,17,65,35,42.,86,25,45,52).
Draw the tree of recursive calls of merge sort, merge functions.[7M]
b) What are different approaches of writing randomized algorithm? Write
randomized sort algorithms.[7M]
4.a) Write a recursive algorithm for binary search and also bring out its efficiency.
[7M]
b) Explain divide-and-conquer technique; write a recursive algorithm for finding
the maximum and minimum element from the list. [7M]
5. a) Explain the Recursive Binary search algorithm with suitable examples. [7M]
b) Derive the time complexity of the Quicksort algorithm for the worst case.
[7M]
6. a) Write Divide – And – Conquer recursive Merge sort algorithm and derive the
time complexity of this algorithm.[7M]
b) Write the general method of Divide – And – Conquer approach.[7M]
1.Describe the time complexity of Divide and Conquer in the recurrence form.
2. Give the general idea of Divide & Conquer algorithms.
3. Write the control abstraction for divide-and-conquer algorithms. [2M]
4. Define the Divide and Conquer Strategy. [2M]
5.Describe the Algorithm Analysis of Binary Search.
The complexity of many divide-and-conquer algorithms is given by
recurrences of the form
T(n) = T(1) n=1
aT(n/b)+f(n) n>1
Time Complexity of Defective Chessboard
t(k)= d k=0
4t(k-1) + c, when k > 0