Design and Analysis of Algorithm

Total Page:16

File Type:pdf, Size:1020Kb

Design and Analysis of Algorithm

UNIT - III

Backtracking:

The general method—8 queens problem—Sum of subsets—Graph coloring— Hamiltonian cycle—Knapsack problem.

BACKTRACKING

 It is one of the most general algorithm design techniques.

 Many problems which deal with searching for a set of solutions or for a optimal solution satisfying some constraints can be solved using the backtracking formulation.

 To apply backtracking method, tne desired solution must be expressible as an n- tuple (x1…xn) where xi is chosen from some finite set Si.

 The problem is to find a vector, which maximizes or minimizes a criterion function P(x1….xn).

 The major advantage of this method is, once we know that a partial vector (x1,… xi) will not lead to an optimal solution that (mi+1………..mn) possible test vectors may be ignored entirely.

 Many problems solved using backtracking require that all the solutions satisfy a complex set of constraints.

 These constraints are classified as:

i) Explicit constraints. ii) Implicit constraints.

1) Explicit constraints: Explicit constraints are rules that restrict each Xi to take values only from a given set. Some examples are, Xi 0 or Si = {all non-negative real nos.} Xi =0 or 1 or Si={0,1}. Li  Xi Ui or Si= {a: Li a  Ui}

 All tupules that satisfy the explicit constraint define a possible solution space for I.

2) Implicit constraints:

1 The implicit constraint determines which of the tuples in the solution space I can actually satisfy the criterion functions.

Algorithm:

Algorithm IBacktracking (n) // This schema describes the backtracking procedure .All solutions are generated in X[1:n] //and printed as soon as they are determined. { k=1; While (k  0) do { 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.

 Bk(X[1]………X[k]) is a boundary function which determines the elements of X[k] which satisfies the implicit constraint.

 Certain problems which are solved using backtracking method are,

1. Sum of subsets. 2. Graph coloring. 3. Hamiltonian cycle. 4. N-Queens problem.

SUM OF SUBSETS:

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

 If the state space tree of the solution, for a node at level I, the left child corresponds to X(i)=1 and right to X(i)=0.

Example:

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

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

 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(2)=1 x(2)=0 x(2)=1 x(2)=0 5,3,58 5,3,58 10,3,58 0,3,58

X(3)=1 x(3)=0 x(3)=1 x(3)=0 27,4,46 15,4,46 17,4,46 5,4,4 10,4,46 C

X(4)=1 x(4)=0 X(4)=0 15,5,33 B 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

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

GENERATION OF STATE SPACE TREE:

 Maintain an array X to represent all elements in the set.

 The value of Xi indicates whether the weight Wi is included or not.

 Sum is initialized to 0 i.e., s=0.

 We have to check starting from the first node.

 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]);

4 } }

HAMILTONIAN CYCLES:

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

1 2 3 4

8 7 6 5

The graph G1 has Hamiltonian cycles:

->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:

1. Define a solution vector X(Xi……..Xn) where Xi represents the I th visited vertex of the proposed cycle.

2. Create a cost adjacency matrix for the given graph.

3. The solution array initialized to all zeros except X(1)=1,b’coz the cycle should start at vertex ‘1’.

4. Now we have to find the second vertex to be visited in the cycle. 5. The vertex from 1 to n are included in the cycle one by one by checking 2 conditions,

5 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 8vertex. if no path, the go back one step and after the previous visited node.

8. Repeat the above steps to generate possible Hamiltonian cycle.

Algorithm:(Finding all Hamiltonian cycle)

