Documentation
¶
Overview ¶
Example 30_min_spanning_tree — minimum-cost backbone design over one shared, immutable CSR snapshot: it builds a seeded, scale-parametrised geographic site network, then computes its minimum spanning tree with BOTH of GoGraph's MST algorithms — Prim (search.PrimMST) and Kruskal (search.KruskalMST) — and cross-checks them against each other as a correctness oracle.
Domain — laying a least-cost fibre backbone ¶
The scenario is a telecom/utility planner who must interconnect a set of physical sites (exchanges, cabinets, substations) with the cheapest possible cable run. Every site has a 2-D geographic position; the cost of a candidate link is its Euclidean distance in metres. Many candidate links exist (the planner surveyed more routes than they will build), so the optimal backbone is the minimum-weight set of links that keeps every site connected — exactly a minimum spanning tree (one connected region) or a minimum spanning forest (several regions that are not interconnected).
The seeded generator lays out `regions` metro areas separated horizontally on the plane. Within a region it first wires a random spanning tree (so the region is always connected), then adds `extra-edges` redundant candidate links per site to nearby-or-random peers, so the graph has strictly more candidate links than a tree and the MST algorithms must genuinely SELECT the cheapest connecting subset rather than copy a fixed tree. When -interconnect is set (the default) consecutive regions are chained by one long inter-region link, yielding a single connected backbone; with -interconnect=false the regions stay disjoint and the result is a spanning FOREST of `regions` trees.
Correctness oracle ¶
Prim and Kruskal are independent algorithms; on the same undirected weighted graph they must agree. run() asserts, and returns a "MODULE BUG" error if any of these fail (the failing config and seed are a complete repro):
- equal TOTAL weight — Prim summed over every connected component equals Kruskal's whole-graph total;
- equal sorted multiset of edge WEIGHTS — a theorem: all minimum spanning trees of a graph share the same multiset of edge weights, so this holds even when ties let the two algorithms pick different edges;
- when every candidate weight is distinct the MST is unique, so the two algorithms must select the identical set of undirected links;
- spanning-forest shape — exactly V-K edges for V live sites in K components, and V_c-1 edges within each component c (a tree).
If any check trips, the discrepancy is surfaced as an error rather than silently reported as a passing fact.
Scale ¶
Run with no flags, the example builds a small deterministic default (3 regions of 40 sites, 2 extra links per site, interconnected — 120 sites) that the regression test pins and that runs in microseconds. Every dimension is a flag, so the same binary scales up to where the per-algorithm cost is observable:
go run ./examples/30_min_spanning_tree -regions 4 -sites 200000 -extra-edges 3 -seed 7
That is ~800 000 sites and ~3.2M candidate links; Kruskal's edge sort then dominates and its wall-clock and allocation cost become measurable. The deterministic facts are reproducible for a fixed -seed; only the telemetry (lines prefixed with "# ") varies between runs and machines.