MoonBit Classic Graph Algorithm Library
API

Every public item, package by package

Generated from the compiler's interface files and the comments beside each declaration, so nothing here is out of date with the code. The name is the item itself; the signature is shown as the compiler sees it.

moongraph130 items

suberror Refused
Why a graph was refused.
fn Refused::at_offset( text : StringView, at : Int32, got : String, want : String, ) -> Refused
The refusal for a byte offset, with the line and column computed from the text it points into. Parsers report positions in all three units because a caller may be reading a file (line and column) or a byte stream (offset).
Refused::equal
Derived: whether two values of this type are the same, field by field.
Refused::not_equal
Derived: whether two values of this type differ.
Refused::to_repr
Derived: the debug view of a value, used when an assertion fails.
struct AllPairs
The result of an all-pairs search: a matrix of costs and a matrix of successors.
fn AllPairs::path( self : AllPairs, from : Int32, to : Int32, ) -> Array[Int32]?
The cheapest route from from to to, both included, or None when there is none.
fn AllPairs::reached(self : AllPairs, from : Int32, to : Int32) -> Bool
Whether to is reachable from from.
struct Cliques
A set of cliques.
struct Coloring
A colouring: a colour per node and how many colours were used.
struct Components
A labelling of nodes by component.
struct Condensation
The condensation of a directed graph: one node per strong component.
struct Cover
An edge cover: the edges that touch every node.
struct Csr[N, E]
A read-only snapshot of a graph in compressed adjacency form: one flat array of neighbours per direction, with an offset per node.
impl[N, E] Topology for Csr[N, E] with fn directed(self)
impl[N, E] Topology for Csr[N, E] with fn edge_count(self)
fn[N, E] Csr::edge_data(self : Csr[N, E], edge : Int32) -> E
The payload of an edge.
impl[N, E] Topology for Csr[N, E] with fn endpoints(self, edge)
fn[N, E] Csr::from(graph : Graph[N, E]) -> Csr[N, E]
Builds the snapshot from a Graph.
fn[N, E] Csr::from_stable(graph : StableGraph[N, E]) -> Csr[N, E]
Builds the snapshot from a StableGraph. Vacant numbers stay vacant: the snapshot keeps the same node numbering, which is the point of the stable form.
impl[N, E] Inbound for Csr[N, E] with fn in_incident(self, node)
impl[N, E] Inbound for Csr[N, E] with fn in_neighbors(self, node)
impl[N, E] Topology for Csr[N, E] with fn incident(self, node)
impl[N, E] Topology for Csr[N, E] with fn neighbors(self, node)
impl[N, E] Topology for Csr[N, E] with fn node_count(self)
fn[N, E] Csr::node_data(self : Csr[N, E], node : Int32) -> N
The payload of a node.
fn[N, E] Csr::to_graph(self : Csr[N, E]) -> Graph[N, E]
Back to a mutable graph carrying the same payloads and the same numbering.
fn[N, E] Csr::weights( self : Csr[N, E], of : (E) -> Double, ) -> (Int32) -> Double
A weight function over edge numbers, derived from one over payloads.
struct Cut
A cut: the value it separates, and which side each node falls on.
struct Flow
A feasible flow: its value and the flow on every edge.
struct Graph[N, E]
An adjacency-list graph: directed or undirected, parallel edges allowed, one payload per node and one per edge.
fn[N, E] Graph::add_edge( self : Graph[N, E], from : Int32, to : Int32, data : E, ) -> Int32
Adds an edge from from to to carrying data, and returns its number. In an undirected graph the edge is reported from both endpoints.
fn[N, E] Graph::add_node(self : Graph[N, E], data : N) -> Int32
Adds a node and returns its number.
fn[N, E] Graph::clear(self : Graph[N, E]) -> Unit
Removes every node and every edge.
fn[N, E] Graph::clear_edges(self : Graph[N, E]) -> Unit
Removes every edge, keeping the nodes.
fn[N, E] Graph::contains_edge(self : Graph[N, E], edge : Int32) -> Bool
Whether the edge exists and has not been removed.
fn[N, E] Graph::contains_node(self : Graph[N, E], node : Int32) -> Bool
Whether a node number is inside the graph.
fn[N, E] Graph::copy(self : Graph[N, E]) -> Graph[N, E]
An independent copy. Payloads are shared, because they are values the caller owns; the structure is not.
impl[N, E] Topology for Csr[N, E] with fn directed(self)
impl[N, E] Topology for Csr[N, E] with fn edge_count(self)
fn[N, E] Graph::edge_data(self : Graph[N, E], edge : Int32) -> E
The payload of an edge.
fn[N, E] Graph::edges(self : Graph[N, E]) -> Iter[Int32]
Every live edge number, ascending.
impl[N, E] Topology for Csr[N, E] with fn endpoints(self, edge)
fn[N, E] Graph::find_edge( self : Graph[N, E], from : Int32, to : Int32, ) -> Int32?
The lowest-numbered edge from from to to, or None when there is none. In an undirected graph either direction counts.
fn[N, E] Graph::find_edges( self : Graph[N, E], from : Int32, to : Int32, ) -> Array[Int32]
Every edge between the pair, in number order.
fn[N, E] Graph::from_edges( edges : Array[(Int32, Int32)], node~ : N, edge~ : E, directed~ : Bool = true, ) -> Graph[N, E]
A graph built from (from, to) pairs, every node carrying node and every edge edge. Nodes are created up to the highest number in the list.
impl[N, E] Inbound for Csr[N, E] with fn in_incident(self, node)
impl[N, E] Inbound for Csr[N, E] with fn in_neighbors(self, node)
impl[N, E] Topology for Csr[N, E] with fn incident(self, node)
impl[N, E] Topology for Csr[N, E] with fn neighbors(self, node)
fn[N, E] Graph::new(directed~ : Bool = true) -> Graph[N, E]
An empty graph. directed decides how every later edge is read; it cannot be changed afterwards, because the adjacency is stored that way.
impl[N, E] Topology for Csr[N, E] with fn node_count(self)
fn[N, E] Graph::node_data(self : Graph[N, E], node : Int32) -> N
The payload of a node. Reading a number outside the graph aborts, which is the only place in this package that does: a payload has no neutral value to give.
fn[N, E] Graph::nodes(self : Graph[N, E]) -> Iter[Int32]
Every node number, ascending.
fn[N, E] Graph::relabel( self : Graph[N, E], labels : Array[Int32], ) -> Graph[N, E] raise Refused
The same graph with its nodes numbered differently. labels[old] is the number the node takes in the answer, and it has to be a permutation of 0..n — a one-to-one map onto the same range, which is what keeps the result dense and keeps every node's number meaningful without a translation table at the call site.
fn[N, E] Graph::remove_edge(self : Graph[N, E], edge : Int32) -> Unit
Removes an edge. Its number stays vacant, and the numbers of the other edges do not change. A number outside the graph, or one already removed, is ignored.
fn[N, E] Graph::remove_node(self : Graph[N, E], node : Int32) -> Unit
Removes a node and every edge on it, and renumbers the nodes after it down by one so the numbering stays dense. A number outside the graph is ignored.
fn[N, E] Graph::set_edge_data( self : Graph[N, E], edge : Int32, data : E, ) -> Unit
Replaces an edge's payload.
fn[N, E] Graph::set_node_data( self : Graph[N, E], node : Int32, data : N, ) -> Unit
Replaces a node's payload.
fn[N, E] Graph::to_directed(self : Graph[N, E]) -> Graph[N, E]
The same graph, made directed: every undirected edge becomes two arcs that point at each other, and a self-loop stays one arc because it already points both ways.
fn[N, E] Graph::to_undirected(self : Graph[N, E]) -> Graph[N, E]
The same graph, made undirected: two arcs that point at each other become one edge, and an arc with no partner becomes one edge.
fn[N, E] Graph::weights( self : Graph[N, E], of : (E) -> Double, ) -> (Int32) -> Double
A weight function over edge numbers, derived from one over payloads. Pass the result as the weight argument of an algorithm.
fn[N, E] Graph::with_capacity( nodes? : Int32, edges? : Int32, directed~ : Bool = true, ) -> Graph[N, E]
An empty graph with room for the given counts, so that a build loop does not reallocate. The counts are hints; exceeding them is not an error.
struct Keyed[K, N, E]
A graph addressed by keys instead of numbers: keys go in, keys come out, and the numbers stay inside.
fn[K : Hash + Eq, N, E] Keyed::add_edge( self : Keyed[K, N, E], from : K, to : K, data : E, ) -> Int32 raise Refused
Adds an edge between two keys and returns its number. Either key missing is refused, because an edge to a node that is not there has no number to use.
fn[K : Hash + Eq, N, E] Keyed::add_node( self : Keyed[K, N, E], key : K, data : N, ) -> Int32 raise Refused
Adds a node under a key and returns its number. A key that is already present is refused rather than silently overwriting the node.
fn[K, N, E] Keyed::graph(self : Keyed[K, N, E]) -> Graph[N, E]
The wrapped graph, for an algorithm that works in numbers.
fn[K, N, E] Keyed::key(self : Keyed[K, N, E], node : Int32) -> K?
The key behind a number.
fn[K : Hash + Eq, N, E] Keyed::new(graph~ : Graph[N, E]) -> Keyed[K, N, E]
Wraps a graph. The wrapper takes ownership of the numbering from here on: add nodes and edges through it so the two views cannot drift.
fn[K : Hash + Eq, N, E] Keyed::node( self : Keyed[K, N, E], key : K, ) -> Int32?
The number behind a key.
fn[K : Hash + Eq, N, E] Keyed::path( self : Keyed[K, N, E], paths : Paths, key : K, ) -> Array[K]?
The route to a key, as keys. None when the node was not reached.
fn[K : Hash + Eq, N, E] Keyed::scores( self : Keyed[K, N, E], scores : Scores, ) -> Map[K, Double]
A score per key, for a caller that thinks in keys.
struct Mapping
A node-to-node map between two graphs.
struct Matching
A matching: pairs of nodes and the weight those pairs add up to.
struct Matrix[E]
A dense V by V grid of cells, one edge payload per cell.
impl[N, E] Topology for Csr[N, E] with fn directed(self)
impl[N, E] Topology for Csr[N, E] with fn edge_count(self)
fn[E] Matrix::edges(self : Matrix[E]) -> Array[(Int32, Int32)]
Every edge of the matrix, as (from, to) pairs in cell order.
impl[N, E] Topology for Csr[N, E] with fn endpoints(self, edge)
fn[E] Matrix::get(self : Matrix[E], from : Int32, to : Int32) -> E?
The payload of a cell, or None when it is empty or the coordinates are outside the matrix.
impl[N, E] Inbound for Csr[N, E] with fn in_incident(self, node)
impl[N, E] Inbound for Csr[N, E] with fn in_neighbors(self, node)
impl[N, E] Topology for Csr[N, E] with fn incident(self, node)
impl[N, E] Topology for Csr[N, E] with fn neighbors(self, node)
fn[E] Matrix::new(nodes : Int32) -> Matrix[E]
A matrix of nodes rows and columns with every cell empty.
impl[N, E] Topology for Csr[N, E] with fn node_count(self)
fn[E] Matrix::remove(self : Matrix[E], from : Int32, to : Int32) -> Unit
Empties a cell.
fn[E] Matrix::set( self : Matrix[E], from : Int32, to : Int32, data : E, ) -> Unit
Writes a cell, adding an edge when it was empty and replacing the payload when it was not.
struct Partition
A partition of the nodes into groups.
struct Paths
The result of a single-source search: one distance and one predecessor per node.
fn Paths::path_to(self : Paths, node : Int32) -> Array[Int32]?
The node numbers from a source to node, source first, or None when the node was not reached.
fn Paths::reached(self : Paths, node : Int32) -> Bool
Whether the node was reached at all.
struct Scores
A score per node.
struct Spanning
A spanning tree: its edges and its weight.
struct StableGraph[N, E]
An adjacency-list graph whose node numbers never change: removing a node leaves its number vacant and occupied numbers stay where they were.
fn[N, E] StableGraph::add_edge( self : StableGraph[N, E], from : Int32, to : Int32, data : E, ) -> Int32
Adds an edge from from to to carrying data, and returns its number.
fn[N, E] StableGraph::add_node(self : StableGraph[N, E], data : N) -> Int32
Adds a node in the lowest vacant number, or at the end when none is vacant, and returns that number.
fn[N, E] StableGraph::contains_edge( self : StableGraph[N, E], edge : Int32, ) -> Bool
Whether the edge exists and has not been removed.
fn[N, E] StableGraph::contains_node( self : StableGraph[N, E], node : Int32, ) -> Bool
Whether a node number is inside the graph and occupied.
fn[N, E] StableGraph::copy(self : StableGraph[N, E]) -> StableGraph[N, E]
An independent copy.
impl[N, E] Topology for Csr[N, E] with fn directed(self)
impl[N, E] Topology for Csr[N, E] with fn edge_count(self)
fn[N, E] StableGraph::edge_data( self : StableGraph[N, E], edge : Int32, ) -> E
The payload of an edge.
fn[N, E] StableGraph::edges(self : StableGraph[N, E]) -> Iter[Int32]
Every live edge number, ascending.
impl[N, E] Topology for Csr[N, E] with fn endpoints(self, edge)
fn[N, E] StableGraph::find_edge( self : StableGraph[N, E], from : Int32, to : Int32, ) -> Int32?
The lowest-numbered edge from from to to.
fn[N, E] StableGraph::find_edges( self : StableGraph[N, E], from : Int32, to : Int32, ) -> Array[Int32]
Every edge between the pair, in number order.
fn[N, E] StableGraph::from_edges( edges : Array[(Int32, Int32)], node~ : N, edge~ : E, directed~ : Bool = true, ) -> StableGraph[N, E]
A graph built from (from, to) pairs, every node carrying node and every edge edge.
fn[N, E] StableGraph::highest_node(self : StableGraph[N, E]) -> Int32?
The largest occupied node number, or None for a graph with no nodes.
impl[N, E] Inbound for Csr[N, E] with fn in_incident(self, node)
impl[N, E] Inbound for Csr[N, E] with fn in_neighbors(self, node)
impl[N, E] Topology for Csr[N, E] with fn incident(self, node)
fn[N, E] StableGraph::is_vacant( self : StableGraph[N, E], node : Int32, ) -> Bool
Whether a node number is inside the graph and vacant.
impl[N, E] Topology for Csr[N, E] with fn neighbors(self, node)
fn[N, E] StableGraph::new(directed~ : Bool = true) -> StableGraph[N, E]
An empty graph.
impl[N, E] Topology for Csr[N, E] with fn node_count(self)
fn[N, E] StableGraph::node_data( self : StableGraph[N, E], node : Int32, ) -> N
The payload of an occupied node.
fn[N, E] StableGraph::nodes(self : StableGraph[N, E]) -> Iter[Int32]
Every occupied node number, ascending.
fn[N, E] StableGraph::remove_edge( self : StableGraph[N, E], edge : Int32, ) -> Unit
Removes an edge, leaving its number vacant.
fn[N, E] StableGraph::remove_node( self : StableGraph[N, E], node : Int32, ) -> Unit
Removes a node and every edge on it, leaving its number vacant.
fn[N, E] StableGraph::set_edge_data( self : StableGraph[N, E], edge : Int32, data : E, ) -> Unit
Replaces an edge's payload.
fn[N, E] StableGraph::set_node_data( self : StableGraph[N, E], node : Int32, data : N, ) -> Unit
Replaces a node's payload.
fn[N, E] StableGraph::slot_count(self : StableGraph[N, E]) -> Int32
How many numbers exist, vacant ones included: one more than the largest number ever handed out.
fn[N, E] StableGraph::slot_data( self : StableGraph[N, E], slot : Int32, ) -> N
The payload in a slot, vacant or not. A vacant slot keeps the payload it had when it was removed, which is what lets a snapshot copy the array whole.
fn[N, E] StableGraph::to_directed( self : StableGraph[N, E], ) -> StableGraph[N, E]
The same graph, made directed, with its slot numbers kept — vacant slots stay vacant, so a node number means the same thing in both graphs. This is the conversion a StableGraph exists for: with Graph a copy is already exact, but here keeping the numbering is the whole point.
fn[N, E] StableGraph::to_undirected( self : StableGraph[N, E], ) -> StableGraph[N, E]
The same graph, made undirected, with its slot numbers kept. Reciprocal arcs become one edge carrying the payload of the lowest-numbered arc, as in Graph::to_undirected.
fn[N, E] StableGraph::vacant_count(self : StableGraph[N, E]) -> Int32
How many numbers are vacant.
fn[N, E] StableGraph::weights( self : StableGraph[N, E], of : (E) -> Double, ) -> (Int32) -> Double
A weight function over edge numbers, derived from one over payloads.
trait Inbound
A graph that can also walk its edges backwards.
trait Topology
What every algorithm needs from a graph.

