Design decisions

46. Graphs stdlib design (spec/lib/rationale/graphs.md, 2026-11-21)

  1. Searches return Paths, not a bare distance array. bfs, bfs01, dijkstra, fixed_cost_paths and bellman_ford return Paths<V, W, A>: p[v] is the distance (so existing dist[v] code reads the same), and parent, via, order and path(t) answer the follow-up (print the path, DP over the shortest-path DAG) without a second search. Reason: the local shortest/walk_back pair 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 in bench/graph/. Reverse: return .dist and drop the struct.
  2. 0-1 BFS and fixed-cost searches are named algorithms (bfs01, fixed_cost_paths), not a dijkstra option. 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 into dijkstra(.., via: Deque).
  3. Unit and 0-1 searches take from: List<V>; weighted ones take from: 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.
  4. euler_walk, returning Walk<V, E> (start vertex plus arcs). The local euler_path returned bare edge indices, losing the direction an undirected edge was walked. Walk is the same arc shape a Graph yields, so w.vertices and w.arcs need no translation. Reverse: rename back and return List<E>.
  5. One spelling: two_color. A scout port wished for two_colour; the stdlib keeps one name per idea. Reverse: none needed.
  6. Promoted from examples/local-lib/graph/: TwoSat/Lit and Functional move unchanged in shape; shortest/walk_back are subsumed by Paths. Minimum spanning trees stay with std.dsu's owner. Reverse: keep the local modules.
  7. out is the one name for a vertex's arcs; Graph.adj is gone. Same signature and cost as out, which Neighbours already used. Reverse: add adj back as a second spelling.
  8. Graph.directed/Graph.undirected stay two constructors, not one with directed: Bool: directedness is fixed when the graph is built, and the list form already picks pairs or triples from the tuple shape. euler_walk takes directed: because its next cannot say. Reverse: add Graph(edges, directed:) with the two as spellings.
  9. std.grid stays a thin module over (R, C) product domains. A cell is a pair of domain values, so [R, C]T indexes by it and every graph algorithm takes p => [q for q in p.adj4 if ...] as next; a Grid type would only rename the pair. Reverse: add struct Grid<R, C> wrapping the cell pair with adj4/adj8 as methods.
  10. 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 a pub fn, which does not exist yet. Reverse: list them again, or replace the suffix test with that marker.
  11. Every search takes until: ?V = none (bfs, bfs01, dijkstra, fixed_cost_paths): stop once that vertex is settled, so order ends there and its distances are exact. An option, not a _to twin, and ?V so 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.