| Crates.io | xdp-knapsack |
| lib.rs | xdp-knapsack |
| version | 0.1.0 |
| created_at | 2025-12-25 21:25:58.392339+00 |
| updated_at | 2025-12-25 21:25:58.392339+00 |
| description | Experimental 0/1 knapsack implementations in Rust focused on optimized XDP approximations and heuristics. |
| homepage | https://github.com/RustedBytes/xdp-knapsack |
| repository | https://github.com/RustedBytes/xdp-knapsack |
| max_upload_size | |
| id | 2004972 |
| size | 39,853 |
Experimental 0/1 knapsack implementations in Rust, focused on optimized XDP approximations plus supporting heuristics.
Paper: An O(n log n) approximate knapsack algorithm (Nick Dawes).
solve_xdp_optimized: optimized XDP solver that returns profit only.solve_xdp_optimized_with_selection: optimized XDP with backtracking; uses O(n * T) memory.
Selection is auto-disabled if bins exceed 65535 or the backtracking table would overflow.approximate_knapsack: XDP approximation with error bound reporting.modified_greedy_pmax: greedy heuristic with fractional upper bound.Item is a simple tuple of identifiers and floating point values:
Item { id: usize, profit: f64, weight: f64 }
KnapsackResult exposes:
max_profit: best profit foundselected_items: list of selected item IDs (empty if selection tracking is disabled)algorithm_name: algorithm labelduration_micros: elapsed time in microsecondspmax: optional fractional upper bound used for error reportingerror_bound: optional relative error bound in [0, 1]If you are using this repository as a path dependency:
xdp_knapsack = { path = "." }
use xdp_knapsack::{approximate_knapsack, solve_xdp_optimized_with_selection, Item};
let items = vec![
Item { id: 0, profit: 10.0, weight: 5.0 },
Item { id: 1, profit: 7.0, weight: 3.0 },
Item { id: 2, profit: 8.0, weight: 4.0 },
];
let capacity = 8.0;
let result = solve_xdp_optimized_with_selection(items.clone(), capacity);
println!("profit={:.2} items={:?}", result.max_profit, result.selected_items);
let approx = approximate_knapsack(items, capacity).expect("approx failed");
println!("profit={:.2} err={:?}", approx.max_profit, approx.error_bound);
The demo in src/main.rs generates a synthetic dataset and runs each solver.
cargo run --release
Python 3.10+ bindings live in python/README.md.
cargo build --release
MIT License. See LICENSE file for details.