-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapsackProblem.swift
More file actions
executable file
·61 lines (54 loc) · 2.4 KB
/
KnapsackProblem.swift
File metadata and controls
executable file
·61 lines (54 loc) · 2.4 KB
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
//
// Created by Daniel Heredia on 2/27/18.
// Copyright © 2018 Daniel Heredia. All rights reserved.
//
// Knapsack problem
// Given weights and values of n items, put these items in a knapsack of capacity
// W to get the maximum total value in the knapsack. In other words, given two
// integer arrays val[0..n-1] and wt[0..n-1] which represent values and weights
// associated with n items respectively. Also given an integer W which represents
// knapsack capacity, find out the maximum value subset of val[] such that sum of
// the weights of this subset is smaller than or equal to W. You cannot break an
// item, either pick the complete item, or don’t pick it (0-1 property).
// This solution could work with floating points but the time complexity would be O(2^n)
//func knapsackProblem(maxWeight: Int, items: [(weight: Int, value: Int)]) -> Int {
// return knapsackProblem(maxWeight: maxWeight, lastIndex: items.count, items: items)
//}
//
//func knapsackProblem(maxWeight: Int, lastIndex: Int, items: [(weight: Int, value: Int)]) -> Int {
// if lastIndex == 0 || maxWeight <= 0 {
// return 0
// }
// let i = lastIndex - 1
// if items[i].weight > maxWeight {
// return knapsackProblem(maxWeight: maxWeight, lastIndex: i, items: items)
// } else {
// let with = items[i].value + knapsackProblem(maxWeight: (maxWeight - items[i].weight), lastIndex: i, items: items)
// let without = knapsackProblem(maxWeight: maxWeight, lastIndex: i, items: items)
// return max(with, without)
// }
//}
// Dynamic programming solution
func knapsackProblem(maxWeight: Int, items: [(weight: Int, value: Int)]) -> Int {
let n = items.count
var solutions = [[Int]](repeating: [Int](repeating: 0, count: maxWeight + 1), count: n + 1)
for i in 1...n {
for w in 1...maxWeight {
if items[i - 1].weight > w {
solutions[i][w] = solutions[i - 1][w]
} else {
let with = items[i - 1].value + solutions[i - 1][w - items[i - 1].weight]
let without = solutions[i - 1][w]
solutions[i][w] = max(with, without)
}
}
}
return solutions[n][maxWeight]
}
let maxWeight = 50
var items = [(weight: Int, value: Int)]()
items.append((10, 60))
items.append((20, 100))
items.append((30, 120))
let maxValue = knapsackProblem(maxWeight: maxWeight, items: items)
print("Max value: \(maxValue)")