moongraph/traverse17 items

fn[G : @moongraph.Topology] bfs(graph : G, source : Int32) -> Array[Int32]
The order a breadth-first walk discovers nodes, ordered by distance from the source and then by node number.
fn[G : @moongraph.Topology] bfs_beam( graph : G, source : Int32, width~ : Int32 = 8, ) -> Array[Int32]
A beam walk: the same layer expansion as bfs_layers, but each layer keeps only its width lowest-numbered nodes, which is how a search stays inside a budget when the frontier explodes.
fn[G : @moongraph.Topology] bfs_edges( graph : G, source : Int32, ) -> Iter[(Int32, Int32)]
The tree edges of a breadth-first walk, as (parent, child), in discovery order.
fn[G : @moongraph.Topology] bfs_layers( graph : G, sources : Array[Int32], ) -> Array[Array[Int32]]
The nodes at each distance from a set of sources, layer by layer, each layer ascending.
fn[G : @moongraph.Topology] bfs_tree( graph : G, source : Int32, ) -> @moongraph.Paths
Distances and parents from a breadth-first walk.
fn[G : @moongraph.Topology] descendants_at( graph : G, source : Int32, distance : Int32, ) -> Array[Int32]
The nodes exactly distance edges from source, ascending. A distance of 0 is the source itself, and a negative distance is nothing.
fn[G : @moongraph.Topology] dfs(graph : G, source : Int32) -> Array[Int32]
The preorder of a depth-first walk: a node is listed when the walk reaches it, and its edges are visited in ascending neighbour number.
fn[G : @moongraph.Topology] dfs_events( graph : G, source : Int32, ) -> Iter[Event]
A depth first walk reported as one event per step: entering a node, looking at an edge to a node already on the stack, looking at an edge to a node already finished, and leaving a node.
fn[G : @moongraph.Topology] dfs_labeled( graph : G, source : Int32, ) -> Array[(Int32, Int32, Int32)]
Every edge a depth-first walk looks at, as (from, to, kind).
fn[G : @moongraph.Topology] dfs_postorder( graph : G, source : Int32, ) -> Array[Int32]
The postorder of the same walk: a node is listed after all of its descendants.
fn[G : @moongraph.Topology] dfs_tree( graph : G, source : Int32, ) -> @moongraph.Paths
The depth-first forest from one source, as distances (how many edges deep) and parents.
fn[G : @moongraph.Topology] edge_bfs( graph : G, source : Int32, ) -> Iter[(Int32, Int32)]
The tree edges of a breadth-first walk that covers every component, as (from, to). Each component is entered at its lowest-numbered node, and the given source goes first when it is in range.
fn[G : @moongraph.Topology] edge_dfs( graph : G, source : Int32, ) -> Iter[(Int32, Int32)]
The tree edges of a depth-first walk that covers every component, as (from, to). Each component is entered at its lowest-numbered node, and the given source goes first when it is in range.
enum Event
A moment in a depth-first walk.
Event::equal
Derived: whether two values of this type are the same, field by field.
Event::not_equal
Derived: whether two values of this type differ.
Event::to_repr
Derived: the debug view of a value, used when an assertion fails.

moongraph/path26 items

fn[G : @moongraph.Topology] all_simple( graph : G, from : Int32, to : Int32, ) -> Iter[Array[Int32]]
Every simple route between two nodes, as node sequences, source first.
fn[G : @moongraph.Topology] all_simple_edges( graph : G, from : Int32, to : Int32, ) -> Iter[Array[Int32]]
The same routes, named by edge number instead of by node.
fn[G : @moongraph.Topology] astar( graph : G, from : Int32, to : Int32, weight? : (Int32) -> Double, estimate~ : (Int32) -> Double, ) -> @moongraph.Paths raise @moongraph.Refused
The cheapest route between two nodes with a hint to steer the search.
fn[G : @moongraph.Topology] bellman_ford( graph : G, source : Int32, weight? : (Int32) -> Double, ) -> @moongraph.Paths raise @moongraph.Refused
Cheapest routes from one source when edges may be negative.
fn[G : @moongraph.Topology] bfs_all(graph : G) -> @moongraph.AllPairs
Every pair on an unweighted graph, by one breadth-first pass per node. On such a graph this is the fastest all-pairs method there is, and naming it keeps a caller from running Dijkstra over unit weights.
fn[G : @moongraph.Topology] contraction_hierarchy( graph : G, weight? : (Int32) -> Double, ) -> ContractionHierarchy raise @moongraph.Refused
Prepare a graph for repeated cheapest-route queries.
fn[G : @moongraph.Topology] dijkstra( graph : G, source : Int32, weight? : (Int32) -> Double, ) -> @moongraph.Paths raise @moongraph.Refused
Cheapest routes from one source, by Dijkstra's method.
fn[G : @moongraph.Topology] dijkstra_all( graph : G, weight? : (Int32) -> Double, ) -> @moongraph.AllPairs raise @moongraph.Refused
Every pair, by one Dijkstra pass per node.
fn[G : @moongraph.Topology + @moongraph.Inbound] dijkstra_bidirectional( graph : G, from : Int32, to : Int32, weight? : (Int32) -> Double, ) -> @moongraph.Paths raise @moongraph.Refused
The cheapest route between two nodes, searched from both ends at once.
fn[G : @moongraph.Topology] dijkstra_multi( graph : G, sources : Array[Int32], weight? : (Int32) -> Double, ) -> @moongraph.Paths raise @moongraph.Refused
Cheapest routes from several sources at once; every source starts at 0.
fn[G : @moongraph.Topology] floyd_warshall( graph : G, weight? : (Int32) -> Double, ) -> @moongraph.AllPairs raise @moongraph.Refused
Every pair, by the triple loop.
fn[G : @moongraph.Topology] has_path( graph : G, from : Int32, to : Int32, ) -> Bool
Whether any route joins the two nodes. Directed graphs follow the edge direction, as every other function here does.
fn[G : @moongraph.Topology] is_simple_path( graph : G, path : Array[Int32], ) -> Bool
Whether a node sequence is a simple path: every node distinct, and every consecutive pair joined by an edge.
fn[G : @moongraph.Topology] johnson( graph : G, weight? : (Int32) -> Double, ) -> @moongraph.AllPairs raise @moongraph.Refused
Every pair, by Johnson's reweighting: one Bellman-Ford pass makes the weights non-negative, then a Dijkstra pass per node.
fn[G : @moongraph.Topology] k_shortest( graph : G, from : Int32, to : Int32, k : Int32, weight? : (Int32) -> Double, ) -> Array[Array[Int32]] raise @moongraph.Refused
Up to k cheapest routes between two nodes, cheapest first, without repeats.
fn[G : @moongraph.Topology] path_weight( graph : G, path : Array[Int32], weight? : (Int32) -> Double, ) -> Double raise @moongraph.Refused
The cost of walking a node sequence, taking the cheapest edge between each consecutive pair.
fn[G : @moongraph.Topology] shortest( graph : G, from : Int32, to : Int32, weight? : (Int32) -> Double, ) -> Array[Int32]? raise @moongraph.Refused
The cheapest route between two nodes, by whichever method the weights allow.
fn[G : @moongraph.Topology] spfa( graph : G, source : Int32, weight? : (Int32) -> Double, ) -> @moongraph.Paths raise @moongraph.Refused
Cheapest routes from one source by the queue-based relaxation that is Bellman-Ford with the redundant passes skipped. Same answer, usually fewer passes.
fn[G : @moongraph.Topology] voronoi( graph : G, centers : Array[Int32], weight? : (Int32) -> Double, ) -> Array[Int32] raise @moongraph.Refused
Which centre each node belongs to: the one it is cheapest to reach. The answer is one label per node, holding the centre's node number, or -1 for a node no centre can reach.
fn[G : @moongraph.Topology] voronoi_cells( graph : G, centers : Array[Int32], weight? : (Int32) -> Double, ) -> Array[Array[Int32]] raise @moongraph.Refused
The same answer, grouped the other way round: one array per centre, in the order the centres were given, holding the nodes that look to it. A centre that reaches nothing is an empty array rather than a missing one, and a node that no centre reaches appears in no cell at all.
fn[G : @moongraph.Topology] widest( graph : G, source : Int32, weight? : (Int32) -> Double, ) -> @moongraph.Paths raise @moongraph.Refused
The route that keeps the least capacity as high as possible: the widest path.
struct ContractionHierarchy
A graph prepared for repeated cheapest-route queries: the contraction order, and the two halves of every arc, arranged so that a query only ever climbs.
fn ContractionHierarchy::path( self : ContractionHierarchy, source : Int32, target : Int32, ) -> Array[Int32]?
The route itself, both ends included, or None when source cannot reach target.
fn ContractionHierarchy::query( self : ContractionHierarchy, source : Int32, target : Int32, ) -> Double?
The cheapest cost from source to target through the prepared graph, or None when the one cannot reach the other.
fn ContractionHierarchy::rank_of( self : ContractionHierarchy, node : Int32, ) -> Int32
The position of a node in the contraction order, or -1 for a number the graph does not have.
fn ContractionHierarchy::shortcut_count( self : ContractionHierarchy, ) -> Int32
How many shortcuts the preprocessing added: the arcs beyond the graph's own edges, counting each undirected edge once per direction.

moongraph/component24 items

fn[G : @moongraph.Topology] articulation_points(graph : G) -> Array[Int32]
The nodes whose removal disconnects the graph, ascending.
fn[G : @moongraph.Topology] attracting_components( graph : G, ) -> Array[Array[Int32]]
The nodes from which a random walk never leaves its component: those with no edge to another strong component.
fn[G : @moongraph.Topology] biconnected_component_nodes( graph : G, ) -> Array[Array[Int32]]
The nodes of each biconnected piece, ascending inside each piece, pieces in the order biconnected_components returns them.
fn[G : @moongraph.Topology] biconnected_components( graph : G, ) -> Array[Array[Int32]]
Every edge that lies on a cycle, grouped by biconnected piece, with an isolated edge forming a piece of its own.
fn[G : @moongraph.Topology] bridges(graph : G) -> Array[Int32]
The edges whose removal disconnects the graph, in number order.
fn[G : @moongraph.Topology] chain_decomposition( graph : G, ) -> Array[Array[Int32]]
Splits the graph into chains around a depth-first tree: every back edge with the tree path under it, and every bridge on its own.
fn[G : @moongraph.Topology + @moongraph.Inbound] component_count( graph : G, ) -> Int32
How many components there are.
fn[G : @moongraph.Topology + @moongraph.Inbound] component_of( graph : G, node : Int32, ) -> Array[Int32]
The nodes in the same component as node, ascending.
fn[G : @moongraph.Topology + @moongraph.Inbound] components( graph : G, ) -> @moongraph.Components
The components of a graph, as a label per node.
fn[G : @moongraph.Topology] condensation( graph : G, ) -> @moongraph.Condensation
The condensation: one node per strong component, one edge per component pair that has an edge between them.
fn[G : @moongraph.Topology] edge_boundary( graph : G, set : Array[Int32], ) -> Array[Int32]
The edges with one endpoint in set and one outside it, in number order.
fn[G : @moongraph.Topology] has_bridges(graph : G) -> Bool
Whether any edge is a bridge.
fn[G : @moongraph.Topology] is_biconnected(graph : G) -> Bool
Whether removing any single node leaves the graph connected.
fn[G : @moongraph.Topology + @moongraph.Inbound] is_connected( graph : G, ) -> Bool
Whether the graph is one piece.
fn[G : @moongraph.Topology] is_semiconnected(graph : G) -> Bool
Whether the nodes can be laid on a line so that every pair has an edge in at least one direction: the condensation has to be a single chain.
fn[G : @moongraph.Topology] is_strongly_connected(graph : G) -> Bool
Whether every node reaches every other.
fn[G : @moongraph.Topology + @moongraph.Inbound] is_weakly_connected( graph : G, ) -> Bool
Whether the graph is one piece when direction is ignored.
fn[G : @moongraph.Topology] local_bridges( graph : G, span~ : Int32 = 1, ) -> Array[Int32]
The edges whose endpoints have no route between them short enough to make the edge part of a small cycle.
fn[G : @moongraph.Topology + @moongraph.Inbound] node_boundary( graph : G, set : Array[Int32], ) -> Array[Int32]
The nodes outside set with an edge to it, ascending.
fn[G : @moongraph.Topology] reachable_count( graph : G, node : Int32, ) -> Int32
How many nodes node reaches, itself included.
fn[G : @moongraph.Topology] scc(graph : G) -> Array[Array[Int32]]
The strongly connected pieces in topological order: every edge runs from a piece at a lower index to one at a higher index, or stays inside a single piece. Each piece lists its node numbers in ascending order.
fn[G : @moongraph.Topology] strong_components( graph : G, ) -> @moongraph.Components
Strongly connected components by Tarjan's method: one depth-first pass, one stack.
fn[G : @moongraph.Topology + @moongraph.Inbound] strong_components_kosaraju( graph : G, ) -> @moongraph.Components
Strong components by two passes: one forward, one over the reversed edges.
fn[G : @moongraph.Topology + @moongraph.Inbound] weak_components( graph : G, ) -> @moongraph.Components
The same components under the name that says the edges were read both ways.

moongraph/dag23 items

fn[G : @moongraph.Topology] all_topological_orders( graph : G, ) -> Iter[Array[Int32]]
Every topological order, in lexicographic order of the node sequences.
fn[G : @moongraph.Topology + @moongraph.Inbound] ancestors( graph : G, node : Int32, ) -> Array[Int32]
The nodes that reach node, ascending, itself excluded.
fn[G : @moongraph.Topology] antichains(graph : G) -> Array[Array[Int32]]
The maximal antichains of the reachability order: sets of nodes no two of which are comparable.
fn[G : @moongraph.Topology + @moongraph.Inbound] colliders( graph : G, ) -> Array[Int32]
The nodes that sit at the head of a fork: two incoming edges from nodes that are not themselves joined.
fn[G : @moongraph.Topology] descendants( graph : G, node : Int32, ) -> Array[Int32]
The nodes node reaches, ascending, itself excluded.
fn[G : @moongraph.Topology + @moongraph.Inbound] dominance_frontiers( graph : G, entry : Int32, ) -> Array[Array[Int32]]
The dominance frontier of each node: the nodes where its dominance stops.
fn[G : @moongraph.Topology + @moongraph.Inbound] dominators( graph : G, entry : Int32, ) -> Array[Int32?]
The immediate dominator of each node from entry: the last node every route from the entry must pass through.
fn[G : @moongraph.Topology] is_aperiodic(graph : G) -> Bool
Whether the lengths of the cycles in the graph have no common divisor above one.
fn[G : @moongraph.Topology] is_dag(graph : G) -> Bool
Whether the graph has no cycle.
fn[G : @moongraph.Topology] longest_path( graph : G, weight? : (Int32) -> Double, ) -> Array[Int32] raise @moongraph.Refused
A longest route in the graph, as node numbers, by relaxing in topological order.
fn[G : @moongraph.Topology] longest_path_length( graph : G, weight? : (Int32) -> Double, ) -> Double raise @moongraph.Refused
The weight of the longest route.
fn[G : @moongraph.Topology] topological_generations( graph : G, ) -> Array[Array[Int32]] raise @moongraph.Refused
The order cut into generations: every node of a generation has all of its predecessors in earlier generations.
fn[G : @moongraph.Topology] topological_order( graph : G, ) -> Array[Int32] raise @moongraph.Refused
A node order in which every edge points forward.
fn[G : @moongraph.Topology] topological_smallest( graph : G, ) -> Array[Int32] raise @moongraph.Refused
The smallest-numbered node that is ready at every step, which is the lexicographically smallest topological order.
fn[G : @moongraph.Topology] transitive_closure( graph : G, ) -> Array[Array[Bool]]
The reachability matrix: closure[from][to] is true when an edge or a chain of them joins the two. A node reaches itself.
fn[G : @moongraph.Topology] transitive_reduction( graph : G, ) -> Array[(Int32, Int32)]
The edges that no other route makes redundant, as (from, to) pairs.
struct Sorter
An incremental topological sorter: nodes are added as they become known, and the caller asks which are ready without restarting the walk.
fn Sorter::add( self : Sorter, node : Int32, after : Array[Int32], ) -> Unit raise @moongraph.Refused
Declares that node must come after every node in after. The node may be one the graph did not have.
fn Sorter::finish( self : Sorter, nodes : Array[Int32], ) -> Unit raise @moongraph.Refused
Marks nodes as done, which may release the nodes that were waiting on them.
fn Sorter::is_active(self : Sorter) -> Bool
Whether any node is still waiting.
fn[G : @moongraph.Topology] Sorter::new(graph : G) -> Sorter
A sorter holding every node of the graph, with the graph's edges as prerequisites.
fn Sorter::order(self : Sorter) -> Array[Int32] raise @moongraph.Refused
The full order, as the sorter sees it from the start, which is the same as topological_order on the graph plus every node added since.
fn Sorter::ready(self : Sorter) -> Array[Int32] raise @moongraph.Refused
The nodes whose prerequisites are all finished, ascending. Each node is handed out once until it is finished.

moongraph/cycle15 items

fn[G : @moongraph.Topology] chinese_postman( graph : G, weight? : (Int32) -> Double, ) -> Array[Int32] raise @moongraph.Refused
A route that walks every edge at least once, duplicating edges when the graph has odd-degree nodes, as edge numbers in walk order.
fn[G : @moongraph.Topology] chordless_cycles( graph : G, length_bound? : Int32, ) -> Iter[Array[Int32]]
The cycles with no edge joining two of their own nodes.
fn[G : @moongraph.Topology] cycle_basis(graph : G) -> Array[Array[Int32]]
A basis of the cycle space: one fundamental cycle per edge that is not a bridge, enough of them to generate every cycle by symmetric difference.
fn[G : @moongraph.Topology] eulerian_circuit( graph : G, ) -> Array[Int32] raise @moongraph.Refused
A closed walk that uses every edge exactly once, as edge numbers.
fn[G : @moongraph.Topology + @moongraph.Inbound] eulerian_path( graph : G, ) -> Array[Int32]?
The one trail that uses every edge exactly once, or None when there is none.
fn[G : @moongraph.Topology] eulerize( graph : G, weight? : (Int32) -> Double, ) -> Array[(Int32, Int32)] raise @moongraph.Refused
The edges to duplicate so that the graph becomes Eulerian, as (from, to) pairs.
fn[G : @moongraph.Topology + @moongraph.Inbound] feedback_arc_set( graph : G, ) -> Array[Int32]
A set of edges whose removal leaves a graph without cycles, chosen greedily.
fn[G : @moongraph.Topology] find_cycle(graph : G) -> Array[Int32]?
One cycle, as node numbers, first and last equal; None when the graph is acyclic.
fn[G : @moongraph.Topology] girth(graph : G) -> Int32?
The length of the shortest cycle, following the arcs of a directed graph, or None when the graph has no closed walk.
fn[G : @moongraph.Topology + @moongraph.Inbound] has_eulerian_path( graph : G, ) -> Bool
Whether every edge can be walked in one trail that is not closed: in an undirected graph exactly two nodes have odd degree and they are the ends, and in a directed one exactly two nodes have their in and out counts one apart, the start leaving one more than it takes and the end taking one more than it leaves.
fn[G : @moongraph.Topology] is_cyclic(graph : G) -> Bool
Whether the graph has a cycle. A self-loop counts.
fn[G : @moongraph.Topology] is_eulerian(graph : G) -> Bool
Whether every edge lies on one closed walk: every node is joined to the rest, and every node either has even degree, or — in a directed graph — has as many edges leaving it as entering it.
fn[G : @moongraph.Topology] min_cycle_ratio( graph : G, cost~ : (Int32) -> Double, weight? : (Int32) -> Double, ) -> Double raise @moongraph.Refused
The smallest cost-to-weight ratio over all cycles.
fn[G : @moongraph.Topology] minimum_cycle_basis( graph : G, weight? : (Int32) -> Double, ) -> Array[Array[Int32]]
A cycle basis of total length as small as possible.
fn[G : @moongraph.Topology] simple_cycles(graph : G) -> Iter[Array[Int32]]
Every simple cycle, each reported once.

moongraph/tree25 items

