Skip to content

skryl/xkcd-knapsack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

XKCD Knapsack Problem

You can read more about it here, here and here. There is also a running demo available here.

How it Works

To use the knapsack solver, simply drag a well formatted menu file into the plate area. Your menu file should look like the example below. Notice that the goal amount for the solver is on the first line. The parser will ignore badly formatted lines.

15.05
mixed fruit, 2.15
french fries, 2.75
side salad, 3.35
hot wings, 3.55
mozzarella sticks, 4.20
sampler plate, 5.80
barbecue, 6.55

Large menus (> 50 items) may take a while to solve. If the number of combinations is too large to be displayed then only the number of solutions will be shown.

Clicking on any platter combination will zoom to show the contents.

The Algorithm

I used an approach similar to the Polynomial Time Approximation algorithm below. In essence it builds a power set of the the price list while simultanesouly trimming all elements that are larger than the goal sum or are equal in magnitude to one another.

initialize a list S to contain one element 0.
 for each i from 1 to N do
   let T be a list consisting of xi + y, for all y in S
   let U be the union of T and S
   sort U
   make S empty 
   let y be the smallest element of U 
   add y to S 
   for each element z of U in increasing order do
      //trim the list by eliminating numbers close to one another
      //and throw out elements greater than s
     if y + cs/N < z ≤ s, set y = z and add z to S 
if S contains a number between (1 − c)s and s, output yes, otherwise no

The straight power set approach is an exponential time algorithm while the one described here is close to O(m * n). For a proof see here.

Caveats

This specific implementation does not allow for identical items to appear in a single platter combination.

About

A JS/D3 solution to the XKCD knapsack problem

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published