You are on page 1of 5

A8.

0/1 KNAPSACK USING DYNAMIC PROGRAMMING Algorithm Knapsack /*This is an implementation of an algorithm to solve Knapsack problem*/ Input: Number of items, maximum weight in the sack, array of benefits an weights !utput: "est item to be chosen with maximum profit #tep $: %ea the number of items an maximum weight using comman line arguments #tep &: %epeat through #tep ' for (int n ) $* n +) N* n,,#tep ': %ea the profit an weight #tep .: %epeat through #tep / for (int n ) $* n +) N* n,,#tep 0: %epeat through #tep / for (int w ) $* w +) 1* w,,#tep 2: option$ ) opt3n4$53w5 #tep 6: option& ) Integer78IN9:;<=> #tep /: if (weight3n5 +) wthen /7$: option& ) profit3n5 , opt3n4$53w4weight3n55 /7&: opt3n53w5 ) 8ath7max(option$, option&/7': sol3n53w5 ) (option& ? option$#tep @: %epeat through #tep $A for (int n ) N, w ) 1* n ? A* n44#tep $A: if (sol3n53w5then $A7$: take3n5 ) true $A7&: w ) w 4 weight3n5 else $A7': take3n5 ) false #tep $$: %epeat through #tep $& for(int w)A*w+)1*w,,#tep $&: "3A53w5)A #tep $': %epeat through step $. for(int i)A*i+)N*i,,#tep $.: "3i53A5)A #tep $0: %epeat through #tep $0 for(int i)$*i+)N,$*i,,#tep $2: %epeat through #tep $0 for(int w)$*w+)1,$*w,,#tep $6: if(weight3i4$5+)wthen $67$: if((profit3i4$5,"3i4$53w4weight3i4$55-?"3i4$53w5then "3i53w5)profit3i4$5,"3i4$53w4weight3i4$55 else "3i53w5)"3i4$53w5 else "3i53w5)"3i4$53w5 #tep $/: Brint the "enefit 8atrix, "3535 #tep $@: Brint all the item number, weights an specify whether it is taken or not taken #tep &A: %epeat through #tep for (int n ) $* n +) N* n,,#tep &$: if(take3n5))truethen &$7$: sumweight,)weight3n5 &$7&: sumprofit,)profit3n5 #tep &&: Brint Ctotal weightD an Ctotal profitD #tep &': >n

PROGRAM: package knapsack* import Eava7io7** public class hello F public static voi main(#tring35 args- throws I!>xceptionF int N ) Integer7parseInt(args3A5-* int 1 ) Integer7parseInt(args3$5-* int35 profit ) new int3N,$5* int35 weight ) new int3N,$5* for (int n ) $* n +) N* n,,- F #ystem7out7print(G>nter the profitG,(n-,G:G-* "uffere %ea er b)new "uffere %ea er(new Input#tream%ea er(#ystem7in--* #tring prof)b7rea <ine(-* profit3n5 ) Integer7parseInt(prof-* #ystem7out7print(G>nter the weightG,(n-,G:G-* #tring weig)b7rea <ine(-* weight3n5)Integer7parseInt(weig-* H int3535 opt ) new int3N,$531,$5* boolean3535 sol ) new boolean3N,$531,$5* for (int n ) $* n +) N* n,,- F for (int w ) $* w +) 1* w,,- F int option$ ) opt3n4$53w5* int option& ) Integer78IN9:;<=>* if (weight3n5 +) w- option& ) profit3n5 , opt3n4$53w4weight3n55* opt3n53w5 ) 8ath7max(option$, option&-* sol3n53w5 ) (option& ? option$-* H H boolean35 take ) new boolean3N,$5* for (int n ) N, w ) 1* n ? A* n44- F if (sol3n53w5- F take3n5 ) true* w ) w 4 weight3n5* H else F take3n5 ) false* H H int "3535)new int30A530A5* for(int w)A*w+)1*w,,"3A53w5)A* for(int i)A*i+)N*i,,"3i53A5)A* for(int i)$*i+)N,$*i,,F for(int w)$*w+)1,$*w,,F if(weight3i4$5+)wF

if((profit3i4$5,"3i4$53w4weight3i4$55-?"3i4$53w5F "3i53w5)profit3i4$5,"3i4$53w4weight3i4$55* H else "3i53w5)"3i4$53w5* H else "3i53w5)"3i4$53w5* H H #ystem7out7println(GInInIn">N>JIT 8;T%IKIn9999999 999999G-* for(int w)$*w+)1*w,,F #ystem7out7println(-* for(int i)$*i+)N,$*i,,#ystem7out7print("3i53w5,GItG-* H #ystem7out7println(GInInG-* #ystem7out7println(GItemG , G G , GBrofitG , G G , G1eightG , GItG , GTakeInG-* for (int n ) $* n +) N* n,,- F #ystem7out7println(n , GItG , profit3n5 , GItG , weight3n5 , GItG , take3n5-* H int sumweight)A,sumprofit)A* for (int n ) $* n +) N* n,,F if(take3n5))trueF #ystem7out7println(GThe item taken isG,n-* sumweight,)weight3n5* sumprofit,)profit3n5* H H #ystem7out7println(GInThe maximum capacity of the sack G,1-* #ystem7out7println(GInThe total number of item is G,N-* #ystem7out7println(GInThe total weight is G,sumweight-* #ystem7out7println(GInThe total profit is G,sumprofit-* #ystem7out7println(-* H H

OUTPUT: T ST CAS 1: >nter the number of items: . >nter the maximum weight: $A >nter the profit$:' >nter the weight$:& >nter the profit&:. >nter the weight&:' >nter the profit':0 >nter the weight':. >nter the profit.:2 >nter the weight.:0 ! N "IT MATRI# A A A A A ' ' ' A ' . . A ' . 0 A ' 6 6 A ' 6 / A ' 6 @ A ' 6 @ A ' 6 $& A ' 6 $& A ' . 0 6 / @ $A $& $'

Item Brofit 1eight Take $ & ' . ' . 0 2 & ' . 0 true true false true

The item taken is$ The item taken is& The item taken is. The maximum capacity of the sack $A The total number of item is . The total weight is $A The total profit is $'

T ST CAS $: >nter the number of items: . >nter the maximum weight: 0 >nter the profit$:' >nter the weight$:& >nter the profit&:. >nter the weight&:' >nter the profit':0 >nter the weight':. >nter the profit.:2 >nter the weight.:0 ! N "IT MATRI# A A A A A A ' ' ' ' A ' . . 6 A ' . 0 6 A ' . 0 6

Item Brofit 1eight Take $ ' & & . ' ' 0 . . 2 0 The item taken is$ The item taken is& true true false false

The maximum capacity of the sack 0 The total number of item is . The total weight is 0 The total profit is 6

You might also like