46. Graphs stdlib design (spec/lib/rationale/graphs.md, 2026-11-21)
- Searches return
Paths, not a bare distance array.bfs,bfs01,dijkstra,fixed_cost_pathsandbellman_fordreturnPaths<V, W, A>:p[v]is the distance (so existingdist[v]code reads the same), andparent,via,orderandpath(t)answer the follow-up (print the path, DP over the shortest-path DAG) without a second search. Reason: the localshortest/walk_backpair and every port that printed a path re-derived parents by hand. Cost: two more arrays of n and one store per settled vertex, measured inbench/graph/. Reverse: return.distand drop the struct. - 0-1 BFS and fixed-cost searches are named algorithms (
bfs01,fixed_cost_paths), not adijkstraoption. Reason: AGENTS.md "name the algorithm"; the deque and the per-move FIFOs have different costs and different preconditions (weights in {0, 1}; fixed non-negative move costs), and each traps when its precondition fails. Reverse: fold intodijkstra(.., via: Deque). - Unit and 0-1 searches take
from: List<V>; weighted ones takefrom: List<(V, W)>. The starting distance is the super source without an extra vertex; a unit search has nothing to offset. Reverse: accept both shapes everywhere. euler_walk, returningWalk<V, E>(start vertex plus arcs). The localeuler_pathreturned bare edge indices, losing the direction an undirected edge was walked.Walkis the same arc shape aGraphyields, sow.verticesandw.arcsneed no translation. Reverse: rename back and returnList<E>.- One spelling:
two_color. A scout port wished fortwo_colour; the stdlib keeps one name per idea. Reverse: none needed. - Promoted from
examples/local-lib/graph/:TwoSat/LitandFunctionalmove unchanged in shape;shortest/walk_backare subsumed byPaths. Minimum spanning trees stay withstd.dsu's owner. Reverse: keep the local modules. outis the one name for a vertex's arcs;Graph.adjis gone. Same signature and cost asout, whichNeighboursalready used. Reverse: addadjback as a second spelling.Graph.directed/Graph.undirectedstay two constructors, not one withdirected: Bool: directedness is fixed when the graph is built, and the list form already picks pairs or triples from the tuple shape.euler_walktakesdirected:because itsnextcannot say. Reverse: addGraph(edges, directed:)with the two as spellings.std.gridstays a thin module over(R, C)product domains. A cell is a pair of domain values, so[R, C]Tindexes by it and every graph algorithm takesp => [q for q in p.adj4 if ...]asnext; aGridtype would only rename the pair. Reverse: addstruct Grid<R, C>wrapping the cell pair withadj4/adj8as methods.- List-form hooks (
<fn>_of_pairs,<fn>_of_triples) are hidden by name from completion (cpl_check::is_list_form_hook), the VS Code tables and the generated API page. Debt: the suffix stands in for a general hidden-from-docs marker on apub fn, which does not exist yet. Reverse: list them again, or replace the suffix test with that marker. - Every search takes
until: ?V = none(bfs,bfs01,dijkstra,fixed_cost_paths): stop once that vertex is settled, soorderends there and its distances are exact. An option, not a_totwin, and?Vso one target costs one comparison per settled vertex. A set of targets or a predicate waits for a port that needs one. Reverse: drop the option; callers stopping at a target pay for the whole space.