You are on page 1of 10

CS 306: DAA IIITDM Jabalpur, 2012

Divide and Conquer: More Examples


Atul Gupta

Divide and Conquer


Divide the problem into a number of subproblems. Conquer the sub-problems by solving them recursively.
If the sub-problem sizes are small enough, however, just solve the sub-problems in a straightforward manner.

Combine the solutions to the sub-problems into the solution for the original problem.

CS 306: DAA IIITDM Jabalpur, 2012

Recursive Algorithms
Recurrence (or Recurrence Equation) Divide: Divide the n-element sequence to be (1) if n <= c sorted into two subsequences of n/2 T(n) = elements aT(n/b) each. + D(n) + C(n) otherwise Conquer: Sort the two subsequences Parts Solution forusing smallest subdivision (Terminating condition) recursively merge sort. Subdivision relation (aT(n/b)) Combine: Merge the two sorted subsequences Division efforts (D(n)) to the sorted produce Combining effort (C(n)) answer.

Matrix Multiplication
The product of two nn matrices X and Y is a third nn matrix Z = XY, where

CS 306: DAA IIITDM Jabalpur, 2012

Matrix Multiplication
O(n3) as there are n2 entries to be computed and each takes O(n) time For quite sometime, this was believed to be the smallest time In 1969, a German mathematician Volker Strassen lowered this bound to O(n2.81) A solution that is based on Divide and Conquer approach

The Idea
Matrix multiplication is particularly easy to break into subproblems, because it can be performed block-wise

CS 306: DAA IIITDM Jabalpur, 2012

Divide and Conquer

Not an improvement !!!

The Strassens Idea

where

CS 306: DAA IIITDM Jabalpur, 2012

Quick Sort(Hoare 1961)


Input: an array A[p, r] Output: the sorted array A[p,r] Quicksort (A, p, r) if (p < r) then q = Partition (A, p, r)
//q is the position of the pivot element

Quicksort (A, p, q-1) Quicksort (A, q+1, r)

p i j 2 i 2 i 2 i 2 8 i 2 1 7 8 7 8 8 j 8 7 j 7 1 j 1 3 j 3 5 6 5 6 3 5 6 1 3 5 6 7 1 3 5 6

10

CS 306: DAA IIITDM Jabalpur, 2012

i 2 1 7 i 2 1 3 i 2 1 3 i 2 1 3 i 2 1 3 4 8 8 8 8

j 3 5 j 7 5 6 j 7 5 6 4 j 7 5 6 4 j 7 5 6 8
11

2 p

4 r

Partition(A, p, r) x A[r] i p-1 for j p to r-1 do if A[j] x then i i+1 exchange A[i] A[j] exchange A[i+1] A[r] return i+1
12

CS 306: DAA IIITDM Jabalpur, 2012

Quicksort Algorithm

CS 306: DAA IIITDM Jabalpur, 2012

Quicksort: Complexity

Analysis of 1:9 split

16

CS 306: DAA IIITDM Jabalpur, 2012

Analysis of 1:9 split

17

Observations
We cant guarantee good splits But intuitively on random inputs we will get good splits

18

CS 306: DAA IIITDM Jabalpur, 2012

Summary
Divide and Conquer Examples
Binary Search Matrix Multiplication Median finding Sorting methods like Mergesort and Quicksort Closest Pair Convex Hull And others

10

You might also like