Sei sulla pagina 1di 39

Divide and

Conquer

Dr. Munesh
Singh
Divide and Conquer

Dr. Munesh Singh

Indian Institute of Information Technology


Design and Manufacturing,
Kancheepuram
Chennai-600127

February 25, 2019


Divide and Conquer Approch

Divide and
Conquer A problem is given of size n, and the problem is to be
Dr. Munesh consider as big
Singh
Such problem divided into a subproblems and execute
independently
Later the generated output of each subproblem combined
together.
Subproblem is also be a same problem as big problem (eg.
Big-Problem: Sorting, Subproblem: sorting)
Divide and conquer approach is recursive because the
subproblem and problem both is of same nature
How to identify whether the problem can be solve through
divide and conquer approch:
Problem needs to be dividable
If problem is dividable then subproblem needs to be of
same nature
Divide and Conquer Algorithm

Divide and
Conquer

Dr. Munesh
Singh
DAC(P)
{
if(small(p)) {
S(P); }
else {
divide p into p1,p2,p3,—pk
Apply DAC(p1),DAC(p2),DAC(p3),....
Combine(DAC(p1)), DAC(p2),....)
}
}
Divide and Conquer Algorithm

Divide and
Conquer

Dr. Munesh
Singh

Binary Search
Finding Maximum and Minimum
Merge Sort
Quick Sort
Strassens Matrix Multiplication
Decresing Value Recusive Algorithm

Divide and
Conquer

Dr. Munesh
Singh
Algorithm
Test(int n) −− > T (n) Recusive tree
{ Lets say time take to print is 1 and call 1
if(n>0) Test(3) 1
{
3 Test(2) 1
printf(” %d”,n); −− > 1
Test(n-1); −− > T (n − 1) 2 Test(1) 1

}——————————– 1 Test(0) 1
} Recursive call will take time= (3+1)
T (n) = T (n − 1) + 1 n>1 Print will take= 3

T (1) = 1 n = 0
Decresing Value Recusive Algorithm

Divide and
Conquer

Dr. Munesh
Singh
Algorithm
Recusive tree
void Test(int n)
Test(n) n
{
if(n>0) n Test(n-1) n-1
{
for(i=0;i<n;i++) n-1 Test(n-2) n-2
printf(” %d”,n);
n-2 Test(1) 1
Test(n-1);
} 1 Test(0)
}
Decresing Value Recusive Algorithm

Divide and
Conquer

Dr. Munesh
Singh Recusive tree
Algorithm
Test(n) n
void Test(int n)
{ log(n) Test(n-1) n-1
if(n>0)
log(n-1) Test(n-2)
{ n-2
for(i=0;i<n;i=i*2) log(n-2) Test(1) 1
printf(” %d”,n);
Test(n-1); log(1) Test(0)

} log(n)+log(n-1)+log(n-2)+....+log(1)
} log(n*(n-1)*(n-2)*..........*1)
log n! ==n(logn)==O(nlogn)
Decresing Value Recusive Algorithm

Divide and
Conquer

Dr. Munesh
Singh
Algorithm
Recusive tree
void Test(int n) Test(n) 1=20
{ 1 Test(n-1) Test(n-1) 2 = 21
if(n>0) 1 1
4=22
{ Test(n-2) Test(n-2) Test(n-2) Test(n-2)

printf(” %d”,n); 1
1 Test(n-3) 1 1
Test(n-3) Test(n-3) Test(n-3) Test(n-3) Test(n-3) Test(n-3) Test(n-3) 8=23
Test(n-1);
Test(n-1); Test(0) Test(0) Test(0) Test(0) Test(0) Test(0) Test(0) k=2k
Test(0)

} 2 +20 1
+222+............+2 K
3 k
a+ar+ar +ar +.....ar =a(rk+1-1)/r-1, r=2,a=1
}
Dividing Value Recusive Algorithm

Divide and
Conquer