fn[G] branching_weight( graph : G, spanning : @moongraph.Spanning, weight? : (Int32) -> Double, ) -> Double
The weight of a branching, which is the sum of the weights of its edges.
fn[G : @moongraph.Topology] broadcast_center( graph : G, ) -> (Int32, Int32) raise @moongraph.Refused
The node whose furthest node is nearest: the best place to start a broadcast.
fn from_nested(nested : Nested) -> (@moongraph.Graph[Int32, Unit], Int32)
The tree a nested shape describes, and the number it gives its root.
fn from_prufer(sequence : Array[Int32]) -> @moongraph.Graph[Int32, Unit]
The tree a Prüfer code describes, over node numbers 0 .. n.
fn[G : @moongraph.Topology] maximum_branching( graph : G, weight? : (Int32) -> Double, root? : Int32, ) -> @moongraph.Spanning raise @moongraph.Refused
The most expensive branching, by the same walk on negated weights.
fn[G : @moongraph.Topology] maximum_spanning( graph : G, weight? : (Int32) -> Double, ) -> @moongraph.Spanning raise @moongraph.Refused
A maximum spanning tree, by the same walk with the comparison turned around.
fn[G : @moongraph.Topology] minimum_branching( graph : G, weight? : (Int32) -> Double, root? : Int32, ) -> @moongraph.Spanning raise @moongraph.Refused
The cheapest set of directed edges that gives every node at most one parent and the whole graph a root, by Chu-Liu/Edmonds.
fn[G : @moongraph.Topology] minimum_spanning( graph : G, weight? : (Int32) -> Double, kind~ : SpanningMethod = Kruskal, ) -> @moongraph.Spanning raise @moongraph.Refused
A minimum spanning tree.
fn[G : @moongraph.Topology] prufer_sequence( graph : G, ) -> Array[Int32] raise @moongraph.Refused
Prüfer's code of a labelled tree: the sequence of labels a leaf-stripping walk leaves behind.
fn[G : @moongraph.Topology] random_spanning( graph : G, rng~ : &@contract.Rng, ) -> @moongraph.Spanning
A spanning tree picked at random, by a random permutation of the edges fed to Kruskal. Every tree is equally likely when the edges are shuffled uniformly.
fn[G : @moongraph.Topology] spanning_count(graph : G) -> Double
How many spanning trees the graph has, by Kirchhoff's determinant.
fn[G : @moongraph.Topology] spanning_trees( graph : G, weight? : (Int32) -> Double, ) -> Iter[@moongraph.Spanning]
Every distinct spanning tree.
fn[G : @moongraph.Topology] steiner_tree( graph : G, terminals : Array[Int32], weight? : (Int32) -> Double, ) -> @moongraph.Spanning raise @moongraph.Refused
A tree that connects the terminals, by the Dreyfus-Wagner dynamic program over subsets of terminals.
fn[G : @moongraph.Topology] subtree_sizes( graph : G, root : Int32, ) -> Array[Int32] raise @moongraph.Refused
The number of nodes in the subtree under each node when the tree is rooted at root, the node itself included.
fn[G : @moongraph.Topology] to_nested( graph : G, root : Int32, ) -> Nested raise @moongraph.Refused
The nested form of the tree rooted at root.
fn[G : @moongraph.Topology] tree_center( graph : G, ) -> Array[Int32] raise @moongraph.Refused
The middle of a tree's longest route: the one or two nodes with the smallest eccentricity. A tree has one centre.
fn[G : @moongraph.Topology] tree_centroid( graph : G, ) -> Array[Int32] raise @moongraph.Refused
The node whose removal leaves the largest remaining piece as small as possible.
fn[G : @moongraph.Topology] tree_depth( graph : G, root : Int32, ) -> Array[Int32] raise @moongraph.Refused
The depth of every node below root.
struct Lca
Lowest common ancestor queries on a rooted tree, after one preprocessing pass.
fn Lca::ancestor_at(self : Lca, node : Int32, up : Int32) -> Int32?
The ancestor up steps above a node, or None when the walk leaves the tree.
fn Lca::depth(self : Lca, node : Int32) -> Int32
How deep a node is under the root the object was built at.
fn[G : @moongraph.Topology] Lca::new( graph : G, root : Int32, ) -> Lca raise @moongraph.Refused
Preprocesses the tree rooted at root, by binary lifting.
fn Lca::of(self : Lca, a : Int32, b : Int32) -> Int32?
The lowest node that is an ancestor of both.
enum Nested
A rooted tree without node numbers: the shape a caller wants when comparing two trees.
enum SpanningMethod
How a spanning tree is grown.

moongraph/flow36 items

fn[G : @moongraph.Topology] all_min_cuts( graph : G, source : Int32, sink : Int32, capacity? : (Int32) -> Double, ) -> Iter[@moongraph.Cut]
Every cut of least capacity between the two nodes.
fn[G : @moongraph.Topology] edge_connectivity( graph : G, capacity? : (Int32) -> Double, ) -> Int32
The smallest number of edges whose removal disconnects the graph.
fn[G : @moongraph.Topology] edge_connectivity_pair( graph : G, from : Int32, to : Int32, ) -> Int32
The smallest number of edges whose removal separates the two nodes.
fn[G : @moongraph.Topology] edge_disjoint_paths( graph : G, from : Int32, to : Int32, ) -> Array[Array[Int32]]
Routes between the two nodes that share no edge, as many as the edge connectivity allows.
fn[G] flow_cost( graph : G, flow : @moongraph.Flow, cost? : (Int32) -> Double, ) -> Double
The cost of a flow, edge by edge.
fn[G : @moongraph.Topology] gomory_hu( graph : G, capacity? : (Int32) -> Double, ) -> Array[(Int32, Int32, Double)]
A cut tree: V - 1 pairs with the capacity that separates them.
fn[G : @moongraph.Topology] k_components(graph : G) -> Array[Array[Int32]]
The pieces that stay joined after any k - 1 nodes are removed.
fn[G : @moongraph.Topology] k_edge_components( graph : G, k : Int32, ) -> Array[Array[Int32]]
The pieces that stay joined after any k - 1 edges are removed.
fn[G : @moongraph.Topology] max_flow( graph : G, source : Int32, sink : Int32, capacity? : (Int32) -> Double, kind~ : FlowMethod = Dinitz, ) -> @moongraph.Flow raise @moongraph.Refused
The maximum flow from source to sink.
fn[G : @moongraph.Topology] max_flow_min_cost( graph : G, source : Int32, sink : Int32, cost? : (Int32) -> Double, capacity? : (Int32) -> Double, ) -> @moongraph.Flow raise @moongraph.Refused
The maximum flow that costs the least to send.
fn[G : @moongraph.Topology] max_flow_value( graph : G, source : Int32, sink : Int32, capacity? : (Int32) -> Double, ) -> Double raise @moongraph.Refused
The value alone, which costs the same as the flow itself.
fn[G : @moongraph.Topology] min_cost_flow( graph : G, supply : Array[Double], cost? : (Int32) -> Double, capacity? : (Int32) -> Double, ) -> @moongraph.Flow raise @moongraph.Refused
A flow that meets the supplies at the least cost.
fn[G : @moongraph.Topology] min_cut( graph : G, source : Int32, sink : Int32, capacity? : (Int32) -> Double, ) -> @moongraph.Cut raise @moongraph.Refused
The cut of least capacity that separates the two nodes.
fn[G : @moongraph.Topology] min_cut_global( graph : G, capacity? : (Int32) -> Double, ) -> @moongraph.Cut
The cut of least capacity anywhere in the graph, by Stoer-Wagner.
fn[G : @moongraph.Topology] min_edge_cut(graph : G) -> Array[Int32]
The edges of one smallest cut anywhere in the graph.
fn[G : @moongraph.Topology] min_node_cut(graph : G) -> Array[Int32]
The nodes of one smallest separating set.
fn[G : @moongraph.Topology] node_connectivity(graph : G) -> Int32
The smallest number of nodes whose removal disconnects the graph, other than the two that are being separated.
fn[G : @moongraph.Topology] node_connectivity_pair( graph : G, from : Int32, to : Int32, ) -> Int32
The smallest number of nodes other than the two ends whose removal separates them.
fn[G : @moongraph.Topology] node_disjoint_paths( graph : G, from : Int32, to : Int32, ) -> Array[Array[Int32]]
Routes between the two nodes that share no node but the ends.
fn[G : @moongraph.Topology] split_network( graph : G, source : Int32, sink : Int32, ) -> Network
The graph split into two nodes per node, joined by a capacity of one, so that a minimum cut through it names a set of nodes rather than a set of edges.
enum FlowMethod
Which solver a caller wants. The answer is the same for all of them; the work and the constant factor are not.
struct Network
A residual network over a graph: two arcs per edge, capacities that fall as flow is sent, and the reverse arc that gives it back.
fn Network::boykov_kolmogorov( self : Network, source : Int32, sink : Int32, ) -> Unit
The maximum flow by the two-tree walk of Boykov and Kolmogorov.
fn[G : @moongraph.Topology] Network::build( graph : G, capacity : (Int32) -> Double, ) -> Network
fn Network::dinitz(self : Network, source : Int32, sink : Int32) -> Unit
Dinitz: build a level graph by breadth-first search, then push a blocking flow through it, and repeat while the sink is still reachable.
fn Network::edmonds_karp( self : Network, source : Int32, sink : Int32, ) -> Unit
Edmonds-Karp: the same walk with a plain breadth-first search for the route.
fn[G] Network::paths( self : Network, graph : G, from : Int32, to : Int32, ) -> Array[Array[Int32]]
The routes the flow takes: one at a time, each found in the graph of arcs that still carry flow and then removed from it, so the next search looks for a different route.
fn Network::paths_split( self : Network, from : Int32, to : Int32, ) -> Array[Array[Int32]]
The same, over a network whose nodes are half-nodes.
fn Network::push_along( self : Network, node : Int32, sink : Int32, limit : Double, level : Array[Int32], cursor : Array[Int32], ) -> Double
Pushes what it can along one route of the level graph, resuming from the cursor so an arc that is full is never looked at twice in one phase.
fn Network::push_relabel( self : Network, source : Int32, sink : Int32, ) -> Unit
Push-relabel: keep an excess at every node and push it towards the sink, lifting a node only when it has nowhere to push.
fn Network::reachable(self : Network, source : Int32) -> Array[Bool]
fn[G : @moongraph.Topology] Network::report( self : Network, graph : G, ) -> @moongraph.Flow
The flow per original edge, after a solve.
fn Network::residual_forward(self : Network, edge : Int32) -> Double
The capacity still free on the forward arc of an edge.
fn Network::shift( self : Network, source : Int32, sink : Int32, cost : (Int32) -> Double, ) -> Double
Sends as much as it can from source to sink along routes of least weight, and answers how much moved.
fn Network::solve( self : Network, source : Int32, sink : Int32, kind : FlowMethod, ) -> Unit raise @moongraph.Refused
Runs one solver to completion.
fn Network::source_side(self : Network, source : Int32) -> Array[Bool]
The nodes the source can still reach, which is the source side of the cut.

moongraph/matching12 items

fn[G : @moongraph.Topology] assignment( graph : G, weight~ : (Int32) -> Double, ) -> @moongraph.Matching raise @moongraph.Refused
An assignment of the left side to the right side with the least total cost, by the Hungarian method.
fn[G : @moongraph.Topology] bipartition(graph : G) -> Array[Int32]?
A two-colouring of the graph, or None when an odd cycle makes one impossible.
fn[G : @moongraph.Topology] edge_cover(graph : G) -> @moongraph.Cover
A smallest set of edges that touches every node.
fn[G : @moongraph.Topology] is_edge_cover( graph : G, cover : @moongraph.Cover, ) -> Bool
Whether the cover leaves no node untouched.
fn[G : @moongraph.Topology] is_matching( graph : G, pairs : Array[(Int32, Int32)], ) -> Bool
Whether the pairs form a matching: no node appears twice and every pair is an edge.
fn[G : @moongraph.Topology] is_maximal_matching( graph : G, pairs : Array[(Int32, Int32)], ) -> Bool
Whether no further pair could be added to the matching.
fn[G : @moongraph.Topology] is_perfect_matching( graph : G, pairs : Array[(Int32, Int32)], ) -> Bool
Whether every node is paired.
fn[G : @moongraph.Topology] matching_bipartite( graph : G, weight? : (Int32) -> Double, ) -> @moongraph.Matching raise @moongraph.Refused
The matching of a bipartite graph, by the augmenting walk of König's theorem.
fn[G : @moongraph.Topology] matching_maximal( graph : G, ) -> @moongraph.Matching
A maximal matching: no two pairs share a node and no further pair can be added.
fn[G : @moongraph.Topology] matching_maximum( graph : G, weight? : (Int32) -> Double, ) -> @moongraph.Matching
A matching of the largest number of pairs, by Edmonds' blossom walk.
fn[G : @moongraph.Topology] matching_min_weight( graph : G, weight~ : (Int32) -> Double, ) -> @moongraph.Matching raise @moongraph.Refused
A minimum-weight matching of the largest size, by the same assignment step with the weights turned around.
fn[G : @moongraph.Topology] vertex_cover_bipartite( graph : G, ) -> Array[Int32]
A smallest set of nodes that touches every edge of a bipartite graph, read off a maximum matching: the nodes on one side of a König cover.

