From 28212ea80883c0a124e2e3eb21d1c4c8f92a8e0c Mon Sep 17 00:00:00 2001 From: Alvreda Ivena Date: Wed, 17 Dec 2014 15:32:48 +0700 Subject: [PATCH] added Knapsack Unbound --- KnapsackUnbounded.go | 72 ++++++++++++++++++++++++++++++++++++++++++++ README.md | 2 +- 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 KnapsackUnbounded.go diff --git a/KnapsackUnbounded.go b/KnapsackUnbounded.go new file mode 100644 index 0000000..83a9c8d --- /dev/null +++ b/KnapsackUnbounded.go @@ -0,0 +1,72 @@ +package main + +import "fmt" + +type item struct { + name string + value int + weight, volume float64 +} + +type Result struct { + counts []int + sum int +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} + +//O(n!) +func knap(wants []item, w, v float64) (res Result) { + if len(wants) == 0 { + return + } + n := len(wants) - 1 + maxCount := min(int(w/wants[n].weight), int(v/wants[n].volume)) + for count:=0; count<=maxCount; count++ { + sol := knap(wants[:n], + w-float64(count)*wants[n].weight, + v-float64(count)*wants[n].volume) + sol.sum += wants[n].value * count + if sol.sum > res.sum { + sol.counts = append(sol.counts, count) + res = sol + } + } + return +} + +func main() { + items := []item{ + //nama, value, weight, volume + {"Mangga", 3000, 0.3, 0.025}, + {"Pisang", 1800, 0.2, 0.015}, + {"Jambu", 2500, 2.0, 0.002}, + {"Nanas", 1500, 1.2, 0.012}, + } + var sumCount, sumValue int + var sumWeight, sumVolume float64 + + result := knap(items, 30, 0.25) + + fmt.Println("Knapsack Unbound") + fmt.Println("Berat Maksimum : 25") + fmt.Println("Volume Maksimum : 0.25") + fmt.Println("Item yang dapat dibawa :") + for i:=0; i Berat: %.3f Volume: %.3f Value: %d\n", + items[i].name, result.counts[i], items[i].weight*float64(result.counts[i]), + items[i].volume*float64(result.counts[i]), items[i].value*result.counts[i]) + + sumCount += result.counts[i] + sumValue += items[i].value * result.counts[i] + sumWeight += items[i].weight * float64(result.counts[i]) + sumVolume += items[i].volume * float64(result.counts[i]) + } + + fmt.Printf("Total %d -> Berat: %.3f Volume: %.3f Value: %d\n", sumCount, sumWeight, sumVolume, sumValue) +} \ No newline at end of file diff --git a/README.md b/README.md index f62c386..2e68c77 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ ChangeLog: * 2014-12-15 added BinarySearch (Siti) * 2014-12-15 added SliceHeap (Siti) * 2014-12-15 added Skiplist (Cicih) -* 2014-12-17 added Knapsack 0-1 (Alvreda) +* 2014-12-17 added Knapsack 0-1, Knapsack Unbounded (Alvreda) TODO: =====