Daa Unit2
Daa Unit2
UNIT-II
Divide-and-Conquer Method
By
Dr.M.Rajababu
Associate Professor&HOD
Dept of Information Technology
Aditya Engineering College(A)
Surampalem.
Aditya Engineering College (A)
Divide-and-Conquer
• The most-well known algorithm design strategy useful for solving many
problems.
• There are three stages in Divide and conquer strategy:
1. DIVIDE: Divide the problem into two or more sub problems.
2. CONQUER: Solve the sub problems recursively or non-recursively.
3. COMBINE: Combine the results of the sub problems to derive a
solution of the given problem.
Divide-and-Conquer
Divide-and-Conquer
• finding the maximum of a set S of n numbers
4 -5
Subdivide the sorting task
H E M G B K A Q F L P D R C J N
H E M G B K A Q F L P D R C J N
H E M G B K A Q F L P D R C J N
H E M G B K A Q F L P D R C J N
H E M G B K A Q F L P D R C J N
H E M G B K A Q F L P D R C J N
H E M G B K A Q F L P D R C J N
Insight Through Computing
Now merge
E H G M B K A Q F L D P C R J N
H E M G B K A Q F L P D R C J N
Insight Through Computing
And again
A B E G H K M Q C D F J L N P R
E G H M A B K Q D F L P C J N R
A B E G H K M Q C D F J L N P R
Where T(ni) is the time complexity of the problem Pi and n1+n2 + ….+ nk=n,
f(n) be the time required for combining the solutions .
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Aditya Engineering College (A)
Divide-and-Conquer applications
• Binary search
• Finding the maximum and minimum
• Sorting:Merge sort and Quick sort
• Defective chessboard
• Multiplication of long integers
• Binary tree traversals
• Matrix multiplication: Strassen’s algorithm
Binary Search
Input: A sorted sequence of n elements stored in an array.
Output: The position of x (to be searched).
Step 1: If only one element remains in the array, solve it directly.
Step 2: Compare x with the middle element of the array.
Step 2.1: If x = middle element, then output it and stop.
Step 2.2: If x < middle element, then recursively solve the problem
with x and the left half array.
Step 2.3: If x > middle element, then recursively solve the problem
with x and the right half array.
Algorithm BinSrch(A, i, j, x)
{ // A Array, i least index, j highest index, x element for search.
if (i = j) then
{ if (x == A[i]) then return i;
else return 0;
}
else
{
mid (i+j)/2;
if (x ==A[mid]) then return mid;
else if (x < Amid]) then
return BinSrch(A, i, mid –1, x)
else
return BinSrch (A, mid + 1, j, x)
}
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
}
Aditya Engineering College (A)
Algorithm binarySearch( A, low,high ,int x)
{
while (low <= high)
{
int mid = (low + high) / 2;
if (x == A[mid])
{ return mid;
}
else if (x < A[mid])
{
high = mid - 1;
} else
{ low = mid + 1; }
}
}
return -1;
}
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Aditya Engineering College (A)
MaxMin requires 2(n-1) element comparisons in the best, average & worst cases.
Time complexity
T(n)= 2T(n/2)+2 n>2
= 1 n=2
T(n) = 2T(n/2)+2
= 2(2T(n/4)+2)+2
= 4T(n/4)+4+2 T(n)=O(n)
:
=2k -1T(n/2k-1)+2k-1+…+4+2
=2k-1+2k-1+…+4+2+1
=2k-1+2k -2=n/2+n-2=3n/2-2
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Aditya Engineering College (A)
Merge sort
• Merge sort is a sorting algorithm that uses the divide and conquer
strategy
• Divide the unsorted array into two sub lists at the middle.
• Further divide these sub lists into sub lists at their middles until each
sub list contain only one element.
• Merge these sub lists two at a time till we get the final sorted list using
the following steps:
1. Compare the sub-list’s first elements.
2. Remove the smallest element and put it into the result array.
3. Continue the process until all elements have been put into the
result array.
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Aditya Engineering College (A)
Merge sort
• Merge sort consists of three steps:
• 1.Partition array into two sub lists s1 and s2 with n/2 elements each.
• 2.Recursively sort s1 and s2.
• 3.Merge s1 and s2 into a single sorted list.
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23
23
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23
23 98
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
14
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
14 23
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
14 23 45
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
14 23 45 98
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
23 98 14 45
14 23 45 98
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67
23 98 14 45
14 23 45 98
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67
23 98 14 45
14 23 45 98 Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67
23 98 14 45 6
14 23 45 98 Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67
23 98 14 45 6 67
14 23 45 98 Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67
14 23 45 98
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67
14 23 45 98 Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33
14 23 45 98 Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23 33
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23 33 42
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23 33 42 45
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
98 23 45 14 6 67 33 42
6 14 23 33 42 45 67 98
Thursday, December 5, 2024 M.Raja Babu
Aditya Engineering College (A)
Quick sort
• Quick sort is another sorting algorithm which uses Divide and Conquer
for its implementation.
• Unlike the Merge Sort, Quick sort doesn't use any extra array in its
sorting process.
• Quick sort first chooses a pivot and then partition the array around this
pivot.
• In the partitioning process, all the elements smaller than the pivot are
put on one side of the pivot and all the elements larger than it on the
other side.
• This partitioning process is repeated on the smaller sub arrays and hence
finally results in a sorted array.
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Aditya Engineering College (A)
Quick sort
Quick sort
• We can pick Pivot element in different ways:
1. pick first element as pivot.
2. pick last element as pivot
3. Pick a random element as pivot.
4. Pick median as pivot.
Quick sort
• Quick sort has three steps:
1. Pick an arbitrary element of the array (the pivot).
2. Divide the array into two segments, those that are smaller and those
that are greater, with the pivot in between (partition).
3. Recursively sort the segments to the left and right of the pivot.
{
v=a[low];i=low; j=high+1;a[j]=∞;
do
{ do{
i++;
}while(a[i]<v);
do{
j--;
}while(v<a[j]);
if(i<j) {swap(a[i[,a[j]);}
}while(i<j);
a[low]=a[j];a[j]=v;
return(j);}
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Aditya Engineering College (A)
10 16 8 12 15 6 3 9 5
45 26 77 14 68 61 97 39 99 90
so T(n)=O(n2)
1.if n=1 then this problem is easily solved and the no of triominoes
used in tiling is zero.
2.if n=2 then there are exactly three non defective squares and these
squares are covered by using one triomino in one of the four
orientations.
3.Divide the board into 4 smaller chess boards of size n/2Xn/2.One of
these is a defective chessboard. Make the other three chess boards
defective by placing a triomino at their common corner.
4.Recursively tile these four defective chess boards. The recursion
terminates when the chess board size has been reduced to 2x2.
5.The solutions of the sub-problems of the problem can be combined as
the solution of the problem.
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Divide and conquer solution Aditya Engineering College (A)
1.if n=1 then this problem is easily solved and the no of triominoes
used in tiling is zero.
2.if n=2 then there are exactly three non defective squares and these
squares are covered by using one triomino in one of the four
orientations.
3.Divide the board into 4 smaller chess boards of size n/2Xn/2.One of
these is a defective chessboard. Make the other three chess boards
defective by placing a triomino at their common corner.
4.Recursively tile these four defective chess boards. The recursion
terminates when the chess board size has been reduced to 2x2.
5.The solutions of the sub-problems of the problem can be combined as
the solution of the problem.
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Time complexity Aditya Engineering College (A)