Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Divide and Conquer

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 95

Prepared By : Mrs. R.

Pallavi Reddy, Assistant


Professor,CSE Dept., GNITS
DIVIDE AND
CONQUER
DIVIDE AND CONQUER
General Method:
 Given a function to compute on ‘n’ inputs, Divide and Conquer
strategy suggests splitting the inputs into k distinct subsets
1<k≤n, yielding “k” sub problems.

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
 These sub problems are solved separately and then a method
must be found to combine sub solutions into a solution of the
whole.
 If sub solutions are relatively large, then Divide and Conquer
strategy can be reapplied.
 For those cases, the reapplication of Divide and Conquer
principle is naturally expressed by a recursive algorithm.
DIVIDE AND CONQUER DESIGN
PARADIGM

1. Divide the problem(instance) into sub problems.

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
2. Conquer the sub problems by solving them recursively.
3. Combine sub problem solutions.
CONTROL ABSTRACTION FOR DIVIDE AND CONQUER

Algorithm DAndC(P)
{
if Small(P) then

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
return S(P)
else
{
divide P into smaller instances P1,P2,----,Pk. k ≥1
Apply DAndC to each of these sub problems.
return Combine( DAndC(P1), DAndC(P2),…DAndC(Pk))
}
}
CONTROL ABSTRACTION FOR DIVIDE AND CONQUER

 Small(P) is a Boolean-Valued function that determines whether


the input size is small enough that the answer can be computed
without splitting, If so, the function ‘S’ is invoked.

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
 Otherwise, the problem ‘P’ is divided into smaller sub
problems. These sub problems P1,P2,----,Pk are solved by
recursive applications of DAndC.
 Combine is a function that determines solution to ‘P’ using the
solutions to the ‘K’ sub problems.
CONTROL ABSTRACTION FOR DIVIDE AND CONQUER

 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.

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
 T(n) = g(n) n small
T(n1) + T(n2) +… + T(nk) + f(n) otherwise

T(n) is time for DAndC on any input of size ‘n’.


G(n) is the time to compute the answer directly for small inputs.
F(n) is the time for dividing ‘P’ and combining solutions to sub problems.
CONTROL ABSTRACTION FOR DIVIDE
AND CONQUER
 For dividing and conquer based algorithms that produce sub problems of
same type as original problem, it is very natural to describe such
algorithm using recursion.

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
 T( n) = T(1) n=1
a T(n/b) + f(n) n>1
where a and b are known constants. We assume that T(1) is known and
‘n’ is a power of b ( i.e., n=bk )

 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

(instance) a problem of size n

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
Sub problem 1 Sub problem 2
of size n/2 of size n/2

Solution to Solution to
Sub problem 1 Sub problem 2

Solution to
Original problem
n
T ( n)  aT    f ( n)
b

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
Ex: a=2,b=2, T(1)=2, f(n)=n

= 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)]

Where U(n)=Σj=1 to k h (bj) and h(n)= f(n)/ nlog ab

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
h(n) U(n)
O(nr) , r<0 O(1)
Θ ( ( log n )i) , i≥0 Θ((log n)i+1/(i+1))
Ω (nr) , r>0 Θ(h(n))

Table: U(n) values for various h(n) values


This table allows us to easily obtain the asymptotic value of T(n) for
many of the recurrences one encounters when analyzing divide and Conquer
algorithms.
Ex1: T(n)= T(1) n=1
T(n/2)+c n>1
Sol: T(n)= nlog ab [ T(1) + U(n)]
a=1,b=2, f(n)=c
T(n)= nlog 12 [ T(1)+U(n)]

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
Log12 means
To know u(n) , calculate h(n)
2?=1
h(n)=f(n)/ nlog ab = c / nlog 12 20=1
c/no = c/1 = c
Θ((log n)0) , when i ≥0
= c*1= c *(log n)0 U(n)=θ(log n)0+1/0+1 = log n/1 = log n
U(n)=θ(log n)
T(n)= nlog 12 [ T(1)+c *(log n)]
= 1 [constant + θ(log n)]
T(n)=θ(log n)
EX:2 A=2,B=2,F(N)=CN
 Sol: T(n)= nlog ab [ T(1) + U(n)]
