Unit Iv R23
Unit Iv R23
Dynamic Programming: General method, applications- 0/1 knapsack problem, All pairs shortest path problem,
Travelling salesperson problem,
Backtracking: General method, applications-n-queen problem, sum of subsets problem, graph coloring, 0 / 1
Knapsack Problem
APPLICATIONS:
1. This problem is similar to ordinary knapsack problem but we may not take a fraction of an
object.
2. We are given ‘ N ‘ object with weight Wi and profits Pi where I varies from l to N and also a
knapsack with capacity ‘ M ‘.
3. The problem is, we have to fill the bag with the help of ‘ N ‘ objects and the resulting profit has
to be maximum.
n
4. Formally, the problem can be started as, maximize Xi Pi
i=l
n
subject to Xi Wi L M
i=l
5. Where Xi are constraints on the solution Xi {0,1}. (u) Xi is required to be 0 or 1. if the object
is selected then the unit in 1. if the object is rejected than the unit is 0. That is why it is called as
0/1, knapsack problem.
6. To solve the problem by dynamic programming we up a table T[1…N, 0…M] (ic) the size is
N. where ‘N’ is the no. of objects and column starts with ‘O’ to capacity (ic) ‘M’.
7. In the table T[i,j] will be the maximum valve of the objects i varies from 1 to n and j varies from
O to M.
2. If i=l and j w (i) then T (i,j) = p(i), the cell is filled with the profit p[i], since only one object
can be selected to the maximum.
3. If i>l and j < w(i) then T(i,l) = T (i-l,j) the cell is filled the profit of previous object since it is not
possible with the current object.
4. If i>l and j w(i) then T (i,j) = {f(i) +T(i-l,j-w(i)),. since only ‘l’ unit can be selected to the
maximum. If is the current profit + profit of the previous object to fill the remaining capacity of the
bag.
Start with the last position of i and j, T[i,j], if T[i,j] = T[i-l,j] then no object of ‘i’ is required so
move up to T[i-l,j].
After moved, we have to check if, T[i,j]=T[i-l,j-w(i)]+ p[I], if it is equal then one unit of object
‘i’ is selected and move up to the position T[i-l,j-w(i)]
Repeat the same process until we reach T[i,o], then there will be nothing to fill the bag stop the
process.
Time is 0(nw) is necessary to construct the table T.
Consider a Example,
M = 6,
N=3
W1 = 2, W2 = 3, W3 = 4
P1 = 1, P2 =2, P3 = 5
i 1 to N
j 0 to 6
o<2 T1,o =0
i=l, j=2
2 o,= T1,2 = l.
i=l, j=3
3>2,= T1,3 = l.
i=l, j=4
4>2,= T1,4 = l.
i=l, j=5
5>2,= T1,5 = l.
i=l, j=6
6>2,= T1,6 = l.
i=2, j=1
l<3= T(2,1) = T(i-l)
T 2,1 =0
5.3. ALL PAIR SHORTEST PATH
Let G=<N,A> be a directed graph ’N’ is a set of nodes and ‘A’ is the set of edges.
1. Each edge has an associated non-negative length.
2. We want to calculate the length of the shortest path between each pair of nodes.
3. Suppose the nodes of G are numbered from 1 to n, so N={1,2,...N},and suppose G matrix L gives
the length of each edge, with L(i,j)=0 for i=1,2...n,L(i,j)>=for all i & j, and L(i,j)=infinity, if the
edge (i,j) does not exist.
4. The principle of optimality applies: if k is the node on the shortest path from i to j then the part
of the path from i to k and the part from k to j must also be optimal, that is shorter.
5. First, create a cost adjacency matrix for the given graph.
6. Copy the above matrix-to-matrix D, which will give the direct distance between nodes.
7. We have to perform N iteration after iteration k.the matrix D will give you the distance between
nodes with only (1,2...,k)as intermediate nodes.
8. At the iteration k, we have to check for each pair of nodes (i,j) whether or not there exists a
path from i to j passing through node k.
D0 =L= 0 5
50 0 15 5
30 0 15
15 5 0
1 7 5 11 12 - -
2 72 21 - - 24
3 3 - 32 - -
4 4 1 41 – 43 -
vertex 1:
7 5 11 12 - -
7 12 2 21 212 - 24
3 - 32 - -
4 9 1 41 412 43 –
vertex 2:
7 5 7 11 12 - 124
7 12 2 21 212 - 24
10 3 5 321 32 - 324
4 9 1 11 41 412 43 4124
vertex 3:
7 5 7 11 12 - 124
7 12 2 21 212 - 24
10 3 5 321 32 - 324
4 4 1 6 41 432 43 4324
vertex 4:
7 5 8 7 11 12 1243 124
6 6 3 2 241 2432 243 24
9 3 6 5 3241 32 3243 324
4 4 1 6 41 432 43 4324
1. At 0th iteration it nil give you the direct distances between any 2 nodes
D0= 0 5
50 0 15 5
30 0 15
15 5 0
2. At 1st iteration we have to check the each pair(i,j) whether there is a path through node 1.if so
we have to check whether it is minimum than the previous value and if I is so than the distance
through 1 is the value of d1(i,j).at the same time we have to solve the intermediate node in the
matrix position p(i,j).
0 5
50 0 15 5 p[3,2]= 1
D1= 30 35 0 15 p[4,2]= 1
15 20 5 0
15
30
5
5 50 5 15
15
Fig: floyd’s algorithm and work
3. likewise we have to find the value for N iteration (ie) for N nodes.
0 5 20 10 P[1,3] = 2
D2= 50 0 15 5 P[1,4] = 2
30 35 0 15
15 20 5 0
0 5 20 10
D3= 45 0 15 5 P[2,1]=3
30 35 0 15
15 20 5 0
0 5 15 10
20 0 10 5 P[1,3]=4
D4= 30 35 0 15 P[2,3]=4
15 20 5 0
0042
3040 0 direct path
P= 0100
0100
ALGORITHM :
Function Floyd (L[1..r,1..r]):array[1..n,1..n]
array D[1..n,1..n]
D=L
For k = 1 to n do
For i = 1 to n do
For j = 1 to n do
D [ i , j ] = min (D[ i, j ], D[ i, k ] + D[ k, j ]
Return D
ANALYSIS:
This algorithm takes a time of (n3)
1. A multistage graph G = (V,E) is a directed graph in which the vertices are portioned into K > =
2 disjoint sets Vi, 1 <= i<= k.
2. In addition, if < u,v > is an edge in E, then u < = Vi and V Vi+1 for some i, 1<= i < k.
If there will be only one vertex, then the sets Vi and Vk are such that [Vi]=[Vk] = 1.
3. Let ‘s’ and ‘t’ be the source and destination respectively.
4. The cost of a path from source (s) to destination (t) is the sum of the costs of the edger on the
path.
5. The MULTISTAGE GRAPH problem is to find a minimum cost path from ‘s’ to ‘t’.
6. Each set Vi defines a stage in the graph. Every path from ‘s’ to ‘t’ starts in stage-1, goes to
stage-2 then to stage-3, then to stage-4, and so on, and terminates in stage-k.
This MULISTAGE GRAPH problem can be solved in 2 ways.
Forward Method.
Backward Method.
4 6
2 2
5 4
9 1
4
7 3 2
7 t
s
3
11 5 5
2
11 6
1. Maintain a cost matrix cost (n) which stores the distance from any vertex to the destination.
2. If a vertex is having more than one path, then we have to choose the minimum distance path and
the intermediate vertex, which gives the minimum distance path, will be stored in the distance
array ‘D’.
3. In this way we will find out the minimum cost path from each and every vertex.
4. Finally cost(1) will give the shortest distance from source to destination.
5. For finding the path, start from vertex-1 then the distance array D(1) will give the minimum cost
neighbour vertex which in turn give the next nearest vertex and proceed in this way till we reach
the Destination.
6. For a ‘k’ stage graph, there will be ‘k’ vertex in the path.
7. In the above graph V1…V5 represent the stages. This 5 stage graph can be solved by using
forward approach as follows,
STEPS: - DESTINATION, D
Cost (12)=0 D (12)=0
Cost (11)=5 D (11)=12
Cost (10)=2 D (10)=12
Cost ( 9)=4 D ( 9)=12
1. For forward approach,
(i.e.)
9 2 3 2
P[1]=1;
P[k]=n;
For j=2 to k-1 do
P[j]=d[p[j-1]];
}
ANALYSIS:
The time complexity of this forward method is O( V + E )
if there one ‘K’ stages in a graph using back ward approach. we will find out the cost of each &
every vertex starting from 1st
stage to the kth stage.
We will find out the minimum cost path from destination to source (ie)[from stage k to stage 1]
PROCEDURE:
It is similar to forward approach, but differs only in two or three ways.
Maintain a cost matrix to store the cost of every vertices and a distance matrix to store the
minimum distance vertex.
Find out the cost of each and every vertex starting from vertex 1 up to vertex k.
To find out the path star from vertex ‘k’, then the distance array D (k) will give the minimum
cost neighbor vertex which in turn gives the next nearest neighbor vertex and proceed till we
reach the destination.
STEP:
PATH:
Start from vertex-12
D(12) = 10
D(10) = 6
D(6) = 3
D(3) = 1
So the minimum cost path is,
17 3 2 6 5 10 2
12
P[1]=1;
P[k]=n;
For j= k-1 to 2 do
P[j]=d[p[j+1]];
}
5.4.TRAVELLING SALESMAN PROBLEM
Let G(V,E) be a directed graph with edge cost cij is defined such that cij >0 for all i and j and cij
= ,if <i,j> E.
Let V =n and assume n>1.
The traveling salesman problem is to find a tour of minimum cost.
A tour of G is a directed cycle that include every vertex in V.
The cost of the tour is the sum of cost of the edges on the tour.
The tour is the shortest path that starts and ends at the same vertex (ie) 1.
APPLICATION :
Suppose we have to route a postal van to pick up mail from the mail boxes located at ‘n’
different sites.
An n+1 vertex graph can be used to represent the situation.
One vertex represent the post office from which the postal van starts and return.
Edge <i,j> is assigned a cost equal to the distance from site ‘i’ to site ‘j’.
the route taken by the postal van is a tour and we are finding a tour of minimum length.
every tour consists of an edge <1,k> for some k V-{} and a path from vertex k to vertex 1.
the path from vertex k to vertex 1 goes through each vertex in V-{1,k} exactly once.
the function which is used to find the path is
g(1,V-{1}) = min{ cij + g(j,s-{j})}
g(i,s) be the length of a shortest path starting at vertex i, going
through all vertices in S,and terminating at vertex 1.
the function g(1,v-{1}) is the length of an optimal tour.
1. Find g(i,) =ci1, 1<=i<n, hence we can use equation(2) to obtain g(i,s) for all s to size 1.
2. That we have to start with s=1,(ie) there will be only one vertex in set ‘s’.
3. Then s=2, and we have to proceed until |s| <n-1.
4. for example consider the graph.
10
15
10
15
20 8 9 13
8 6
12
7
Cost matrix
0 10 15 20
5 0 9 10
6 13 0 12
8 8 9 0
starting position
g(i,s) =min{cij +g(j,s-{j})
STEP 1:
g(1,{2,3,4})=min{c12+g(2{3,4}),c13+g(3,{2,4}),c14+g(4,{2,3})}
min{10+25,15+25,20+23}
min{35,35,43}
=35
STEP 2:
g(2,{3,4}) = min{c23+g(3{4}),c24+g(4,{3})}
min{9+20,10+15}
min{29,25}
=25
g(3,{2,4}) =min{c32+g(2{4}),c34+g(4,{2})}
min{13+18,12+13}
min{31,25}
=25
g(4,{2,3}) = min{c42+g(2{3}),c43+g(3,{2})}
min{8+15,9+18}
min{23,27}
=23
STEP 3:
1. g(3,{4}) = min{c34 +g{4,}}
12+8 =20
2. g(4,{3}) = min{c43 +g{3,}}
9+6 =15
3. g(2,{4}) = min{c24 +g{4,}}
10+8 =18
i =1 to n.
g(1,) = c11 => 0
g(2,) = c21 => 5
g(3,) = c31 => 6
g(4,) = c41 => 8
s =1
i =2 to 4
g(2,{3}) = c23 + g(3,)
= 9+6 =15
g(2,{4}) = c24 + g(4,)
= 10+8 =18
g(3,{2}) = c32 + g(2,)
= 13+5 =18
g(3,{4}) = c34 + g(4,)
= 12+8 =20
g(4,{2}) = c42 + g(2,)
= 8+5 =13
s =2
i 1, 1 s and i s.
g(2,{3,4}) = min{c23+g(3{4}),c24+g(4,{3})}
min{9+20,10+15}
min{29,25}
=25
g(3,{2,4}) =min{c32+g(2{4}),c34+g(4,{2})}
min{13+18,12+13}
min{31,25}
=25
g(4,{2,3}) = min{c42+g(2{3}),c43+g(3,{2})}
min{8+15,9+18}
min{23,27}
=23
s = 3
g(1,{2,3,4})=min{c12+g(2{3,4}),c13+g(3,{2,4}),c14+g(4,{2,3})}
min{10+25,15+25,20+23}
min{35,35,43}
=35
optimal cost is 35
All solutions are generated in X[1:n] and printed as soon as they are determined.
T(X[1]…..X[k-1]) is all possible values of X[k] gives that X[1],……..X[k-1] have already
been chosen.
Applications:
N-Queens problem.
Sum of subsets.
Graph coloring.
Hamiltonian cycle.
This 8 queens problem is to place n-queens in an ‘N*N’ matrix in such a way that no two queens
attack each otherwise no two queens should be in the same row, column, diagonal.
Solution:
The solution vector X (X1…Xn) represents a solution in which Xi is the column of the th
row
where I th queen is placed.
First, we have to check no two queens are in same row.
Second, we have to check no two queens are in same column.
The function, which is used to check these two conditions, is [I, X (j)], which gives position of
the I th queen, where I represents the row and X (j) represents the column position.
Third, we have to check no two queens are in it diagonal.
Consider two dimensional array A[1:n,1:n] in which we observe that every element on the
same diagonal that runs from upper left to lower right has the same value.
Also, every element on the same diagonal that runs from lower right to upper left has the same
value.
Suppose two queens are in same position (i,j) and (k,l) then two queens lie on the same
diagonal , if and only if |j-l|=|I-k|.
Initialize x array to zero and start by placing the first queen in k=1 in the first row.
To find the column position start from value 1 to n, where ‘n’ is the no. Of columns or no. Of
queens.
If k=1 then x (k)=1.so (k,x(k)) will give the position of the k th queen. Here we have to check
whether there is any queen in the same column or diagonal.
For this considers the previous position, which had already, been found out. Check whether
X (I)=X(k) for column |X(i)-X(k)|=(I-k) for the same diagonal.
If any one of the conditions is true then return false indicating that k th queen can’t be placed in
position X (k).
For not possible condition increment X (k) value by one and precede d until the position is
found.
If the position X (k) n and k=n then the solution is generated completely.
If k<n, then increment the ‘k’ value and find position of the next queen.
If the position X (k)>n then k th queen cannot be placed as the size of the matrix is ‘N*N’.
So decrement the ‘k’ value by one i.e. we have to back track and after the position of the
previous queen.
Algorithm:
Algorithm place (k,I)
//return true if a queen can be placed in k th row and I th column. otherwise it returns //
//false .X[] is a global array whose first k-1 values have been set. Abs® returns the //absolute value
of r.
{
For j=1 to k-1 do
If ((X [j]=I) //two in same column.
Or (abs (X [j]-I)=Abs (j-k)))
Then return false;
Return true;
}
Algorithm Nqueen (k,n)
//using backtracking it prints all possible positions of n queens in ‘n*n’ chessboard. So
//that they are non-tracking.
{
For I=1 to n do
{
If place (k,I) then
{
X [k]=I;
If (k=n) then write (X [1:n]);
Else nquenns(k+1,n) ;
}
}
}
Example: 4 queens.
Two possible solutions are
Solutin-1 Solution 2
(2 4 1 3) (3 1 4 2)
5.6.2 SUM OF SUBSETS:
1) We are given ‘n’ positive numbers called weights and we have to find all combinations of
these numbers whose sum is M. this is called sum of subsets problem.
2) If we consider backtracking procedure using fixed tuple strategy , the elements X(i) of the
solution vector is either 1 or 0 depending on if the weight W(i) is included or not.
3) If the state space tree of the solution, for a node at level I, the left child corresponds to X(i)=1
and
4) right to X(i)=0.
Example:
a. Given n=6,M=30 and W(1…6)=(5,10,12,13,15,18).We have to generate all possible
combinations of subsets whose sum is equal to the given value M=30.
b. In state space tree of the solution the rectangular node lists the values of s, k, r, where s
is the sum of subsets,’k’ is the iteration and ‘r’ is the sum of elements after ‘k’ in the
original set.
c. The state space tree for the given problem is,
S, n, r
0,1,73
X(1)=1 x(1)=0
5,2,68 0,2,68
X(4)=1 x(4)=0
X(4)=0
B
15,5,33 5,5,33 10,5,33
X(5)=1 x(5)=1
A 20,6,18
Ist solution is A -> 1 1 0 0 1 0
IInd solution is B -> 1 0 1 1 0 0
III rd solution is C -> 0 0 1 0 0 1
In the state space tree, edges from level ‘i’ nodes to ‘i+1’ nodes are labeled with the values of Xi,
which is either 0 or 1.
The left sub tree of the root defines all subsets containing Wi.
The right subtree of the root defines all subsets, which does not include Wi.
Assign X(k)<- 1.
If S+X(k)=M then we print the subset b’coz the sum is the required output.
If the above condition is not satisfied then we have to check S+X(k)+W(k+1)<=M. If so, we
have to generate the left sub tree. It means W(t) can be included so the sum will be incremented
and we have to check for the next k.
After generating the left sub tree we have to generate the right sub tree, for this we have to check
S+W(k+1)<=M.B’coz W(k) is omitted and W(k+1) has to be selected.
Repeat the process and find all the possible combinations of the subset.
Algorithm:
Algorithm sumofsubset(s,k,r)
{
//generate the left child. note s+w(k)<=M since Bk-1 is true.
X{k]=1;
If (S+W[k]=m) then write(X[1:k]); // there is no recursive call here as W[j]>0,1<=j<=n.
Else if (S+W[k]+W[k+1]<=m) then sum of sub (S+W[k], k+1,r- W[k]);
//generate right child and evaluate Bk.
If ((S+ r- W[k]>=m)and(S+ W[k+1]<=m)) then
{
X{k]=0;
sum of sub (S, k+1, r- W[k]);
}
}
Let ‘G’ be a graph and ‘m’ be a given positive integer. If the nodes of ‘G’ can be colored in such
a way that no two adjacent nodes have the same color. Yet only ‘M’ colors are used. So it’s
called M-color ability decision problem.
The graph G can be colored using the smallest integer ‘m’. This integer is referred to as chromatic
number of the graph.
A graph is said to be planar iff it can be drawn on plane in such a way that no two edges cross
each other.
Suppose we are given a map then, we have to convert it into planar. Consider each and every
region as a node. If two regions are adjacent then the corresponding nodes are joined by an edge.
4 5
2
1
1 is adjacent to 2, 3, 4.
2 is adjacent to 1, 3, 4, 5
3 is adjacent to 1, 2, 4
4 is adjacent to 1, 2, 3, 55 is adjacent to 2, 4
Steps to color the Graph:
1. First create the adjacency matrix graph(1:m,1:n) for a graph, if there is an edge between i,j then
C(i,j) = 1 otherwise C(i,j) =0.
2. The Colors will be represented by the integers 1,2,…..m and the solutions will be stored in the
array X(1),X(2), ............. ,X(n) ,X(index) is the color, index is the node.
3. He formula is used to set the color is,
X(k) = (X(k)+1) % (m+1)
4. First one chromatic number is assigned ,after assigning a number for ‘k’ node, we have to check
whether the adjacent nodes has got the same values if so then we have to assign the next value.
5. Repeat the procedure until all possible combinations of colors are found.
6. The function which is used to check the adjacent nodes and same color is,
If(( Graph (k,j) == 1) and X(k) = X(j))
Example:
1 2
4 3
N= 4
M= 3
Adjacency Matrix:
0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0
Algorithm:
Algorithm mColoring(k)
// the graph is represented by its Boolean adjacency matrix G[1:n,1:n] .All assignments //of
1,2,……….,m to the vertices of the graph such that adjacent vertices are assigned //distinct integers
are printed. ’k’ is the index of the next vertex to color.
{
repeat
{
// generate all legal assignment for X[k].
Nextvalue(k); // Assign to X[k] a legal color.
If (X[k]=0) then return; // No new color possible.
If (k=n) then // Almost ‘m’ colors have been used to color the ‘n’ vertices
Write(x[1:n]);
Else mcoloring(k+1);
}until(false);
}
Algorithm Nextvalue(k)
// X[1],……X[k-1] have been assigned integer values in the range[1,m] such that //adjacent values
have distinct integers. A value for X[k] is determined in the //range[0,m].X[k] is assigned the next
highest numbers color while maintaining //distinctness form the adjacent vertices of vertex K. If
no such color exists, then X[k] is 0.
{
repeat
{
X[k] = (X[k]+1)mod(m+1); // next highest color.
If(X[k]=0) then return; //All colors have been used.
For j=1 to n do
{
// Check if this color is distinct from adjacent color.
If((G[k,j] 0)and(X[k] = X[j]))
// If (k,j) is an edge and if adjacent vertices have the same color.
Then break;
}
if(j=n+1) then return; //new color found.
} until(false); //otherwise try to find another color.
}