Unit 2 - DAA
Unit 2 - DAA
Divide-and-conquer
Syllabus
• Introduction, divide-and-conquer,
• Binary Search and its algorithm analysis
• Merge sort and its algorithm analysis
• Quick sort and its algorithm analysis
• Strassen's Matrix multiplication
• Finding Maximum and minimum
• Algorithm for finding closest pair
• Convex Hull Problem
Introduction
• Divide and Conquer is an algorithmic pattern. In
algorithmic methods, the design is to take a
dispute on a huge input, break the input into
minor pieces, decide the problem on each of the
small pieces, and then merge the piecewise
solutions into a global solution. This mechanism
of solving the problem is called the Divide &
Conquer Strategy.
• Divide and Conquer algorithm consists of a
dispute using the following three steps.
1. Divide the original problem into a set of
subproblems.
2. Conquer: Solve every subproblem individually,
recursively.
Introduction
• Divide / Break
• Conquer / Solve
• Merge / Combine
Examples : Divide and Conquer Technique
General Method:
The Divide and Conquer Technique splits n inputs
into k subsets , 1< k ≤ n, yielding k subproblems.
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 subproblems;
return Combine(DAndC(p1), DAndC(p2),……,DAndC(pk));
}
}
General format
If the size of p is n and the sizes of the k subproblems are
n1,n2,….,nk,then the computing time of DAndC 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 DAndC on any input of size n and g(n) is
the time to compute the answer directly for small inputs.
The function f(n) is the time for dividing p and combining the
solutions to subproblems.
The Complexity of many divide-and-conquer
algorithms is given by recurrences of the form
c n small
T(n)=
aT(n/b)+f(n) Otherwise
• Here it is, 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array.
How Binary Search Works?
• We change our low to mid + 1 and find the new mid value again.
low = mid + 1
mid = Big + end / 2
Divide into
A FirstPart SecondPart
two halves
Recursively
Recursively sort
sort
FirstPart SecondPart
Merge
A is sorted!
Merge Sort: Algorithm
MergeSort ( low,high)
// sorts the elements a[low],…,a[high] which reside in the global array
//a[1:n] into ascending order.
// Small(p) is true if there is only one element to sort. In this case the list is
// already sorted.
{
mid ← (low+high)/2; Recursive Calls
MergeSort(low,mid);
MergeSort(mid+1, high);
Merge(low, mid, high);
}
}
Merge(A,L,M,H)
Merge Algorithm
{
Split the array into two array a and b;
i=0;j=0;k=0;
for(k=1;k<size[A];k++)
{
If (a[i]<b[j])
{
list[k]=a[i];
i++;
}
else
{
list[k]=b[j];
j++;
}
} list[k] = remaining_elements;
}
Merge-Sort: Merge Example
hig
low mid h
A: 2
5 3
5 7 28
15 8 30
1 4
6 5 14
10 6
B: 5 5 15 28 30 6 10 14
L: R:
3 5 15 28 6 10 14 22
Merge-Sort: Merge Example
A: 5 5 15 28 30 6 10 14
B:
3
1 5 15 28 30 6 10 14
k=low
L: R:
3
2 15
3 28
7 30
8 6
1 10
4 14
5 22
6
i=low j=mid+1
Merge-Sort: Merge Example
A: 5 5 15 28 30 6 10 14
B:
1 2
5 15 28 30 6 10 14
L: R:
3
2 5
3 15
7 28
8 6
1 10
4 14
5 22
6
i j
Merge-Sort: Merge Example
A: 5 5 15 28 30 6 10 14
B:
1 2 3 28 30
15 6 10 14
L: R:
2 3 7 8 6
1 10
4 14
5 22
6
i j
Merge-Sort: Merge Example
A: 5 5 15 28 30 6 10 14
B:
1 2 3 4 6 10 14
L: R:
2 3 7 8 6
1 10
4 14
5 22
6
i j
Merge-Sort: Merge Example
A: 5 5 15 28 30 6 10 14
B:
1 2 3 4 5 6 10 14
L: R:
2 3 7 8 6
1 10
4 14
5 22
6
i j
Merge-Sort: Merge Example
A: 5 5 15 28 30 6 10 14
B:
1 2 3 4 5 6 10 14
L: R:
2 3 7 8 6
1 10
4 14
5 22
6
i j
Merge-Sort: Merge Example
A: 5 5 15 28 30 6 10 14
B:
1 2 3 4 5 6 7 14
L: R:
2 3 7 8 6
1 10
4 14
5 22
6
i j
Merge-Sort: Merge Example
A: 5 5 15 28 30 6 10 14
B:
1 2 3 4 5 6 7 8
14
L: R:
3
2 5
3 15
7 28
8 6
1 10
4 14
5 22
6
i j
Merge-Sort: Merge Example
A: 5 5 15 28 30 6 10 14
B:
1 2 3 4 5 6 7 8
L: R:
3
2 5
3 15
7 28
8 6
1 10
4 14
5 22
6
i j
A:
5 5 15 28 30 6 10 14
B: 1 2 3 4 5 6 7 8
Merge-Sort Analysis
n
n/2 n/2
2 2 2
Analysis of Merge Sort:
• Note stopping condition T(1)=0 because at last, there will be only 1 element
that need to be copied, and there will be no comparison.
• So, we get,
Merge-Sort Time Complexity
If the time for the merging operation is proportional to n, then the
computing time for merge sort is described by the recurrence relation
…..
…..
=2k T(1)+ kc2n
= c1n+c2nlogn = = O(nlogn)
Summary
• Merge-Sort
– Most of the work done in combining the
solutions.
– Best case takes o(n log(n)) time
– Average case takes o(n log(n)) time
– Worst case takes o(n log(n)) time
Quick Sort
3. Quick Sort
• Divide:
• Pick any one of the following as pivot,
– First element
– Last element
– Middle element
– Random element
• Partition the remaining elements into
FirstPart, which contains all elements < pivot
SecondPart, which contains all elements > pivot
4 2 7 8 1 9 3 6 5
pivo
t
x y
The whole process
4 2 7 8 1 9 3 6 5
2 1 3 4 7 8 9 6 5
1 2 3 6 5 7 8 9
1 3 5 6 8 9
5 9
Quick Sort Algorithm :
Algorithm QuickSort(low,high)
//Sorts the elements a[low],…..,a[high] which resides
//in the global array a[1:n] into ascending order;
// a[n+1] is considered to be defined and must ≥ all the
// elements in a[1:n].
{
if( low< high ) // if there are more than one element
{ // divide p into two subproblems.
j :=Partition(low,high);
// j is the position of the partitioning element.
QuickSort(low,j-1);
QuickSort(j+1,high);
// There is no need for combining solutions.
}
}
Algorithm Partition(l,h)
{
pivot:= a[l] ; i:=l; j:= h+1;
while( i < j ) do
{
i++;
while( a[ i ] < pivot ) do
i++;
j--;
while( a[ j ] > pivot ) do
j--;
n/2 n/2
2 2 2
• Prove O(nlogn)
QUICK SORT
•
Time complexity analysis
A worst/bad case
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
3 4 5 6 7 8 9
4 5 6 7 8 9
5 6 7 8 9 O(n2)
6 7 8 9
7 8 9
8 9
9
Worst/bad Case
n cn
n-1 c(n-1)
n-2 c(n-2)
3c
3
Happens only if
• input is sorted 2 2c
• input is reversely sorted 1c
1
Total time:
O(n2)
T (n) =T(n-1)+n
1 1
C11=A11B11+A12B21
1 2
C12=A11B12+A12B22 c c
C21=A21B11+A22B21 2 2
A A B B
1 2
C22=A21B12+A22B22 21 22 21 22
T(n)= c1 if n<=2
8T(n/2)+ c2n2 if n>2
T(n) = O(n3)
Strassen’s method
C11=M1 + M4 - M5 + M7
C12= M3 + M5
C21= M2 + M4
C22=M1 + M3 - M2 + M6
C11 C12 A11 A12 B11 B12
= *
C21 C22 A21 A22 B21 B22
M 1 + M 4 - M 5 + M7 M 3 + M5
=
M 2 + M4 M1 + M3 - M2 + M6
T(n)= c1 n<=2
7T(n/2) +c2n2 n>2
T(n)= 7kT(1) + c2n2 2 3
1+ 7/4 + (7/4) + (7/4) +……………..+ (7/4)
k-1
..
.
=
7log2n c1 +c2 n2 (7/4)log2n
=
nlog27 + nlog24 ( n log27-log24 )
• T(n) = 1 l=h
2T(n/2) + n l<h
Analysis
Finding closest pair
Introduction - finding closest pair
• Given an n points in the plane, and the problem is to
find out the closest pair of points in the array.
• For example, in air-traffic control, you may want to
monitor planes that come too close together, since this
may indicate a possible collision.
• Recall the following formula for distance between two
points p and q.
• Distance between two points:
• P = (Px , Py), q = (qx , qy)
Real-life Applications
» Track the closest pairs in air traffic control
to detect and prevent collision.
By introducing
Real-life Applications
Brute Force attack 🡪
By introducing
Algorithm for finding closest pair
• Given a set of points, find the closest pair
(measured in Euclidean distance).
• a set of n points are given on the 2D
plane. In this problem, we have to find the
pair of points, whose distance is minimum.
• The Brute force solution is O(n2), compute
the distance between each pair and return
the smallest.
• We can calculate the smallest distance in
? time using Divide and Conquer strategy.
• Minimum Criteria
Algorithm
Input: An array of n points P[]
Output: The smallest distance between two points in the given array.
As a pre-processing step, input array is sorted with x coordinates.
1) Find the middle point in the sorted array, we can take P[n/2] as middle
point.
2) Divide the given array in two halves. The first subarray contains points
from P[0] to P[n/2]. The second subarray contains points from P[n/2+1] to
P[n-1].
3) Recursively find the smallest distances in both subarrays. Let the
distances be dl and dr. Find the minimum d = min(dl, dr). Let the
minimum be d.
4) From above 3 steps, we have an upper bound d of minimum distance.
Now we need to consider the pairs such that one point in pair is from
left half and other is from right half or may be on the mid point itself.
Consider the vertical line passing through P[n/2] and find all points whose
x coordinate is closer than d to the middle vertical line. Build an array
strip[] of all such points.
5) Sort the array strip[] according to y coordinates. It can be optimized to O(n)
by recursively sorting and merging.
Complexity
best= average =worst= 2(n-1) comparisons
56 34 12 1 76 34 23 8 16
56 34 12 1 76 34 23 8 16
56 34 12 1 76 34 23 8 16
Min 8
Max 76
Min 1
Max 76
Algorithm
• Algorithm: max_min(x, y)
if y – x ≤ 1 then
return (max(numbers[x], numbers[y]), min((numbers[x], numbers[y]))
else
Mid=(l+h)/2
(max1, min1):= max_min(x, mid)
(max2, min2):= max_min(mid+1,y)
return (max(max1, max2), min(min1, min2))
Complexity Analysis
1.if size= 1 return current element as both max and min //base condition
2.else if size= 2 one comparison to determine max and min //base condition
recur for max and min of left half recur for max and min of right half.