Skip to content

Commit 8af1c85

Browse files
committed
Algorithm for 0-1 Knapsack Problem
1 parent d104334 commit 8af1c85

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

algorithm/etc/dp/desc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"sliding_window": "Finding the largest sum of three contiguous number",
1313
"max_sum_path": "Finding the maximum sum in a path from (0, 0) to (N-1, M-1) when can only move to right or down",
1414
"longest_increasing_subsequence": "Find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order",
15-
"max_subarray": "Find the sum of the maximum Subarray in the given Array"
15+
"max_subarray": "Find the sum of the maximum Subarray in the given Array",
16+
"knapsack_problem": "Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible."
1617
}
1718
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
for ( var i = 0; i <= N; i++ ) {
3+
for( var j = 0; j <= W; j++ ) {
4+
if( i === 0 || j === 0 ) {
5+
/*
6+
If we have no items or maximum weight we can take in collection is 0
7+
then the total weight in our collection is 0
8+
*/
9+
DP[i][0] = 0;
10+
tracer._notify( i, j, DP[i][j])._wait();
11+
tracer._denotify( i, j);
12+
} else if ( wt[i-1] <= j ) { // take the current item in our collection
13+
14+
dataViewer1._select(i-1)._wait();
15+
dataViewer2._select(i-1)._wait();
16+
tracer._select( i-1, j)._wait();
17+
18+
var A = val[i - 1] + DP[i - 1][j - wt[i - 1]];
19+
var B = DP[i - 1][j];
20+
/*
21+
find the maximum of these two values
22+
and take which gives us a greater weight
23+
*/
24+
if (A > B) {
25+
DP[i][j] = A;
26+
tracer._notify( i, j, DP[i][j])._wait();
27+
} else {
28+
DP[i][j] = B;
29+
tracer._notify( i, j, DP[i][j])._wait();
30+
}
31+
32+
tracer._deselect( i-1, j);
33+
tracer._denotify( i, j);
34+
dataViewer2._deselect(i-1);
35+
dataViewer1._deselect(i-1);
36+
37+
} else { // leave the current item from our collection
38+
39+
DP[i][j] = DP[i - 1][j];
40+
tracer._notify( i, j, DP[i][j])._wait();
41+
tracer._denotify( i, j);
42+
}
43+
}
44+
}
45+
46+
logger._print(' Best value we can achieve is ' + DP[N][W]);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var val = [1,4,5,7]; // The value of all available items
2+
var wt = [1,3,4,5]; // The weights of available items
3+
var W = 7; // The maximum weight we can carry in our collection
4+
var N = val.length;
5+
var DP = new Array(N+1);
6+
7+
for (var i = 0; i < N + 1; i++) {
8+
DP[i] = new Array(W+1);
9+
for (var j = 0; j < W + 1; j++) {
10+
DP[i][j] = 0;
11+
}
12+
}
13+
14+
var tracer = new Array2DTracer()._setData(DP);
15+
var dataViewer1 = new Array1DTracer()._setData(val);
16+
var dataViewer2 = new Array1DTracer()._setData(wt);
17+
var logger = new LogTracer();

0 commit comments

Comments
 (0)