Dr. Munesh
Singh Algorithm
Test(int n) −− > T (n)
{ Recusive tree
if(n>1)
Test(n) 1
{ n/2k=1
k
printf(” %d”,n); −− > 1 1 Test(n/2) 1 n=2
k=log2n
Test(n/2); −− > T (n/2)
1 Test(n/22)
} 1
} 1 Test(n/2k) n/2k=1
——————–
T(n)=T(n/2)+1, n>0
T(1)=1, n=0;
Dividing Value Recusive Algorithm

Divide and
Conquer

Dr. Munesh Algorithm


Singh
Test(int n) −− > T (n)
{ Recusive tree
if(n>1)
Test(n) 1
{
for(i=0;i<n;i++) n Test(n/2) 1
printf(” %d”,n); −− > n n/2 Test(n/22) 1
Test(n/2); −− > T (n/2)
} n/22 Test(n/2k) n/2k=1
2 k
} T(n)=n+n/2+n/2 +........+n/2
T(n)=n(1+1/2+1/4+......+1/2k)=O(n)
——————–
T(n)=T(n/2)+n, n>0
T(1)=1, n=0;
Dividing Value Recusive Algorithm

Divide and
Conquer

Dr. Munesh
Singh
Algorithm
Algorithm
Test(int n) −− > T (n)
Test(int n) −− > T (n)
{
{
if(n>1)
if(n>1)
{
{
for(i=0;i<n;i++)
for(i=0;i<n;i=i*2)
printf(” %d”,n); −− > n
printf(” %d”,n); −− > n
Test(n/2); −− > T (n/2)
Test(n/2); −− > T (n/2)
Test(n/2); −− > T (n/2)
}
}
}
}
Binary Search Divide and Conquer Approch

Divide and
Conquer

Dr. Munesh
Singh BS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
3 6 8 12 14 17 25 29 31 36 42 45 51 56 62
l mid l mid mid h
key=42
key=30
l h mid=(h+1)/2
l h mid=(h+1)/2
1 15 8
1 15 8
9 15 12
9 15 12
9 11 10
9 11 10
11 11 11
9 9 9
9 8 h>l (element is not found)
Binary Search Iterative Algorithm