Algorithm Hamiltonian (k) { Loop Next value (k) 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

8-QUEENS PROBLEM:

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

STEPS TO GENERATE THE SOLUTION:

 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

7  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

Q Q Q Q

Q Q

Q Q

Solutin-1 Solution 2

8 (2 4 1 3) (3 1 4 2)

GRAPH COLORING:

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

Consider a map with five regions and its graph.

4 5

2 1 3

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, 5 5 is adjacent to 2, 4

1

2 3

5 4 A Steps to color the Graph:l g o r i 9 t h m

m C o l o r i n g ( k ) / / t h e g r a p h

i s r e p r e s e n t e d

b y

i t s B o o l e a n

a d j a c e n c y

m a t r i x

G [ 1 : n , 1 : n ]

. A l l a s s i g n m e n t s / / o f 1 , 2 , … … … . , m

t o

t h e v e r t i c e s o f t h e g r a p h

s u c h

t h a t a d j a c e n c t v e r t i c e s a r e a s s i g n r d

/ / d i s t i n c t i n t e g e r s a r e p r i n t e d . ’ k ’ i s t h e i n d e x

o f t h e n e x t v e r t e x

t o

c o l o r .

{

r e p e a t {

/ / g e n e r a t e a l l l e g a l a s s i g n m e n t f o r X [ k ] .

N e x t v a l u e ( k ) ;

/ / A s s i g n

t o

X [ k ] a l e g a l c o l o r .

I f ( X [ k ] = 0 ) t h e n

r e t u r n ;

/ / N o

n e w

c o l o r p o s s i b l e .

I f ( k = n ) t h e n

/ / A l m o s t ‘ m ’ c o l o r s h a v e b e e n

u s e d

t o

c o l o r t h e ‘ n ’ v e r t i c e s

W r i t e ( x [ 1 : n ] ) ;

E l s e m c o l o r i n g ( k + 1 ) ; } u n t i l ( f a l s e ) ; }

A l g o r i t h m

N e x t v a l u e ( k ) / / X [ 1 ] , … … X [ k - 1 ] h a v e b e e n

a s s i g n e d

i n t e g e r v a l u e s i n

t h e r a n g e [ 1 , m ] s u c h

t h a t / / a d j a c e n t v a l u e s h a v e d i s t i n c t i n t e g e r s . A

v a l u e f o r X [ k ] i s d e t e r m i n e d

i n

t h e / / r a n g e [ 0 , m ] . X [ k ] i s a s s i g n e d

t h e n e x t h i g h e s t n u m b e r e s c o l o r w h i l e m a i n t a i n i n g

/ / d i s t i n c t n e s s f o r m

t h e a d j a c e n t v e r t i c e s o f v e r t e x

K . I f n o

s u c h

c o l o r e x i s t s , t h e n

X [ k ] i s 0 .

{

r e p e a t

{

X [ k ] =

( X [ k ] + 1 ) m o d ( m + 1 ) ;

/ / n e x t h i g h e s t c o l o r .

I f ( X [ k ] = 0 ) t h e n

r e t u r n ;

/ / A l l c o l o r s h a v e b e e n

u s e d .

F o r j = 1

t o

n

d o

{

/ / C h e c k

i f t h i s c o l o r i s d i s t i n c t f r o m

a d j a c e n t c o l o r .

I f ( ( G [ k , j ]

0 ) a n d ( X [ k ] =

X [ j ] ) )

/ / I f ( k , j ) i s a n

e d g e a n d

i f a d j a c e n t v e r t i c e s h a v e t h e s a m e c o l o r .

T h e n

b r e a k ;

}

i f ( j = n + 1 ) t h e n

r e t u r n ;

/ / n e w

c o l o r f o u n d .

}

u n t i l ( f a l s e ) ;

/ / o t h e r w i s e t r y

t o

f i n d

a n o t h e r c o l o r . }

T h e t i m e s p e n t b y

N e x t v a l u e t o

d e t e r m i n e t h e c h i l d r e n

i s ( m n )  T o t a l t i m e i s =

( m n n ) .

K n a p s a c k

P r o b l e m

u s i n g

B a c k t r a c k i n g :

T h e p r o b l e m

i s s i m i l a r t o

t h e z e r o - o n e ( 0 / 1 ) k n a p s a c k

o p t i m i z a t i o n

p r o b l e m

i s d y n a m i c p r o g r a m m i n g

a l g o r i t h m .

W e a r e g i v e n

‘ n ’ p o s i t i v e w e i g h t s W i a n d ’ n ’ p o s i t i v e p r o f i t s P i , a n d

a p o s i t i v e n u m b e r ‘ m ’ t h a t i s t h e k n a p s a c k

c a p a c i t y , t h g i s p r o b l e m

c a l l s f o r c h o s s i n g

a s u b s e t o f t h e w e i g h t s s u c h

t h a t ,

m

a n d

i s M a x i m i z e d .

X i  C o n s t i t u t e Z e r o - o n e v a l u e d

V e c t o r .

T h e S o l u t i o n

s p a c e i s t h e s a m e a s t h a t f o r t h e s u m

o f s u b s e t ’ s p r o b l e m .

B o u n d i n g

f u n c t i o n s a r e n e e d e d

t o

h e l p

k i l l s o m e l i v e n o d e s w i t h o u t e x p a n d i n g

t h e m . A

g o o d

b o u n d i n g

f u n c t i o n

f o r t h i s p r o b l e m

i s o b t a i n e d

b y

u s i n g

a n

u p p e r b o u n d

o n

t h e v a l u e o f t h e b e s t f e a s i b l e s o l u t i o n

o b t a i n a b l e b y

e x p a n d i n g

t h e g i v e n

l i v e n o d e .

T h e p r o f i t s a n d

w e i g h t s a r e a s s i g n e d

i n

d e s c e n d i n g

o r d e r d e p e n d

u p o n

t h e r a t i o .

( i . e ) P i / W i

P ( I + 1 ) / W ( I + 1 )

S o l u t i o n

:

A f t e r a s s i g n i n g

t h e p r o f i t a n d

w e i g h t s , w e h a v e t o

t a k e t h e f i r s t o b j e c t w e i g h t s a n d

c h e c k

i f t h e f i r s t w e i g h t i s l e s s t h a n

o r e q u a l t o

t h e c a p a c i t y , i f s o

t h e n

w e i n c l u d e t h a t o b j e c t ( i . e ) t h e u n i t i s 1 . ( i . e ) K 

1 .

T h e n

W e a r e g o i n g

t o

t h e n e x t o b j e c t , i f t h e o b j e c t w e i g h t i s e x c e e d e d

t h a t o b j e c t d o e s n a t f i t . S o

u n i t o f t h a t o b j e c t i s ‘ 0 ’ . ( i . e ) K = 0 .

T h e n

W e a r e g o i n g

t o

t h e b o u n d i n g

f u n c t i o n

, t h i s f u n c t i o n

d e t e r m i n e s a n

u p p e r b o u n d

o n

t h e b e s t s o l u t i o n

b t a i n a l \ b l e a t l e v e l K + 1 .

R e p e a t t h e p r o c e s s u n t i l w e r e a c h

t h e o p t i m a l s o l u t i o n .

A l g o r i t h m :

A l g o r i t h m

B k n a p ( k , c p , c w ) / / ‘ m ’ i s t h e s i z e o f t h e k n a p s a c k ;

‘ n ’ 

n o . o f w e i g h t s &

p r o f i t s . W [ ] & P [ ] a r e t h e / / w e i g h t s &

w e i g h t s . P [ I ] / W [ I ]

P [ I + 1 ] / W [ I + 1 ] . / / f w  F i n a l w e i g h t s o f k n a p s a c k . / / f p 

f i n a l m a x . p r o f i t . / / x [ k ] =

0

i f W [ k ] i s n o t t h e k n a p s a c k , e l s e X [ k ] = 1 . {

/ / G e n e r a t e l e f t c h i l d .

I f ( ( W + W [ k ]

m ) t h e n

{

Y [ k ] = 1 ;

I f ( k < n ) t h e n

B n a p ( k + 1 , c p + P [ k ] , C w

+ W [ k ] )

I f ( ( C p

+

p [ w ] >

f p ) a n d

( k = n ) ) t h e n

{

f p

=

c p

+

P [ k ] ;

f w

=

C w + W [ k ] ;

f o r j = 1

t o

k

d o

X [ j ] =

Y [ j ] ;

}

}

i f ( B o u n d ( c p , c w , k ) f p ) t h e n

{

y [ k ] =

0 ;

i f ( k < n ) t h e n

B n a p

( K + 1 , c p , c w ) ;

i f ( ( c p > f p ) a n d

( k = n ) ) t h e n

{

f p

=

c p ;

f w

=

c w ;

f o r j = 1

t o

k

d o

X [ j ] =

Y [ j ] ;

} } }

A l g o r i t h m

f o r B o u n d i n g

f u n c t i o n :

A l g o r i t h m

B o u n d ( c p , c w , k ) / / c p 

c u r r e n t p r o f i t t o t a l . / / c w 

c u r r e n t w e i g h t t o t a l . / / k  t h e i n d e x

o f t h e l a s t r e m o v e d

i t e m . / / m  t h e k n a p s a c k

s i z e .

{

b = c p ;

c = c w ;

f o r I = - k + 1

t o

n

d o

{

c =

c + w [ I ] ;

i f ( c < m ) t h e n

b = b + p [ I ] ;

e l s e r e t u r n

b +

( 1 - ( c - m ) / W [ I ] ) *

P [ I ] ; } r e t u r n

b ; }

E x a m p l e :

M =

6

W i =

2 , 3 , 4

4

2

2

N =

3

P i =

1 , 2 , 5

P i / W i ( i . e )

5

2

1

X i =

1

0

1 T h e m a x i m u m

w e i g h t i s 6 T h e M a x i m u m

p r o f i t i s ( 1 * 5 ) +

( 0 * 2 ) +

( 1 * 1 )

5 + 1

6 .

F p

=

( - 1 )

1

3

&

0 + 4

6

c w

=

4 , c p

=

5 , y ( 1 ) = 1

k

=

k + 2

2

3

b u t 7 > 6

s o

y ( 2 ) =

0

S o

b o u n d ( 5 , 4 , 2 , 6 )

B = 5

C = 4

I = 3

t o

3 C = 6 6

6 S o

r e t u r n

5 + ( 1 - ( 6 - 6 ) ) / ( 2 * 1 )

5 . 5

i s n o t l e s s t h a n

f p .

S o , k = k + 1

( i . e ) 3 .

3 = 3

&

4 + 2

6

c w =

6 , c p

=

6 , y ( 3 ) = 1 .

K = 4 .

I f 4 >

3

t h e n

F p

= 6 , f w = 6 , k = 3

, x ( 1 ) 1

0

1

T h e s o l u t i o n

X i 

1

0

1

P r o f i t 

6

W e i g h t  6 . 4 A l g o r i t h m

m C o l o  First create the radjacency matrix graph(1:m,1:n) for a graph, if there is an edge between i,j then iC(i,j) = 1 otherwise C(i,j) =0. n  The Colors will gbe 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. k )  He formula is used/ to set the color is, X(k) = (X(k)+1)/ % (m+1) t  First one chromatich number is assigned ,after assigning a number for ‘k’ node, we have to check whethere the adjacent nodes has got the same values if so then we have to assign theg next value. r  Repeat the procedurea until all possible combinations of colors are found. p  The function whichh is used to check the adjacent nodes and same color is, If(( Graph (k,j) == 1) and X(k) = X(j)) i Example: s r e 1 p 2 r e s e 4 3 n N= 4 t M= 3 e d Adjacency Matrix: b 0 1 0 1 y 1 0 1 0 0 1 0 1 i 1 0 1 0 t s  Problem is to color theB given graph of 4 nodes using 3 colors. o Node-1 can take the giveno graph of 4 nodes using 3 colors. l  The state space tree wille give all possible colors in that ,the numbers which are inside the circles are nodes ,anda the branch with a number is the colors of the nodes. n State Space Tree: a d j a 10 c e n c y

m a t r i x

G [ 1 : n , 1 : n ]

. A l l a s s i g n m e n t s / / o f 1 , 2 , … … … . , m

t o

t h e v e r t i c e s o f t h e g r a p h

s u c h

t h a t a d j a c e n c t v e r t i c e s a r e a s s i g n r d

/ / d i s t i n c t i n t e g e r s a r e p r i n t e d . ’ k ’ i s t h e i n d e x

o f t h e n e x t v e r t e x

t o

c o l o r .

{

r e p e a t {

/ / g e n e r a t e a l l l e g a l a s s i g n m e n t f o r X [ k ] .

N e x t v a l u e ( k ) ;

/ / A s s i g n

t o

X [ k ] a l e g a l c o l o r .

I f ( X [ k ] = 0 ) t h e n

r e t u r n ;

/ / N o

n e w

c o l o r p o s s i b l e .

I f ( k = n ) t h e n

/ / A l m o s t ‘ m ’ c o l o r s h a v e b e e n

u s e d

t o

c o l o r t h e ‘ n ’ v e r t i c e s

W r i t e ( x [ 1 : n ] ) ;

E l s e m c o l o r i n g ( k + 1 ) ; } u n t i l ( f a l s e ) ; }

A l g o r i t h m

N e x t v a l u e ( k ) / / X [ 1 ] , … … X [ k - 1 ] h a v e b e e n

a s s i g n e d

i n t e g e r v a l u e s i n

t h e r a n g e [ 1 , m ] s u c h

t h a t / / a d j a c e n t v a l u e s h a v e d i s t i n c t i n t e g e r s . A

v a l u e f o r X [ k ] i s d e t e r m i n e d

i n

t h e / / r a n g e [ 0 , m ] . X [ k ] i s a s s i g n e d

t h e n e x t h i g h e s t n u m b e r e s c o l o r w h i l e m a i n t a i n i n g

/ / d i s t i n c t n e s s f o r m

t h e a d j a c e n t v e r t i c e s o f v e r t e x

K . I f n o

s u c h

c o l o r e x i s t s , t h e n

X [ k ] i s 0 .

{

r e p e a t

{

X [ k ] =

( X [ k ] + 1 ) m o d ( m + 1 ) ;

/ / n e x t h i g h e s t c o l o r .

I f ( X [ k ] = 0 ) t h e n

r e t u r n ;

/ / A l l c o l o r s h a v e b e e n

u s e d .

F o r j = 1

t o

n

d o

{

/ / C h e c k

i f t h i s c o l o r i s d i s t i n c t f r o m

a d j a c e n t c o l o r .

I f ( ( G [ k , j ]

0 ) a n d ( X [ k ] =

X [ j ] ) )

/ / I f ( k , j ) i s a n

e d g e a n d

i f a d j a c e n t v e r t i c e s h a v e t h e s a m e c o l o r .

T h e n

b r e a k ;

}

i f ( j = n + 1 ) t h e n

r e t u r n ;

/ / n e w

c o l o r f o u n d .

}

u n t i l ( f a l s e ) ;

/ / o t h e r w i s e t r y

t o

f i n d

a n o t h e r c o l o r . }

T h e t i m e s p e n t b y

N e x t v a l u e t o

d e t e r m i n e t h e c h i l d r e n

i s ( m n )  T o t a l t i m e i s =

( m n n ) .

K n a p s a c k

P r o b l e m

u s i n g

B a c k t r a c k i n g :

T h e p r o b l e m

i s s i m i l a r t o

t h e z e r o - o n e ( 0 / 1 ) k n a p s a c k

o p t i m i z a t i o n

p r o b l e m

i s d y n a m i c p r o g r a m m i n g

a l g o r i t h m .

W e a r e g i v e n

‘ n ’ p o s i t i v e w e i g h t s W i a n d ’ n ’ p o s i t i v e p r o f i t s P i , a n d

a p o s i t i v e n u m b e r ‘ m ’ t h a t i s t h e k n a p s a c k

c a p a c i t y , t h g i s p r o b l e m

c a l l s f o r c h o s s i n g

a s u b s e t o f t h e w e i g h t s s u c h

t h a t ,

m

a n d

i s M a x i m i z e d .

X i  C o n s t i t u t e Z e r o - o n e v a l u e d

V e c t o r .

T h e S o l u t i o n

s p a c e i s t h e s a m e a s t h a t f o r t h e s u m

o f s u b s e t ’ s p r o b l e m .

B o u n d i n g

f u n c t i o n s a r e n e e d e d

t o

h e l p

k i l l s o m e l i v e n o d e s w i t h o u t e x p a n d i n g

t h e m . A

g o o d

b o u n d i n g

f u n c t i o n

f o r t h i s p r o b l e m

i s o b t a i n e d

b y

u s i n g

a n

u p p e r b o u n d

o n

t h e v a l u e o f t h e b e s t f e a s i b l e s o l u t i o n

o b t a i n a b l e b y

e x p a n d i n g

t h e g i v e n

l i v e n o d e .

T h e p r o f i t s a n d

w e i g h t s a r e a s s i g n e d

i n

d e s c e n d i n g

o r d e r d e p e n d

u p o n

t h e r a t i o .

( i . e ) P i / W i

P ( I + 1 ) / W ( I + 1 )

S o l u t i o n

:

A f t e r a s s i g n i n g

t h e p r o f i t a n d

w e i g h t s , w e h a v e t o

t a k e t h e f i r s t o b j e c t w e i g h t s a n d

c h e c k

i f t h e f i r s t w e i g h t i s l e s s t h a n

o r e q u a l t o

t h e c a p a c i t y , i f s o

t h e n

w e i n c l u d e t h a t o b j e c t ( i . e ) t h e u n i t i s 1 . ( i . e ) K 

1 .

T h e n

W e a r e g o i n g

t o

t h e n e x t o b j e c t , i f t h e o b j e c t w e i g h t i s e x c e e d e d

t h a t o b j e c t d o e s n a t f i t . S o

u n i t o f t h a t o b j e c t i s ‘ 0 ’ . ( i . e ) K = 0 .

T h e n

W e a r e g o i n g

t o

t h e b o u n d i n g

f u n c t i o n

, t h i s f u n c t i o n

d e t e r m i n e s a n

u p p e r b o u n d

o n

t h e b e s t s o l u t i o n

b t a i n a l \ b l e a t l e v e l K + 1 .

R e p e a t t h e p r o c e s s u n t i l w e r e a c h

t h e o p t i m a l s o l u t i o n .

A l g o r i t h m :

A l g o r i t h m

B k n a p ( k , c p , c w ) / / ‘ m ’ i s t h e s i z e o f t h e k n a p s a c k ;

‘ n ’ 

n o . o f w e i g h t s &

p r o f i t s . W [ ] & P [ ] a r e t h e / / w e i g h t s &

w e i g h t s . P [ I ] / W [ I ]

P [ I + 1 ] / W [ I + 1 ] . / / f w  F i n a l w e i g h t s o f k n a p s a c k . / / f p 

f i n a l m a x . p r o f i t . / / x [ k ] =

0

i f W [ k ] i s n o t t h e k n a p s a c k , e l s e X [ k ] = 1 . {

/ / G e n e r a t e l e f t c h i l d .

I f ( ( W + W [ k ]

m ) t h e n

{

Y [ k ] = 1 ;

I f ( k < n ) t h e n

B n a p ( k + 1 , c p + P [ k ] , C w

+ W [ k ] )

I f ( ( C p

+

p [ w ] >

f p ) a n d

( k = n ) ) t h e n

{

f p

=

c p

+

P [ k ] ;

f w

=

C w + W [ k ] ;

f o r j = 1

t o

k

d o

X [ j ] =

Y [ j ] ;

}

}

i f ( B o u n d ( c p , c w , k ) f p ) t h e n

{

y [ k ] =

0 ;

i f ( k < n ) t h e n

B n a p

( K + 1 , c p , c w ) ;

i f ( ( c p > f p ) a n d

( k = n ) ) t h e n

{

f p

=

c p ;

f w

=

c w ;

f o r j = 1

t o

k

d o

X [ j ] =

Y [ j ] ;

} } }

A l g o r i t h m

f o r B o u n d i n g

f u n c t i o n :

A l g o r i t h m

B o u n d ( c p , c w , k ) / / c p 

c u r r e n t p r o f i t t o t a l . / / c w 

c u r r e n t w e i g h t t o t a l . / / k  t h e i n d e x

o f t h e l a s t r e m o v e d

i t e m . / / m  t h e k n a p s a c k

s i z e .

{

b = c p ;

c = c w ;

f o r I = - k + 1

t o

n

d o

{

c =

c + w [ I ] ;

i f ( c < m ) t h e n

b = b + p [ I ] ;

e l s e r e t u r n

b +

( 1 - ( c - m ) / W [ I ] ) *

P [ I ] ; } r e t u r n

b ; }

E x a m p l e :

M =

6

W i =

2 , 3 , 4

4

2

2

N =

3

P i =

1 , 2 , 5

P i / W i ( i . e )

5

2

1

X i =

1

0

1 T h e m a x i m u m

w e i g h t i s 6 T h e M a x i m u m

p r o f i t i s ( 1 * 5 ) +

( 0 * 2 ) +

( 1 * 1 )

5 + 1

6 .

F p

=

( - 1 )

1

3

&

0 + 4

6

c w

=

4 , c p

=

5 , y ( 1 ) = 1

k

=

k + 2

2

3

b u t 7 > 6

s o

y ( 2 ) =

0

S o

b o u n d ( 5 , 4 , 2 , 6 )

B = 5

C = 4

I = 3

t o

3 C = 6 6

6 S o

r e t u r n

5 + ( 1 - ( 6 - 6 ) ) / ( 2 * 1 )

5 . 5

i s n o t l e s s t h a n

f p .

S o , k = k + 1

( i . e ) 3 .

3 = 3

&

4 + 2

6

c w =

6 , c p

=

6 , y ( 3 ) = 1 .

K = 4 .

I f 4 >

3

t h e n

F p

= 6 , f w = 6 , k = 3

, x ( 1 ) 1

0

1

T h e s o l u t i o n

X i 

1

0

1

P r o f i t 

6

W e i g h t  6 . 4 A l g o r i t h m

m C o l o r i n g ( k ) / / t h e g r a p h

i s r e p r e s e n t e d

b y

i t s B o o l e a n

a d j a c e n c y

m a t r i x

G [ 1 : n , 1 : n ]

. A Algorithm: l l Algorithm mColoring(k)a // the graph is representeds by its Boolean adjacency matrix G[1:n,1:n] .All assignments //of 1,2,……….,ms to the vertices of the graph such that adjacent vertices are assigned //distinct integersi are printed. ’k’ is the index of the next vertex to color. g { n repeat m { e // generate all legal assignmentn for X[k]. Nextvalue(k); // Assignt to X[k] a legal color. If (X[k]=0) then return;s // 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);o }until(false); f } 1 , Algorithm Nextvalue(k)2 , // 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. , { m

repeat t o

t 11 h e v e r t i c e s o f t h e g r a p h

s u c h

t h a t a d j a c e n c t v e r t i c e s a r e a s s i g n r d

/ / d i s t i n c t i n t e g e r s a r e p r i n t e d . ’ k ’ i s t h e i n d e x

o f t h e n e x t v e r t e x

t o

c o l o r .

{

r e p e a t {

/ / g e n e r a t e a l l l e g a l a s s i g n m e n t f o r X [ k ] .

N e x t v a l u e ( k ) ;

/ / A s s i g n

t o

X [ k ] a l e g a l c o l o r .

I f ( X [ k ] = 0 ) t h e n

r e t u r n ;

/ / N o

n e w

c o l o r p o s s i b l e .

I f ( k = n ) t h e n

/ / A l m o s t ‘ m ’ c o l o r s h a v e b e e n

u s e d

t o

c o l o r t h e ‘ n ’ v e r t i c e s

W r i t e ( x [ 1 : n ] ) ;

E l s e m c o l o r i n g ( k + 1 ) ; } u n t i l ( f a l s e ) ; }

A l g o r i t h m

N e x t v a l u e ( k ) / / X [ 1 ] , … … X [ k - 1 ] h a v e b e e n

a s s i g n e d

i n t e g e r v a l u e s i n

t h e r a n g e [ 1 , m ] s u c h

t h a t / / a d j a c e n t v a l u e s h a v e d i s t i n c t i n t e g e r s . A

v a l u e f o r X [ k ] i s d e t e r m i n e d

i n

t h e / / r a n g e [ 0 , m ] . X [ k ] i s a s s i g n e d

t h e n e x t h i g h e s t n u m b e r e s c o l o r w h i l e m a i n t a i n i n g

/ / d i s t i n c t n e s s f o r m

t h e a d j a c e n t v e r t i c e s o f v e r t e x

K . I f n o

s u c h

c o l o r e x i s t s , t h e n

X [ k ] i s 0 .

{

r e p e a t

{

X [ k ] =

( X [ k ] + 1 ) m o d ( m + 1 ) ;

/ / n e x t h i g h e s t c o l o r .

I f ( X [ k ] = 0 ) t h e n

r e t u r n ;

/ / A l l c o l o r s h a v e b e e n

u s e d .

F o r j = 1

t o

n

d o

{

/ / C h e c k

i f t h i s c o l o r i s d i s t i n c t f r o m

a d j a c e n t c o l o r .

I f ( ( G [ k , j ]

0 ) a n d ( X [ k ] =

X [ j ] ) )

/ / I f ( k , j ) i s a n

e d g e a n d

i f a d j a c e n t v e r t i c e s h a v e t h e s a m e c o l o r .

T h e n

b r e a k ;

}

i f ( j = n + 1 ) t h e n

r e t u r n ;

/ / n e w

c o l o r f o u n d .

}

u n t i l ( f a l s e ) ;

/ / o t h e r w i s e t r y

t o

f i n d

a n o t h e r c o l o r . }

T h e t i m e s p e n t b y

N e x t v a l u e t o

d e t e r m i n e t h e c h i l d r e n

i s ( m n )  T o t a l t i m e i s =

( m n n ) .

K n a p s a c k

P r o b l e m

u s i n g

B a c k t r a c k i n g :

T h e p r o b l e m

i s s i m i l a r t o

t h e z e r o - o n e ( 0 / 1 ) k n a p s a c k

o p t i m i z a t i o n

p r o b l e m

i s d y n a m i c p r o g r a m m i n g

a l g o r i t h m .

W e a r e g i v e n

‘ n ’ p o s i t i v e w e i g h t s W i a n d ’ n ’ p o s i t i v e p r o f i t s P i , a n d

a p o s i t i v e n u m b e r ‘ m ’ t h a t i s t h e k n a p s a c k

c a p a c i t y , t h g i s p r o b l e m

c a l l s f o r c h o s s i n g

a s u b s e t o f t h e w e i g h t s s u c h

t h a t ,

m

a n d

i s M a x i m i z e d .

X i  C o n s t i t u t e Z e r o - o n e v a l u e d

V e c t o r .

T h e S o l u t i o n

s p a c e i s t h e s a m e a s t h a t f o r t h e s u m

o f s u b s e t ’ s p r o b l e m .

B o u n d i n g

f u n c t i o n s a r e n e e d e d

t o

h e l p

k i l l s o m e l i v e n o d e s w i t h o u t e x p a n d i n g

t h e m . A

g o o d

b o u n d i n g

f u n c t i o n

f o r t h i s p r o b l e m

i s o b t a i n e d

b y

u s i n g

a n

u p p e r b o u n d

o n

t h e v a l u e o f t h e b e s t f e a s i b l e s o l u t i o n

o b t a i n a b l e b y

e x p a n d i n g

t h e g i v e n

l i v e n o d e .

T h e p r o f i t s a n d

w e i g h t s a r e a s s i g n e d

i n

d e s c e n d i n g

o r d e r d e p e n d

u p o n

t h e r a t i o .

( i . e ) P i / W i

P ( I + 1 ) / W ( I + 1 )

S o l u t i o n

:

A f t e r a s s i g n i n g

t h e p r o f i t a n d

w e i g h t s , w e h a v e t o

t a k e t h e f i r s t o b j e c t w e i g h t s a n d

c h e c k

i f t h e f i r s t w e i g h t i s l e s s t h a n

o r e q u a l t o

t h e c a p a c i t y , i f s o

t h e n

w e i n c l u d e t h a t o b j e c t ( i . e ) t h e u n i t i s 1 . ( i . e ) K 

1 .

T h e n

W e a r e g o i n g

t o

t h e n e x t o b j e c t , i f t h e o b j e c t w e i g h t i s e x c e e d e d

t h a t o b j e c t d o e s n a t f i t . S o

u n i t o f t h a t o b j e c t i s ‘ 0 ’ . ( i . e ) K = 0 .

T h e n

W e a r e g o i n g

t o

t h e b o u n d i n g

f u n c t i o n

, t h i s f u n c t i o n

d e t e r m i n e s a n

u p p e r b o u n d

o n

t h e b e s t s o l u t i o n

b t a i n a l \ b l e a t l e v e l K + 1 .

R e p e a t t h e p r o c e s s u n t i l w e r e a c h

t h e o p t i m a l s o l u t i o n .

A l g o r i t h m :

A l g o r i t h m

B k n a p ( k , c p , c w ) / / ‘ m ’ i s t h e s i z e o f t h e k n a p s a c k ;

‘ n ’ 

n o . o f w e i g h t s &

p r o f i t s . W [ ] & P [ ] a r e t h e / / w e i g h t s &

w e i g h t s . P [ I ] / W [ I ]

P [ I + 1 ] / W [ I + 1 ] . / / f w  F i n a l w e i g h t s o f k n a p s a c k . / / f p 

f i n a l m a x . p r o f i t . / / x [ k ] =

0

i f W [ k ] i s n o t t h e k n a p s a c k , e l s e X [ k ] = 1 . {

/ / G e n e r a t e l e f t c h i l d .

I f ( ( W + W [ k ]

m ) t h e n

{

Y [ k ] = 1 ;

I f ( k < n ) t h e n

B n a p ( k + 1 , c p + P [ k ] , C w

+ W [ k ] )

I f ( ( C p

+

p [ w ] >

f p ) a n d

( k = n ) ) t h e n

{

f p

=

c p

+

P [ k ] ;

f w

=

C w + W [ k ] ;

f o r j = 1

t o

k

d o

X [ j ] =

Y [ j ] ;

}

}

i f ( B o u n d ( c p , c w , k ) f p ) t h e n

{

y [ k ] =

0 ;

i f ( k < n ) t h e n

B n a p

( K + 1 , c p , c w ) ;

i f ( ( c p > f p ) a n d

( k = n ) ) t h e n

{

f p

=

c p ;

f w

=

c w ;

f o r j = 1

t o

k

d o

X [ j ] =

Y [ j ] ;

} } }

A l g o r i t h m

f o r B o u n d i n g

f u n c t i o n :

A l g o r i t h m

B o u n d ( c p , c w , k ) / / c p 

c u r r e n t p r o f i t t o t a l . / / c w 

c u r r e n t w e i g h t t o t a l . / / k  t h e i n d e x

o f t h e l a s t r e m o v e d

i t e m . / / m  t h e k n a p s a c k

s i z e .

{

b = c p ;

c = c w ;

f o r I = - k + 1

t o

n

d o

{

c =

c + w [ I ] ;

i f ( c < m ) t h e n

b = b + p [ I ] ;

e l s e r e t u r n

b +

( 1 - ( c - m ) / W [ I ] ) *

P [ I ] ; } r e t u r n

b ; }

E x a m p l e :

M =

6

W i =

2 , 3 , 4

4

2

2

N =

3

P i =

1 , 2 , 5

P i / W i ( i . e )

5

2

1

X i =

1

0

1 T h e m a x i m u m

w e i g h t i s 6 T h e M a x i m u m

p r o f i t i s ( 1 * 5 ) +

( 0 * 2 ) +

( 1 * 1 )

5 + 1

6 .

F p

=

( - 1 )

1

3

&

0 + 4

6

c w

=

4 , c p

=

5 , y ( 1 ) = 1

k

=

k + 2

2

3

b u t 7 > 6

s o

y ( 2 ) =

0

S o

b o u n d ( 5 , 4 , 2 , 6 )

B = 5

C = 4

I = 3

t o

3 C = 6 6

6 S o

r e t u r n

5 + ( 1 - ( 6 - 6 ) ) / ( 2 * 1 )

5 . 5

i s n o t l e s s t h a n

f p .

S o , k = k + 1

( i . e ) 3 .

3 = 3

&

4 + 2

6

c w =

6 , c p

=

6 , y ( 3 ) = 1 .

K = 4 .

I f 4 >

3

t h e n

F p

= 6 , f w = 6 , k = 3

, x ( 1 ) 1

0

1

T h e s o l u t i o n

X i 

1

0

1

P r o f i t 

6

W e i g h t  6 . 4 A l g o r i t h m

m C o l o r i n g ( k ) / / t h e g r a p h

i s r e p r e s e n t e d

b y

i t s B o o l e a n

a d j a c e n c y

m a t r i x

G [ 1 : n , 1 : n ]

. A l l a s s i g n m e n t s / / o f 1 , 2 , … … … . , m

t o

t h e v e r t i c e { s X[k] = (X[k]+1)mod(m+1);o // next highest color. If(X[k]=0) then return;f //All colors have been used. For j=1 to n dot { h // Check if thise color is distinct from adjacent color. If((G[k,j]  0)and(X[k]g = X[j])) // If (k,j) is anr edge and if adjacent vertices have the same color. Then break; a } p h if(j=n+1) then return; //new color found. } until(false); //otherwises try to find another color. } u c  The time spent by Nextvalueh to determine the children is  (mn)

Total time is =  (mn n).t h Knapsack Problema using Backtracking: t  The problem is asimilar to the zero-one (0/1) knapsack optimization problem is dynamic programmingd algorithm. j  We are given ‘n’a positive weights Wi and ’n’ positive profits Pi, and a positive number ‘m’ thatc is the knapsack capacity, the is problem calls for choosing a subset of the weightse such that, n c WiXi  m and  PiXi is Maximized. 1int 1in v Xi e Constitute Zero-one valued Vector. r  The Solution spacet is the same as that for the sum of subset’s problem. i  Bounding functionsc are needed to help kill some live nodes without expanding them. A good boundinge function for this problem is obtained by using an upper bound on the values of the best feasible solution obtainable by expanding the given live node. a r  The profits and weightse are assigned in descending order depend upon the ratio. a (i.e.)s Pi/Wi  P(I+1) / W(I+1) s Solution : i g n r d 12 / / d i s t i n c t i n t e g e r s a r e p r i n t e d . ’ k ’ i s t h e i n d e x

o f t h e n e x t v e r t e x

t o

c o l o r .

{

r e p e a t {

/ / g e n e r a t e a l l l e g a l a s s i g n m e n t f o r X [ k ] .

N e x t v a l u e ( k ) ;

/ / A s s i g n

t o

X [ k ] a l e g a l c o l o r .

I f ( X [ k ] = 0 ) t h e n

r e t u r n ;

/ / N o

n e w

c o l o r p o s s i b l e .

I f ( k = n ) t h e n

/ / A l m o s t ‘ m ’ c o l o r s h a v e b e e n

u s e d

t o

c o l o r t h e ‘ n ’ v e r t i c e s

W r i t e ( x [ 1 : n ] ) ;

E l s e m c o l o r i n g ( k + 1 ) ; } u n t i l ( f a l s e ) ; }

A l g o r i t h m

N e x t v a l u e ( k ) / / X [ 1 ] , … … X [ k - 1 ] h a v e b e e n

a s s i g n e d

i n t e g e r v a l u e s i n

t h e r a n g e [ 1 , m ] s u c h

t h a t / / a d j a c e n t v a l u e s h a v e d i s t i n c t i n t e g e r s . A

v a l u e f o r X [ k ] i s d e t e r m i n e d

i n

t h e / / r a n g e [ 0 , m ] . X [ k ] i s a s s i g n e d

t h e n e x t h i g h e s t n u m b e r e s c o l o r w h i l e m a i n t a i n i n g

/ / d i s t i n c t n e s s f o r m

t h e a d j a c e n t v e r t i c e s o f v e r t e x

K . I f n o

s u c h

c o l o r e x i s t s , t h e n

X [ k ] i s 0 .

{

r e p e a t

{

X [ k ] =

( X [ k ] + 1 ) m o d ( m + 1 ) ;

/ / n e x t h i g h e s t c o l o r .

I f ( X [ k ] = 0 ) t h e n

r e t u r n ;

/ / A l l c o l o r s h a v e b e e n

u s e d .

F o r j = 1

t o

n

d o

{

/ / C h e c k

i f t h i s c o l o r i s d i s t i n c t f r o m

a d j a c e n t c o l o r .

I f ( ( G [ k , j ]

0 ) a n d ( X [ k ] =

X [ j ] ) )

/ / I f ( k , j ) i s a n

e d g e a n d

i f a d j a c e n t v e r t i c e s h a v e t h e s a m e c o l o r .

T h e n

b r e a k ;

}

i f ( j = n + 1 ) t h e n

r e t u r n ;

/ / n e w

c o l o r f o u n d .

}

u n t i l ( f a l s e ) ;

/ / o t h e r w i s e t r y

t o

f i n d

a n o t h e r c o l o r . }

T h e t i m e s p e n t b y

N e x t v a l u e t o

d e t e r m i n e t h e c h i l d r e n

i s ( m n )  T o t a l t i m e i s =

( m n n ) .

K n a p s a c k

P r o b l e m

u s i n g

B a c k t r a c k i n g :

T h e p r o b l e m

i s s i m i l a r t o

t h e z e r o - o n e ( 0 / 1 ) k n a p s a c k

o p t i m i z a t i o n

p r o b l e m

i s d y n a m i c p r o g r a m m i n g

a l g o r i t h m .

W e a r e g i v e n

‘ n ’ p o s i t i v e w e i g h t s W i a n d ’ n ’ p o s i t i v e p r o f i t s P i , a n d

a p o s i t i v e n u m b e r ‘ m ’ t h a t i s t h e k n a p s a c k

c a p a c i t y , t h g i s p r o b l e m

c a l l s f o r c h o s s i n g

a s u b s e t o f t h e w e i g h t s s u c h

t h a t ,

m

a n d

i s M a x i m i z e d .

X i  C o n s t i t u t e Z e r o - o n e v a l u e d

V e c t o r .

T h e S o l u t i o n

s p a c e i s t h e s a m e a s t h a t f o r t h e s u m

o f s u b s e t ’ s p r o b l e m .

B o u n d i n g

f u n c t i o n s a r e n e e d e d

t o

h e l p

k i l l s o m e l i v e n o d e s w i t h o u t e x p a n d i n g

t h e m . A

g o o d

b o u n d i n g

f u n c t i o n

f o r t h i s p r o b l e m

i s o b t a i n e d

b y

u s i n g

a n

u p p e r b o u n d

o n

t h e v a l u e o f t h e b e s t f e a s i b l e s o l u t i o n

o b t a i n a b l e b y

e x p a n d i n g

t h e g i v e n

l i v e n o d e .

T h e p r o f i t s a n d

w e i g h t s a r e a s s i g n e d

i n

d e s c e n d i n g

o r d e r d e p e n d

u p o n

t h e r a t i o .

( i . e ) P i / W i

P ( I + 1 ) / W ( I + 1 )

S o l u t i o n

:

A f t e r a s s i g n i n g

t h e p r o f i t a n d

w e i g h t s , w e h a v e t o

t a k e t h e f i r s t o b j e c t w e i g h t s a n d

c h e c k

i f t h e f i r s t w e i g h t i s l e s s t h a n

o r e q u a l t o

t h e c a p a c i t y , i f s o

t h e n

w e i n c l u d e t h a t o b j e c t ( i . e ) t h e u n i t i s 1 . ( i . e ) K 

1 .

T h e n

W e a r e g o i n g

t o

t h e n e x t o b j e c t , i f t h e o b j e c t w e i g h t i s e x c e e d e d

t h a t o b j e c t d o e s n a t f i t . S o

u n i t o f t h a t o b j e c t i s ‘ 0 ’ . ( i . e ) K = 0 .

T h e n

W e a r e g o i n g

t o

t h e b o u n d i n g

f u n c t i o n

, t h i s f u n c t i o n

d e t e r m i n e s a n

u p p e r b o u n d

o n

t h e b e s t s o l u t i o n

b t a i n a l \ b l e a t l e v e l K + 1 .

R e p e a t t h e p r o c e s s u n t i l w e r e a c h

t h e o p t i m a l s o l u t i o n .

A l g o r i t h m :

A l g o r i t h m

B k n a p ( k , c p , c w ) / / ‘ m ’ i s t h e s i z e o f t h e k n a p s a c k ;

‘ n ’ 

n o . o f w e i g h t s &

p r o f i t s . W [ ] & P [ ] a r e t h e / / w e i g h t s &

w e i g h t s . P [ I ] / W [ I ]

P [ I + 1 ] / W [ I + 1 ] . / / f w  F i n a l w e i g h t s o f k n a p s a c k . / / f p 

f i n a l m a x . p r o f i t . / / x [ k ] =

0

i f W [ k ] i s n o t t h e k n a p s a c k , e l s e X [ k ] = 1 . {

/ / G e n e r a t e l e f t c h i l d .

I f ( ( W + W [ k ]

m ) t h e n

{

Y [ k ] = 1 ;

I f ( k < n ) t h e n

B n a p ( k + 1 , c p + P [ k ] , C w

+ W [ k ] )

I f ( ( C p

+

p [ w ] >

f p ) a n d

( k = n ) ) t h e n

{

f p

=

c p

+

P [ k ] ;

f w

=

C w + W [ k ] ;

f o r j = 1

t o

k

d o

X [ j ] =

Y [ j ] ;

}

}

i f ( B o u n d ( c p , c w , k ) f p ) t h e n

{

y [ k ] =

0 ;

i f ( k < n ) t h e n

B n a p

( K + 1 , c p , c w ) ;

i f ( ( c p > f p ) a n d

( k = n ) ) t h e n

{

f p

=

c p ;

f w

=

c w ;

f o r j = 1

t o

k

d o

X [ j ] =

Y [ j ] ;

} } }

A l g o r i t h m

f o r B o u n d i n g

f u n c t i o n :

A l g o r i t h m

B o u n d ( c p , c w , k ) / / c p 

c u r r e n t p r o f i t t o t a l . / / c w 

c u r r e n t w e i g h t t o t a l . / / k  t h e i n d e x

o f t h e l a s t r e m o v e d

i t e m . / / m  t h e k n a p s a c k

s i z e .

{

b = c p ;

c = c w ;

f o r I = - k + 1

t o

n

d o

{

c =

c + w [ I ] ;

i f ( c < m ) t h e n

b = b + p [ I ] ;

e l s e r e t u r n

b +

( 1 - ( c - m ) / W [ I ] ) *

P [ I ] ; } r e t u r n

b ; }

E x a m p l e :

M =

6

W i =

2 , 3 , 4

4

2

2

N =

3

P i =

1 , 2 , 5

P i / W i ( i . e )

5

2

1

X i =

1

0

1 T h e m a x i m u m

w e i g h t i s 6 T h e M a x i m u m

p r o f i t i s ( 1 * 5 ) +

( 0 * 2 ) +

( 1 * 1 )

5 + 1

6 .

F p

=

( - 1 )

1

3

&

0 + 4

6

c w

=

4 , c p

=

5 , y ( 1 ) = 1

k

=

k + 2

2

3

b u t 7 > 6

s o

y ( 2 ) =

0

S o

b o u n d ( 5 , 4 , 2 , 6 )

B = 5

C = 4

I = 3

t o

3 C = 6 6

6 S o

r e t u r n

5 + ( 1 - ( 6 - 6 ) ) / ( 2 * 1 )

5 . 5

i s n o t l e s s t h a n

f p .

S o , k = k + 1

( i . e ) 3 .

3 = 3

&

4 + 2

6

c w =

6 , c p

=

6 , y ( 3 ) = 1 .

K = 4 .

I f 4 >

3

t h e n

F p

= 6 , f w = 6 , k = 3

, x ( 1 ) 1

0

1

T h e s o l u t i o n

X i 

1

0

1

P r o f i t 

6

W e i g h t  6 . 4 A l g o r i t h m

m C o l o r i n g ( k ) / / t h e g r a p h

i s r e p r e s e n t e d

b y

i t s B o o l e a n

a d j a c e n c y

m a t r i x

G [ 1 : n , 1 : n ]

. A l l a s s i g n m e n t s / / o f 1 , 2 , … … … . , m

t o

t h e v e r t i c e s o f t h e g r a p h

s u c h

t h a t a d j a c e n c t v e r t i c e s a r e a s s i g n r d

/ / d i s t i n c  After assigning the profit and weights ,we have to take the first object weights and t check if the first weight is less than or equal to the capacity, if so then we include i that object (i.e.) the unit is 1.(i.e.) K 1. n t  Then We are going to the next object, if the object weight is exceeded that object e does not fit. So unit of that object is ‘0’.(i.e.) K=0. g  Then We are going to the bounding function ,this function determines an upper e bound on the best solution obtainable at level K+1. r s  Repeat the process until we reach the optimal solution. a r Algorithm: e p Algorithm Bknap(k,cp,cw) r i // ‘m’ is the size of the knapsack; ‘n’  no.of weights & profits. W[]&P[] are the n //weights & weights. P[I]/W[I]  P[I+1]/W[I+1]. t //fwFinal weights of knapsack. e //fp final max.profit. d //x[k] = 0 if W[k] is not the knapsack,else X[k]=1. . ’ { k // Generate left child. ’ If((W+W[k]  m) then i { s Y[k] =1; t If(k fp) and (k=n)) then e i { n fp = cp + P[k]; d fw = Cw+W[k]; e for j=1 to k do X[j] = Y[j]; x }

} o f if(Bound(cp,cw,k)  fp) then t { h y[k] = 0; e if(kfp) and (k=n)) then e { x fp = cp; t fw = cw; v for j=1 to k do X[j] = Y[j]; e } r t e x 13

t o

c o l o r .

{

r e p e a t {

/ / g e n e r a t e a l l l e g a l a s s i g n m e n t f o r X [ k ] .

N e x t v a l u e ( k ) ;

/ / A s s i g n

t o

X [ k ] a l e g a l c o l o r .

I f ( X [ k ] = 0 ) t h e n

r e t u r n ;

/ / N o

n e w

c o l o r p o s s i b l e .

I f ( k = n ) t h e n

/ / A l m o s t ‘ m ’ c o l o r s h a v e b e e n

u s e d

t o

c o l o r t h e ‘ n ’ v e r t i c e s

W r i t e ( x [ 1 : n ] ) ;

E l s e m c o l o r i n g ( k + 1 ) ; } u n t i l ( f a l s e ) ; }

A l g o r i t h m

N e x t v a l u e ( k ) / / X [ 1 ] , … … X [ k - 1 ] h a v e b e e n

a s s i g n e d

i n t e g e r v a l u e s i n

t h e r a n g e [ 1 , m ] s u c h

t h a t / / a d j a c e n t v a l u e s h a v e d i s t i n c t i n t e g e r s . A

v a l u e f o r X [ k ] i s d e t e r m i n e d

i n

t h e / / r a n g e [ 0 , m ] . X [ k ] i s a s s i g n e d

t h e n e x t h i g h e s t n u m b e r e s c o l o r w h i l e m a i n t a i n i n g

/ / d i s t i n c t n e s s f o r m

t h e a d j a c e n t v e r t i c e s o f v e r t e x

K . I f n o

s u c h

c o l o r e x i s t s , t h e n

X [ k ] i s 0 .

{

r e p e a t

{

X [ k ] =

( X [ k ] + 1 ) m o d ( m + 1 ) ;

/ / n e x t h i g h e s t c o l o r .

I f ( X [ k ] = 0 ) t h e n

r e t u r n ;

/ / A l l c o l o r s h a v e b e e n

u s e d .

F o r j = 1

t o

n

d o

{

/ / C h e c k

i f t h i s c o l o r i s d i s t i n c t f r o m

a d j a c e n t c o l o r .

I f ( ( G [ k , j ]

0 ) a n d ( X [ k ] =

X [ j ] ) )

/ / I f ( k , j ) i s a n

e d g e a n d

i f a d j a c e n t v e r t i c e s h a v e t h e s a m e c o l o r .

T h e n

b r e a k ;

}

i f ( j = n + 1 ) t h e n

r e t u r n ;

/ / n e w

c o l o r f o u n d .

}

u n t i l ( f a l s e ) ;

/ / o t h e r w i s e t r y

t o

f i n d

a n o t h e r c o l o r . }

T h e t i m e s p e n t b y

N e x t v a l u e t o

d e t e r m i n e t h e c h i l d r e n

i s ( m n )  T o t a l t i m e i s =

( m n n ) .

K n a p s a c k

P r o b l e m

u s i n g

B a c k t r a c k i n g :

T h e p r o b l e m

i s s i m i l a r t o

t h e z e r o - o n e ( 0 / 1 ) k n a p s a c k

o p t i m i z a t i o n

p r o b l e m

i s d y n a m i c p r o g r a m m i n g

a l g o r i t h m .

W e a r e g i v e n

‘ n ’ p o s i t i v e w e i g h t s W i a n d ’ n ’ p o s i t i v e p r o f i t s P i , a n d

a p o s i t i v e n u m b e r ‘ m ’ t h a t i s t h e k n a p s a c k

c a p a c i t y , t h g i s p r o b l e m

c a l l s f o r c h o s s i n g

a s u b s e t o f t h e w e i g h t s s u c h

t h a t ,

m

a n d

i s M a x i m i z e d .

X i  C o n s t i t u t e Z e r o - o n e v a l u e d

V e c t o r .

T h e S o l u t i o n

s p a c e i s t h e s a m e a s t h a t f o r t h e s u m

o f s u b s e t ’ s p r o b l e m .

B o u n d i n g

f u n c t i o n s a r e n e e d e d

t o

h e l p

k i l l s o m e l i v e n o d e s w i t h o u t e x p a n d i n g

t h e m . A

g o o d

b o u n d i n g

f u n c t i o n

f o r t h i s p r o b l e m

i s o b t a i n e d

b y

u s i n g

a n

u p p e r b o u n d

o n

t h e v a l u e o f t h e b e s t f e a s i b l e s o l u t i o n

o b t a i n a b l e b y

e x p a n d i n g

t h e g i v e n

l i v e n o d e .

T h e p r o f i t s a n d

w e i g h t s a r e a s s i g n e d

i n

d e s c e n d i n g

o r d e r d e p e n d

u p o n

t h e r a t i o .

( i . e ) P i / W i

P ( I + 1 ) / W ( I + 1 )

S o l u t i o n

:

A f t e r a s s i g n i n g

t h e p r o f i t a n d

w e i g h t s , w e h a v e t o

t a k e t h e f i r s t o b j e c t w e i g h t s a n d

c h e c k

i f t h e f i r s t w e i g h t i s l e s s t h a n

o r e q u a l t o

t h e c a p a c i t y , i f s o

t h e n

w e i n c l u d e t h a t o b j e c t ( i . e ) t h e u n i t i s 1 . ( i . e ) K 

1 .

T h e n

W e a r e g o i n g

t o

t h e n e x t o b j e c t , i f t h e o b j e c t w e i g h t i s e x c e e d e d

t h a t o b j e c t d o e s n a t f i t . S o

u n i t o f t h a t o b j e c t i s ‘ 0 ’ . ( i . e ) K = 0 .

T h e n

W e a r e g o i n g

t o

t h e b o u n d i n g

f u n c t i o n

, t h i s f u n c t i o n

d e t e r m i n e s a n

u p p e r b o u n d

o n

t h e b e s t s o l u t i o n

b t a i n a l \ b l e a t l e v e l K + 1 .

R e p e a t t h e p r o c e s s u n t i l w e r e a c h

t h e o p t i m a l s o l u t i o n .

A l g o r i t h m :

A l g o r i t h m

B k n a p ( k , c p , c w ) / / ‘ m ’ i s t h e s i z e o f t h e k n a p s a c k ;

‘ n ’ 

n o . o f w e i g h t s &

p r o f i t s . W [ ] & P [ ] a r e t h e / / w e i g h t s &

w e i g h t s . P [ I ] / W [ I ]

P [ I + 1 ] / W [ I + 1 ] . / / f w  F i n a l w e i g h t s o f k n a p s a c k . / / f p 

f i n a l m a x . p r o f i t . / / x [ k ] =

0

i f W [ k ] i s n o t t h e k n a p s a c k , e l s e X [ k ] = 1 . {

/ / G e n e r a t e l e f t c h i l d .

I f ( ( W + W [ k ]

m ) t h e n

{

Y [ k ] = 1 ;

I f ( k < n ) t h e n

B n a p ( k + 1 , c p + P [ k ] , C w

+ W [ k ] )

I f ( ( C p

+

p [ w ] >

f p ) a n d

( k = n ) ) t h e n

{

f p

=

c p

+

P [ k ] ;

f w

=

C w + W [ k ] ;

f o r j = 1

t o

k

d o

X [ j ] =

Y [ j ] ;

}

}

i f ( B o u n d ( c p , c w , k ) f p ) t h e n

{

y [ k ] =

0 ;

i f ( k < n ) t h e n

B n a p

( K + 1 , c p , c w ) ;

i f ( ( c p > f p ) a n d

( k = n ) ) t h e n

{

f p

=

c p ;

f w

=

c w ;

f o r j = 1

t o

k

d o

X [ j ] =

Y [ j ] ;

} } }

A l g o r i t h m

f o r B o u n d i n g

f u n c t i o n :

A l g o r i t h m

B o u n d ( c p , c w , k ) / / c p 

c u r r e n t p r o f i t t o t a l . / / c w 

c u r r e n t w e i g h t t o t a l . / / k  t h e i n d e x

o f t h e l a s t r e m o v e d

i t e m . / / m  t h e k n a p s a c k

s i z e .

{

b = c p ;

c = c w ;

f o r I = - k + 1

t o

n

d o

{

c =

c + w [ I ] ;

i f ( c < m ) t h e n

b = b + p [ I ] ;

e l s e r e t u r n

b +

( 1 - ( c - m ) / W [ I ] ) *

P [ I ] ; } r e t u r n

b ; }

E x a m p l e :

M =

6

W i =

2 , 3 , 4

4

2

2

N =

3

P i =

1 , 2 , 5

P i / W i ( i . e )

5

2

1

X i =

1

0

1 T h e m a x i m u m

w e i g h t i s 6 T h e M a x i m u m

p r o f i t i s ( 1 * 5 ) +

( 0 * 2 ) +

( 1 * 1 )

5 + 1

6 .

F p

=

( - 1 )

1

3

&

0 + 4

6

c w

=

4 , c p

=

5 , y ( 1 ) = 1

k

=

k + 2

2

3

b u t 7 > 6

s o

y ( 2 ) =

0

S o

b o u n d ( 5 , 4 , 2 , 6 )

B = 5

C = 4

I = 3

t o

3 C = 6 6

6 S o

r e t u r n

5 + ( 1 - ( 6 - 6 ) ) / ( 2 * 1 )

5 . 5

i s n o t l e s s t h a n

f p .

S o , k = k + 1

( i . e ) 3 .

3 = 3

&

4 + 2

6

c w =

6 , c p

=

6 , y ( 3 ) = 1 .

K = 4 .

I f 4 >

3

t h e n

F p

= 6 , f w = 6 , k = 3

, x ( 1 ) 1

0

1

T h e s o l u t i o n

X i 

1

0

1

P r o f i t 

6

W e i g h t  6 . 4 A l g o r i t h m

m C o l o r i n g ( k ) / / t h e g r a p h

i s r e p r e s e n t e d

b y

i t s B o o l e a n

a d j a c e n c y

m a t r i x

G [ 1 : n , 1 : n ]

. A l l a s s i g n m e n t s / / o f 1 , 2 , … … … . , m

t o

t h e v e r t i c e s o f t h e g r a p h

s u c h

t h a t a d j a c e n c t v e r t i c e s a r e a s s i g n r d

/ / d i s t i n c t i n t e g e r s a r e p r i n t e d . ’ k ’ i s t h e i n d e x

o f t h e n e x t v e r t e x

t o

c o l o r } . } { Algorithm for Bounding function: r Algorithm Bound(cp,cw,k)e // cp current profit total.p //cw current weight total.e //kthe index of the lasta removed item. //mthe knapsack size.t { { b=cp; c=cw; / for I =- k+1 to n do / { g c= c+w[I]; e if (c6 f so y(2) = 0 o r  So bound(5,4,2,6)X [ k ] . 14

N e x t v a l u e ( k ) ;

/ / A s s i g n

t o

X [ k ] a l e g a l c o l o r .

I f ( X [ k ] = 0 ) t h e n

r e t u r n ;

/ / N o

n e w

c o l o r p o s s i b l e .

I f ( k = n ) t h e n

/ / A l m o s t ‘ m ’ c o l o r s h a v e b e e n

u s e d

t o

c o l o r t h e ‘ n ’ v e r t i c e s

W r i t e ( x [ 1 : n ] ) ;

E l s e m c o l o r i n g ( k + 1 ) ; } u n t i l ( f a l s e ) ; }

A l g o r i t h m

N e x t v a l u e ( k ) / / X [ 1 ] , … … X [ k - 1 ] h a v e b e e n

a s s i g n e d

i n t e g e r v a l u e s i n

t h e r a n g e [ 1 , m ] s u c h

t h a t / / a d j a c e n t v a l u e s h a v e d i s t i n c t i n t e g e r s . A

v a l u e f o r X [ k ] i s d e t e r m i n e d

i n

t h e / / r a n g e [ 0 , m ] . X [ k ] i s a s s i g n e d

t h e n e x t h i g h e s t n u m b e r e s c o l o r w h i l e m a i n t a i n i n g

/ / d i s t i n c t n e s s f o r m

t h e a d j a c e n t v e r t i c e s o f v e r t e x

K . I f n o

s u c h

c o l o r e x i s t s , t h e n

X [ k ] i s 0 .

{

r e p e a t

{

X [ k ] =

( X [ k ] + 1 ) m o d ( m + 1 ) ;

/ / n e x t h i g h e s t c o l o r .

I f ( X [ k ] = 0 ) t h e n

r e t u r n ;

/ / A l l c o l o r s h a v e b e e n

u s e d .

F o r j = 1

t o

n

d o

{

/ / C h e c k

i f t h i s c o l o r i s d i s t i n c t f r o m

a d j a c e n t c o l o r .

I f ( ( G [ k , j ]

0 ) a n d ( X [ k ] =

X [ j ] ) )

/ / I f ( k , j ) i s a n

e d g e a n d

i f a d j a c e n t v e r t i c e s h a v e t h e s a m e c o l o r .

T h e n

b r e a k ;

}

i f ( j = n + 1 ) t h e n

r e t u r n ;

/ / n e w

c o l o r f o u n d .

}

u n t i l ( f a l s e ) ;

/ / o t h e r w i s e t r y

t o

f i n d

a n o t h e r c o l o r . }

T h e t i m e s p e n t b y

N e x t v a l u e t o

d e t e r m i n e t h e c h i l d r e n

i s ( m n )  T o t a l t i m e i s =

( m n n ) .

K n a p s a c k

P r o b l e m

u s i n g

B a c k t r a c k i n g :

T h e p r o b l e m

i s s i m i l a r t o

t h e z e r o - o n e ( 0 / 1 ) k n a p s a c k

o p t i m i z a t i o n

p r o b l e m

i s d y n a m i c p r o g r a m m i n g

a l g o r i t h m .

W e a r e g i v e n

‘ n ’ p o s i t i v e w e i g h t s W i a n d ’ n ’ p o s i t i v e p r o f i t s P i , a n d

a p o s i t i v e n u m b e r ‘ m ’ t h a t i s t h e k n a p s a c k

c a p a c i t y , t h g i s p r o b l e m

c a l l s f o r c h o s s i n g

a s u b s e t o f t h e w e i g h t s s u c h

t h a t ,

m

a n d

i s M a x i m i z e d .

X i  C o n s t i t u t e Z e r o - o n e v a l u e d

V e c t o r .

T h e S o l u t i o n

s p a c e i s t h e s a m e a s t h a t f o r t h e s u m

o f s u b s e t ’ s p r o b l e m .

B o u n d i n g

f u n c t i o n s a r e n e e d e d

t o

h e l p

k i l l s o m e l i v e n o d e s w i t h o u t e x p a n d i n g

t h e m . A

g o o d

b o u n d i n g

f u n c t i o n

f o r t h i s p r o b l e m

i s o b t a i n e d

b y

u s i n g

a n

u p p e r b o u n d

o n

t h e v a l u e o f t h e b e s t f e a s i b l e s o l u t i o n

o b t a i n a b l e b y

e x p a n d i n g

t h e g i v e n

l i v e n o d e .

T h e p r o f i t s a n d

w e i g h t s a r e a s s i g n e d

i n

d e s c e n d i n g

o r d e r d e p e n d

u p o n

t h e r a t i o .

( i . e ) P i / W i

P ( I + 1 ) / W ( I + 1 )

S o l u t i o n

:

A f t e r a s s i g n i n g

t h e p r o f i t a n d

w e i g h t s , w e h a v e t o

t a k e t h e f i r s t o b j e c t w e i g h t s a n d

c h e c k

i f t h e f i r s t w e i g h t i s l e s s t h a n

o r e q u a l t o

t h e c a p a c i t y , i f s o

t h e n

w e i n c l u d e t h a t o b j e c t ( i . e ) t h e u n i t i s 1 . ( i . e ) K 

1 .

T h e n

W e a r e g o i n g

t o

t h e n e x t o b j e c t , i f t h e o b j e c t w e i g h t i s e x c e e d e d

t h a t o b j e c t d o e s n a t f i t . S o

u n i t o f t h a t o b j e c t i s ‘ 0 ’ . ( i . e ) K = 0 .

T h e n

W e a r e g o i n g

t o

t h e b o u n d i n g

f u n c t i o n

, t h i s f u n c t i o n

d e t e r m i n e s a n

u p p e r b o u n d

o n

t h e b e s t s o l u t i o n

b t a i n a l \ b l e a t l e v e l K + 1 .

R e p e a t t h e p r o c e s s u n t i l w e r e a c h

t h e o p t i m a l s o l u t i o n .

A l g o r i t h m :

A l g o r i t h m

B k n a p ( k , c p , c w ) / / ‘ m ’ i s t h e s i z e o f t h e k n a p s a c k ;

‘ n ’ 

n o . o f w e i g h t s &

p r o f i t s . W [ ] & P [ ] a r e t h e / / w e i g h t s &

w e i g h t s . P [ I ] / W [ I ]

P [ I + 1 ] / W [ I + 1 ] . / / f w  F i n a l w e i g h t s o f k n a p s a c k . / / f p 

f i n a l m a x . p r o f i t . / / x [ k ] =

0

i f W [ k ] i s n o t t h e k n a p s a c k , e l s e X [ k ] = 1 . {

/ / G e n e r a t e l e f t c h i l d .

I f ( ( W + W [ k ]

m ) t h e n

{

Y [ k ] = 1 ;

I f ( k < n ) t h e n

B n a p ( k + 1 , c p + P [ k ] , C w

+ W [ k ] )

I f ( ( C p

+

p [ w ] >

f p ) a n d

( k = n ) ) t h e n

{

f p

=

c p

+

P [ k ] ;

f w

=

C w + W [ k ] ;

f o r j = 1

t o

k

d o

X [ j ] =

Y [ j ] ;

}

}

i f ( B o u n d ( c p , c w , k ) f p ) t h e n

{

y [ k ] =

0 ;

i f ( k < n ) t h e n

B n a p

( K + 1 , c p , c w ) ;

i f ( ( c p > f p ) a n d

( k = n ) ) t h e n

{

f p

=

c p ;

f w

=

c w ;

f o r j = 1

t o

k

d o

X [ j ] =

Y [ j ] ;

} } }

A l g o r i t h m

f o r B o u n d i n g

f u n c t i o n :

A l g o r i t h m

B o u n d ( c p , c w , k ) / / c p 

c u r r e n t p r o f i t t o t a l . / / c w 

c u r r e n t w e i g h t t o t a l . / / k  t h e i n d e x

o f t h e l a s t r e m o v e d

i t e m . / / m  t h e k n a p s a c k

s i z e .

{

b = c p ;

c = c w ;

f o r I = - k + 1

t o

n

d o

{

c =

c + w [ I ] ;

i f ( c < m ) t h e n

b = b + p [ I ] ;

e l s e r e t u r n

b +

( 1 - ( c - m ) / W [ I ] ) *

P [ I ] ; } r e t u r n

b ; }

E x a m p l e :

M =

6

W i =

2 , 3 , 4

4

2

2

N =

3

P i =

1 , 2 , 5

P i / W i ( i . e )

5

2

1

X i =

1

0

1 T h e m a x i m u m

w e i g h t i s 6 T h e M a x i m u m

p r o f i t i s ( 1 * 5 ) +

( 0 * 2 ) +

( 1 * 1 )

5 + 1

6 .

F p

=

( - 1 )

1

3

&

0 + 4

6

c w

=

4 , c p

=

5 , y ( 1 ) = 1

k

=

k + 2

2

3

b u t 7 > 6

s o

y ( 2 ) =

0

S o

b o u n d ( 5 , 4 , 2 , 6 )

B = 5

C = 4

I = 3

t o

3 C = 6 6

6 S o

r e t u r n

5 + ( 1 - ( 6 - 6 ) ) / ( 2 * 1 )

5 . 5

i s n o t l e s s t h a n

f p .

S o , k = k + 1

( i . e ) 3 .

3 = 3

&

4 + 2

6

c w =

6 , c p

=

6 , y ( 3 ) = 1 .

K = 4 .

I f 4 >

3

t h e n

F p

= 6 , f w = 6 , k = 3

, x ( 1 ) 1

0

1

T h e s o l u t i o n

X i 

1

0

1

P r o f i t 

6

W e i g h t  6 . 4 A l g o r i t h m

m C o l o r i n g ( k ) / / t h e g r a p h

i s r e p r e s e n t e d

b y

i t s B o o l e a n

a d j a c e n c y

m a t r i x

G [ 1 : n , 1 : n ]

. A l l a s s i g n m e n t s / / o f 1 , 2 , … … … . , m

t o

t h e v e r t i c e s o f t h e g r a p h

s u c h

t h a t a d j a c e n c t v e r t i c e s a r e a s s i g n r d

/ / d i s t i n c t i n t e g e r s a r e p r i n t e d . ’ k ’ i s t h e i n d e x

o f t h e n e x t v e r t e x

t o

c o l o r .

{

r e p e a t {

/ / g e n e r a t e a l l l e g a l a s s i g n m e n t f o r X [ k ] .

N e x t v a l u B=5 e C=4 ( I=3 to 3 k C=6 ) 6  6 ; So return 5+(1-(6-6))/(2*1)

 5.5 is not less than/ fp. So, k=k+1 (i.e.) /3. A 3=3 & 4+2  6 s s cw= 6,cp = 6, y(3)=1.i K=4. g n

 If 4> 3 then t Fp =6,fw=6,k=3 ,x(1) 1 0 1 o The solution Xi  1 0 1

X Profit  6 [ Weight 6. k ] a l e g a l c o l o r .

I f ( X [ k ] = 0 ) 15 t h e n

r e t u r n ;

/ / N o

n e w

c o l o r p o s s i b l e .

I f ( k = n ) t h e n

/ / A l m o s t ‘ m ’ c o l o r s h a v e b e e n

u s e d

t o

c o l o r t h e ‘ n ’ v e r t i c e s

W r i t e ( x [ 1 : n ] ) ;

E l s e m c o l o r i n g ( k + 1 ) ; } u n t i l ( f a l s e ) ; }

A l g o r i t h m

N e x t v a l u e ( k ) / / X [ 1 ] , … … X [ k - 1 ] h a v e b e e n

a s s i g n e d

i n t e g e r v a l u e s i n

t h e r a n g e [ 1 , m ] s u c h

t h a t / / a d j a c e n t v a l u e s h a v e d i s t i n c t i n t e g e r s . A

v a l u e f o r X [ k ] i s d e t e r m i n e d

i n

t h e / / r a n g e [ 0 , m ] . X [ k ] i s a s s i g n e d

t h e n e x t h i g h e s t n u m b e r e s c o l o r w h i l e m a i n t a i n i n g

/ / d i s t i n c t n e s s f o r m

t h e a d j a c e n t v e r t i c e s o f v e r t e x

K . I f n o

s u c h

c o l o r e x i s t s , t h e n

X [ k ] i s 0 .

{

r e p e a t

{

X [ k ] =

( X [ k ] + 1 ) m o d ( m + 1 ) ;

/ / n e x t h i g h e s t c o l o r .

I f ( X [ k ] = 0 ) t h e n

r e t u r n ;

/ / A l l c o l o r s h a v e b e e n

u s e d .

F o r j = 1

t o

n

d o

{

/ / C h e c k

i f t h i s c o l o r i s d i s t i n c t f r o m

a d j a c e n t c o l o r .

I f ( ( G [ k , j ]

0 ) a n d ( X [ k ] =

X [ j ] ) )

/ / I f ( k , j ) i s a n

e d g e a n d

i f a d j a c e n t v e r t i c e s h a v e t h e s a m e c o l o r .

T h e n

b r e a k ;

}

i f ( j = n + 1 ) t h e n

r e t u r n ;

/ / n e w

c o l o r f o u n d .

}

u n t i l ( f a l s e ) ;

/ / o t h e r w i s e t r y

t o

f i n d

a n o t h e r c o l o r . }

T h e t i m e s p e n t b y

N e x t v a l u e t o

d e t e r m i n e t h e c h i l d r e n

i s ( m n )  T o t a l t i m e i s =

( m n n ) .

K n a p s a c k

P r o b l e m

u s i n g

B a c k t r a c k i n g :

T h e p r o b l e m

i s s i m i l a r t o

t h e z e r o - o n e ( 0 / 1 ) k n a p s a c k

o p t i m i z a t i o n

p r o b l e m

i s d y n a m i c p r o g r a m m i n g

a l g o r i t h m .

W e a r e g i v e n

‘ n ’ p o s i t i v e w e i g h t s W i a n d ’ n ’ p o s i t i v e p r o f i t s P i , a n d

a p o s i t i v e n u m b e r ‘ m ’ t h a t i s t h e k n a p s a c k

c a p a c i t y , t h g i s p r o b l e m

c a l l s f o r c h o s s i n g

a s u b s e t o f t h e w e i g h t s s u c h

t h a t ,

m

a n d

i s M a x i m i z e d .

X i  C o n s t i t u t e Z e r o - o n e v a l u e d

V e c t o r .

T h e S o l u t i o n

s p a c e i s t h e s a m e a s t h a t f o r t h e s u m

o f s u b s e t ’ s p r o b l e m .

B o u n d i n g

f u n c t i o n s a r e n e e d e d

t o

h e l p

k i l l s o m e l i v e n o d e s w i t h o u t e x p a n d i n g

t h e m . A

g o o d

b o u n d i n g

f u n c t i o n

f o r t h i s p r o b l e m

i s o b t a i n e d

b y

u s i n g

a n

u p p e r b o u n d

o n

t h e v a l u e o f t h e b e s t f e a s i b l e s o l u t i o n

o b t a i n a b l e b y

e x p a n d i n g

t h e g i v e n

l i v e n o d e .

T h e p r o f i t s a n d

w e i g h t s a r e a s s i g n e d

i n

d e s c e n d i n g

o r d e r d e p e n d

u p o n

t h e r a t i o .

( i . e ) P i / W i

P ( I + 1 ) / W ( I + 1 )

S o l u t i o n

:

A f t e r a s s i g n i n g

t h e p r o f i t a n d

w e i g h t s , w e h a v e t o

t a k e t h e f i r s t o b j e c t w e i g h t s a n d

c h e c k

i f t h e f i r s t w e i g h t i s l e s s t h a n

o r e q u a l t o

t h e c a p a c i t y , i f s o

t h e n

w e i n c l u d e t h a t o b j e c t ( i . e ) t h e u n i t i s 1 . ( i . e ) K 

1 .

T h e n

W e a r e g o i n g

t o

t h e n e x t o b j e c t , i f t h e o b j e c t w e i g h t i s e x c e e d e d

t h a t o b j e c t d o e s n a t f i t . S o

u n i t o f t h a t o b j e c t i s ‘ 0 ’ . ( i . e ) K = 0 .

T h e n

W e a r e g o i n g

t o

t h e b o u n d i n g

f u n c t i o n

, t h i s f u n c t i o n

d e t e r m i n e s a n

u p p e r b o u n d

o n

t h e b e s t s o l u t i o n

b t a i n a l \ b l e a t l e v e l K + 1 .

R e p e a t t h e p r o c e s s u n t i l w e r e a c h

t h e o p t i m a l s o l u t i o n .

A l g o r i t h m :

A l g o r i t h m

B k n a p ( k , c p , c w ) / / ‘ m ’ i s t h e s i z e o f t h e k n a p s a c k ;

‘ n ’ 

n o . o f w e i g h t s &

p r o f i t s . W [ ] & P [ ] a r e t h e / / w e i g h t s &

w e i g h t s . P [ I ] / W [ I ]

P [ I + 1 ] / W [ I + 1 ] . / / f w  F i n a l w e i g h t s o f k n a p s a c k . / / f p 

f i n a l m a x . p r o f i t . / / x [ k ] =

0

i f W [ k ] i s n o t t h e k n a p s a c k , e l s e X [ k ] = 1 . {

/ / G e n e r a t e l e f t c h i l d .

I f ( ( W + W [ k ]

m ) t h e n

{

Y [ k ] = 1 ;

I f ( k < n ) t h e n

B n a p ( k + 1 , c p + P [ k ] , C w

+ W [ k ] )

I f ( ( C p

+

p [ w ] >

f p ) a n d

( k = n ) ) t h e n

{

f p

=

c p

+

P [ k ] ;

f w

=

C w + W [ k ] ;

f o r j = 1

t o

k

d o

X [ j ] =

Y [ j ] ;

}

}

i f ( B o u n d ( c p , c w , k ) f p ) t h e n

{

y [ k ] =

0 ;

i f ( k < n ) t h e n

B n a p

( K + 1 , c p , c w ) ;

i f ( ( c p > f p ) a n d

( k = n ) ) t h e n

{

f p

=

c p ;

f w

=

c w ;

f o r j = 1

t o

k

d o

X [ j ] =

Y [ j ] ;

} } }

A l g o r i t h m

f o r B o u n d i n g

f u n c t i o n :

A l g o r i t h m

B o u n d ( c p , c w , k ) / / c p 

c u r r e n t p r o f i t t o t a l . / / c w 

c u r r e n t w e i g h t t o t a l . / / k  t h e i n d e x

o f t h e l a s t r e m o v e d

i t e m . / / m  t h e k n a p s a c k

s i z e .

{

b = c p ;

c = c w ;

f o r I = - k + 1

t o

n

d o

{

c =

c + w [ I ] ;

i f ( c < m ) t h e n

b = b + p [ I ] ;

e l s e r e t u r n

b +

( 1 - ( c - m ) / W [ I ] ) *

P [ I ] ; } r e t u r n

b ; }

E x a m p l e :

M =

6

W i =

2 , 3 , 4

4

2

2

N =

3

P i =

1 , 2 , 5

P i / W i ( i . e )

5

2

1

X i =

1

0

1 T h e m a x i m u m

w e i g h t i s 6 T h e M a x i m u m

p r o f i t i s ( 1 * 5 ) +

( 0 * 2 ) +

( 1 * 1 )

5 + 1

6 .

F p

=

( - 1 )

1

3

&

0 + 4

6

c w

=

4 , c p

=

5 , y ( 1 ) = 1

k

=

k + 2

2

3

b u t 7 > 6

s o

y ( 2 ) =

0

S o

b o u n d ( 5 , 4 , 2 , 6 )

B = 5

C = 4

I = 3

t o

3 C = 6 6

6 S o

r e t u r n

5 + ( 1 - ( 6 - 6 ) ) / ( 2 * 1 )

5 . 5

i s n o t l e s s t h a n

f p .

S o , k = k + 1

( i . e ) 3 .

3 = 3

&

4 + 2

6

c w =

6 , c p

=

6 , y ( 3 ) = 1 .

K = 4 .

I f 4 >

3

t h e n

F p

= 6 , f w = 6 , k = 3

, x ( 1 ) 1

0

1

T h e s o l u t i o n

X i 

1

0

1

P r o f i t 

6

W e i g h t  6 .

Recommended publications