T(n)= nlog 22 [ T(1)+U(n)]
2?=2
21=2

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
h(n)=f(n)/ nlog ab Log22 = 1
Log33 = 1
= c*n / nlog 22 = c*n / n = c
U(n)=θ(log n) where i≥0

T(n)= nlog 22 [ T(1)+ θ(log n) ]


= n [ constant + θ(log n) ]
T(n)= n * θ(log n)
T(n)= θ(nlog n)
EX:3 T(n)=7T(n/2) + 18n2
a=7,b=2,f(n)=18n2

Sol: T(n)= nlog ab [ T(1) + U(n)] 2?=7


T(n)= nlog 72 [ T(1) + U(n)] 22=4
23=8

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
h(n)= f(n)/ nlog ab 22.897=7
=18n2 / nlog 72 = 18n2 / n2.897 = 18n2-2.897 = 18n-ve
U(n)= O(1) i<0, O(nr)=O(1)

T(n)= nlog 72 [ T(1) + U(n)]


= nlog 72 [ T(1) + O(1)]
= nlog 72
= n2.897
Solve the following recurrence relations using Master’s Method.

4. T(n)= 9T(n/3) + 4n6


5. a=1,b=2,f(n)=c n
a=5,b=4,f(n)=c n2

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
6.

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.

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
 It works by comparing the search key “k” with array’s middle
element A[m].
 If match, algorithm stops.

 Otherwise same operation is repeated recursively for the first


half of the array, if k<A[m] and for second half array if
k>A[m].

A[0]……….A[m-1] A[m] A[m+1]……….A[n-1]


Algorithm BinSrch (a, f, l, x)
// Given an array a[f..l] of elements in increasing order.
// x is the element to be searched and return the position where element is found.
{
if (l=f) then // If small(p)
{
if (x=a[f]) then
return f;
else Recursive Binary Search

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
return 0;
}
else
{ // Reduce P into a smaller sub problem
mid:=[ (f + l)/2];
if (x=a[mid]) then
return mid;
else if( x<a[mid]) then
return BinSrch( a,f,mid-1,x);
else
return BinSrch(a,mid+1,l,x);
}
}
ITERATIVE BINARY SEARCH
Algorithm BinSrch( a, n, x)
// Given an array a[1..n] of elements in increasing order
// Determine whether ‘x’ is present, and if so, return j such that x=a[j]; else return ‘0’
{
low := 1;
high := n;

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
while(low ≤ high) do
{
mid := [ (low + high) / 2];
if( x < a[mid] ) then
high := mid-1;
else if( x > a[mid] ) then
low := mid+1;
else
return mid;
}
return 0;
}
DIVIDE AND CONQUER PARADIGM FOR BINARY
SEARCH

 Divide: Divide the array into 2 equal halves, compute mid.


 Conquer: Find the element to be searched by using 3
conditions

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
 If element to be searched is mid element then return position.
 If element to be searched is less when compared to mid, search the

element in left sub array by recursive call to binary search.


 If element to be searched is greater than mid element, search in right

sub array by recursive call to binary search.


 Combine: Since answer to the new sub problem is also the
answer of original problem, there is no need of any combining.
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].

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
 Ex. Binary search for 33.

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].

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
 Ex. Binary search for 33.

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].

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
 Ex. Binary search for 33.

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].

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
 Ex. Binary search for 33.

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].

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
 Ex. Binary search for 33.

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].

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
 Ex. Binary search for 33.

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].

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
 Ex. Binary search for 33.

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].

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
 Ex. Binary search for 33.

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].

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
 Ex. Binary search for 33.

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

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
whether k is smaller, equal to or larger than A[m].
 It is also based on the instance of the problem.
WORST CASE ANALYSIS
 Worst case inputs include all arrays that do not contain a given search key.
 Since after one comparision algorithm faces same situation but for an array half the size.

T(n) = T(n/2) + 1 for n>1