moongraph/color8 items

fn[G : @moongraph.Topology] chromatic_number( graph : G, limit~ : Int32 = 32, ) -> Int32 raise @moongraph.Refused
The fewest colours any colouring needs, by trying each count in turn.
fn[G : @moongraph.Topology] dsatur(graph : G) -> @moongraph.Coloring
The colouring that saturates first: at each step the node whose neighbours already use the most distinct colours gets the next colour.
fn[G : @moongraph.Topology] edge_coloring(graph : G) -> @moongraph.Coloring
A colouring of the edges: no two edges of the same colour share a node.
fn[G : @moongraph.Topology] equitable_coloring( graph : G, colors : Int32, ) -> @moongraph.Coloring raise @moongraph.Refused
A colouring in which the colour classes differ in size by at most one.
fn[G : @moongraph.Topology] greedy_coloring( graph : G, order~ : ColoringOrder = LargestFirst, rng? : &@contract.Rng, ) -> @moongraph.Coloring
A colouring of the nodes, one colour at a time, taking the lowest colour that no neighbour already has.
fn[G : @moongraph.Topology] is_coloring( graph : G, coloring : @moongraph.Coloring, ) -> Bool
Whether no two neighbours share a colour.
fn[G : @moongraph.Topology] is_edge_coloring( graph : G, coloring : @moongraph.Coloring, ) -> Bool
Whether no two edges of the same colour touch.
enum ColoringOrder
The order in which nodes are offered a colour.

moongraph/clique15 items

fn[G : @moongraph.Topology] all_cliques( graph : G, at_least~ : Int32 = 3, ) -> @moongraph.Cliques
Every clique of at least at_least nodes.
fn[G : @moongraph.Topology] chordal_cliques( graph : G, ) -> @moongraph.Cliques
The maximal cliques of a chordal graph, read off its elimination order: the node and its later neighbours form one, and the ones that another contains are dropped.
fn[G : @moongraph.Topology] chordal_fill_in( graph : G, ) -> Array[(Int32, Int32)]
The edges that have to be added to make the graph chordal, keeping the elimination order the search found. A graph that is already chordal gets none.
fn[G : @moongraph.Topology] chordal_treewidth(graph : G) -> Int32
The width of the tree decomposition the elimination order gives: one less than the largest clique in it.
fn[G : @moongraph.Topology] clique_graph( graph : G, ) -> @moongraph.Graph[Int32, Unit]
The graph whose nodes are the maximal cliques, joined when those cliques share a node.
fn[G : @moongraph.Topology] clique_number(graph : G) -> Int32
The size of the largest clique.
fn[G : @moongraph.Topology] clique_numbers(graph : G) -> Array[Int32]
The size of the largest clique each node belongs to.
fn[G : @moongraph.Topology] independence_number(graph : G) -> Int32
The size of the largest independent set.
fn[G : @moongraph.Topology] is_chordal(graph : G) -> Bool
Whether every cycle of four or more nodes has a chord.
fn[G : @moongraph.Topology] is_clique( graph : G, nodes : Array[Int32], ) -> Bool
Whether every pair of the given nodes is joined, so that they form a clique.
fn[G : @moongraph.Topology] is_perfect(graph : G) -> Bool
Whether every induced subgraph has a clique and an independent set of the same size, which chordal graphs and bipartite graphs both satisfy.
fn[G : @moongraph.Topology] max_weight_clique( graph : G, weight~ : (Int32) -> Double, ) -> @moongraph.Cliques
The clique with the largest total weight, where a node's weight is its payload.
fn[G : @moongraph.Topology] maximal_cliques( graph : G, ) -> @moongraph.Cliques
Every maximal clique, each one sorted and reported once.
fn[G : @moongraph.Topology] maximum_independent_set( graph : G, ) -> Array[Int32]
A largest set of nodes no two of which are joined.
fn[G : @moongraph.Topology] perfect_elimination_order( graph : G, ) -> Array[Int32]?
A perfect elimination order: a node order in which the later neighbours of each node are all joined to each other. None when the graph is not chordal.

moongraph/centrality13 items

fn[G : @moongraph.Topology] betweenness_centrality( graph : G, ) -> @moongraph.Scores
The number of shortest routes that pass through each node, halved for an undirected graph so that a route counts once.
fn[G : @moongraph.Topology] closeness_centrality( graph : G, ) -> @moongraph.Scores
One over the total distance from a node to every other, so a node at the middle of the graph scores highest.
fn[G : @moongraph.Topology] degree_centrality( graph : G, ) -> @moongraph.Scores
The share of nodes a node is joined to.
fn[G : @moongraph.Topology] edge_betweenness(graph : G) -> Array[Double]
The number of shortest routes that use each edge.
fn[G : @moongraph.Topology] eigenvector_centrality( graph : G, iterations~ : Int32 = 100, tolerance~ : Double = 1.0e-9, ) -> @moongraph.Scores
The score a walk that keeps stepping to a neighbour settles on, by power iteration over the adjacency matrix.
fn[G : @moongraph.Topology] group_betweenness( graph : G, group : Array[Int32], ) -> Double
How central a whole set is, read as the routes that pass through any member.
fn[G : @moongraph.Topology] group_closeness( graph : G, group : Array[Int32], ) -> Double
How close a whole set is to everything else: one over the summed distance from the set.
fn[G : @moongraph.Topology] harmonic_centrality( graph : G, ) -> @moongraph.Scores
Harmonic centrality: the sum of one over the distance to every other node, which is defined even when the graph is not connected.
fn[G : @moongraph.Topology] hits( graph : G, iterations~ : Int32 = 100, ) -> (@moongraph.Scores, @moongraph.Scores)
The hub and authority scores a link graph implies, by the iteration that gives each node the sum of its neighbours' authority and the other way round.
fn[G : @moongraph.Topology] katz( graph : G, alpha~ : Double = 0.1, beta~ : Double = 1.0, iterations~ : Int32 = 100, ) -> @moongraph.Scores
Katz centrality: every route between two nodes counts, with longer routes counting less, which makes it work on graphs a power iteration would stall on.
fn[G : @moongraph.Topology] load_centrality(graph : G) -> @moongraph.Scores
Load centrality: the share of the shortest routes through each node when every pair sends one unit, counted without halving.
fn[G : @moongraph.Topology] pagerank( graph : G, damping~ : Double = 0.85, iterations~ : Int32 = 100, tolerance~ : Double = 1.0e-9, ) -> @moongraph.Scores
PageRank: the share of a walk's time each node holds, with a jump back to a random node now and then.
fn[G : @moongraph.Topology] subgraph_centrality( graph : G, ) -> @moongraph.Scores
Subgraph centrality: the weighted count of closed routes from a node back to itself, longer ones counting less.

moongraph/community6 items

fn[G : @moongraph.Topology] girvan_newman( graph : G, count : Int32, ) -> @moongraph.Partition
Groups formed by taking out the edges that carry the most routes, one at a time, and stopping when the graph falls into count pieces. The pieces are what comes back.
fn[G : @moongraph.Topology] greedy_modularity( graph : G, ) -> @moongraph.Partition
Groups grown greedily on modularity: start from every node alone and merge the pair of groups that raises modularity most, until no merge helps.
fn[G : @moongraph.Topology] k_clique_communities( graph : G, k : Int32, ) -> @moongraph.Partition
The groups of a graph that overlap: the maximal cliques of size at least k, merged when they share k - 1 nodes.
fn[G : @moongraph.Topology] label_propagation( graph : G, rounds~ : Int32 = 20, ) -> @moongraph.Partition
Groups found by letting every node shout its label and take the most common one among its neighbours until nothing changes.
fn[G : @moongraph.Topology] leiden( graph : G, resolution~ : Double = 1.0, rounds~ : Int32 = 8, ) -> @moongraph.Partition
Groups grown by the Leiden walk: local moving, then a refinement that cuts every group into connected pieces, then a fold that treats those pieces as nodes.
fn[G : @moongraph.Topology] louvain( graph : G, resolution~ : Double = 1.0, rounds~ : Int32 = 8, ) -> @moongraph.Partition
Groups grown by local search on modularity: every node starts alone, moves to the group that raises modularity most, and the groups are then treated as nodes and the walk repeats.

moongraph/metric52 items

