Nodes are numbers
From zero, dense, so an algorithm indexes an array instead of hashing a key. Names and
records live in the payload, or in Keyed, which keeps the translation at the
edge and hands the numbers inward.
MoonBit · graphs
Dense node numbers from zero, four storage types behind one interface, and twenty-one packages of algorithms written once against that interface. Build a graph, hand it to anything, and get an answer indexed by node — or a refusal that names the node at fault.
From zero, dense, so an algorithm indexes an array instead of hashing a key. Names and
records live in the payload, or in Keyed, which keeps the translation at the
edge and hands the numbers inward.
An edge payload can be anything, so a weight is (Int) -> Double over edge
numbers. Give one and the algorithm is weighted; leave it out and every edge costs one.
A negative edge in Dijkstra, a cycle in a topological order, a graph in pieces when a
spanning tree was asked for: each comes back as Refused, carrying the node,
the edge, or a witness cycle.
| Type | Shape | Parallel edges | After a deletion | Reach for it when |
|---|---|---|---|---|
Graph[N, E] | adjacency list | yes | nodes renumbered | the default: build it, change it, run algorithms on it |
StableGraph[N, E] | adjacency list with holes | yes | numbers kept | you hold node numbers across deletions |
Csr[N, E] | compressed, both directions | yes | — | read-only, built once, walked a million times |
Matrix[E] | V × V cells | no | renumbered | dense graphs, and the passes that visit every pair |
Every one implements Topology; three also implement Inbound, which
adds the view of edges coming the other way. A storage type of your own implements the same
traits and gets every algorithm without a conversion.
traverse · component · dag · cycle ·
tree · struct · iso · op
path · flow · matching · color ·
clique · approx
centrality · community · metric
gen · io · io/graphml
# add the library
moon add moonbitstack/moongraph
# then, in a package
import {
"moonbitstack/moongraph",
"moonbitstack/moongraph/path",
}
let g : @moongraph.Graph[Unit, Double] = @moongraph.Graph::new()
for _ in 0..<4 { g.add_node(()) |> ignore }
g.add_edge(0, 1, 2.0) |> ignore
g.add_edge(1, 2, 3.0) |> ignore
let paths = @path.dijkstra(g, 0, weight=g.weights(fn(cost) { cost }))
paths.distance[2] // Some(5.0)
paths.path_to(2) // Some([0, 1, 2])