Conversation
|
@priya-sundaram-dev, please review. I have not done much with the knapsack, so perhaps I am missing something, but is this really adding educational value? When would someone reach for this algorithm? There are tons of single-letter variable names, which make the logic tricky to follow... w vs. weight vs. weights ... Is n even needed? Functions return |
|
Reviewed, @cclauss — I share your skepticism. The idea is genuinely nice: reduce 0/1 knapsack to a shortest-path problem on a layered DAG (each layer = one item, each node = a (item, weight-used) state, "take" edges carry
Verdict: the DAG-to-SPP framing is worth encouraging, but this needs (1) an actual solve step with a doctest verifying it matches the DP result, and (2) a typed edge structure. Given the pre-Oct-1 push, I'd request changes; if @author can't turn it around, close with a pointer to reopen once it solves the problem end-to-end. Happy to re-review if they add the solver. |
Description:
This pull request adds a function to generate a directed acyclic graph (DAG) representation of a classic 0/1 Knapsack problem.
The graph representation enables solving the problem using shortest/longest path algorithms in DAGs, providing an alternative approach to traditional dynamic programming solutions.
Each path from the source node s to the sink node t corresponds to a valid subset of items.
Key points:
Input:
capacity — maximum knapsack capacity (integer)
weights — list of item weights (list of integers)
values — list of item values (list of integers)
Output:
Implements Python type hints and doctests.
Code style follows PEP 8 and is Black-formatted.
Passes Ruff tests.
Checklist: