Divide and Conquer
Divide and Conquer
Tanaji B Patil
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.
f (n) is the time required for dividing and combining the solutions
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
Index: [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]
Comparisons: 3 4 2 4 3 4 1 4 3 4 2 4 3 4
Possible Improvement:
T (n/2) + T (n/2) + 2,
n>2
T (n) = 1, n=2
0, n=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
(
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)