Skip to content

Commit dce1bbc

Browse files
committed
KnapSack Problem Recursion and Memoization
1 parent 8cdf024 commit dce1bbc

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

DynamicProgramming/KnapSack.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Knap Sack Problem.
3+
*/
4+
5+
package main
6+
7+
import ("fmt"
8+
)
9+
10+
// Returns the max value of the KnapSack
11+
func KnapSackRecursion(ItemWeight,ItemValue []int,n,c int) int {
12+
if (n == 0 || c == 0) {
13+
return 0
14+
} else if (ItemWeight[n-1] > c) {
15+
return KnapSackRecursion(ItemWeight,ItemValue,n-1,c)
16+
} else {
17+
tmp1 := KnapSackRecursion(ItemWeight,ItemValue,n-1,c)
18+
tmp2 := ItemValue[n-1] + KnapSackRecursion(ItemWeight,ItemValue,n-1,c-ItemWeight[n-1])
19+
if tmp1 >= tmp2 {
20+
return tmp1
21+
} else {
22+
return tmp2
23+
}
24+
}
25+
}
26+
27+
// Memoized version of the problem, which minimizes recusrsion
28+
func KnapSackMemoization(MemoizedMap map[string]int,ItemWeight,ItemValue []int,n,c int) int {
29+
nc := fmt.Sprintf("%d:%d",n,c)
30+
if val,ok := MemoizedMap[nc];ok {
31+
return val
32+
}
33+
if (n == 0 || c == 0) {
34+
return 0
35+
} else if (ItemWeight[n-1] > c) {
36+
return KnapSackMemoization(MemoizedMap,ItemWeight,ItemValue,n-1,c)
37+
} else {
38+
tmp1 := KnapSackMemoization(MemoizedMap,ItemWeight,ItemValue,n-1,c)
39+
tmp2 := ItemValue[n-1] + KnapSackMemoization(MemoizedMap,ItemWeight,ItemValue,n-1,c-ItemWeight[n-1])
40+
if tmp1 >= tmp2 {
41+
MemoizedMap[nc] = tmp1
42+
} else {
43+
MemoizedMap[nc] = tmp2
44+
}
45+
return MemoizedMap[nc]
46+
}
47+
}
48+
49+
func main() {
50+
capacity := 10
51+
n := 5
52+
ItemWeight := []int {1,2,4,2,5}
53+
ItemValue := []int {5,3,5,3,2}
54+
var MemoizedMap = make(map[string]int)
55+
valueRecursive := KnapSackRecursion(ItemWeight,ItemValue,n,capacity)
56+
fmt.Println("The max value in the knapsack from Recursion is:",valueRecursive)
57+
valueMemoized := KnapSackMemoization(MemoizedMap,ItemWeight,ItemValue,n,capacity)
58+
fmt.Println("The max value in the knapsack from Memoization is:",valueMemoized)
59+
60+
}

DynamicProgramming/KnapSack_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
KnapSack problem Unit Test
3+
*/
4+
5+
package main
6+
7+
import ("testing")
8+
9+
func TestKnapSack(t *testing.T) {
10+
11+
ItemWeight := []int {1,2,4,2,5}
12+
ItemValue := []int {5,3,5,3,2}
13+
var MemoizedMap = make(map[string]int)
14+
n := 5
15+
16+
tables := []struct {
17+
capacity,maxValue int
18+
} {
19+
{1,5},
20+
{2,5},
21+
{4,8},
22+
{5,11},
23+
{6,11},
24+
}
25+
26+
for _,table := range tables {
27+
//total := KnapSackRecursion(ItemWeight,ItemValue,n,table.capacity)
28+
total := KnapSackMemoization(MemoizedMap,ItemWeight,ItemValue,n,table.capacity)
29+
if total != table.maxValue {
30+
t.Errorf("KnapSack value of capacity %d is incorrect, got:%d, want: %d.",table.capacity,total,table.maxValue)
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)