MoonPath is a MoonBit graph algorithms and pathfinding library. It targets reusable infrastructure for MoonBit ecosystem projects: game maps, routing tools, compiler passes, dependency planning, workflow engines, visualization tools, and any code that needs graph traversal.
This repository is prepared for the MoonBit 开源生态项目贡献赛 track. The current implementation includes a usable core, tests, a runnable demo, CI configuration, and contest submission materials under docs/.
Features
Directed weighted graph with explicit nodes and edges.
Dynamic graph mutation with edge removal, node removal, and edge clearing.
Weighted graph summaries for total, minimum, and maximum edge cost.
Edge-list export and reconstruction with Arc[N], plus node-filtered induced subgraphs and reachable subgraphs.
Safe all-or-nothing graph construction from external edge lists with try_from_arcs.
JSON and line-oriented text import/export for Graph[String] and Grid.
BFS, BFS-tree, and DFS traversal helpers for directed graphs.
Dijkstra, bidirectional Dijkstra, A*, and bidirectional A* shortest paths for non-negative weighted graphs, shortest-path tree export, DAG path enumeration, DAG longest paths for critical-path analysis, plus explicit path scoring, one-source distance maps, and all-pairs distance maps.
Bellman-Ford helpers for negative-edge path and distance queries over raw Arc[N] inputs.
test "summary demo" {
let graph = @moonpath.Graph::new()
graph.add_edge("A", "B", 2)
graph.add_edge("B", "C", 3)
let all = graph.all_pairs_distances()
guard all.get("A") is Some(from_a) else { fail("missing A") }
assert_eq(from_a.get_or_default("C", -1), 5)
assert_true(graph.has_path("A", "C"))
assert_true(graph.diameter() == Some(5))
}
Shortest-path trees:
test "shortest path tree demo" {
let graph = @moonpath.Graph::new()
graph.add_edge("A", "B", 5)
graph.add_edge("A", "C", 2)
graph.add_edge("C", "B", 1)
let tree = graph.shortest_path_tree("A")
assert_eq(tree.length(), 2)
}
Traversal tree export:
test "bfs tree demo" {
let graph = @moonpath.Graph::new()
graph.add_edge("A", "B", 1)
graph.add_edge("A", "C", 2)
graph.add_edge("B", "D", 3)
let tree = graph.bfs_tree("A")
assert_eq(tree.length(), 3)
}
Reachable subgraphs:
test "reachable subgraph demo" {
let graph = @moonpath.Graph::new()
graph.add_edge("A", "B", 1)
graph.add_edge("B", "C", 2)
graph.add_edge("X", "Y", 3)
let subgraph = graph.reachable_subgraph("A")
assert_eq(subgraph.node_count(), 3)
assert_true(!subgraph.contains_node("X"))
}
Edge-list workflows:
test "edge list demo" {
let graph = @moonpath.Graph::new()
graph.add_edge("A", "B", 2)
graph.add_edge("B", "C", 3)
let rebuilt = @moonpath.Graph::from_arcs(graph.arcs())
assert_true(rebuilt.path_cost(["A", "B", "C"]) == Some(5))
let subgraph = graph.induced_subgraph(node => node != "C")
assert_true(!subgraph.contains_node("C"))
}
Safe graph construction:
test "safe build demo" {
let arcs = [
@moonpath.Arc::{ from: "A", to: "B", cost: 2 },
@moonpath.Arc::{ from: "B", to: "C", cost: 3 },
]
assert_true(@moonpath.Graph::try_from_arcs(arcs) is Some(_))
}
Serialization:
test "serialization demo" {
let graph = @moonpath.Graph::new()
graph.add_node("isolated")
graph.add_edge("A", "B", 2)
let json = graph.to_json_string()
assert_true(@moonpath.Graph::from_json_string(json) is Some(_))
let text = graph.to_text()
assert_true(@moonpath.Graph::from_text(text) is Some(_))
}
Bellman-Ford for negative edges:
test "bellman ford demo" {
let arcs = [
@moonpath.Arc::{ from: "S", to: "A", cost: 4 },
@moonpath.Arc::{ from: "A", to: "T", cost: -2 },
]
guard @moonpath.bellman_ford_path(["S", "A", "T"], arcs, "S", "T")
is Some(path) else {
fail("expected a path")
}
assert_eq(path.cost, 2)
}
docs/final-submission.md: final contest submission summary.
docs/submission-form-answers.md: copy-ready contest form answers.
docs/competition-requirements.md: extracted requirements from the contest page.
Release Status
The contest MVP roadmap items are implemented: serialization, generated property checks, bidirectional A*, negative-edge Bellman-Ford helpers, and timed benchmark summaries are all present. Version v0.30.1 is tagged and published as a GitHub Release, with GitHub, GitLink, and the desktop artifact folder synchronized.
License
Apache-2.0.
关于
MoonBit graph and pathfinding toolkit for MoonBit open-source ecosystem competition
MoonPath
MoonPath is a MoonBit graph algorithms and pathfinding library. It targets reusable infrastructure for MoonBit ecosystem projects: game maps, routing tools, compiler passes, dependency planning, workflow engines, visualization tools, and any code that needs graph traversal.
This repository is prepared for the
MoonBit 开源生态项目贡献赛track. The current implementation includes a usable core, tests, a runnable demo, CI configuration, and contest submission materials underdocs/.Features
Arc[N], plus node-filtered induced subgraphs and reachable subgraphs.try_from_arcs.Graph[String]andGrid.Arc[N]inputs.moonbitlang/core/benchtiming JSON.Install
Install MoonBit from the official site, then run:
This repository was verified with:
Usage
Grid pathfinding:
Weighted terrain and 8-direction pathfinding:
Graph analysis:
Dependency direction:
Distance maps and DAG layers:
DAG critical path:
DAG path enumeration:
All-pairs distances and graph summary:
Shortest-path trees:
Traversal tree export:
Reachable subgraphs:
Edge-list workflows:
Safe graph construction:
Serialization:
Bellman-Ford for negative edges:
Dynamic graph updates:
Weighted graph summaries:
Directed graph structure queries:
Bulk grid updates:
Grid resizing:
Obstacle inflation:
Grid reachability and open regions:
Path smoothing:
Path validation:
Run the CLI demo:
Expected output:
Run the benchmark smoke command:
Expected output:
Repository Layout
types.mbt: public data types.graph_core.mbt: graph construction, edge queries, degree/source/sink queries, and transpose.graph_search.mbt: BFS, Dijkstra, bidirectional Dijkstra, A*, bidirectional A*, Bellman-Ford, path scoring, and distance summaries.graph_traversal.mbt: reachability, ancestors/descendants, reachable subgraphs, path existence, BFS tree export, and DFS traversal.graph_components.mbt: weak/strong components, connectivity predicates, and DAG helpers.grid.mbt: grid construction, terrain updates, terrain clearing, obstacle inflation, reachable regions, connectivity predicates, rectangular updates, grid neighbors, and grid pathfinding.serialization.mbt: JSON and text import/export for string graphs and grids.moonpath.mbt: package-level entry point.moonpath_test.mbt: blackbox behavior tests.cmd/main: runnable example.cmd/bench: deterministic benchmark smoke scenarios for grid, sparse DAG, and dense graph workloads..github/workflows/ci.yml: CI for format, build, tests, demo, and benchmark smoke.docs/project-proposal.md: one-page contest proposal draft.docs/development-report.md: completion report draft.docs/complexity.md: complexity guide and API selection notes.docs/api.md: public API index.docs/error-semantics.md: abort,None, and empty collection behavior.docs/release-checklist.md: release and repository metadata checklist.docs/submission-checklist.md: GitHub/GitLink submission checklist.docs/final-submission.md: final contest submission summary.docs/submission-form-answers.md: copy-ready contest form answers.docs/competition-requirements.md: extracted requirements from the contest page.Release Status
The contest MVP roadmap items are implemented: serialization, generated property checks, bidirectional A*, negative-edge Bellman-Ford helpers, and timed benchmark summaries are all present. Version
v0.30.1is tagged and published as a GitHub Release, with GitHub, GitLink, and the desktop artifact folder synchronized.License
Apache-2.0.