GU
gustavo-rolim/Knapsack-Solver
This is a simple dynamic programming solver in Julia to solve 0-1 Knapsack Problems.
Knapsack-Solver
This repository provides a dynamic programming solver in Julia for the 0-1 Knapsack Problem.
The solver contains two main scripts:
- gen.jl — Generates a random knapsack instance and saves it as a
.csvfile. An accompanying file, example.csv, is provided to illustrate the expected CSV format and can be used to test the solver immediately. - knapsack_solver.jl — Reads a
.csvinstance and solves it using dynamic programming. The algorithm runs in pseudo-polynomial time O(W·n), where W is the knapsack capacity and n is the number of items in the instance. The solver outputs the optimal total value and the list of selected items.
Usage
Generate an Instance
- Run the generator with the following syntax, e.g.:
julia gen.jl --n 8 --min_val 1 --max_val 15 --min_weight 1 --max_weight 10 --capacity 10 --filename example.csv- Alternatively, use the provided example.csv file.
Solve the instance
- Run the solver by passing the filename as an argument:
julia knapsack_solver.jl --file example.csvOutput example
Optimal objective: 27.0
Selected items: [5, 6, 7]
Run time: 0.0
Requirements
- Julia 1.11 or later
- Packages:
CSV,DataFrames,ArgParse
On this page
Contributors
MIT License
Created August 29, 2025
Updated September 9, 2025