0% found this document useful (0 votes)
15 views

Apex Institute of Technology Department of Computer Science & Engineering

Uploaded by

Kartik Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Apex Institute of Technology Department of Computer Science & Engineering

Uploaded by

Kartik Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Apex Institute of Technology

Department of Computer Science & Engineering


Bachelor of Engineering (Computer Science & Engineering)
Design and Analysis of Algorithms– (21CSH-282)
Prepared By: Mr. Vikas Kumar (E13657)

07/21/2024 DISCOVER . LEARN . EMPOWER


1
Content

 Divide and Conquer: Understanding of the


divide and conquer approach
 Example

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.

•DAndC(Algorithm) is initially invoked as DandC(P), where ‘p’ is the problem to be solved.


•Small(P) is a Boolean valued function that determines whether the i/p size is small enough that the
answer can be computed without splitting. If this is so, the function ‘S’ is invoked. Otherwise, problem P
is divided into smaller sub-problems. These sub-problems P1, P2 …Pk are solved by recursive application
of DAndC.

•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

•The complexity of many divide-and-conquer algorithms is given by


the recurrence relation of the form
T(n) = T(1) n=1
T(n) = 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=bk)

7
An Example: Merge Sort

Sorting Problem: Sort a sequence of n elements into non-decreasing


order.

Divide: Divide the n-element sequence to be sorted into two sub-


sequences of n/2 elements each

Conquer: Sort the two sub-sequences recursively using merge sort.

Combine: Merge the two sorted sub-sequences to produce the


sorted answer.

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

MergeSort (A, p, r) // sort A[p..r] by divide & conquer


1 if p < r
2 then q  (p+r)/2
3 MergeSort (A, p, q)
4 MergeSort (A, q+1, r)
5 Merge (A, p, q, r) // merges A[p..q] with A[q+1..r]

Initial Call: MergeSort(A, 1, n)

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 i1
10 j1 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 i1
•i = j = 1.
10 j1 •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 i1
Termination:
10 j1 •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.

Task 2. Suppose you are choosing between the following 3 algorithms:


• Algorithm A solves the problem of size n by dividing it into 5 subproblems of size n/2, recursively
solving each subproblem, and then combining the solutions in linear time.
• Algorithm B solves the problem of size n by recursively solving two subproblems of size n − 1 and then
combining the solutions in constant time
• Algorithm C solves the problem of size n by dividing it into nine subproblems of size n/3, recursively
solving each subproblem, and then combining the solutions in O(n2) time.
What are the running times of each algorithm and which would you choose and why?

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

You might also like