fn[G : @moongraph.Topology] accessibility( graph : G, cutoff : Double, weight? : (Int32) -> Double, ) -> Array[Int32]
How many nodes each node can reach within cutoff, itself included: the count of nodes whose cheapest route from it costs at most cutoff.
fn[G : @moongraph.Topology] accessibility_decay( graph : G, cutoff : Double, decay : Double, weight? : (Int32) -> Double, ) -> Array[Double]
How much each node can reach within cutoff, with the near neighbourhood weighted more than the far one: a node at cost d contributes exp(-decay · d), so decay of zero counts everything at one and a larger decay narrows the view to what is close.
fn[G : @moongraph.Topology] adamic_adar( graph : G, a : Int32, b : Int32, ) -> Double
The same idea with the neighbourhood size taken as just over one, which fades faster as a hub grows.
fn[G : @moongraph.Topology] assortativity(graph : G) -> Double
The degree correlation: whether nodes of like degree prefer each other.
fn[G : @moongraph.Topology + @moongraph.Inbound] average_clustering( graph : G, neighborhood~ : Neighborhood = Outward, ) -> Double
The mean of the local clustering over every node, under the same reading.
fn[G : @moongraph.Topology] average_path_length(graph : G) -> Double
The average number of hops between a pair of nodes, over the pairs that can reach each other. The reference graphs a small-world question needs are built by the caller — random_rewire in the approximation package makes one — so this stays a measurement.
fn[G : @moongraph.Topology] barycenter( graph : G, weight? : (Int32) -> Double, ) -> Array[Int32]
The nodes that sit closest to everything else put together: the ones with the smallest total distance to the rest of the graph, not the smallest worst case. A centre minimises the worst trip; a barycentre minimises the sum of all of them, which is the node to put a depot on when every trip happens once.
fn[G : @moongraph.Topology] center(graph : G) -> Array[Int32]
The nodes at the radius: the middle of the graph.
fn[G : @moongraph.Topology + @moongraph.Inbound] clustering( graph : G, node : Int32, neighborhood~ : Neighborhood = Outward, ) -> Double
How many of a node's neighbours are themselves neighbours, as a fraction of the pairs that could be.
fn[G : @moongraph.Topology] common_neighbors( graph : G, a : Int32, b : Int32, ) -> Int32
How many neighbours two nodes share. The pair itself does not count, which is what a link prediction asks: who else knows both of them.
fn[G : @moongraph.Topology] communicability( graph : G, weight? : (Int32) -> Double, ) -> Array[Array[Double]]
How many routes of every length join each pair of nodes, summed into one number: the entry (u,v) of exp(A), where A is the adjacency matrix. The diagonal is the number of closed walks from a node back to itself, and the off-diagonal is the count of walks of any length — a measure of how many ways the two nodes can reach each other, which is the matrix counterpart of "how well connected" in a network with cycles.
fn[G : @moongraph.Topology + @moongraph.Inbound] constraint( graph : G, weight? : (Int32) -> Double, ) -> Array[Double?]
Burt's constraint: how much of a node's time is spent on neighbours who are themselves tied to the node's other neighbours. A node whose friends all know each other is constrained — every favour it does travels down the same channels, and it has no way to play one side against the other. A node whose neighbours are strangers to each other sits in a structural hole, and the constraint is low.
fn[G : @moongraph.Topology + @moongraph.Inbound] current_flow_betweenness( graph : G, weight? : (Int32) -> Double, ) -> @moongraph.Scores
Current-flow betweenness: how much of the current between every pair of nodes passes through a node, when the graph is a circuit and each pair sends one unit. A node that every route has to cross scores high; a node beside a fat alternative route scores low, which is the difference between counting shortest routes and measuring capacity.
fn[G : @moongraph.Topology + @moongraph.Inbound] current_flow_closeness( graph : G, weight? : (Int32) -> Double, ) -> @moongraph.Scores
Current-flow closeness: how close a node is to the rest when distance is measured as electrical resistance rather than as the length of a route. A node with many parallel paths scores better than one behind a single bridge, even when both are the same number of hops from everything, which is the difference between counting routes and measuring how much they can carry.
fn[G : @moongraph.Topology] degree(graph : G) -> Array[Int32]
The number of edges out of each node.
fn[G : @moongraph.Topology] degree_histogram(graph : G) -> Array[Int32]
How many nodes have each degree, from 0 up to the largest.
fn[G : @moongraph.Topology] density(graph : G) -> Double
How many of the possible edges are present: 0 for an empty graph, 1 for a complete one.
fn[G : @moongraph.Topology] diameter(graph : G) -> Int32
The largest eccentricity: the longest shortest route.
fn[G : @moongraph.Topology] distinct_edge_count(graph : G) -> Int32
How many edges there are, counting a pair once however many parallel edges join it.
fn[G : @moongraph.Topology] eccentricity(graph : G, node : Int32) -> Int32
The number of hops from node to its furthest node.
fn[G : @moongraph.Topology + @moongraph.Inbound] effective_size( graph : G, weight? : (Int32) -> Double, ) -> Array[Double?]
Effective size: the number of neighbours a node has, discounted by how much those neighbours are tied to each other. A node whose five friends are five strangers has an effective size near five; a node in a clique of five has one near one, because every friend is the same friend.
fn[G : @moongraph.Topology] efficiency(graph : G) -> Double
How well the graph moves information: the mean of one over the shortest route between every pair.
fn[G : @moongraph.Topology + @moongraph.Inbound] in_degree( graph : G, ) -> Array[Int32]
The number of edges into each node.
fn[G : @moongraph.Topology] link_prediction( graph : G, kind~ : SimilarityKind = Jaccard, ) -> Array[(Int32, Int32, Double)]
Every pair of nodes with no edge between them, scored as a candidate link. The pairs come in ascending order, so two runs on the same graph answer the same array.
fn[G : @moongraph.Topology] low_connectivity_nodes( graph : G, impedance~ : Double, count~ : Int32, weight? : (Int32) -> Double, ) -> Array[Int32]
The nodes that sit in a piece of the network too small to count: cut the graph down to the edges a trip of impedance can use, and a node belongs to a piece of that graph. A piece with fewer than count nodes is a pocket — a suburb with one road in, a station whose platforms are joined to nothing else — and the nodes in it are the ones a low-connectivity study is looking for.
fn[G : @moongraph.Topology] modularity( graph : G, partition : @moongraph.Partition, ) -> Double
How well the edges between two groups explain themselves: the edge weight inside each group compared with what a random rewiring would give.
fn[G : @moongraph.Topology] nearest_pois( graph : G, pois : Array[Int32], count~ : Int32 = 1, cutoff~ : Double, weight? : (Int32) -> Double, ) -> Array[Array[Double?]]
The distances from every node to its nearest points of interest, one entry per rank. This is Pandana's nearest_pois: not "how many shops are within a mile" but "how far is the nearest, the second nearest, the third", which is the question a site-selection study actually asks.
fn[G : @moongraph.Topology] percolation_centrality( graph : G, states : Array[Double], ) -> @moongraph.Scores
Percolation centrality: betweenness where a route counts for how much of the graph it starts in. Each node carries a state — the share of it that is "percolated", one for a node already infected, zero for one untouched — and a pair (s,t) contributes to a node in proportion to x(s) / (Σx − x(v)), the infected share of the source over the infected share available beside the node being measured.
fn[G : @moongraph.Topology] periphery(graph : G) -> Array[Int32]
The nodes at the diameter: the ends of the longest shortest route.
fn[G : @moongraph.Topology] radius(graph : G) -> Int32
The smallest eccentricity: the best node to stand at.
fn[G : @moongraph.Topology] reciprocity(graph : G) -> Double
The fraction of edges that point both ways, over the edges a directed graph has.
fn[G : @moongraph.Topology + @moongraph.Inbound] resistance_distance( graph : G, a : Int32, b : Int32, weight? : (Int32) -> Double, ) -> Double?
The resistance distance between every pair of nodes: what a circuit built from the graph would resist between them. None outside the graph, and for a pair in different pieces.
fn[G : @moongraph.Topology + @moongraph.Inbound] resistance_matrix( graph : G, weight? : (Int32) -> Double, ) -> Array[Array[Double?]]
Every pair's resistance distance, as a matrix. The answer is symmetric, its diagonal is zero, and a pair in different pieces of a graph that falls apart is None — no current can pass between them, which is not the same as a resistance of zero.
fn[G : @moongraph.Topology] resource_allocation( graph : G, a : Int32, b : Int32, ) -> Double
How much traffic a route could carry between two nodes under the similarity measures above: the resources a node and its neighbours allocate to it.
fn[G : @moongraph.Topology] rich_club(graph : G) -> Array[Double?]
Whether the well-connected nodes of a graph are connected to each other. For each degree k, phi(k) is the share of the edges that could join the nodes of degree above k that actually do: 2·E_k / (N_k·(N_k - 1)).
fn[G : @moongraph.Topology] rich_club_at( graph : G, degree : Int32, ) -> Double?
The coefficient at one threshold, by name rather than by walking the whole array.
fn[G : @moongraph.Topology] similarity( graph : G, a : Int32, b : Int32, kind~ : SimilarityKind = Jaccard, ) -> Double
How alike two nodes are: how much their neighbourhoods overlap. The seven measures differ only in what they divide by, or in how much they trust a hub, and all of them read the neighbourhoods out of the graph rather than out of a stored embedding.
fn[ G : @moongraph.Topology + @moongraph.Inbound, H : @moongraph.Topology + @moongraph.Inbound, ] smallworld_omega( graph : G, lattice~ : H, ) -> Double
The small-world coefficient of Watts and Strogatz rewritten as a difference rather than a ratio, which behaves near the ends: L_lattice/L − C/C_lattice. A graph that is as clustered as the lattice and as short as random scores near zero; a lattice scores positive, and a graph that has lost the lattice's clustering scores negative.
fn[ G : @moongraph.Topology + @moongraph.Inbound, H : @moongraph.Topology + @moongraph.Inbound, K : @moongraph.Topology, ] smallworld_sigma( graph : G, lattice~ : H, random~ : K, ) -> Double
The small-world coefficient: how much more clustered a graph is than a random graph with the same size and average degree, divided by how much longer its routes are. A value well above one is the signature of a small world — clustered like a lattice, short like a random graph — and a value near one means the graph is indistinguishable from random on these two measures.
fn[G : @moongraph.Topology] square_clustering( graph : G, node : Int32, ) -> Double
The square clustering of a node: how many of the pairs of its neighbours that share a non-neighbour are joined by a four-node square.
fn[G : @moongraph.Topology + @moongraph.Inbound] total_degree( graph : G, ) -> Array[Int32]
The number of edges on each node, both directions together.
fn[G : @moongraph.Topology + @moongraph.Inbound] transitivity( graph : G, neighborhood~ : Neighborhood = Outward, ) -> Double
The corners the nodes close over the corners they could close, summed over the graph: the chance that two neighbours of a node are neighbours of each other, weighted by node.
let triad_names : Array[String] = [ "003", "012", "102", "021D", "021U", "021C", "111D", "111U", "030T", "030C", "201", "120D", "120U", "120C", "210", "300", ]
The sixteen triad types, in the order the census counts them. The name is the shape's canonical label: how many arcs it has, then a letter for the direction they take.
fn[G : @moongraph.Topology + @moongraph.Inbound] triadic_census( graph : G, ) -> TriadCensus
The census of the sixteen triad types: how many triples of nodes carry no arc, one, two or three, and in which directions. An undirected graph is read as the directed graph that has both arcs for every edge, so its triples are the four symmetric shapes.
fn[G : @moongraph.Topology + @moongraph.Inbound] triangles( graph : G, neighborhood~ : Neighborhood = Outward, ) -> (Array[Int32], Int32)
The triangles each node takes part in, and how many the graph has.
fn[G : @moongraph.Topology] voterank(graph : G) -> Array[Int32]
The nodes a spreading process should start from, by the VoteRank rule: every node starts with a vote of one and a score of zero, the score of a node is the abilities of its neighbours, the best score is elected, and the elected node's neighbours lose a little ability — a share of 1/avg(degree) each — so that a cluster cannot elect itself twice.
fn[G : @moongraph.Topology] wiener_index( graph : G, weight? : (Int32) -> Double, ) -> Double?
The Wiener index: the sum of the shortest-path distances over every pair of nodes. It is the one number a chemist asks of a molecule's graph — how spread out it is — and the one number a network planner asks of a road graph, summed over all the trips there are.
fn[G : @moongraph.Topology + @moongraph.Inbound] wl_hash( graph : G, iterations~ : Int32 = 3, ) -> String
The Weisfeiler-Lehman hash of a graph: a fingerprint that isomorphic graphs always share.
enum Neighborhood
Which neighbours a triangle or a clustering coefficient reads when the graph has directions.
enum SimilarityKind
Which similarity measure to use. The first four divide a shared-neighbour count by a size; the last three weigh the shared neighbours by how rare they are, or ignore them and read only the two degrees.
struct TriadCensus
How many triples of each of the sixteen shapes a graph has. count follows the order of triad_names, and the sixteen counts always add up to the number of triples the graph has room for.
fn TriadCensus::of(self : TriadCensus, name : String) -> Int32?
The census of one shape, looked up by its name.

moongraph/struct28 items

fn[G : @moongraph.Topology] bipartition(graph : G) -> Array[Int32]?
A two-colouring of the graph, or None when an odd cycle makes one impossible.
fn[G : @moongraph.Topology] core_numbers(graph : G) -> Array[Int32]
The core number of each node: the largest k such that the node lies in a subgraph where every node has at least k neighbours inside it.
fn[G : @moongraph.Topology] d_separator( graph : G, x : Array[Int32], y : Array[Int32], z : Array[Int32], ) -> Bool
Whether the three sets separate the first two in the sense of d-separation: every route between them meets the third.
fn gale_ryser(rows : Array[Int32], columns : Array[Int32]) -> Bool
The Gale-Ryser criterion for two non-increasing sequences to be the row and column sums of a 0-1 matrix: the totals agree and every row prefix fits in the columns.
fn[G : @moongraph.Topology + @moongraph.Inbound] is_arborescence( graph : G, ) -> Bool
Whether the directed graph is a tree with every edge pointing away from its root.
fn[G : @moongraph.Topology] is_at_free(graph : G) -> Bool
Whether the graph has no four nodes that induce exactly two edges with nothing else.
fn[G : @moongraph.Topology] is_bipartite(graph : G) -> Bool
Whether the graph has two colours.
fn[G : @moongraph.Topology + @moongraph.Inbound] is_branching( graph : G, ) -> Bool
Whether every node has at most one edge entering it.
fn is_digraphical(degrees : Array[(Int32, Int32)]) -> Bool
Whether the pairs can be the in- and out-degrees of a directed graph without repeated edges, by the Gale-Ryser criterion.
fn[G : @moongraph.Topology] is_distance_regular(graph : G) -> Bool
Whether any two non-adjacent nodes have the same number of common neighbours.
fn[G : @moongraph.Topology] is_forest(graph : G) -> Bool
Whether every component is a tree.
fn is_graphical(degrees : Array[Int32]) -> Bool
Whether the degrees can be the degrees of a simple graph, by the Erdős-Gallai criterion.
fn[G : @moongraph.Topology] is_isolate(graph : G, node : Int32) -> Bool
Whether a node has no edges.
fn is_multigraphical(degrees : Array[Int32]) -> Bool
Whether the degrees can be the degrees of a graph that may repeat an edge.
fn[G : @moongraph.Topology] is_planar(graph : G) -> Bool
Whether the graph can be drawn in the plane without crossed edges.
fn[G : @moongraph.Topology] is_regular(graph : G) -> Bool
Whether every node has the same degree.
fn[G : @moongraph.Topology] is_threshold(graph : G) -> Bool
Whether the graph is a threshold graph: one that can be built by adding isolated nodes and dominating nodes one at a time.
fn[G : @moongraph.Topology] is_tournament(graph : G) -> Bool
Whether every pair of nodes has exactly one edge, in one direction or the other.
fn[G : @moongraph.Topology] is_tree(graph : G) -> Bool
Whether the graph is connected and has one edge fewer than nodes.
fn[G : @moongraph.Topology] isolates(graph : G) -> Array[Int32]
The nodes with no edges, ascending.
fn[G : @moongraph.Topology] k_core(graph : G, k : Int32) -> Array[Int32]
The nodes of the k-core: the largest subgraph where every node has at least k neighbours inside it.
fn[G : @moongraph.Topology] k_shell(graph : G, k : Int32) -> Array[Int32]
The nodes of the k-shell: in the k-core but not in the k+1-core.
fn[G : @moongraph.Topology] k_truss(graph : G, k : Int32) -> Array[Int32]
The nodes of the k-truss: the largest subgraph in which every edge lies in at least k - 2 triangles inside it.
fn[G : @moongraph.Topology] minimal_d_separator( graph : G, x : Array[Int32], y : Array[Int32], ) -> Array[Int32]?
A smallest set of nodes that separates the first two sets, or None when they cannot be separated.
fn[G : @moongraph.Topology + @moongraph.Inbound] moral_graph( graph : G, ) -> @moongraph.Graph[Int32, Unit]
The moral graph: the graph with every pair of parents of a node joined, which turns a directed model into one where separation is plain reachability.
fn[G : @moongraph.Topology] onion_layers(graph : G) -> Array[Int32]
The onion layer of each node: how many rounds of peeling it takes to strip the graph down to it.
fn[G : @moongraph.Topology] planarity_embedding( graph : G, ) -> Array[Array[Int32]]?
A rotation system that draws the graph on a surface with no crossing, or None when no such drawing was found.
fn[G : @moongraph.Topology] score_sequence(graph : G) -> Array[Int32]
The out-degree of each node, without the self-loops a tournament cannot have.

