Skip to content

Commit 5a15252

Browse files
12:22 dynamic
1 parent e1079ff commit 5a15252

File tree

2 files changed

+113
-48
lines changed

2 files changed

+113
-48
lines changed

daa/13./main.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// write the c code for the 0/1 knapsack for the dyanamic and greedy algorithm
2+
#include<stdio.h>
3+
int max(int x, int y){
4+
if(x > y){
5+
return x;
6+
}else{
7+
return y;
8+
}
9+
}
10+
11+
int greedy(int *w, int *v, int n, int W){
12+
int i, j, k
13+
int dynamic(int n, int w[], int v[], int W)
14+
{
15+
int i, j;
16+
int k[n+1][W+1];
17+
for (i = 0; i <= n; i++)
18+
{
19+
for (j = 0; j <= W; j++)
20+
{
21+
if (i == 0 || j == 0)
22+
{
23+
k[i][j] = 0;
24+
}
25+
else if (w[i-1] <= j)
26+
{
27+
k[i][j] = max(v[i-1] + k[i-1][j-w[i-1]], k[i-1][j]);
28+
}
29+
else
30+
{
31+
k[i][j] = k[i-1][j];
32+
}
33+
}
34+
}
35+
return k[n][W];
36+
}
37+
38+
int main(int argc, char *argv[])
39+
{
40+
int n = 3;
41+
int w[] = {2, 3, 4};
42+
int v[] = {3, 4, 5};
43+
int W = 5;
44+
45+
printf("%d\n", dynamic(n, w, v, W));
46+
return 0;
47+
}
Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,71 @@
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

Comments
 (0)