Divide and Conquer
Divide and Conquer
Divide and Conquer
Algorithm DAndC(P)
{
if Small(P) then
If the size of ‘P’ is ‘n’, and the sizes of ‘K’ sub problems are n1,n2,…,nk
respectively, then computing time of DAndC is described in recurrence
relation.
One of the method for solving any such recurrence relation is called the
substitution method. This method repeatedly makes substitution for each
occurrence of the function ‘T’ in the RHS until all such occurrences
disappear.
DIVIDE AND CONQUER TECHNIQUE
Solution to Solution to
Sub problem 1 Sub problem 2
Solution to
Original problem
n
T ( n) aT f ( n)
b
= 2 T(n/2) + n
= 2[2T(n/4) + n/2] + n = 4T(n/4)+ 2n
= 4[2T(n/8) + n/4]+ 2 n = 8T(n/8)+3n …
T(n) = 2i T(n/2i ) + i n
= n T(n/n) + n log2 n
= n T(1) + n log2 n
= n (2) + n log2 n
T(n) = n log2 n + 2n
Beginning with the recurrence relation, and using the substitution
method, it can be shown that
T(n) = nlog ab [ T(1) + U(n)]
7. a=28,b=3,f(n)=c n3
BINARY SEARCH
It is a searching technique that can be applied only to a sorted
list of items.
Items are sorted in increasing order of values for binary search.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo hi
BINARY SEARCH
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo mid hi
BINARY SEARCH
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo hi
BINARY SEARCH
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo mid hi
BINARY SEARCH
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo hi
BINARY SEARCH
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo mid hi
BINARY SEARCH
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo
hi
BINARY SEARCH
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo
hi
mid
BINARY SEARCH
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo
hi
mid
ANALYSIS OF BINARY SEARCH
Standard way to analyze efficiency of binary search is to count
the number of times search key is compared with an element
of the array.
i.e. one comparision for k with A[m] which determines
Therefore x = log2 n
T(2k) = T (2k -1) + 1
= [T (2k-2) + 1] + 1
= T(2k-2) + 2
= T (2k-3) + 3 ……. n= 2k
of ‘n’ elements.
Therefore key comparision is 1.
Therefore T(n) = 1
DIVIDE AND CONQUER : MERGE
SORT
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 71 5 4
2 3 8 9 1 4 5 7
1 2 3 4 5 7 8 9
The binary decision tree that represents the Tree calls of Merge
Sort(1,10) is given by
310, 285, 179, 652, 351, 423, 861, 254, 450, 520
5 1 3 9 7 0 4 2 6 8
i j
i j
5 1 3 9 7 0 4 2 6 8
i j
5 1 3 2 7 0 4 9 6 8
5 1 3 2 7 0 4 9 6 8
i j
5 1 3 2 7 0 4 9 6 8
5 1 3 2 4 0 7 9 6 8
i j
5 1 3 2 4 0 7 9 6 8
j i
5 1 3 2 4 0 7 9 6 8
j i
0 1 4 2 4 5 6 9 7 8
7
50
60
80
10
EXAMPLE
20
40
PICK PIVOT ELEMENT
There are a number of ways to pick the pivot element. In this
example, we will use the first element in the array:
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
pivot_index = 0 40 20 10 30 60 50 7 80 100
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
pivot_index = 0 40 20 10 30 60 50 7 80 100
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
pivot_index = 0 40 20 10 30 60 50 7 80 100
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
pivot_index = 0 40 20 10 30 60 50 7 80 100
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
pivot_index = 0 40 20 10 30 60 50 7 80 100
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
pivot_index = 0 40 20 10 30 60 50 7 80 100
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
pivot_index = 0 40 20 10 30 7 50 60 80 100
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
pivot_index = 0 40 20 10 30 7 50 60 80 100
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
pivot_index = 0 40 20 10 30 7 50 60 80 100
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
pivot_index = 0 40 20 10 30 7 50 60 80 100
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
pivot_index = 0 40 20 10 30 7 50 60 80 100
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
pivot_index = 0 40 20 10 30 7 50 60 80 100
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
pivot_index = 0 40 20 10 30 7 50 60 80 100
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
pivot_index = 0 40 20 10 30 7 50 60 80 100
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
pivot_index = 0 40 20 10 30 7 50 60 80 100
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
pivot_index = 0 40 20 10 30 7 50 60 80 100
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
pivot_index = 4 7 20 10 30 40 50 60 80 100
too_big_index too_small_index
PARTITION RESULT
The problem:
Multiply two matrices A and B, each of size
n n
subtractions.
Strassen’s matrix multiplication:
· Discover a way to compute the Cij’s using 7 multiplications
and 18 additions or subtractions
P ( A11 A22 )( B11 B22 )
Q ( A21 A22 ) B11
k 1 n 2 7 k 2
7 T ( k 1 ) an ( ) 1
2 4
7 k 1
( ) 1
7 k 1 b an 2 4
P = (A11+A22) (B11+B22)
= (1+4) (5+8) = 65
Q = (A21+A22) * B11
= (3+4)*5 = 35
Q = 2 2 1 0 = 2 2
2 2 0 1 2 2
R = -1 -1
-1 -1
S = -1 -1
-1 -1
T= 2 2
1 1 1 1 1 0 1 1 3 3
C11
1 1 1 1 1 0 0 0 3 3
1 1 1 0 1 0 1 0 3 0
C12
1 1 1 0 1 0 0 0 3 0
1 1 1 1 1 0 1 1 3 3
C 21
0 0 1 1 0 0 0 0 0 0
1 1 1 0 1 0 1 0 3 0
C 22
0 0 1 0 0 0 0 0 0 0