-
Notifications
You must be signed in to change notification settings - Fork 0
/
KnapsackProblemTree.c
64 lines (55 loc) · 1.33 KB
/
KnapsackProblemTree.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
/**
*
* @file KnapsackProblemTree.c
* @author Max Base (maxbasecode@gmail.com)
* @brief Knapsack Problem Implementation In C Using Tree Structure
* @version 0.1
* @date 2022-12-12
* @copyright Copyright (c) 2022
*
*/
#include <stdio.h>
#include <stdlib.h>
int max(int a, int b)
{
return a > b ? a : b;
}
int main(int argc, char **argv)
{
int *values = (int *)malloc(sizeof(int) * 4);
values[0] = 1;
values[1] = 2;
values[2] = 3;
values[3] = 4;
int *weights = (int *)malloc(sizeof(int) * 4);
weights[0] = 1;
weights[1] = 2;
weights[2] = 2;
weights[3] = 1;
int capacity = 5;
int n = 4;
int **dp = (int **)malloc(sizeof(int *) * (n + 1));
for (int i = 0; i < n + 1; i++)
{
dp[i] = (int *)malloc(sizeof(int) * (capacity + 1));
}
for (int i = 0; i < n + 1; i++)
{
for (int j = 0; j < capacity + 1; j++)
{
if (i == 0 || j == 0)
{
dp[i][j] = 0;
}
else if (weights[i - 1] <= j)
{
dp[i][j] = max(values[i - 1] + dp[i - 1][j - weights[i - 1]], dp[i - 1][j]);
}
else
{
dp[i][j] = dp[i - 1][j];
}
}
}
return 0;
}