8000 minigraf-algorithms: add DFS, shortest path, topological sort, connected components by adityamukho · Pull Request #15 · project-minigraf/minigraf-examples · GitHub
[go: up one dir, main page]

Skip to content

minigraf-algorithms: add DFS, shortest path, topological sort, connected components - #15

Merged
adityamukho merged 2 commits into
mainfrom
claude/gallant-feynman-yxadbk
Sep 26, 2026
Merged

adityamukho merged 2 commits into
mainfrom
claude/gallant-feynman-yxadbk

Conversation

@adityamukho
Copy link
Copy Markdown
Contributor

Closes #12.

Summary

This PR adds four graph algorithms to minigraf-algorithms. Each one is a free function that takes &impl GraphView and runs in O(V + E).

Function Returns
depth_first(graph, start) Nodes reachable from start, in depth-first (preorder) order. Like reachable, it leaves out start unless a cycle leads back to it.
shortest_path(graph, from, to) Option<Vec<Value>> with the fewest-hop path, both ends included.
topological_sort(graph) Result<Vec<Value>>. Uses Kahn's algorithm. Returns an error that names a node in the cycle if the graph is cyclic.
connected_components(graph) Weakly connected components. Edge direction is ignored.

Breaking change: GraphView::nodes()

Topological sort and connected components must see every node, including nodes with no outgoing edges. outgoing() alone cannot give that, so GraphView now has a required nodes() method.

  • EdgeList keeps nodes in the order it first sees them. Ties in the sort and the order of components are the same on every run.
  • Any GraphView implemented outside this repo must add nodes(). The crate is not on crates.io yet, so I bumped it to 0.2.0.

CI fix

The workflow ran plain cargo build and cargo test. These cover only the root package, so minigraf-algorithms and minigraf-schema were never tested in CI. Both commands now use --workspace.

Tests

  • Each algorithm has EdgeList unit tests. They cover cycles, self-loops, edge direction, and an empty graph.
  • Each algorithm also has one test that loads data from a live Minigraf database.
  • cargo test --workspace passes locally. Clippy shows no warnings on the crate.

Docs: each function has doc comments with its time complexity. The crate README has a new table of the algorithms and a note on implementing GraphView.

🤖 Generated with Claude Code

https://claude.ai/code/session_018zSbkRnGmq7ekEJZZQtr4e


Generated by Claude Code

…connected components

Closes #12.

- Add `nodes()` to `GraphView` so whole-graph algorithms can see nodes
  with no outgoing edges. `EdgeList` keeps nodes in first-seen order,
  which gives stable results.
- Add `depth_first`, `shortest_path` (unweighted, BFS),
  `topological_sort` (Kahn, returns an error on cycles), and
  `connected_components` (weakly connected).
- Document each function with its time complexity.
- Add EdgeList unit tests and live Minigraf tests for each algorithm.
- Bump crate to 0.2.0 because the new trait method breaks outside
  `GraphView` implementations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018zSbkRnGmq7ekEJZZQtr4e
Plain `cargo build` and `cargo test` only cover the root package, so
minigraf-algorithms and minigraf-schema were never tested in CI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018zSbkRnGmq7ekEJZZQtr4e
@adityamukho
adityamukho merged commit bbe3927 into main Sep 26, 2026
1 check passed
@adityamukho
adityamukho deleted the claude/gallant-feynman-yxadbk branch September 26, 2026 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

post-1.0: add graph traversal algorithms to minigraf-algorithms

2 participants

0