moongraph

MoonBit · graphs

A graph is numbers,
and the rest is edges.

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.

4
storage types
21
packages
7
file formats
4
backends
01

Three decisions the whole library is built on

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.

Weights are functions

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.

Refusals name the fault

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.

02

Four storage types, one interface

an algorithm is written once
TypeShapeParallel edgesAfter a deletionReach for it when
Graph[N, E]adjacency listyesnodes renumbered the default: build it, change it, run algorithms on it
StableGraph[N, E]adjacency list with holesyesnumbers kept you hold node numbers across deletions
Csr[N, E]compressed, both directionsyes— read-only, built once, walked a million times
Matrix[E]V × V cellsnorenumbered 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.

03

What is in the box

one package per family, so an import pays for itself

Structure

traverse · component · dag · cycle · tree · struct · iso · op

Optimisation

path · flow · matching · color · clique · approx

Measurement

centrality · community · metric

Getting graphs in and out

gen · io · io/graphml

04

Start here

# 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])