if n=2k , T(2k) = T (2k / 2) + 1
= T (2k-1) + 1

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
If number of elements on which we need to search is ‘n’, then number of times array can be
split is derived as follows:
For n=3 no. of split = 1
For any values of ‘n’ , array split
For n=4 no. of split = 2
is continued till size
For n=5 no. of split = 2 of array becomes 1
For n=6 no. of split = 2
For n=7 no. of split = 2
For n=8 no. of split = 3
Therefore 2x = n ‘n’ is the size of the array and ‘x’ is the number of splits.

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

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
T(2k) = T (2k -k) + k K=log2n
T(1)=1
= T(20) + k
= T(1) + k
Therefore T(n) = T(1) + k
= 1+ log2 n
Therefore T(n) ≈ log2 n WORST CASE ANALYSIS
AVERAGE CASE ANALYSIS

 Average number of key comparisions made by binary search is


slightly smaller than in worst case.
Therefore T(n) ≈ log2 n

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
BEST CASE ANALYSIS
Element to be searched is present in mid position of given array

of ‘n’ elements.
Therefore key comparision is 1.

Therefore T(n) = 1
DIVIDE AND CONQUER : MERGE
SORT

 The elements are to be sorted in an increasing order.


 Given a sequence of ‘n’ elements a[1],…..,a[n], the general
idea is to imagine them spilt into 2 sets a[1],…a[n/2] and

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
a([n/2]+1),…a[n].
 Each set is individually sorted, and the resulting sorted
sequences are merged to produce a single sorted sequence of
‘n’ elements.
This algorithm closely follows divide and conquer
paradigm. It operates as follows:
 Divide: Divide n-element sequence to be sorted in two
subsequences of n/2 elements each.

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
 Conquer: Sort two subsequences recursively using Merge sort.

 Combine: Merge two sorted subsequences to produce sorted


answer.

 Recursion “ Bottoms out” when the sequence to be sorted has


length 1, in which case there is no work to be done, since every
sequence of length 1 is already in sorted order.
MERGE SORT EXAMPLE

8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

8 3 2 9 71 5 4

8 3 2 9 7 1 5 4 The non-recursive version


of Merge sort starts from
merging single elements
3 8 2 9 1 7 4 5 into sorted pairs.

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

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
Prepared By : Mrs. R.Pallavi Reddy, Assistant
Professor,CSE Dept., GNITS
ALGORITHM FOR MERGE SORT
Algorithm for Merging two sorted sub arrays using Auxilliary storage

Algorithm Merge( low, mid, high ) if(h>mid) then


{ for k:=j to high do
h:= low; i:= low; j:=mid+1; {
while((h ≤ mid) and ( j≤ high)) do b[i]:=a[k];
{ i:=i+1;

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
if( a[h] ≤ a[j]) then }
{ else
b[i]:=a[h]; for k:= h to mid do
h:=h+1; {
} b[i]:=a[k];
else i:=i+1;
{
}
b[i]:=a[j];
for k:=low to high do
j:=j+1;
a[k]:=b[k];
}
}
i:=i+1;
}
Prepared By : Mrs. R.Pallavi Reddy, Assistant
Professor,CSE Dept., GNITS
ANALYSIS OF MERGE SORT
Q) SORT THE FOLLOWING USING MERGE SORT.

310, 285, 179, 652, 351, 423, 861, 254, 450, 520

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
DIVIDE AND CONQUER : QUICK SORT

 Fastest known sorting algorithm in practice


 If the number of elements in A is 0 or 1, then return.
 Pick any element P in A (called the pivot).

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
 Partition the elements in A except P into two disjoint groups:
 S1 = {x A – {P} | x  P}
 S2 = {x A – {P} | x  P}
 Return {Quick Sort(A1) + P + Quick Sort(A2)}
QUICK SORT

pick a pivot value from the array


partition the list around the pivot value

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
sort the left half
sort the right half
QUICK SORT
 Follows the divide-and-conquer paradigm.

 Divide: Partition (rearrange) the array A[low..high] into two


possible sub arrays A[low..m–1] and A[m+1..high].

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
 Each element in A[low..m–1] < A[m].
 A[m] < each element in A[m+1..high].
 Index m is computed as part of the partitioning procedure.

 Conquer: Sort the two sub arrays A[low,q-1] and A[q+1,high]


