https://github.com/kolosovpetro/knapsackproblem
Study on the Knapsack problem
https://github.com/kolosovpetro/knapsackproblem
dynamic-programming knapsack-problem knapsack-solver
Last synced: 7 months ago
JSON representation
Study on the Knapsack problem
- Host: GitHub
- URL: https://github.com/kolosovpetro/knapsackproblem
- Owner: kolosovpetro
- Created: 2022-02-08T20:55:06.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-10-31T09:16:22.000Z (almost 3 years ago)
- Last Synced: 2025-01-16T06:15:37.221Z (9 months ago)
- Topics: dynamic-programming, knapsack-problem, knapsack-solver
- Language: C#
- Homepage:
- Size: 24.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# KnapsackProblem
Given an array P of products
- { Title = "Radio", Price = 3000, Weight = 4 },
- { Title = "Laptop", Price = 2000, Weight = 3 },
- { Title = "Guitar", Price = 1500, Weight = 1 }Given a knapsack of positive integer capacity N. Find a combination of products such that price is maximal and does not
exceed the size of knapsack.## Cases
- N = 4. Output: [Laptop, Guitar]
- N = 5. Output: [Laptop, Guitar, Guitar]## Pseudocode
- Initialize jagged array `int[K][N]` where `K` -- count of products, `N` -- is knapsack capacity
- Sort array of products by the `Weight`
- For each product:
- If current weight is lesser then product weight: add previous product weight
- Else add MAX(previous weight, current product weight + remaining weight maximum)## Example