moongraph/iso4 items

fn[G : @moongraph.Topology] could_be_isomorphic(a : G, b : G) -> Bool
A cheap necessary condition: the same number of nodes, edges and the same degree sequence. Two graphs that pass may still differ; two that fail cannot match.
fn[G : @moongraph.Topology] fast_could_be_isomorphic(a : G, b : G) -> Bool
The cheapest necessary condition there is: the same node and edge counts.
fn[G : @moongraph.Topology] is_isomorphic(a : G, b : G) -> Bool
Whether the two graphs are the same graph with the nodes renamed.
fn[G : @moongraph.Topology] vf2_mapping( a : G, b : G, ) -> @moongraph.Mapping?
A renaming that turns the first graph into the second, or None when there is none.

moongraph/op26 items

fn bind( shape : @moongraph.Graph[Int32, Unit], pieces : Array[@moongraph.Graph[Int32, Unit]], ) -> @moongraph.Graph[Int32, Unit]
Replaces every node of a shape with a graph: the answer holds each piece side by side, and wherever the shape had an edge, the two pieces are joined completely — every node of one to every node of the other.
fn[G : @moongraph.Topology] bipartite_projection( graph : G, side : Array[Int32], ) -> @moongraph.Graph[Int32, Unit]
The graph that one side of a bipartite graph sees: hand it a set of nodes, and every two of them that share a neighbour become adjacent. This is how a graph of people and events becomes a graph of people — co-stars, co-authors, co-purchases — and the shape it asks about is the one the events were hiding.
fn cartesian_product( a : @moongraph.Graph[Int32, Unit], b : @moongraph.Graph[Int32, Unit], ) -> @moongraph.Graph[Int32, Unit]
The pair graph: one node per pair of inputs, joined when both coordinates are joined.
fn closure( graph : @moongraph.Graph[Int32, Unit], ) -> @moongraph.Graph[Int32, Unit]
The graph with an edge from every node to every node it can reach, itself included: the reflexive transitive closure, alga's closure.
fn[G : @moongraph.Topology] complement( graph : G, ) -> @moongraph.Graph[Int32, Unit]
The graph with the same nodes and the edges it does not have.
fn compose( a : @moongraph.Graph[Int32, Unit], b : @moongraph.Graph[Int32, Unit], ) -> @moongraph.Graph[Int32, Unit]
The graph with b laid over a where their node numbers agree.
fn connect( a : @moongraph.Graph[Int32, Unit], b : @moongraph.Graph[Int32, Unit], ) -> @moongraph.Graph[Int32, Unit]
The two graphs side by side, with every node of the first joined to every node of the second: alga's connect.
fn contraction( graph : @moongraph.Graph[Int32, Unit], nodes : Array[Int32], ) -> @moongraph.Graph[Int32, Unit]
The graph with a set of nodes replaced by one: the contraction.
fn difference( a : @moongraph.Graph[Int32, Unit], b : @moongraph.Graph[Int32, Unit], ) -> @moongraph.Graph[Int32, Unit]
The edges of the first graph that the second does not have.
fn disjoint_union( a : @moongraph.Graph[Int32, Unit], b : @moongraph.Graph[Int32, Unit], ) -> @moongraph.Graph[Int32, Unit]
The two graphs side by side with the second one's numbers shifted up, so nothing collides.
fn edge_subgraph( graph : @moongraph.Graph[Int32, Unit], edges : Array[Int32], ) -> @moongraph.Graph[Int32, Unit]
The part of the graph on the given edges, keeping the node numbers.
fn[N, E, F] emap( graph : @moongraph.Graph[N, E], f : (E) -> F, ) -> @moongraph.Graph[N, F]
The graph with every edge payload put through f, and the nodes kept as they are.
fn[N, E, M, F] gmap( graph : @moongraph.Graph[N, E], node_f : (N) -> M, edge_f : (E) -> F, ) -> @moongraph.Graph[M, F]
Both at once: alga's gmap, the node payloads through node_f and the edge payloads through edge_f.
fn graph_power( graph : @moongraph.Graph[Int32, Unit], power : Int32, ) -> @moongraph.Graph[Int32, Unit]
The graph reached in at most power steps of the input.
fn intersection( a : @moongraph.Graph[Int32, Unit], b : @moongraph.Graph[Int32, Unit], ) -> @moongraph.Graph[Int32, Unit]
The graph that keeps the edges both inputs have.
fn[G : @moongraph.Topology, H : @moongraph.Topology] is_subgraph_of( small : G, big : H, ) -> Bool
Whether one graph sits inside another: every node number it uses exists in the larger graph, and every edge it has is an edge there too, in the same direction when the larger graph is directed.
fn line_graph( graph : @moongraph.Graph[Int32, Unit], ) -> @moongraph.Graph[Int32, Unit]
The line graph: one node per edge of the input, joined when the edges met.
fn[N, E, M] nmap( graph : @moongraph.Graph[N, E], f : (N) -> M, ) -> @moongraph.Graph[M, E]
The graph with every node payload put through f, and the edges kept as they are.
fn[N, E] remove_edge( graph : @moongraph.Graph[N, E], from : Int32, to : Int32, ) -> @moongraph.Graph[N, E]
A copy of the graph with one edge between two nodes taken out: the first one found, which in a simple graph is the only one. Asking for an edge the graph does not have changes nothing.
fn[N, E] remove_vertex( graph : @moongraph.Graph[N, E], node : Int32, ) -> @moongraph.Graph[N, E]
A copy of the graph with one node and every edge touching it taken out.
fn reverse( graph : @moongraph.Graph[Int32, Unit], ) -> @moongraph.Graph[Int32, Unit]
The same edges with every direction turned around.
fn strong_product( a : @moongraph.Graph[Int32, Unit], b : @moongraph.Graph[Int32, Unit], ) -> @moongraph.Graph[Int32, Unit]
The strong product: an edge where either coordinate has one or both do.
fn subgraph( graph : @moongraph.Graph[Int32, Unit], nodes : Array[Int32], ) -> @moongraph.Graph[Int32, Unit]
The part of the graph on the given nodes, with the numbers renumbered from 0 in the order the nodes are listed.
fn symmetric_difference( a : @moongraph.Graph[Int32, Unit], b : @moongraph.Graph[Int32, Unit], ) -> @moongraph.Graph[Int32, Unit]
The edges exactly one of the two graphs has.
fn tensor_product( a : @moongraph.Graph[Int32, Unit], b : @moongraph.Graph[Int32, Unit], ) -> @moongraph.Graph[Int32, Unit]
The tensor product: an edge where both coordinates have one.
fn union( a : @moongraph.Graph[Int32, Unit], b : @moongraph.Graph[Int32, Unit], ) -> @moongraph.Graph[Int32, Unit]
The graph that keeps every node and edge present in either input.

moongraph/approx9 items

fn[G : @moongraph.Topology] densest_subgraph(graph : G) -> Array[Int32]
The subgraph with the most edges per node, by peeling away the node of least degree until the density stops rising.
fn[G : @moongraph.Topology] dominating_set(graph : G) -> Array[Int32]
A set of nodes every other node is joined to or part of, by taking the highest-degree node until none is left uncovered.
fn[G : @moongraph.Topology] random_rewire( graph : G, rng~ : &@contract.Rng, ) -> @moongraph.Graph[Int32, Unit]
The graph with every edge cut and reattached somewhere else, keeping the degree sequence.
fn[G : @moongraph.Topology] spanner( graph : G, stretch~ : Int32 = 2, ) -> @moongraph.Graph[Int32, Unit]
A subgraph that keeps every short route short: the distance between any two nodes grows by at most the given factor, and the spanner has far fewer edges than the original.
fn[G : @moongraph.Topology] tour_length( graph : G, tour : Array[Int32], weight? : (Int32) -> Double, ) -> Double?
How long a tour is, counting every leg it lists. A tour that repeats its start at the end is measured whole, so this is the number to compare against the exact answer; a tour that stops after its last stop is measured as the open walk it is.
fn[G : @moongraph.Topology] traveling_salesman( graph : G, start~ : Int32, weight? : (Int32) -> Double, ) -> Array[Int32] raise @moongraph.Refused
A tour that visits every node and returns to the start, by taking the nearest unvisited node each step and then straightening the result.
fn[G : @moongraph.Topology] traveling_salesman_exact( graph : G, start~ : Int32, weight? : (Int32) -> Double, ) -> Array[Int32] raise @moongraph.Refused
The shortest tour that visits every node once and comes back, by Held and Karp's dynamic program: for every subset of the stops and every stopping point, the cheapest way to cover the subset and end there, built up one subset at a time.
fn[G : @moongraph.Topology] treewidth_bound(graph : G) -> Int32
An upper bound on the treewidth, by eliminating the node of least degree and remembering the largest neighbourhood that had to be made a clique.
fn[G : @moongraph.Topology] vertex_cover(graph : G) -> Array[Int32]
A set of nodes that touches every edge, at most twice the smallest such set.

moongraph/gen16 items

fn barabasi_albert( nodes : Int32, attach~ : Int32, rng~ : &@contract.Rng, ) -> @moongraph.Graph[Int32, Unit]
A preferential-attachment graph: every new node joins the existing ones that already have the most edges, which is what makes the degree distribution heavy tailed.
fn barbell(nodes : Int32) -> @moongraph.Graph[Int32, Unit]
Two complete graphs joined by one edge: the graph a minimum cut is worst on.
fn complete( nodes : Int32, directed~ : Bool = false, ) -> @moongraph.Graph[Int32, Unit]
The complete graph on nodes nodes: every pair joined.
fn cycle(nodes : Int32) -> @moongraph.Graph[Int32, Unit]
A single cycle through every node.
fn[G : @moongraph.Topology] double_edge_swap( graph : G, swaps~ : Int32, rng~ : &@contract.Rng, ) -> @moongraph.Graph[Int32, Unit]
A graph with the same degree sequence where the edges have been shuffled: pick two edges that share no endpoint, and swap their ends. A double edge swap keeps every node's degree exactly, so it is the null model for "is this structure more than its degree sequence explains" — the rewiring that a rich-club or assortativity question compares against.
fn grid(rows : Int32, columns : Int32) -> @moongraph.Graph[Int32, Unit]
A grid of rows by columns nodes, each joined to the ones beside it.
fn ladder(nodes : Int32) -> @moongraph.Graph[Int32, Unit]
Two paths with rungs between them: a ladder.
fn path( nodes : Int32, directed~ : Bool = false, ) -> @moongraph.Graph[Int32, Unit]
A path through every node, in number order.
fn petersen() -> @moongraph.Graph[Int32, Unit]
The Petersen graph: three nodes joined to three, with the matching inside each.
fn random_gnm( nodes : Int32, edges : Int32, rng~ : &@contract.Rng, directed~ : Bool = false, ) -> @moongraph.Graph[Int32, Unit]
A random graph with exactly edges edges, drawn without repeats.
fn random_gnp( nodes : Int32, probability~ : Double, rng~ : &@contract.Rng, directed~ : Bool = false, ) -> @moongraph.Graph[Int32, Unit]
A random graph in which every pair is joined with the same chance.
fn random_regular( nodes : Int32, degree~ : Int32, rng~ : &@contract.Rng, ) -> @moongraph.Graph[Int32, Unit]
A graph in which every node has the same number of edges, by repeatedly retrying pairs until the degrees line up.
fn random_tree( nodes : Int32, rng~ : &@contract.Rng, ) -> @moongraph.Graph[Int32, Unit]
A tree grown at random: every new node joins one node already in the tree.
fn star(nodes : Int32) -> @moongraph.Graph[Int32, Unit]
One node joined to all the others, which are joined to nothing else.
fn watts_strogatz( nodes : Int32, neighbours~ : Int32, probability~ : Double, rng~ : &@contract.Rng, ) -> @moongraph.Graph[Int32, Unit]
A small-world graph: a ring with each node joined to its nearest neighbours and a few edges rewired at random.
fn wheel(nodes : Int32) -> @moongraph.Graph[Int32, Unit]
A cycle with a hub in the middle joined to every node of it.

