1- // write a program for the knapsack problem using greedy solution in c
21
3- #include <stdio.h>
2+ #include <stdio.h>
43
5- int main ()
4+ void
5+ main ()
66{
7- int n , i , j , t , m , p [100 ], w [100 ], x [100 ], tp = 0 , tw = 0 ;
8- float u ;
9- printf ( "Enter the number of objects : " );
10- scanf ( "%d" , & n );
11- printf ( "Enter the weights and profits of each object : " );
12- for (i = 0 ; i < n ; i ++ )
13- scanf ( "%d %d" , & w [i ], & p [i ]);
14- printf ( "Enter the capacity of knapsack : " );
15- scanf ( "%d" , & m );
16- for (i = 0 ; i < n ; i ++ )
17- x [i ] = 0 ;
18- for (i = 0 ; i < n ; i ++ )
19- for (j = i + 1 ; j < n ; j ++ )
20- if (p [i ] < p [j ])
21- {
22- t = p [i ];
23- p [i ] = p [j ];
24- p [j ] = t ;
25- t = w [i ];
26- w [i ] = w [j ];
27- w [j ] = t ;
28- }
29- u = m ;
30- for (i = 0 ; i < n ; i ++ )
7+ int capacity , no_items , cur_weight , item ;
8+ int used [10 ];
9+ float total_profit ;
10+ int i ;
11+ int weight [10 ];
12+ int value [10 ];
13+ int j ;
14+ printf ("Enter the capacity of knapsack:\n" );
15+ scanf ("%d" , & capacity );
16+
17+ printf ("Enter the number of items:\n" );
18+ scanf ("%d" , & no_items );
19+
20+ printf ("Enter the weight and value of %d item:\n" , no_items );
21+ for (i = 0 ; i < no_items ; i ++ )
3122 {
32- if (w [i ] > u )
33- break ;
34- else
35- {
36- x [i ] = 1 ;
37- tw = tw + w [i ];
38- tp = tp + p [i ];
39- u = u - w [i ];
40- }
23+ printf ("Weight[%d]:\t" , i );
24+ scanf ("%d" , & weight [i ]);
25+ printf ("Value[%d]:\t" , i );
26+ scanf ("%d" , & value [i ]);
4127 }
42- if (i < n )
43- x [i ] = u / w [i ];
44- tw = tw + (u * x [i ]);
45- tp = tp + (p [i ] * x [i ]);
46- printf ( "The result vector is : " );
47- for (i = 0 ; i < n ; i ++ )
48- printf ( "%d " , x [i ]);
49- printf ( "Maximum profit is : %d " , tp );
50-
51- printf ("\n" );
52- return 0 ;
53- }
28+
29+ for (i = 0 ; i < no_items ; ++ i )
30+ used [i ] = 0 ;
31+
32+ cur_weight = capacity ;
33+ do
34+ {
35+
36+
37+
38+ j = j + 1 ;
39+ item = -1 ;
40+ for (i = 0 ; i < no_items ; ++ i )
41+ if ((used [i ] == 0 ) &&
42+ ((item == -1 )
43+ || ((float ) value [i ] / weight [i ] >
44+ (float ) value [item ] / weight [item ])))
45+ item = i ;
46+
47+ used [item ] = 1 ;
48+ cur_weight -= weight [item ];
49+ total_profit += value [item ];
50+ printf ("%d" , i );
51+ if (cur_weight >= 0 )
52+ printf
53+ ("Added object %d (%d Rs., %dKg) completely in the bag. Space left: %d.\n" ,
54+ item + 1 , value [item ], weight [item ], cur_weight );
55+ else
56+ {
57+ int item_percent =
58+ (int ) ((1 + (float ) cur_weight / weight [item ]) * 100 );
59+ printf ("Added %d%% (%d Rs., %dKg) of object %d in the bag.\n" ,
60+ item_percent , value [item ], weight [item ], item + 1 );
61+ total_profit -= value [item ];
62+ total_profit +=
63+ (1 + (float ) cur_weight / weight [item ]) * value [item ];
64+ }
65+
66+ }
67+ while (cur_weight > 0 && j < no_items );
68+
69+ printf ("Filled the bag with objects worth %.2f Rs.\n" , total_profit );
70+ }
71+
0 commit comments