Divide and
Conquer RBS
Dr. Munesh
Singh BS int RBS(l,h,key){
if(l==h){
int BS(a,n,key)
if(a[l]==key)
{
return l;
l=1,h=n; while(l<=h) {
else
mid=(l+h)/2;
return 0; }
if(key==a[mid])
else {
return mid;
mid=(l+h)/2;
if(key>mid)
if(a[mid]==key)
l=mid+1;
return mid;
else
if(key<a[mid]) {
h=mid-1;
return(l,mid-1,key);
}
else return(mid+1,h,key);
}
}
2way Merge

Divide and
Conquer

Dr. Munesh
Singh

After comparing and coping move index

A B C
i 1 j 5 1k
i 3 j 7 3k
i 9 j 8 5k
i 6 j 10 7k
8k
9k
6k
10 k
2way Merge

Divide and
Conquer

Dr. Munesh Algo Merge(a,m,n){


Singh
i=1,k=1,j=1;
while(i<=m && j<=n)
After comparing and coping move index {
A B C
if(a[i]<b[j])
i 1 j 5 1k c[k++]=a[i++];
i 3 j 7 3k else
i 9 j 8 5k
i j 10 7k c[k++]=b[i++];
6
8k }
9k
6k for(; i<m;i++)
10 k c[k++]=a[i];
for(;j<n;j++)
c[k++]=b[j];
}
M-Way Merge

Divide and
Conquer

Dr. Munesh
Singh A B C D
4 3 8 2
6 5 10 4
12 9 16 18

A
2-way Merge

Divide and
Conquer

Dr. Munesh
Singh

A n 8/2/2/2
8/23=1
I pass n log28=3

II pass n At each pass


logn times
III pass n it take
O(nlogn)
Merge Sort

Divide and
Conquer

Dr. Munesh A
Singh

Algo merge(l,h)
{
if(l<h){
mid=(l+h)/2;
mergesort(l,mid);
mergesort(mid+1,h);
merge(l,mid,h);
}
}
Quick Sort

Divide and
Conquer increment of i till find element larger than pivot
Dr. Munesh
Singh decrement of j till find element less than pivot
0 1 2 3 4 5 6 7 8 9
A
pivot=10 i j
0 1 2 3 4 5 6 7 8 9
A 16
i j
0 1 2 3 4 5 6 7 8 9
A 12 16
i j
0 1 2 3 4 5 6 7 8 9
A 15 12 16
j i
whenever j cross i, place the pivot to j position
Quick Sort

Divide and
Conquer
partition(l,h)
Dr. Munesh
{
Singh
0 1 2 3 4 5 6 7 8 9 pivot=a[l];
A 10 15 12 16 while(i<j){
j i do
{
quicksort(l,h)
i++;
{
}
if(l<h)
while(a[i]>=pivot);
{
j=partition(l,h);
do
quicksort(l,j);
{
quicksort(j+1,h);
j++;
}
}
}
while(a[j]<pivot);
swap(a[i],a[j]);
}
swap(a[l],a[j]);
return j;
}
Greedy Method

Divide and
Conquer Greedy Approch Of Problem Solving
Dr. Munesh
Singh
This approch used to solve the optimization problem.
Optimization problem where problem demands minimum
or maximum results
suppose we have a problem to go from one city to other
city solution for above problem can be many
(transportation), but the constraint in this problem is to
cover the travel within 10 hours.
Since the constraint is there all solution can not be
feasible.
The solution that satisfies the constraint such solution
called feasible solution.
Out of the feasible solution one solution is optimal ( Best
solution)
Greedy Method

Divide and
Conquer

Dr. Munesh
Singh

Greedy method.
Dynamic Programming
Branch and Bound
Greedy Method

Divide and
Conquer

Dr. Munesh
Singh
Algorithm
Algo Greedy(a,n)
{
for i=1 to n do
{
x=select(a);
if feasible(x) then
Solution=solution+x;
}
}
Greedy Problem

Divide and
Conquer

Dr. Munesh
Singh
Knapsack Problem
Objects O 1 2 3 4 5 6 7
Profits P 10 5 15 7 6 18 3
Weights W 2 3 5 7 1 4 1
P/W:
X=(x1,x2,x3,x4,x5,x6,x7)
P
Xi Wi :<=15
P
Xi Wi :=Maximum
Knapsack capacity m=15
Job Sequencing Problem

Divide and
Conquer

Dr. Munesh
Singh

Solve Using Greedy Approch


Jobs j 1 2 3 4 5
Profits P 20 15 10 5 1
deadline d 2 2 1 3 3
Each job will be performed in 1 unit of time
slots 0-1-2-3
Job Sequencing Problem

Divide and
Conquer

Dr. Munesh
Singh

Solve Using Greedy Approch


Jobs j 1 2 3 4 5 6 7
Profits P 35 30 25 20 15 12 5
deadline d 3 4 4 2 3 1 2
Each job will be performed in 1 unit of time
slots 0-1-2-3-4
Huffman Coding

Divide and
Conquer

Dr. Munesh
Singh

Solve Using Greedy Approch


Message BCCABBDDAECCBBAEDDCC
Length 20
ASCII code for each charter representation will take 8 bit
So total size of message will be 20*8=160 bits
Huffman suggested to reduce the number of bit
representation using our own ASCII table
Huffman Coding

Divide and
Conquer

Dr. Munesh
Solve Using Greedy Approch
Singh
A 65 01000001
B 66 01000010 20*3=60bits
C 67 01000011
But message is encoded
Charaters count/freq code at the sender end
How received decode it
A 3 000
so in addition of code
B 5 001
we will send table
C 6 010
D 4 011
E 2 100 A=ASCII(8 bit)=5*8=40
code=5*3=15

Total bits=60+40+15=115 bits


Variable Length Huffman Coding

Divide and
Conquer

Dr. Munesh
Solve Using Greedy Approch
Singh
Charaters count/freq code 3*3=9
A 3 001 5*2=10
B 5 10 6*2=12
C 6 11 4*2=8
D 4 01 2*3=6
45 bits
E 2 000 0 20
5*8+12 bits
9 1
sum(distance*frequency) 0 1
5 11
0 1 0 1
distance from root node 2 3 4 5 6
E A D B C
Minimum Cost Spanning Tree

Divide and
Conquer Solve Using Greedy Approch
Dr. Munesh
Singh
Spanning tree is the subgraph of the graph

U
S G
V"=V
E"=E-1
G(V E)
V={1 2 3 4 5 6}
E={12} {23}....

How many spanning tree i can form= 6C5


Number of spanning tree=ECV-1-no of cycles
Minimum Cost Spanning Tree

Divide and
Conquer
Solve Using Greedy Approch
Dr. Munesh
Singh
5 5

4 2 3 cost=5+3+6 3

6 6

4 3 4 2 3

6
cost=4+6+3 cost=4+2+3
Minimum Cost Spanning Tree

Divide and
Conquer

Dr. Munesh
Singh
Solve Using Greedy Approch
Prism Algorithm (minimum cost edge)
Select the minimum edge between the vertices’s
Next selection of edge needs to be connected with the first
selection
Kruskals Algorithm
Select the minimum edge between the vertices’s
Next selection of edge needs not to be connected with the
first selection
What will be the time complexity of kruskals O(n2 )
We can improve it by using min heap data structure
Minimum Cost Spanning Tree

Divide and
Conquer Solve Using Greedy Approch
Dr. Munesh
Singh

10 28

14 16
25
24
18
12
22
Dynamic Programming

Divide and
Conquer

Dr. Munesh
Greedy Algorithm
Singh
Dynamic Programming
Both the approach is used for solving the Optimization
problem
Greedy follows the predefined procedure to solve the
problem
Unlike dynamic programming find all the solutions and
then choose the best one
Dynamic programming is time consuming
Dynamic programming follows the principle of optimality
In dynamic programming, decision is take at each steps
dynamic problem are used through recursive formula
Dynamic Programming Example

Divide and
Conquer
Solve Using Dynamic Programming
Dr. Munesh
Singh
5 5

4 2 3 cost=5+3+6 3

6 6

4 3 4 2 3

6
cost=4+6+3 cost=4+2+3
Dynamic Programming Example

Divide and
Conquer Solve Using Dynamic Programming Memoriztion
Dr. Munesh
Singh
Memorization technique uses the top down approach

int fib(int n)
{
if(n<=1)
return n; 0 1 2 3 4 5
return fib(n-2)+fib(n-1);
} fib(n)=(n+1);
fib(5) O(n)

fib(3) 2 fib(4)

fib(2) 1 fib(3)
fib(1) fib(2)
1
fib(0) fib(1) fib(0) fib(1) fib(1) fib(2)
0 1 0 1 1
fib(0) fib(1)
0 1
Dynamic Programming Example

Divide and
Conquer Solve Using Dynamic Programming Tabulation
Dr. Munesh
Singh
Tabulation technique uses the bottom up approach

int fib(int n)
fib(5)=5 {
if(n<=1)
return n;
F[0]=0;f[1]=1;
for(i=2;i<n;i++){
F[i]=F[i-2]+F[i-2];
}
Matrix Chain Multiplication

Divide and
Conquer

Dr. Munesh
Singh
A1 A2 A3 A4
5x4 4x6 6x2 2x7

5x4 4x3 5x3=15


For generating 15 element how many multiplication required
5x3x4=60
Matrix Chain Multiplication

Divide and
Conquer

Dr. Munesh
Singh A1 A2 A3 A4
5x4 4x6 6x2 2x7

m 1 2 3 4 s
1 2 3 4
1 1

2 2

3 3

4 4
m[1,1] m[1,2] m[1,3]
A1 A1 A2 1: A1*(A2*A3) = m[1,1]+m[2,3]+5x4x2 =88
2: (A1*A2)*A3 =m[1,2]+5x6x2+m[3,3]=180

Potrebbero piacerti anche