moongraph/io13 items

fn[G : @moongraph.Topology] adjacency_list(graph : G) -> String
A graph as an adjacency list: one line per node, starting with its number.
fn[G : @moongraph.Topology] edge_list( graph : G, data~ : Bool = false, ) -> String
A graph as an edge list: one from to pair per line, an optional third column for the weight, and # for comments.
fn[G : @moongraph.Topology] gml(graph : G) -> String
A graph in GML: a nested list of key and value.
fn[G : @moongraph.Topology] graph6(graph : G) -> String
A graph in graph6: the node count in the first bytes, then one bit per possible edge.
fn[G : @moongraph.Topology] pajek(graph : G) -> String
A graph in Pajek's format: a vertex count, then the arcs.
fn parse_adjacency( text : StringView, directed~ : Bool = false, ) -> @moongraph.Graph[Unit, Unit] raise @moongraph.Refused
The graph an adjacency list describes.
fn parse_edges( text : StringView, directed~ : Bool = false, ) -> @moongraph.Graph[Unit, Double] raise @moongraph.Refused
The graph an edge list describes.
fn parse_gml( text : StringView, ) -> @moongraph.Graph[Unit, Unit] raise @moongraph.Refused
The graph a GML document describes.
fn parse_graph6( text : StringView, ) -> @moongraph.Graph[Unit, Unit] raise @moongraph.Refused
The graph a graph6 string describes.
fn parse_pajek( text : StringView, ) -> @moongraph.Graph[Unit, Unit] raise @moongraph.Refused
The graph a Pajek document describes. Node numbers are shifted down by one, because the format counts from one.
fn parse_sparse6( text : StringView, ) -> @moongraph.Graph[Unit, Unit] raise @moongraph.Refused
The graph a sparse6 string describes.
fn[G : @moongraph.Topology] sparse6(graph : G) -> String
A graph in sparse6, which stores only the edges that are there, in the format's own bytes.
fn[G : @moongraph.Topology] text(graph : G) -> String
A graph in a plain form, which is its adjacency list.

moongraph/io/graphml4 items

fn read(text : StringView) -> Loaded raise @moongraph.Refused
Reads a GraphML document.
fn[G : @moongraph.Topology] write( graph : G, labels~ : (Int32) -> String = fn(node)
Writes a graph as GraphML.
struct Keys
The keys a document declares, so that a caller can tell a label from a weight.
struct Loaded
A graph carrying node labels and edge weights.

moongraph/dsu9 items

struct Dsu
A disjoint-set forest over the numbers 0 .. count - 1.
fn Dsu::count(self : Dsu) -> Int32
How many pieces are left.
fn Dsu::find(self : Dsu, node : Int32) -> Int32
The representative of the piece node is in. Two numbers are in the same piece exactly when their representatives are equal.
fn Dsu::groups(self : Dsu) -> Array[Array[Int32]]
Every piece, as the numbers it holds in ascending order; the pieces themselves in the order their smallest number appears.
fn Dsu::length(self : Dsu) -> Int32
How many numbers the forest was built for.
fn Dsu::new(count : Int32) -> Dsu
A forest of count pieces, each holding one number.
fn Dsu::same(self : Dsu, a : Int32, b : Int32) -> Bool
Whether a and b are in the same piece.
fn Dsu::size(self : Dsu, node : Int32) -> Int32
How many numbers the piece node is in holds.
fn Dsu::union(self : Dsu, a : Int32, b : Int32) -> Bool
Joins the pieces a and b are in, and answers whether they were apart — false when the two were already in one piece, which is what a spanning-tree walk counts with.

moongraph/grid28 items

struct DStar
A board search that has been planned once and can be repaired: it holds the distance estimate of every cell, the queue of cells whose estimate is unsettled, and the offset that keeps the heuristic honest as the start moves.
fn DStar::cost(self : DStar) -> Double
The cost of the route from the start to the goal as last planned, or a very large number when there is none.
fn DStar::path(self : DStar) -> Array[Int32]?
The route as last planned, both ends included, or None when the goal cannot be reached.
fn DStar::replan( self : DStar, start : Int32, changed : Array[Int32], ) -> Unit
Repair the plan after the board changed and the walker moved.
struct Grid
A board of width × height cells, each free or blocked, numbered y * width + x.
fn Grid::a_star(self : Grid, start : Int32, goal : Int32) -> Array[Int32]?
A cheapest route from start to goal, or None when the goal cannot be reached.
fn Grid::bfs(self : Grid, start : Int32, goal : Int32) -> Array[Int32]?
A cheapest route from start to goal when every step costs one, or None when the goal cannot be reached. Breadth first, so the route has the fewest cells.
fn Grid::block(self : Grid, x : Int32, y : Int32) -> Unit
Blocks a cell. Blocking a cell already blocked changes nothing.
fn Grid::blocked_at(self : Grid, node : Int32) -> Bool
Whether the cell a number names is blocked, or off the board.
fn Grid::d_star_lite(self : Grid, start : Int32, goal : Int32) -> DStar
Plan a route from start to goal, keeping the work so the next plan can reuse it.
fn Grid::dijkstra(self : Grid, start : Int32) -> Array[Double]
The cheapest cost of reaching every cell from start, 1.0e300 where a cell cannot be reached. Dijkstra over the eight-connected board.
fn Grid::estimate(self : Grid, from : Int32, to : Int32) -> Double
The octile distance between two cells: the cheapest a walk could possibly be, which is what A* steers by. It never overestimates, so A* still finds a cheapest route.
fn Grid::height(self : Grid) -> Int32
How tall the board is.
fn Grid::inside(self : Grid, x : Int32, y : Int32) -> Bool
Whether a cell is on the board.
fn Grid::is_blocked(self : Grid, x : Int32, y : Int32) -> Bool
Whether a cell is blocked, or off the board.
fn Grid::jps(self : Grid, start : Int32, goal : Int32) -> Array[Int32]?
A cheapest route from start to goal found by the jump-point search, or None when the goal cannot be reached.
fn Grid::line_of_sight(self : Grid, from : Int32, to : Int32) -> Bool
Whether a straight line between two cells stays on the board and off the blocked cells: the test Theta* uses before it lets a route skip a corner.
fn Grid::neighbors(self : Grid, node : Int32) -> Array[Int32]
The free neighbours of a cell, in the order up, down, left, right, then the four diagonals.
fn Grid::new(width : Int32, height : Int32) -> Grid
A board with every cell free.
fn Grid::node(self : Grid, x : Int32, y : Int32) -> Int32
The number the cell (x, y) carries.
fn Grid::path_cost(self : Grid, path : Array[Int32]) -> Double
The cost of walking a route, cell by cell; zero for a route of fewer than two cells.
fn Grid::step_cost(self : Grid, from : Int32, to : Int32) -> Double
The cost of one step between two neighbouring cells: one straight, the diagonal otherwise.
fn Grid::straight_cost(self : Grid, route : Array[Int32]) -> Double
The cost of an any-angle route: the octile length of each straight run, added up. Unlike path_cost, the stops need not be neighbours, only in sight of each other.
fn Grid::theta_star( self : Grid, start : Int32, goal : Int32, ) -> Array[Int32]?
The cheapest any-angle route from start to goal, both included, or None when no route exists.
fn Grid::unblock(self : Grid, x : Int32, y : Int32) -> Unit
Frees a cell that was blocked.
fn Grid::width(self : Grid) -> Int32
How wide the board is.
fn Grid::x(self : Grid, node : Int32) -> Int32
The column of a cell number.
fn Grid::y(self : Grid, node : Int32) -> Int32
The row of a cell number.

moongraph/incremental15 items

struct CostFlow
A cost flow network you keep. Each edge has a capacity and a cost per unit, and the answer to slope is the whole shape of the trade — how much the next unit costs, for every unit the network can carry.
fn CostFlow::add_edge( self : CostFlow, from : Int32, to : Int32, capacity : Double, cost : Double, ) -> Int32 raise @moongraph.Refused
Adds an edge with a capacity and a cost per unit, and returns its number. A negative cost is allowed: it means the edge pays to be used.
fn CostFlow::edge_count(self : CostFlow) -> Int32
How many edges the network has.
fn CostFlow::edge_flow( self : CostFlow, edge : Int32, ) -> (Int32, Int32, Double, Double, Double)?
What an edge carries: where it starts, where it ends, its capacity, its flow and its cost.
fn CostFlow::flow( self : CostFlow, source : Int32, sink : Int32, ) -> (Double, Double) raise @moongraph.Refused
The cheapest way to send as much as possible from one node to another: the amount and what it costs. This is the last point of slope, for the caller who only wants the total.
fn CostFlow::new(nodes : Int32) -> CostFlow
An empty network on a fixed number of nodes.
fn CostFlow::slope( self : CostFlow, source : Int32, sink : Int32, ) -> Array[(Double, Double)] raise @moongraph.Refused
The price curve from one node to another: the points (flow, cost) where the cost of the next unit changes. The first point is always (0, 0) and the last is the cheapest way to send as much as the network allows.
struct Flow
A flow network you keep. Edges are added one at a time, capacities can be changed after the fact, and every call to flow pushes only what the change made possible — the flow already there is the starting point, not something to recompute.
fn Flow::add_edge( self : Flow, from : Int32, to : Int32, capacity : Double, ) -> Int32 raise @moongraph.Refused
Adds an edged from one node to another with a capacity, and returns its number. The edge carries no flow to begin with.
fn Flow::change_edge( self : Flow, edge : Int32, capacity : Double, flow : Double, ) -> Unit raise @moongraph.Refused
Replaces one edge's capacity and flow. The flow has to sit between zero and the new capacity, which is what makes the result a flow again; flow then pushes whatever the change allows.
fn Flow::edge_count(self : Flow) -> Int32
How many edges the network has.
fn Flow::edge_flow( self : Flow, edge : Int32, ) -> (Int32, Int32, Double, Double)?
What an edge carries, in the order AtCoder reports it: where it starts, where it ends, its capacity, and its flow.
fn Flow::flow( self : Flow, source : Int32, sink : Int32, limit? : Double, ) -> Double raise @moongraph.Refused
Pushes as much as possible from one node to another, starting from the flow already in the network, and answers how much was added. limit stops the search early, which is what a caller who only needs a known amount asks for.
fn Flow::min_cut(self : Flow, source : Int32) -> Array[Bool]
The source side of a minimum cut, read off the residual network: every node still reachable from the source through an edge with room left. After a flow call this is the cut of smallest capacity, and the nodes on the other side are the ones it separates.
fn Flow::new(nodes : Int32) -> Flow
An empty network on a fixed number of nodes. Nodes are numbered 0..n; the count is chosen here and never changes, since a flow is a question about one fixed set of places.

moongraph/sat5 items

struct TwoSat
A two-satisfiability instance over count boolean variables.
fn TwoSat::add_clause( self : TwoSat, i : Int32, f : Bool, j : Int32, g : Bool, ) -> Unit
Adds the clause "(variable i is f) or (variable j is g)".
fn TwoSat::answer(self : TwoSat) -> Array[Bool]?
An assignment that satisfies every clause, or None when none exists.
fn TwoSat::new(count : Int32) -> TwoSat
An instance over count variables with no clauses yet.
fn TwoSat::satisfiable(self : TwoSat) -> Bool
Whether some assignment makes every clause true.