Algorithms and Problem Solving (15B11CI411)
EVEN 2022
Module 3: Lecture 1 and 2
Design Technique: Divide and Conquer
Jaypee Institute of Information Technology (JIIT)
A-10, Sector 62, Noida
Divide-and-Conquer : Algorithm Design Strategy
The most-well known algorithm design strategy:
1. Divide instance of problem into two or more smaller instances
1. Solve smaller instances recursively
1. Obtain solution to original (larger) instance by combining these
solutions
Divide-and-Conquer Technique (cont.)
a problem of size
n(instance)
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
a solution to It general leads to a recursive algorithm!
the original problem
Approach
DAC(P)
{
if (small (P))
// P is small and solution is obvious
return solution (P);
k= divide(P); // divide the problem into k sub problems
return combine ( DAC(P1), DAC(P2), DAC(P3).. DAC(PK))
}
Divide-and-Conquer Examples
• Sorting: mergesort and quicksort
• Binary search
• Binary tree traversals
• Matrix multiplication: Strassen’s algorithm
• And many more
General Divide-and-Conquer Recurrence
Solved using master’s theorem
Divide and Conquer Algorithms which you
already know:
1. Merge Sort
2. Quick Sort
3. Binary Search
• Homework: Revise these algorithms and try to understand why
these are divide and conquer algorithms.
4. Binary Tree Algorithms
Binary tree is a divide-and-conquer ready structure
Binary Tree Algorithms (cont.)
5. Matrix multiplication
Multiply two matrices of size nxn.
Conventional procedure is:
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
C[i][j] = 0;
for (int k = 0; k < N; k++)
{
To calculate one element
C[i][j] += A[i][k]*B[k][j]; N multiplications are required
}
}
}
D & C approach
T(N) = 8T(N/2) + O(N2)
From Master's Theorem, time complexity of above method is O(N3) which
is unfortunately same as the naive method in previous slide.
Strassen’s matrix multiplication
Complexity of strassen’s algorithm
• T(N) = 7T(N/2) + O(N2)
• From Master's Theorem, time complexity of above method is
O(NLog7) which is approximately
• O(N2.8074)
6. Stock pricing problem
The cost of a stock on each day is given in an array, find the max
profit that you can make by buying and selling in those days. For
example, if the given array is {100, 180, 260, 310, 40, 535, 695}, the
maximum profit can earned by buying on day 4 and sell on day 6. If
the given array of prices is sorted in decreasing order, then profit
cannot be earned at all.
Brute force : O(n2)
Brute force :
Profit=0;
Buy=0;
Sell=0;
for(i=0;i<n;i++)
for (j=i;j<n;j++)
if(A[j]-A[i]>profit)
{ profit= A[j]-A[i];
buy=i;
sell=j;
}
Divide and Conquer:
• Divide the input list into two parts and recursively find the solution in
both parts:
• Three cases:
1) Buy and sell index both are in left half (can be done using recursion)
2) Buy and sell index both are in right half (can be done using recursion)
3) Buy is in earlier part and sell is in later part (needs care)
-> find min and max in two subparts and can solve in linear time
Pseudo Code
Structure i.e encapsulated info
void stock(int A[], int left, int right)
{ if (left==right) Structure i.e. encapsulated info
return(0,left, left);
mid=(left+right)/2;
{profitleft, buyleft, sellleft} = stock(A, left, mid);
{profitright, buyright, sellright} = stock(A, mid, right);
minleft = min(A, left, mid);
maxright=max(A, mid, right); Structure i.e. encapsulated info
profit = maxright- minleft;
return max(profitleft , profitright, profit along with indices);
}
}
7. Closest-Pair of points Problem
• We are given an array of n points in the plane, and the problem is to
find out the closest pair of points in the array. This problem arises in
a number of applications. For example, in air-traffic control, you
may want to monitor planes that come too close together, since
this may indicate a possible collision.
Closest-Pair Problem by Divide-and-Conquer
Step 0 Sort the points by x (list one) and then by y (list two).
Step 1 Divide the points given into two subsets S1 and S2 by a vertical line x
= c so that half the points lie to the left or on the line and half the
points lie to the right or on the line.
Closest Pair by Divide-and-Conquer (cont.)
Step 2 Find recursively the closest pairs for the left and right subsets.
Step 3 Set d = min{d1, d2}
We can limit our attention to the points in the symmetric vertical strip of width 2d as possible
closest pair. Let C1 and C2 be the subsets of points in the left subset S1 and of the right subset
S2, respectively, that lie in this vertical strip. The points in C1 and C2 are stored in increasing
order of their y coordinates, taken from the second list.
Step 4 For every point P(x,y) in C1, we inspect points inC2 that may be closer to P than d. There can
be no more than 6 such points (because d ≤ d2)!
Closest Pair by Divide-and-Conquer: Worst Case
The worst case scenario is depicted below:
Efficiency of the Closest-Pair Algorithm
Running time of the algorithm (without sorting) is:
T(n) = 2T(n/2) + M(n), where M(n) ∈ Θ(n)
By the Master Theorem (with a = 2, b = 2, d = 1)
T(n) ∈ Θ(n log n)
So the total time is Θ(n log n).
Lecture slides adapted from:
∙ Chapter 4 of A. Levitin “Introduction to design and analysis of algorithms “, Second edition.
∙ https://www.geeksforgeeks.org/closest-pair-of-points-onlogn-implementation/
∙ https://www.geeksforgeeks.org/strassens-matrix-multiplication/