File tree 2 files changed +140
-0
lines changed
0/1 Knapsack Problem using Dynamic Programming Approach
2 files changed +140
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" standalone =" yes" ?>
2
+ <CodeBlocks_project_file >
3
+ <FileVersion major =" 1" minor =" 6" />
4
+ <Project >
5
+ <Option title =" knapsack" />
6
+ <Option pch_mode =" 2" />
7
+ <Option compiler =" gcc" />
8
+ <Build >
9
+ <Target title =" Debug" >
10
+ <Option output =" bin/Debug/knapsack" prefix_auto =" 1" extension_auto =" 1" />
11
+ <Option object_output =" obj/Debug/" />
12
+ <Option type =" 1" />
13
+ <Option compiler =" gcc" />
14
+ <Compiler >
15
+ <Add option =" -g" />
16
+ </Compiler >
17
+ </Target >
18
+ <Target title =" Release" >
19
+ <Option output =" bin/Release/knapsack" prefix_auto =" 1" extension_auto =" 1" />
20
+ <Option object_output =" obj/Release/" />
21
+ <Option type =" 1" />
22
+ <Option compiler =" gcc" />
23
+ <Compiler >
24
+ <Add option =" -O2" />
25
+ </Compiler >
26
+ <Linker >
27
+ <Add option =" -s" />
28
+ </Linker >
29
+ </Target >
30
+ </Build >
31
+ <Compiler >
32
+ <Add option =" -Wall" />
33
+ </Compiler >
34
+ <Unit filename =" main.c" >
35
+ <Option compilerVar =" CC" />
36
+ </Unit >
37
+ <Extensions >
38
+ <lib_finder disable_auto =" 1" />
39
+ </Extensions >
40
+ </Project >
41
+ </CodeBlocks_project_file >
Original file line number Diff line number Diff line change
1
+ /*Yash Ashok Shirsath*/
2
+ /*0/1 Knapsack Problem using Dynamic Programming Approach*/
3
+
4
+ #include <stdio.h>
5
+
6
+ void kp (int n ,int pro [],int wt [],int m );
7
+ int max (int a ,int b );
8
+
9
+ void main ()
10
+ {
11
+ int i ,j ,n ;
12
+ int wt [20 ], pro [20 ];
13
+ int m ;
14
+
15
+ printf ("Enter no. of weights:" );
16
+ scanf ("%d" ,& n );
17
+ printf ("Enter the capacity:" );
18
+ scanf ("%d" ,& m );
19
+
20
+ printf ("Enter the weights\n" );
21
+ for (i = 1 ;i <=n ;i ++ )
22
+ {
23
+ printf ("\nWT %d:" ,i );
24
+ scanf ("%d" ,& wt [i ]);
25
+ }
26
+
27
+ printf ("Enter the profit\n" );
28
+ for (i = 1 ;i <=n ;i ++ )
29
+ {
30
+ printf ("\npro %d:" ,i );
31
+ scanf ("%d" ,& pro [i ]);
32
+ }
33
+ kp (n ,pro ,wt ,m );
34
+ }
35
+
36
+ int max (int a ,int b )
37
+ {
38
+ return ((a > b )?a :b );
39
+ }
40
+
41
+
42
+ void kp (int n ,int pro [],int wt [],int m )
43
+ {
44
+ int x [20 ],v [20 ][20 ],i ,j ;
45
+ for (i = 1 ;i <=n ;i ++ )
46
+ {
47
+ x [i ]= 0 ;
48
+ }
49
+ for (i = 0 ;i <=n ;i ++ )
50
+ {
51
+ v [i ][0 ]= 0 ;
52
+ }
53
+ for (i = 0 ;i <=m ;i ++ )
54
+ {
55
+ v [0 ][i ]= 0 ;
56
+ }
57
+ for (i = 1 ;i <=n ;i ++ )
58
+ {
59
+ for (j = 1 ;j <=m ;j ++ )
60
+ {
61
+ if (wt [i ]> j )
62
+ {
63
+ v [i ][j ]= v [i - 1 ][j ];
64
+ }
65
+ else
66
+ {
67
+ v [i ][j ]= max (v [i - 1 ][j ],pro [i ]+ v [i - 1 ][j - wt [i ]]);
68
+ }
69
+ }
70
+ }
71
+
72
+ printf ("Output:\n" );
73
+
74
+ for (i = 0 ;i <=n ;i ++ )
75
+ {
76
+ for (j = 0 ;j <=m ;j ++ )
77
+ {
78
+ printf ("%d " ,v [i ][j ]);
79
+ }
80
+ printf ("\n" );
81
+ }
82
+
83
+
84
+ printf ("THE PROFIT IS:%d" ,v [n ][m ]);
85
+ printf ("\n" );
86
+ i = n ;
87
+ j = m ;
88
+ while (i != 0 )
89
+ {
90
+ if (v [i ][j ]!= v [i - 1 ][j ])
91
+ {
92
+ x [i ]= 1 ;
93
+ j = j - wt [i ];
94
+ }
95
+ i = i - 1 ;
96
+ }
97
+ for (i = 1 ;i <=n ;i ++ )
98
+ printf ("%d " ,x [i ]);
99
+ }
You can’t perform that action at this time.
0 commit comments