1 · A first graph
A graph is built by adding nodes and edges; both answer with the number they were given. The numbers are the identity of a node: there is no handle to keep, and no lookup to make.
let g : @moongraph.Graph[String, Double] = @moongraph.Graph::new(directed=false)
let a = g.add_node("a") // 0
let b = g.add_node("b") // 1
let c = g.add_node("c") // 2
g.add_edge(a, b, 1.5) |> ignore
g.add_edge(b, c, 2.0) |> ignore
g.neighbors(a) // [1]
g.node_data(c) // "c"
An undirected edge is reported from both of its endpoints; a directed one only from its
source. Parallel edges are allowed and each keeps its own number, so
find_edge answers the lowest one and find_edges answers them all.
let e = g.add_edge(a, b, 9.0)
g.find_edges(a, b) // [0, 2] — two edges, numbers kept
g.endpoints(e) // (0, 1)
g.edge_data(e) // 9.0
Removing a node renumbers the nodes above it, which is the one operation that invalidates
numbers. StableGraph exists for the case where that matters: it leaves a hole
and never moves a node.
2 · Names instead of numbers
When the data is keyed by name, wrap the graph in Keyed: keys go in, keys come
out, and the numbers stay inside where the algorithms want them.
let inner : @moongraph.Graph[Unit, Double] = @moongraph.Graph::new()
let k = @moongraph.Keyed::new(graph=inner)
k.add_node("oslo", ()) |> ignore
k.add_node("bergen", ()) |> ignore
k.add_edge("oslo", "bergen", 3.0) |> ignore
k.node("bergen") // Some(1)
k.key(0) // Some("oslo")
Adding a key that is already there is refused rather than silently overwriting the node, and an edge whose endpoint has no key is refused too — the two sides cannot drift apart.
3 · Weights and routes
A weight is a function from edge number to cost. Graph::weights adapts one over
payloads; leaving the argument out means every edge costs one.
let g : @moongraph.Graph[Unit, Double] = @moongraph.Graph::new()
for _ in 0..<5 { g.add_node(()) |> ignore }
g.add_edge(0, 1, 1.0) |> ignore
g.add_edge(1, 2, 2.0) |> ignore
g.add_edge(0, 2, 5.0) |> ignore
g.add_edge(1, 3, 1.0) |> ignore
g.add_edge(3, 2, 2.0) |> ignore
let paths = @path.dijkstra(g, 0, weight=g.weights(fn(cost) { cost }))
paths.distance[2] // Some(3.0) — 0 → 1 → 2 beats 0 → 2
paths.path_to(2) // Some([0, 1, 2])
paths.distance[4] // None — node 4 cannot be reached
distance holds Double? and previous holds
Int?, both indexed by node number: no entry can be missing, and an unreached node
is None rather than a number that looks like a distance.
A negative weight is not a puzzle to be worked around: Dijkstra refuses it and says which edge
carried it. bellman_ford and johnson handle negatives, and refuse
only a negative cycle — the one case where no cost exists.
let outcome = try {
@path.dijkstra(g, 0, weight=w)
"answered"
} catch {
@moongraph.Refused::NegativeWeight(edge~) => "edge " + edge.to_string() + " is negative"
_ => "something else"
}
4 · Walks
Every walk visits the edges of a node in ascending neighbour number, so two runs over the
same graph agree. bfs answers the discovery order — by distance, then by node
number — and dfs the preorder of a walk that dives in.
let g : @moongraph.Graph[Unit, Unit] = @moongraph.Graph::from_edges(
[(0, 1), (1, 2), (0, 2)],
node=(),
edge=(),
directed=false,
)
@traverse.bfs(g, 0) // [0, 1, 2]
@traverse.bfs_layers(g, [0]) // [[0], [1, 2]]
@traverse.dfs(g, 0) // [0, 1, 2]
A depth-first walk can also be read as events: entering a node, leaving it, and looking at an edge back to a node still on the stack. That is what cycle detection wants, without a visitor object.
5 · What shape is this
Components, strong components and the structure that survives a single cut all answer with arrays indexed by node.
let parts = @component.components(g)
parts.count // 1
parts.label // [0, 0, 0]
@component.bridges(g) // [] — nothing here is a bridge
@component.articulation_points(g) // []
6 · Trees and flow
A spanning tree comes back as its edge numbers and its weight; a maximum flow comes back as its value and the flow on every edge.
let tree = @tree.minimum_spanning(g, weight=w)
tree.edges // [0, 1]
tree.weight // 3.0
let flow = @flow.max_flow(directed_graph, 0, 3, capacity=w)
flow.value // 2.0
flow.flow // the amount on each edge, by edge number
A minimum spanning tree refuses a graph in pieces instead of quietly returning a forest; ask
for components first if a forest is what you want.
7 · Reading and writing
Seven formats, and one reader shape: what could not be read is refused with the byte offset, the line and the column.
let text = @io.edge_list(g)
// "0 1\n1 2\n2 0\n"
let back = @io.parse_edges(text) // Graph[Unit, Double]
let same = @io.graph6(back) // one byte per six bits
let from_xml = @graphml.read(document) // labels and weights
GraphML lives in io/graphml so that the XML parser it needs stays behind that
one package; nothing in the algorithms carries it.
8 · Where to go next
The demo runs the library in the browser: pick a graph, pick an algorithm, and click a node to start somewhere else. The API reference lists every public item with the paragraph that explains it. The repository has the examples.