Apex Institute of Technology Department of Computer Science & Engineering
Apex Institute of Technology Department of Computer Science & Engineering
2
Divide and Conquer
Recursive in structure
Divide the problem into sub-problems that are similar to the
original but smaller in size
Conquer the sub-problems by solving them recursively. If they are
small enough, just solve them in a straightforward manner.
Combine the solutions to create a solution to the original problem
3
Divide and Conquer
General method:
Given a function to compute on ‘n’ inputs the divide-and-conquer
strategy suggests splitting the inputs into ‘k’ distinct subsets,
1<k<=n, yielding ‘k’ subproblems.
These sub-problems must be solved, and then a method must be
found to combine sub-solutions into a solution of the whole.
4
Divide and Conquer
•If the sub-problems are still relatively large, then the divide-and-conquer strategy can possibly be
reapplied.
•Often the sub-problems resulting from a divide-and-conquer design are of the same type as the original
problem. For those cases, the re-application of the divide and- conquer principle is naturally expressed by
a recursive algorithm.
•Combine is a function that determines the solution to p using the solutions to the ‘k’ subproblems.If the
size of ‘p’ is n and the sizes of the ‘k’ subproblems are n1, n2 ….nk, respectively, then the computing time
of DAndC is described by the recurrence relation.
5
Divide and Conquer
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 sub-problems problems;
return
combine (DAndC(P1),DAndC(P2),…….,DAndC(Pk));
}
}
6
Divide and Conquer
7
An Example: Merge Sort
8
Merge Sort Example
9
Merge-Sort (A, p, r)
INPUT: a sequence of n numbers stored in array A
OUTPUT: an ordered sequence of n numbers
10
Procedure Merge
Merge(A, p, q, r)
1 n1 q – p + 1
2 n2 r – q Input: Array containing sorted subarrays
3 for i 1 to n1 A[p..q] and A[q+1..r].
4 do L[i] A[p + i – 1]
5for j 1 to n2 Output: Merged sorted subarray in
6do R[j] A[q + j] A[p..r].
7 L[n1+1]
8 R[n2+1]
9 i1
10 j1 Sentinels, to avoid having to
11for k p to r check if either subarray is
12do if L[i] R[j] fully copied at each step.
13then A[k]
14 i i + 1
L[i]
15 else A[k]
R[j] 11
Merge Sort
Merge(A, p, q, r)
1 n1 q – p + 1 Loop Invariant for the for loop At the start
of each iteration of the for loop:
2 n2 r – q
Subarray A[p..k – 1] contains the k – p smallest elements of L and
3 for i 1 to n1 R in sorted order. L[i] and R[j] are the smallest elements of L and R
4 do L[i] A[p + i – 1] that have not been copied back into A.
5for j 1 to n2
6do R[j] A[q + j]
7 L[n1+1] Initialization:
8 R[n2+1] Before the first iteration:
•A[p..k – 1] is empty.
9 i1
•i = j = 1.
10 j1 •L[1] and R[1] are the smallest
11for k p to r elements of L and R not copied to A.
12do if L[i] R[j]
13then A[k]
14 i i + 1
L[i]
15 else A[k]
R[j] 12
Merge Sort
Merge(A, p, q, r)
1 n1 q – p + 1 Maintenance:
2 n2 r – q Case 1: L[i] R[j]
3 for i 1 to n1 •By LI, A contains p – k smallest elements of L and R in sorted order.
•By LI, L[i] and R[j] are the smallest elements of L and R not yet copied
4 do L[i] A[p + i – 1]
into A.
5for j 1 to n2 •Line 13 results in A containing p – k + 1 smallest elements (again in
6do R[j] A[q + j] sorted order). Incrementing i and k reestablishes the LI for the next
7 L[n1+1] iteration.
8 R[n2+1] Similarly for L[i] > R[j].
9 i1
Termination:
10 j1 •On termination, k = r + 1.
11for k p to r •By LI, A contains r – p + 1 smallest elements of L and R in sorted
12do if L[i] R[j] order.
13then A[k] •L and R together contain r – p + 3 elements. All but the two sentinels
14 i i + 1
L[i] have been copied back into A.
15 else A[k]
R[j] 13
TASKS END OF LECTURE LEARNING (TELL):
Task 1:
Given a sorted array of distinct integers A[1...n], you want to find out whether there is an index i for which
A[i] = i. Give a divide and conquer algorithm that runs in O(log n) time to find out an index i if it exists.
14
TASKS END OF LECTURE LEARNING (TELL):
Task 3:
Trade Unions are common these days in industries. But the new manager of ByteComp doesn’t like those
unions. He wants the division between them. Before he goes on the process of division, he wants to find
out the number of ways in which he can divide the existing trade union into two parts by kicking out one
of the workers. Given N , the number of workers in the company, and ‘R’, the number of connections in
the union and R pairs of connections. Find the ways in which the original configuration of the union can
be divided.
15
References
• Fundamentals of Computer Algorithms 2nd Edition (2008) by Horowitz, Sahni
and Rajasekaran
• Introduction to Algorithms 3rd Edition (2012) by Thomas H Cormen, Charles E
Lieserson, Ronald
16
THANK YOU
For queries
Email: [email protected]
07/21/2024 17