TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The Knapsack Problem

24 pointsby specularalmost 11 years ago

9 comments

Domenic_Salmost 11 years ago
Does this actually answer the stated problem of &quot;deciding the subset of items to pack&quot;?<p>Looking at the solution table I can see that there are two possible solutions, and I can see a max <i>value</i> of 6. But I can&#x27;t see how v[4,3] implies packing objects 1 and 3.
评论 #8183821 未加载
评论 #8183573 未加载
评论 #8183568 未加载
评论 #8183557 未加载
mgraczykalmost 11 years ago
I wrote a solution using this table building DP algorithm a few years ago.<p><pre><code> https:&#x2F;&#x2F;github.com&#x2F;mgraczyk&#x2F;DiscreteOptimization&#x2F;blob&#x2F;master&#x2F;knapsack&#x2F;speed_up&#x2F;solve_it.cpp </code></pre> This was part of an assignment for a Coursera Discrete Optimization course created by The University of Melbourne. It&#x27;s a great course and I recommend it to anybody who wants to hone their understanding of dynamic programming and solving computationally expensive problems.<p><pre><code> https:&#x2F;&#x2F;www.coursera.org&#x2F;course&#x2F;optimization</code></pre>
评论 #8183919 未加载
chris_vaalmost 11 years ago
For those of you planning on interviewing...<p>Having done interviews for a large tech firm, dynamic programming questions are a favorite for weeding out candidates. Anecdotally, it correlates rather well with problem solving skills.
评论 #8183534 未加载
codeheroalmost 11 years ago
Off the top of my head I look at it as a graph 2 coloring problem. The graph starts out with 1 node per item and zero edges. If we think two items will be in the same set (either in the knapsack or out) we merge the nodes and their weight and value combine into the new node. We draw edges between two nodes if their combined weights exceed the knapsack capacity. When we recurse out of our merge choice, we also draw an edge between the two nodes that composed the merge. Our recursion depth ends at a clique.
hayksaakianalmost 11 years ago
Interesting problem. If you play a game like elder scrolls skyrim or oblivion, you run into this problem literally.<p>Without reading, I already knew the solution.<p>To solve it in your head: sort items by most value&#x2F;weight efficient to least efficient.<p>Then take as much as you can until you&#x27;re full.<p>(in a game like elder scrolls oblivion, that means jewelry first, and silver vases last)<p>-----<p>edit: I am mistaken, as some of the below comments accurately pointed out.
评论 #8183817 未加载
评论 #8183832 未加载
评论 #8183854 未加载
pramalinalmost 11 years ago
May be relevant: there is a cool game - knapsack on a radial graph, with Scala source code. <a href="http://krishnanraman.github.io/scala-js/examples/helloworld/helloworld.html" rel="nofollow">http:&#x2F;&#x2F;krishnanraman.github.io&#x2F;scala-js&#x2F;examples&#x2F;helloworld&#x2F;...</a>
Retricalmost 11 years ago
This still can easily be worse than n^2 if your weight is significantly higher than your number of objects. Aka max weight of 10,000 and 1,000 objects.<p>Edit: Note, n^2 is generally far better than 2^n so it&#x27;s still useful even if it&#x27;s slow.
评论 #8183604 未加载
alta22433almost 11 years ago
I was asked this on a recent programming interview. As a recent college graduate, I&#x27;m not sure this is a fair question to ask under that context given the complexity of the problem. Nice article though.
评论 #8183415 未加载
评论 #8183464 未加载
评论 #8183796 未加载
评论 #8183572 未加载
lunzalmost 11 years ago
This is a domain where Unconventional Computing seems to work well, e.g., &quot;DNA computing&quot; through its massively parallel processing capabilities.