by recursive call to quick sort.

 Combine: The sub arrays are sorted in place – no work is


needed to combine them.
 Therefore the entire array A[low..high] is now sorted.
QUICK SORT EXAMPLE
i j

5 1 3 9 7 0 4 2 6 8

i j

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
5 1 3 9 7 0 4 2 6 8

i j

5 1 3 9 7 0 4 2 6 8

i j

5 1 3 2 7 0 4 9 6 8

•Move i to the right to be larger than pivot.


•Move j to the left to be smaller than pivot.
•Swap
QUICK SORT EXAMPLE
i j

5 1 3 2 7 0 4 9 6 8

i j

5 1 3 2 7 0 4 9 6 8

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
i j

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

S1 < pivot pivot S2 > pivot


Prepared By : Mrs. R.Pallavi Reddy, Assistant
Professor,CSE Dept., GNITS
100
30
We are given array of n integers to sort:

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:

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
40 20 10 80 60 50 7 30 100
PARTITIONING ARRAY

Given a pivot, partition the elements of the array


such that the resulting array consists of:

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
1. One sub-array that contains elements >= pivot
2. Another sub-array that contains elements < pivot

The sub-arrays are stored in the original data array.

Partitioning loops through, swapping elements


below/above pivot.
Professor,CSE Dept., GNITS
Prepared By : Mrs. R.Pallavi Reddy, Assistant
pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

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

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

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

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

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]

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

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]

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

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]

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

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]

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

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]

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

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]

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

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]

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

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]

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

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]

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

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]

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

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]

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

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]

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

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]

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

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]

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

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]

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

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]

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

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]

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

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]

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
4. While too_small_index > too_big_index, go to 1.
5. Swap data[too_small_index] and data[pivot_index]

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

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]

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
4. While too_small_index > too_big_index, go to 1.
5. Swap data[too_small_index] and data[pivot_index]

pivot_index = 4 7 20 10 30 40 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index
PARTITION RESULT

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
7 20 10 30 40 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

<= data[pivot] > data[pivot]


RECURSION: QUICKSORT SUB-ARRAYS

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
7 20 10 30 40 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

<= data[pivot] > data[pivot]


Prepared By : Mrs. R.Pallavi Reddy, Assistant
Professor,CSE Dept., GNITS
Prepared By : Mrs. R.Pallavi Reddy, Assistant
Professor,CSE Dept., GNITS
Prepared By : Mrs. R.Pallavi Reddy, Assistant
Professor,CSE Dept., GNITS
Prepared By : Mrs. R.Pallavi Reddy, Assistant
Professor,CSE Dept., GNITS
Prepared By : Mrs. R.Pallavi Reddy, Assistant
Professor,CSE Dept., GNITS
STRASSEN’S MATRIX MULTIPLICATION
BASİC MATRİX MULTİPLİCATİON

Suppose we want to multiply two matrices of size N x N:

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
for example A x B = C.

C11 = a11b11 + a12b21


C12 = a11b12 + a12b22
C21 = a21b11 + a22b21
C22 = a21b12 + a22b22 2x2 matrix multiplication can be
accomplished in 8 multiplication.(2log28 =23)
BASİC MATRİX MULTİPLİCATİON

void matrix_mult (){


for (i = 1; i <= N; i++) { algorithm

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
for (j = 1; j <= N; j++) {
compute Ci,j;
}
}} N
Ci , j   ai ,k bk , j
Time analysis k 1
N N N
Thus T ( N )   c  cN 3  O ( N 3 )
i 1 j 1 k 1
Matrix multiplication

The problem:
 
Multiply two matrices A and B, each of size
n  n

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
     
 A   B   C 
     
  nn   nn   nn
The Divide-and-Conquer way:

C11 C12   A11 A12   B11 B12 


 C  A  B 
     
C 21 C 22   A21 A22   B21 B22 

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
C11  A11  B11  A12  B21
C12  A11  B12  A12  B22
C 21  A21  B11  A22  B21
C 22  A21  B12  A22  B22
transform the problem of multiplying A and B, each of size [n×n] into 8 subproblems,
each of size n n
 2  2 
Prepared By : Mrs. R.Pallavi Reddy, Assistant
Professor,CSE Dept., GNITS
Prepared By : Mrs. R.Pallavi Reddy, Assistant
Professor,CSE Dept., GNITS
n
 T (n)  8  T ( )  an 2
2
 O(n 3 )
which an2 is for addition

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
so, it is no improvement compared with the traditional way
STRASSENS’S MATRİX MULTİPLİCATİON

 Strassen showed that 2x2 matrix multiplication can


be accomplished in 7 multiplication and 18

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
additions or subtractions. .(2log27 =22.807)

 Thisreduce can be done by Divide and Conquer


Approach.
STRASSENS’S MATRİX MULTİPLİCATİON
 To have faster algorithm with fewer multiplications and more
additions.
 Volker Strassen discovered a way to compute product of 2*2
matrices using only 7 multiplications and 18 additions or

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
subtractions.
 This method involves computing seven n/2 *n/2 matrices
P,Q,R,S,T,U,V which uses 7 matrix multiplications and 10
matrix addition or subtractions.
 The product matrix Cij requires an additional 8 addition or

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

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
R  A11 ( B12  B22 )
S  A22 ( B21  B11 )
T  ( A11  A12 ) B22
U  ( A21  A11 )( B11  B12 )
V  ( A12  A22 )( B21  B22 )
C11  P  S  T  V
C12  R  T
C 21  Q  S
C 22  P  R  Q  U
Analysis:
 n
7  T ( )  an 2 n2
T ( n)   2
b n2

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
n
T (n)  7  T ( )  an 2
2
n 7
 7  T ( 2 )  ( )an 2  an 2
2
2 4
n 7 2 7
 7  T ( 3 )  ( )  an  ( )  an 2  an 2
3 2
2 4 4

Assume n = 2k for some integer k

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 

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
 7
1 
 4 
7
 b  7k  c  n2  ( )k
4
7
7 Lg
 b  7 Lgn  cn 2  ( ) Lgn  b  7 Lgn  cn 2 (n) 4
4
 b  n Lg 7  cn Lg 7  (b  c)  n Lg 7
 O(n Lg 7 )  O(n 2.81 )
Example:
1 2 5 6
3 4 * 7 8
use Strassen’s matrix multiplication

P = (A11+A22) (B11+B22)
= (1+4) (5+8) = 65
Q = (A21+A22) * B11
= (3+4)*5 = 35

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
R = A11(B12-B22)
= 1(6-8) = -2
S = A22(B21-B11)
= 4(7-5) = 8
T = (A11+A12)B22
= (1+2) 8 = 24
U = (A21-A11)*(B11+B12)
= (3-1) * (5+6) = 22
V = (A12-A22)*(B21+B22)
= (2-4)*(7-8) = -30
Cij = 19 22
C11 = P+S-T+V = 19 43 50
C12 = R + T = 22
C21 = Q+S = 43
C22 = P+R-Q+U = 50
EX: Find the Strassens’s Matrix multiplication for 4*4
matrix
1 1 1 1 1 0 0 0
A= 1 1 1 1 B= 0 1 0 0

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
1 1 1 1 0 0 1 0
1 1 1 1 0 0 0 1
Sol:
P= 2 2 2 0 = 4 4
2 2 0 2 4 4

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

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
2 2
U= 0 0
0 0
V= 0 0
0 0
1 1 1 1
1 1 1 1
C = 1 1 1 1 Final resultant matrix of
1 1 1 1 size 4*4
Example:
1 1 1 1 1 1
1 1 1  1 1 1
   
1 1 1 1 1 1
use Divide-and-Conquer way to solve it as following:
1 1 1 0  1 1 1 0  3 3 3 0
1 1 1 0 1 1 1 0 3 3 3 0
  

Professor,CSE Dept., GNITS


Prepared By : Mrs. R.Pallavi Reddy, Assistant
1 1 1 0  1 1 1 0  3 3 3 0
     
0 0 0 0  0 0 0 0 0 0 0 0

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
 

You might also like