0% found this document useful (0 votes)
7 views22 pages

Divide and Conquer

Uploaded by

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

Divide and Conquer

Uploaded by

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

Divide and Conquer

Tanaji B Patil

Design & Analysis of Algorithms [23DSEU4P02]

Tanaji B Patil Divide and Conquer 23DSEU4P02 1 / 22


Introduction

Divide-and-Conquer is a problem-solving
strategy that breaks a problem into
subproblems.
If subproblems are still large, then
divide-and-conquer is reapplied.
Each subproblem is solved recursively
and then combined to obtain the final
solution.

Tanaji B Patil Divide and Conquer 23DSEU4P02 2 / 22


General Method

1 Problem:Given function to compute on n input i.e. f (n)


2 Divide: Split the problem into k distinct subsets 1 < k ≤ n
3 Conquer: Solve each subproblem recursively.
4 Combine: Merge the results to get the final solution.

Tanaji B Patil Divide and Conquer 23DSEU4P02 3 / 22


Complexity Analysis

Let the problem size be n.


Assume it divides into ‘a’ subproblems, each of size ‘n/b’.
The recurrence relation is given by:
(
T (1), n=1
T (n) =
aT (n/b) + f (n), n>1

f (n) is the time required for dividing and combining the solutions

Tanaji B Patil Divide and Conquer 23DSEU4P02 4 / 22


Complexity Analysis Example

Consider the case where a = 2, b = 2 and f (n) = n

T (n) = 2T (n/2) + n
= 2[2T (n/4) + n/2] + n
= 4T (n/4) + 2n
= 4[2T (n/8) + n/4] + 2n
= 8T (n/8) + 3n
..
.
= 2i T (n/2i ) + i.n
= 2log n T (n/2log n ) + n log n
= nT (1) + n log n
= n log n + 2n

Tanaji B Patil Divide and Conquer 23DSEU4P02 5 / 22


Binary Search: Recursive

Tanaji B Patil Divide and Conquer 23DSEU4P02 6 / 22


Binary Search: Iterative

Tanaji B Patil Divide and Conquer 23DSEU4P02 7 / 22


Binary Search: Comparisons n = 14

Index: [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]

Elements: -15 -6 0 7 9 23 54 82 101 112 125 131 142 151

Comparisons: 3 4 2 4 3 4 1 4 3 4 2 4 3 4

Tanaji B Patil Divide and Conquer 23DSEU4P02 8 / 22


Binary Search: Decision Tree for n = 14

Tanaji B Patil Divide and Conquer 23DSEU4P02 9 / 22


Finding Maximum and Minimum

Possible Improvement:

Best Case: (n − 1) (Elements of a are in increasing order)


Worst Case: 2(n − 1) (Elements of a are in decreasing order)
Average Case: 32 (n − 1)
Tanaji B Patil Divide and Conquer 23DSEU4P02 10 / 22
Finding Maximum and Minimum: Recursive

Tanaji B Patil Divide and Conquer 23DSEU4P02 11 / 22


Finding Maximum and Minimum: Recursive ... Cont

Tanaji B Patil Divide and Conquer 23DSEU4P02 12 / 22


Complexity Analysis of MaxMin


T (n/2) + T (n/2) + 2,
 n>2
T (n) = 1, n=2

0, n=1

When n is a power of 2, i.e. n = 2k the recurrence relation is:


T (n) = 2T (n/2) + 2
= 2[2T (n/4) + 2] + 2
= 4T (n/4) + 4 + 2
= 4[2T (n/8) + 2] + 4 + 2
= 8T (n/8) + 8 + 4 + 2
...
k−1
X
= 2k−1 T (2) + 2i
1

= 2k−1 + 2k − 2
= 3n/2 − 2
Tanaji B Patil Divide and Conquer 23DSEU4P02 13 / 22
Finding Maximum and Minimum: Recursive

Index: [1] [2] [3] [4] [5] [6] [7] [8] [9]
Elements: 22 13 -5 -8 15 60 17 31 47

Tanaji B Patil Divide and Conquer 23DSEU4P02 14 / 22


Merge Sort

Tanaji B Patil Divide and Conquer 23DSEU4P02 15 / 22


Merge Sort

Tanaji B Patil Divide and Conquer 23DSEU4P02 16 / 22


Merge Sort

Tanaji B Patil Divide and Conquer 23DSEU4P02 17 / 22


Complexity Analysis of Merge Sort

(
a n = 1, a constant
T (n) =
2 T (n/2) + c n n > 1, c constant
When n is a power of 2, i.e. n = 2k the recurrence relation is:

T (n) = 2T (n/2) + cn
= 2[2T (n/4) + cn/2] + cn
= 4T (n/4) + 2cn
= 4[2T (n/8) + cn/4] + 2cn
= 8T (n/8) + 3cn
...
= 2k T (1) + kcn
= an + cn log n
...
= O(n log n)

Tanaji B Patil Divide and Conquer 23DSEU4P02 18 / 22


Quick Sort

Tanaji B Patil Divide and Conquer 23DSEU4P02 19 / 22


Quick Sort

Tanaji B Patil Divide and Conquer 23DSEU4P02 20 / 22


Conclusion

Divide-and-Conquer is a fundamental algorithm design paradigm.


Used in sorting, searching, matrix multiplication, and many more problems.
Master Theorem provides a systematic way to analyze its complexity.

Tanaji B Patil Divide and Conquer 23DSEU4P02 21 / 22


Thank you . . .

Tanaji B Patil Divide and Conquer 23DSEU4P02 22 / 22

You might also like