Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The brute-force idea is easy:</p> <ol> <li>Generate all possible subsets of your 20 items, saving only those which satisfy your weight constraint. If you want to be fancy, you can even only consider subsets to which you cannot add anything else without violating the weight constraint, since only these can possibly be the right answer. O(2^n)</li> <li>Find the subset with maximum weight. linear in terms of the number of candidates, and since we have O(2^n) candidates, this is O(2^n).</li> </ol> <p>Please comment if you'd like some pseudocode.</p> <p>EDIT: What the hey, here's the pseudocode just in case.</p> <pre><code> GetCandidateSubsets(items[1..N], buffer, maxw) 1. addedSomething = false 2. for i = 1 to N do 3. if not buffer.contains(item[i]) and weight(buffer) + weight(items[i]) &lt;= maxw then 4. add items[i] to buffer 5. GetCandidateSubsets(items[1..N], buffer) 6. remove items[i] from buffer 7. addedSomething = true 8. if not addedSomething then 9. emit &amp; store buffer </code></pre> <p>Note that the GetCandidateSubsets function is not very efficient, even for a brute force implementation. Thanks to amit for pointing that out. You could rework this to only walk the combinations, rather than the permutations, of the item set, as a first-pass optimization. </p> <pre><code> GetMaximalCandidate(candidates[1..M]) 1. if M = 0 then return Null 2. else then 3. maxel = candidates[1] 4. for i = 2 to M do 5. if weight(candidates[i]) &gt; weight(maxel) then 6. maxel = candidates[i] 7. return maxel </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload