-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknapsackDP.c
78 lines (71 loc) · 1.47 KB
/
knapsackDP.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Lab Program 6
// 0/1 Knapsack Problem (dynamic programming)
// Time Complexity: O(nW)
// n -> no of items ; W -> knapsack capacity
#include <stdio.h>
int i, j, n, c, w[10], p[10], v[10][10];
int max(int a, int b)
{
return ((a > b) ? a : b);
}
void knapsack(int n, int w[10], int p[10], int c)
{
for(i = 0; i <= n; i++)
{
for(j = 0; j <= c; j++)
{
if(i == 0 || j == 0)
v[i][j] = 0;
else if (w[i] > j)
v[i][j] = v[i - 1][j];
else
v[i][j] = max(v[i-1][j], (v[i-1][j-w[i]]+p[i]));
}
}
printf("the maximum profit is : %d", v[n][c]);
printf("\n\t________Table________\n");
for(i = 0; i <= n; i++)
{
for(j = 0; j <= c; j++)
{
printf("%d\t", v[i][j]);
}
printf("\n");
}
}
void main()
{
printf("enter the no of objects:\n");
scanf("%d", &n);
printf("enter the weights:\n");
for(i = 1; i <= n; i++)
{
scanf("%d", &w[i]);
}
printf("enter the profits:\n");
for(i = 1; i <= n; i++)
{
scanf("%d", &p[i]);
}
printf("enter the capacity:\n");
scanf("%d", &c);
knapsack(n, w, p, c);
}
/*
sample input output:
enter the no of objects:
4
enter the weights:
2 1 3 2
enter the profits:
12 10 20 15
enter the capacity:
5
the maximum profit is : 37
________Table________
0 0 0 0 0 0
0 0 12 12 12 12
0 10 12 22 22 22
0 10 12 22 30 32
0 10 12 22 30 37
*/