# `kgrid` — Minimum-Perimeter Convex Polygon Containing $k$ Lattice Points Anonymous Rust implementation and reproduction artifact for computing the minimum Euclidean perimeter $L_k$ (and an optimal convex lattice polygon) containing exactly $k$ integer grid points. ## Quick Links * **Tables & Results:** [RESULTS.md](RESULTS.md) · [`results/scaling.csv`](results/scaling.csv) · [`results/spread_7_13.csv`](results/spread_7_13.csv) · [`results/spread_14.csv`](results/spread_14.csv) · [`results/rowmax_compare.csv`](results/rowmax_compare.csv) · [`results/k_1_10000.csv`](results/k_1_10000.csv) · [LaTeX tables (`paper/data/`)](paper/data/) * **Graphs:** * [Optimal polygons for $k = 3,\dots,12$ (`graphs/polygons_small.png`)](graphs/polygons_small.png) * [Single-query running time up to $k = 10^{16}$ (`graphs/single_query_time.png`)](graphs/single_query_time.png) * [Batch DP work per radius (`graphs/batch_work.png`)](graphs/batch_work.png) * [Structural scaling of optimal polygons (`graphs/polygon_structure.png`)](graphs/polygon_structure.png) * **Code:** * [`Cargo.toml`](Cargo.toml) · [`src/main.rs`](src/main.rs) (CLI) · [`src/solver.rs`](src/solver.rs) (certified windows & per-radius solver) · [`src/dp.rs`](src/dp.rs) (grouped knapsack DP & Hirschberg recovery) · [`src/smawk.rs`](src/smawk.rs) (SMAWK row maxima) · [`src/tree.rs`](src/tree.rs) (Stern–Brocot cut trees) · [`src/polygon.rs`](src/polygon.rs) (vertex reconstruction & Pick's theorem verification) · [`src/oracle.rs`](src/oracle.rs) (unrestricted knapsack oracle) * **Scripts:** [`scripts/`](scripts/) (`compare.rb`, `scaling.rb`, `spread.rb`, `rowmax_compare.rb`, `paper_data.rb`, `paper_polys.rb`) ## Graphs Preview | Optimal polygons ($k=3,\dots,12$) | Single-query scaling ($10^4 \le k \le 10^{16}$) | | :---: | :---: | | ![Optimal polygons for k=3..12](graphs/polygons_small.png) | ![Single-query running time](graphs/single_query_time.png) | | Batch DP work per radius ($m \le 1784$) | Polygon structural laws ($k \le 10^{16}$) | | :---: | :---: | | ![Batch DP work](graphs/batch_work.png) | ![Polygon structure statistics](graphs/polygon_structure.png) | ## Building and Running Requires a Rust toolchain (`rustc` / `cargo`, edition 2021; no external crate dependencies) and Ruby (standard library only) for the benchmark scripts. ```bash # Build optimized binary and run unit tests (DP & SMAWK checks) cargo test cargo build --release ``` ### Single query (`query`) ```bash # Compute L_k, bounding box, certified window stats, and verify polygon target/release/kgrid query 1000000000000 # Write the counterclockwise integer vertices to poly_1e12.txt target/release/kgrid query 1000000000000 --poly-file out/poly_1e12.txt # Perimeter only (skip polygon recovery) target/release/kgrid query 1000000000000000 --no-poly ``` ### Batch range (`range`) and exact oracle (`oracle`) ```bash mkdir -p out # Solve all 1 <= k <= 10^5 on 3 threads and write CSV results target/release/kgrid range 1 100000 --threads 3 --out out/range_100000.csv --stats out/stats_100000.csv # Run the unrestricted knapsack oracle (no windows/certificates) and compare target/release/kgrid oracle 100000 --out out/oracle_100000.csv ruby scripts/compare.rb out/range_100000.csv out/oracle_100000.csv ``` ## Reproducing Tables and Plots All scripts expect the release binary at `target/release/kgrid` and write CSVs/logs relative to the repository root: ```bash mkdir -p out out/polys_10000 # 1. Batch run 1..10^7 + per-radius stats (~76 s on 3 threads with polygons, ~12 s with --no-poly) target/release/kgrid range 1 10000000 --threads 3 --out out/range_1e7.csv --stats out/stats_1e7.csv --no-poly # 2. Single-query scaling table (10^4..10^15; 10^16 takes ~80 min and ~12 GB RAM) ruby scripts/scaling.rb 4 15 --mem-limit 4 | tee out/scaling.csv # 3. Within-decade robustness sweep (solves normally and with --inflate 2) ruby scripts/spread.rb 7 12 3 1 | tee out/spread_7_12.csv # 4. Group-update comparison: SMAWK vs. divide-and-conquer vs. direct sweep ruby scripts/rowmax_compare.rb out/rowmax_compare.csv 100000000 1000000000 10000000000 100000000000 # 5. Extract fitted exponents, .dat plot files, and LaTeX tables into paper/data/ ruby scripts/paper_data.rb paper/data # 6. Export TikZ polygon figures (requires polygon files in out/polys_10000/ or out/) ruby scripts/paper_polys.rb 2.2 3 4 5 6 7 8 9 10 11 12 ``` See [RESULTS.md](RESULTS.md) for the recorded benchmark tables and verification summary.