Created
October 3, 2012 17:42
-
-
Save likr/3828522 to your computer and use it in GitHub Desktop.
DP for Knapsack Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type item = {w:int; p:int} | |
(* 2004, Kellerer et al., Knapsack Problems, p.23 *) | |
let dp2 items c = | |
let n = Array.length items in | |
let z = Array.make (c + 1) 0 in | |
for j = 0 to n - 1 do | |
let {w=wj; p=pj} = items.(j) in | |
for d = c downto wj do | |
let o = z.(d - wj) + pj in | |
if o > z.(d) then | |
z.(d) <- o | |
done | |
done; | |
z.(c) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment