Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bounded knapsack problem
import Data.List
type Weight = Int
type Value = Int
type Item = (String,Weight,Value)
type Pack = ([String],Weight,Value)
knapsack :: (Pack -> Bool) -> [Item] -> [Pack]
knapsack f = sortBy (value) . filter (f) . map (foldl (pack) ([],0,0)) . tail . subsequences
where value (_,_,v1) (_,_,v2) = compare v2 v1
pack (ns,ws,vs) (n,w,v) = (n:ns,ws+w,vs+v)
maxWeight max (_,weight,_) = weight <= max
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment