Skip to content

Instantly share code, notes, and snippets.

@likr
Created October 3, 2012 17:42
Show Gist options
  • Save likr/3828522 to your computer and use it in GitHub Desktop.
Save likr/3828522 to your computer and use it in GitHub Desktop.
DP for Knapsack Problem
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