|
|
|
|
|
by mbmjertan
657 days ago
|
|
Your solution is not an exact solution to the knapsack problem, but a (relatively well known) greedy heuristic approach towards the problem. It is a good approach towards a solution, but the knapsack problem isn’t as trivial as it seems. For a counterexample, imagine you have a ton of items that have a weight of 10^-20 and value of 1, and a single item that has the weight of whatever is the capacity of the knapsack (so it’s only thing that fits if it’s inserted to the knapsack) and the value of +infinity. All items have their cost ≈ 0, so whether you fit all of the “light but valueless” items or the single “heavy but extremely valuable” item depends on how you sort when costs are equal. An improved heuristic would take the value into account more than just having it as a factor, but it’s not easy to design. I would recommend finding an open course on algorithm design and analysis and going through the lessons while trying to extrapolate approaches to problems instead of just solutions to them. |
|
can point me to where to find more about this.
> For a counterexample, imagine you have a ton of items that have a weight of 10^-20 and value of 1, and a single item that has the weight of whatever is the capacity of the knapsack (so it’s only thing that fits if it’s inserted to the knapsack) and the value of +infinity.
that is rather an extreme example. i do not see the how it affects the validity of the solution in most cases.
> I would recommend finding an open course on algorithm design and analysis and going through the lessons while trying to extrapolate approaches to problems instead of just solutions to them.
implementing solutions is the concrete result of extrapolating approaches and choosing the best one to implement. and that is the pragmatic approach.