-
Notifications
You must be signed in to change notification settings - Fork 0
/
knapsack.js
137 lines (123 loc) · 3.38 KB
/
knapsack.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
function knapSack1(left, value, i = 0)
{
if (i == weights.length || left == 0)
return value
if (left < weights[i])
return knapSack1(left, value, i + 1)
else
return Math.max(
knapSack1(left, value, i + 1),
knapSack1(left - weights[i], value + values[i], i + 1)
)
}
function knapSack2(left, value, i = weights.length - 1)
{
if (i == -1 || left == 0)
return value
if (left < weights[i])
return knapSack2(left, value, i - 1)
else
return Math.max(
knapSack2(left, value, i - 1),
knapSack2(left - weights[i], value + values[i], i - 1)
)
}
function knapSack3(left, i = 0)
{
if (i == weights.length || left == 0)
return 0
if (left < weights[i])
return knapSack3(left, i + 1)
else
return Math.max(
knapSack3(left, i + 1),
values[i] + knapSack3(left - weights[i], i + 1)
)
}
function knapSack4(left, i = weights.length - 1)
{
if (i == -1 || left == 0)
return 0
if (left < weights[i])
return knapSack4(left, i - 1)
else
return Math.max(
knapSack4(left, i - 1),
values[i] + knapSack4(left - weights[i], i - 1)
)
}
function knapSackMemo(left, value, cache, i = 0)
{
if (i == weights.length || left == 0)
return value
if (cache[i][left] == -1)
{
if (left < weights[i])
cache[i][left] = knapSackMemo(left, value, cache, i + 1)
else
cache[i][left] = Math.max(
knapSackMemo(left, value, cache, i + 1),
knapSackMemo(left - weights[i], value + values[i], cache, i + 1)
)
}
return cache[i][left]
}
function knapSackDynamicProgramming(left, value, cache, i = weights.length - 1)
{
if (i == -1 || left == 0)
return value
if (cache[i][left] == -1)
{
if (left < weights[i])
cache[i][left] = knapSackDynamicProgramming(left, value, cache, i - 1)
else
cache[i][left] = Math.max(
knapSackDynamicProgramming(left, value, cache, i - 1),
knapSackDynamicProgramming(left - weights[i], value + values[i], cache, i - 1)
)
}
return cache[i][left]
}
function getCache(r, c)
{
let A = new Array(r)
for (let i = 0; i < r; i++) {
A[i] = (new Array(c)).fill(-1)
}
return A
}
function printCache(A, r, c)
{
let s = ''
for (let i = 0; i < r; i++) {
for (let j = 0; j < c; j++) {
s += A[i][j] + ' '
}
s += '\n'
}
console.log(s)
}
let weights = [1, 2, 4, 2, 5]
let values = [5, 3, 5, 3, 2]
console.time("knapSack1")
console.log(knapSack1(10, 0))
console.timeEnd("knapSack1")
console.time("knapSack2")
console.log(knapSack2(10, 0))
console.timeEnd("knapSack2")
console.time("knapSack3")
console.log(knapSack3(10))
console.timeEnd("knapSack3")
console.time("knapSack4")
console.log(knapSack4(10))
console.timeEnd("knapSack4")
let cache = getCache(weights.length, 10 + 1)
console.time("knapSackMemo")
console.log(knapSackMemo(10, 0, cache))
console.timeEnd("knapSackMemo")
// printCache(cache, weights.length, 10)
cache = getCache(weights.length, 10 + 1)
console.time("knapSackDynamicProgramming")
console.log(knapSackDynamicProgramming(10, 0, cache))
console.timeEnd("knapSackDynamicProgramming")
// printCache(cache, weights.length, 10)