Ads Unit-5
Ads Unit-5
Dynamic Programming: General method, applications- 0/1 knapsack problem, All pairs
shortest path problem, Travelling salesperson problem, Reliability design.
Backtracking: General method, applications-n-queen problem, sum of subsets problem, graph
coloring, Hamiltonian cycles.
Introduction to NP-Hard and NP-Complete problems: Basic Concepts
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
101 @PkeLibrary
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)]
102 @PkeLibrary
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
103 @PkeLibrary
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 7 2 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
104 @PkeLibrary
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
93 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.
105 @PkeLibrary
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= 0 1 0 0
0100
Since,p[1,3]=4,the shortest path from 1 to3 passes through 4.
Looking now at p[1,4]&p[4,3] we discover that between 1 & 4, we have to go to node 2 but
that from 4 to 3 we proceed directly.
Finally we see the trips from 1 to 2, & from 2 to 4, are also direct.
The shortest path from 1 to 3 is 1,2,4,3.
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 ]
106 @PkeLibrary
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.
107 @PkeLibrary
PROCEDURE:
V1 V2 V3 V4 V5
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
108 @PkeLibrary
Cost (10)=2 D (10)=12
Cost ( 9)=4 D ( 9)=12
1. For forward approach,
109 @PkeLibrary
= min(9,12)
=9
cost(3) = 9 =>D(3) = 6
cost(2) = min (c (2,6) + cost(6),c (2,7) +cost(7) ,c (2,8) +cost(8))
= min(4+7 , 2+5 , 1+7 )
= min(11,7,8)
=7
cost(2) = 7 =>D(2) = 7
cost(1) = min (c (1,2)+cost(2) ,c (1,3)+cost(3) ,c (1,4)+cost(4) ,c(1,5)+cost(5))
= min(9+7 , 7 +9 , 3+18 , 2+15)
= min(16,16,21,17)
= 16
cost(1) = 16 =>D(1) = 2
The path through which you have to find the shortest distance.
(i.e.)
9 2 3 2
110 @PkeLibrary
cost[n]=0.0;
for j=n-1 to 1 step-1 do
{
//compute cost[j],
// let ‘r’ be the vertex such that <j,r> is an edge of ‘G’ &
// c[j,r]+cost[r] is minimum.
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
111 @PkeLibrary
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
112 @PkeLibrary
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]];
}
113 @PkeLibrary
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.
114 @PkeLibrary
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
115 @PkeLibrary
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
116 @PkeLibrary
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
117 @PkeLibrary
optimal cost is 35
Input:
A system composed of several devices in serial
Each device (D) has a fixed reliability rating (r)
Multiple copies of the same device can be used in parallel to increase reliability
Output:
A system with highest reliability rating subject to a cost constraint
D1 D2 Di
Dn
D1 Di
D2
D1 Di
Di
Di
118 @PkeLibrary
Comparison
A knapsack of Total expenditure of C
capacity C Stages of cost ci and
Objects of size ci and reliability ri
profit pi Construct a system
Fill up the knapsack with 1 or more copies
with 0 or 1 copy of i of i
Maximize profit Maximize reliability
n
C c j
u 1 j 1
i c
i
How to build solutions recursively?
One stage and one device at a time
How does principle of optimality apply?
m1m 2 m m n
i
(1 , x x x ):( x x x ) m u s t b e o p t i m a l f o r C c1
(2, y y y ):( y y y ) m u s t b e o pt i m a l f o r C 2 c1
(0.9,65) (0.99,95)
1 (1 0.8) 0.8
1 (1 0.8 )2 0.96
1 (1 0.8 )3 0.992
1 (1 0.5) 0.5
1 (1 0.5) 2 0.75
1 (1 0.5 )3 0.875
119 @PkeLibrary
5.6 BACKTRACKING
120 @PkeLibrary
{
if (there remains all untried
X[k] T (X[1],[2],…..X[k-1]) and Bk (X[1],…..X[k])) is true ) then
{
if(X[1],……X[k] )is the path to the answer node)
Then write(X[1:k]);
k=k+1; //consider the next step.
}
else k=k-1; //consider backtracking to the previous set.
}
}
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.
121 @PkeLibrary
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.
122 @PkeLibrary
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)
123 @PkeLibrary
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
124 @PkeLibrary
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.
125 @PkeLibrary
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
126 @PkeLibrary
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
127 @PkeLibrary
State Space Tree:
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
128 @PkeLibrary
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.
}
Let G=(V,E) be a connected graph with ‘n’ vertices. A HAMILTONIAN CYCLE is a round
trip path along ‘n’ edges of G which every vertex once and returns to its starting position.
If the Hamiltonian cycle begins at some vertex V1 belongs to G and the vertex are visited in
the order of V1,V2…….Vn+1,then the edges are in E,1<=I<=n and the Vi are distinct except
V1 and Vn+1 which are equal.
Consider an example graph G1.
129 @PkeLibrary
1 2 3 4
8 7 6 5
->1,3,4,5,6,7,8,2,1 and
->1,2,8,7,6,5,4,3,1.
The backtracking algorithm helps to find Hamiltonian cycle for any type of graph.
Procedure:
Define a solution vector X(Xi……..Xn) where Xi represents the I th visited vertex of the
proposed cycle.
Create a cost adjacency matrix for the given graph.
The solution array initialized to all zeros except X(1)=1,b’coz the cycle should start at vertex
‘1’.
Now we have to find the second vertex to be visited in the cycle.
The vertex from 1 to n are included in the cycle one by one by checking 2 conditions,
1.There should be a path from previous visited vertex to current vertex.
2.The current vertex must be distinct and should not have been visited earlier.
6. When these two conditions are satisfied the current vertex is included in the cycle, else the
next vertex is tried.
7. When the nth vertex is visited we have to check, is there any path from nth vertex to first
vertex. if no path, the go back one step and after the previous visited node.
8. Repeat the above steps to generate possible Hamiltonian cycle.
130 @PkeLibrary
If (x (k)=0) then return;
{
If k=n then
Print (x)
Else
Hamiltonian (k+1);
End if
}
Repeat
}
Algorithm Nextvalue (k)
{
Repeat
{
X [k]=(X [k]+1) mod (n+1); //next vertex
If (X [k]=0) then return;
If (G [X [k-1], X [k]] 0) then
{
For j=1 to k-1 do if (X [j]=X [k]) then break;
// Check for distinction.
If (j=k) then //if true then the vertex is distinct.
If ((k<n) or ((k=n) and G [X [n], X [1]] 0)) then return;
}
} Until (false);
}
Tractability: Some problems are tractable: that is the problems are solvable in reasonable
amount of time called polynomial time. Some problems are intractable: that is as problem grow
large, we are unable to solve them in reasonable amount of time called polynomial time.
131 @PkeLibrary
Polynomial Time Complexity: An algorithm is of Polynomial Complexity, if there exists a
polynomial p() such that the computing time is O(p(n)) for every input size of ‘n’. Polynomial
time is the worst-case running time required to an algorithm to process an input of size n the is
O(nk) for some constant k
132 @PkeLibrary
in NP can be reducible (solvable) using non-deterministic algorithm in polynomial time.
The class of problems which are NP-hard and belong to NP.
The NP-Complete problems are always decision problems only.
Example : TSP, Vertex covering problem
Nondeterministic Algorithms:
Deterministic Algorithms:
• Let A be an algorithm to solve problem P. A is called deterministic if it has only one choice in
each step throughout its execution. Even if we run algorithm A again and again, there is no
change in output.
• Deterministic algorithms are identified with uniquely defined results in terms of output for a
certain input.
Nondeterministic Algorithms:
• Let A be a nondeterministic algorithm for a problem P. We say that algorithm A accepts an
instance of P if and only if, there exists a guess that leads to a yes answer.
• In non deterministic algorithms, there is no uniquely defined result in terms of output for a
133 @PkeLibrary
certain input.
• Nondeterministic algorithms are allowed to contain operations whose outcomes are limited to a
given set of instances of P, instead of being uniquely defined results.
• A Non-deterministic algorithm A on input x consists of two phases:
• The Nondeterministic algorithm uses three basic procedures, whose time complexity is O(1).
Nondeterministic Sort Algorithm: The following algorithm sorts ‘n’ positive integers in non-
decreasing order and produces output in sorted order. The array B[] is an auxiliary array initialized to
0 and is used for convenience.
Algorithm nd_sort ( A, n )
{
for ( i = 0; i < n; B[i++] = 0; );
for ( i = 0; i < n; i++ )
{
j = choice ( 0, n - 1 );
if ( B[j] != 0 ) failure();
B[j] = A[i];
}
// Verify order
for ( i = 0; i < n-1; i++ )
if ( B[i] > B[i+1] ) failure();
write ( B );
success();
}
The time complexity of nd_sort is O(n). Best-known deterministic sorting algorithm like binary
search has a complexity of (n log n).
Satisfiability problem is to determine whether a formula is true for some assignment of truth
values to the variables
CNF--satisfiability is the satisfiability problem for CNF formulas
DNF--satisfiability is the satisfiability problem for DNF formulas
Polynomial time nondeterministic algorithm that terminates successfully iff a given
propositional formula E(x1, . . . , xn) is satisfiable
Non deterministically choose one of the 2n possible assignments of truth values to (x1, . . . , xn)
and verify that E(x1, . . . , xn) is true for that assignment
Algorithm eval ( E, n )
{
// Determine whether the propositional formula E is satisfiable.
Here variable are x1, x2, ..., xn
for ( i = 1; i <= n; i++ )
x(i) = choice ( true, false );
if ( E ( x1, ..., xn ) )
success();
else
failure();
}
136
@PkeLibrary
Decision Problem Vs Optimization Problem:
Decision Problem and Algorithm : Any problem for which the answer is either zero or one is called
a decision problem. An algorithm for a decision problem is termed a decision algorithm.
A decision algorithm will output 0 or 1
Implicit in the signals success() and failure()
Output from a decision algorithm is uniquely defined by input parameters and algorithm
specification.
Optimization Problem and Algorithm: Any problem that involves the identification of an optimal
(either minimum or maximum) value of a given cost function is known as an optimization problem.
An optimization algorithm is used to solve an optimization problem.
An optimization problem may have many feasible solutions
The problem is to find out the feasible solution with the best associated value
NP-completeness applies directly not to optimization problems but to decision problems.
Maximal Clique:
Clique is a maximal complete sub-graph of a graph G = (V,E), that is a subset of vertices in V all
connected to each other by edges in E (i.e., forming a complete graph).
137
@PkeLibrary
Example:
The Size of a clique is the number of vertices in it. The Maximal clique problem is an optimization
problem that has to determine the size of a largest clique in G. A decision problem is to determine
whether G has a clique of size at least ‘k’.
Input for Maximal clique problem: Input can be provided as a sequence of edges. Each edge in
E(G) is a pair of vertices (i, j) .The size of input for each edge (i, j) in binary representation is
Let us denote the deterministic decision algorithm for the clique decision problem as dclique(G, k).
If |V | = n, then the size of a maximal clique can be found by
for ( k = n; dclique ( G, k ) != 1; k-- );
If time complexity of dclique is f(n), size of maximal clique can be found in time
g(n) <= n.f(n). Therefore, the decision problem can be solved in time g(n)
Note that Maximal clique problem can be solved in polynomial time if and only if the clique
decision problem can be solved in polynomial time.
138
@PkeLibrary
Non deterministic Clique Algorithm:
Algorithm DCK(G, n, k)
{
S=0; // Empty set.
for i=1 to k do
{
t = Choice(1,n);
if t ε S then Failure();
S= SU{ t };
}
for all pairs (i,j) such that i ε S, j ε S and i!=j do
if (i, j) is not an edge of G then Failure();
Success();
}
The for loop selects or discards each of the n items It also re-computes the total weight and
profit corresponding to the selection
The if statement checks to see the feasibility of assignment and whether the profit is above a
lower bound r
The time complexity of the algorithm is O(n) . If the input length is q in binary, then O(q).
Algorithm nd_knapsack ( p, w, n, m, r, x )
{
W = 0; P = 0;
for ( i = 1; i <= n; i++ )
{
x[i] = choice ( 0, 1 );
W += x[i] * w[i];
139
@PkeLibrary
P += x[i] * p[i];
}
if ( ( W > m ) || ( P < r ) )
failure();
else
success();
}
Algorithm Sum_of_subsets ( A, n, m )
{
// A[n] is an array of integers
s = 1 // s is an m+1 bit word
// bit 0 is always 1
for i = 1 to n
s |= ( s << A[i] ) // shift s left by A[i] bits
if bit m in s is 1
write ( "A subset sums to m" );
else
write ( "No subset sums to m" );
}
140
@PkeLibrary
5.12. COOK’S THEOREM:
We know that, Class P problems are the set of all decision problems solvable by deterministic
algorithms in polynomial time. Similarly Class NP problems are set of all decision problems
solvable by nondeterministic algorithms in polynomial time.
Since deterministic algorithms are a special case of nondeterministic algorithms, P NP
Cook formulated the following question: Is there any single problem in NP such that if we showed
it to be in P, then that would imply that P = NP? This led to Cook’s theorem as :
3. Write the difference between the Greedy method and Dynamic programming.
Greedy method
1. Only one sequence of decision is generated.
2. It does not guarantee to give an optimal solution always.
Dynamic programming
1. Many number of decisions are generated.
2. It definitely gives an optimal solution always.
142
@PkeLibrary
other by being in the same row or in the same column or in the same diagonal.
7. Define Backtracking
Ans: Backtracking is used to solve problems with tree structures. Even problems seemingly
remote to trees such as a walking a maze are actually trees when the decision \'back-left-straight-
right\' is considered a node in a tree. The principle idea is to construct solutions one component at
a time and evaluate such partially constructed candidates
143
@PkeLibrary
13. Define Knapsack Problem
Ans: Given n items of known weight wi and values vi=1,2,..,n and a knapsack of capacity w, find
the most valuable subset of the items that fit in the knapsack.
145
@PkeLibrary