Skip to content

Commit 496ed65

Browse files
committed
Problem 170: 0-1 Knapsack problem
1 parent 7b49406 commit 496ed65

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 169 |
9+
| Total Problems | 170 |
1010

1111
</center>
1212

@@ -106,6 +106,7 @@ Include contains single header implementation of data structures and some algori
106106
| Maximum Value Contigous Subsequence Problem [wiki](https://en.wikipedia.org/wiki/Maximum_subarray_problem)| [max_subsequence.cpp](dynamic_programming_problems/max_subsequence.cpp)|
107107
| Catalan number - Count the number of possible Binary Search Trees with n keys | [catalan_number.cpp](dynamic_programming_problems/catalan_number.cpp)|
108108
| Calculate the number of unique paths from source origin (0, 0) to destination (m-1, n-1) in a m x n grid. You can only move either in down or right direction.|[unique_paths.cpp](dynamic_programming_problems/unique_paths.cpp)|
109+
| 0-1 Knapsack Problem: Imagine you are a thief and you want to steal things with room full of things. You have a knapsack which can handle maximum capacity of weight W, and you want to fill it up such that it's worth is maximum. Being an intelligent thief, you know weights and values of each item in room. How would you fill your knapsack, such that you get the maximum possible value|[0_1_knapsack_problem.cpp](dynamic_programming_problems/0_1_knapsack_problem.cpp)|
109110

110111
### Tree Problems
111112
| Problem | Solution |
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* 0-1 Knapsack Problem: Imagine you are a thief and you want to steal things with room full of things.
3+
* You have a knapsack which can handle maximum capacity of weight W, and you want to fill it up such
4+
* that it's worth is maximum. Being an intelligent thief, you know weights and values of each item in room.
5+
* How would you fill your knapsack, such that you get the maximum possible value.
6+
*
7+
* The problem essentially boils down to whether item i would be part of your loot or not.
8+
* This has optimal substructure:
9+
* case 1: Item I is in your loot:
10+
* Then the maximum value of the loot is determined by remaining n-1 items whose possible weight would be
11+
* W-(weight of Item I)
12+
*
13+
* case 2: Item I is not in your loot:
14+
* Then the maximum value of the loot is determined by remaining n-1 items whose possible weight would be
15+
* W. (i.e excluding the ith item)
16+
*
17+
* We could get all subsets of possible items in your loot, and choose the one with maximum value.
18+
*
19+
* Since this problem will have overlapping subproblems, we could use dynamic programming to solve it.
20+
* So that we don't recurse to calculate the same subproblem which we have already calculated.
21+
*
22+
*/
23+
24+
#include <iostream>
25+
#include <vector>
26+
27+
int maximum_possible_loot_value(const std::vector<int>& weights,
28+
const std::vector<int>& values, const int capacity)
29+
{
30+
int items_count = values.size();
31+
std::vector<std::vector<int>> loots(items_count + 1, std::vector<int>(capacity + 1));
32+
for (int i = 0; i <= items_count; ++i) {
33+
for (int w = 0; w <= capacity; ++w) {
34+
if (i == 0 || w == 0) {
35+
loots[i][w] = 0;
36+
}
37+
else if (weights[i-1] <= w) {
38+
loots[i][w] = std::max(values[i-1] + loots[i-1][w-weights[i-1]], loots[i-1][w]);
39+
} else {
40+
loots[i][w] = loots[i-1][w];
41+
}
42+
}
43+
}
44+
return loots[items_count][capacity];
45+
}
46+
47+
void print_vector(const std::vector<int>& vec) {
48+
for (auto v : vec) {
49+
std::cout << v << " ";
50+
}
51+
std::cout << std::endl;
52+
}
53+
54+
int main()
55+
{
56+
std::vector<int> values{40, 10, 90};
57+
std::vector<int> weights{20, 40, 60};
58+
int capacity = 100;
59+
std::cout << "Weights of items: ";
60+
print_vector(weights);
61+
std::cout << "Values of items: ";
62+
print_vector(values);
63+
std::cout << "Capacity :" << capacity << std::endl;
64+
std::cout << "Maximum possible loot value for capacity " << capacity
65+
<< ": " << maximum_possible_loot_value(weights, values, capacity) << std::endl;
66+
return 0;
67+
}

0 commit comments

Comments
 (0)