Skip to content

Instantly share code, notes, and snippets.

@tron1point0
Created May 8, 2014 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tron1point0/cd3ad70022185eb04f72 to your computer and use it in GitHub Desktop.
Save tron1point0/cd3ad70022185eb04f72 to your computer and use it in GitHub Desktop.
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