Skip to content

Graph ​

Graph<N, W> represents a directed weighted graph. Nodes are identified by values of type N and edges carry a weight of type W. Nodes are created automatically when an edge is added, so building a graph never requires separate registration.

Construction ​

SyntaxDescription
Graph<N, W>()Create a new empty directed graph with node type N and weight type W

Supported node and weight types are string, bool, and the numeric types i8–i64, u8–u64, f32, and f64. The two type parameters must be declared explicitly; they cannot be inferred.

Methods ​

MethodSignatureReturnsDescription
addEdge(source: N, target: N, weight: W) => voidvoidAdd an edge; creates the nodes if absent, replaces the weight if the ordered pair already has an edge
addNode(node: N) => voidvoidAdd an isolated node
removeNode(node: N) => booleanbooleanRemove a node and all its incident edges; returns true if the node existed
removeEdge(source: N, target: N) => booleanbooleanRemove the edge between source and target; the nodes remain
hasNode(node: N) => booleanbooleanWhether the node exists
hasEdge(source: N, target: N) => booleanbooleanWhether the ordered pair has an edge
edge(source: N, target: N) => W?W?The weight of the edge, or empty when there is no edge
neighbors(node: N) => N[]N[]The out-neighbors of a node, in insertion order
degree(node: N) => numbernumberThe out-degree of a node
nodeCount() => numbernumberThe total number of nodes
edgeCount() => numbernumberThe total number of edges
isEmpty() => booleanbooleanWhether the graph has no nodes
nodes() => N[]N[]All nodes, in insertion order
edges() => [N, N, W][][N, N, W][]All edges as (source, target, weight) tuples
reverse() => Graph<N, W>Graph<N, W>A new graph with every edge direction reversed
mapEdges((weight: W) => W') => Graph<N, W'>Graph<N, W'>A new graph with weights transformed by the callback
mapNodes((node: N) => N') => Graph<N', W>Graph<N', W>A new graph with node values transformed by the callback
toDot() => stringstringThe graph rendered as Graphviz DOT
toString() => stringstringA compact one-line adjacency representation

neighbors, nodes, and edges return freshly allocated arrays that the caller owns. The native graph handle is released automatically when the binding goes out of scope.

Complexity ​

OperationTime Complexity
addEdge / addNodeO(1) amortized, O(1) expected
hasNode / edge / hasEdgeO(1) expected
removeNodeO(V + E)
removeEdgeO(deg(source))
neighbors / degreeO(deg(node))
nodes / nodeCount / isEmptyO(V) / O(1)
edges / edgeCountO(E) / O(1)
reverse / mapEdges / mapNodesO(V + E)
toDot / toStringO(V + E)

Examples ​

Building a graph ​

tyrus
import { Graph } from "tyrus:algorithm";

const g = Graph<string, i32>();
g.addEdge("a", "b", 1);
g.addEdge("b", "c", 2);
g.addEdge("a", "c", 3);
g.addNode("isolated");

print(g.nodeCount());  // 4
print(g.edgeCount());  // 3
print(g.isEmpty());    // false

Queries ​

tyrus
print(g.hasNode("a"));     // true
print(g.hasEdge("a", "b"));  // true
print(g.edge("a", "b") ?? 0);  // 1
print(g.edge("b", "a") ?? 0);  // 0

print(g.degree("a"));  // 2
print(g.neighbors("a"));  // ["b", "c"]

Iteration ​

tyrus
for (const node of g.nodes()) {
    print(node);
}

for (const [source, target, weight] of g.edges()) {
    print(source, "->", target, "(", weight, ")");
}

Mutation ​

tyrus
print(g.removeEdge("a", "b"));  // true
print(g.hasEdge("a", "b"));     // false

print(g.removeNode("isolated"));  // true
print(g.hasNode("isolated"));     // false

Transformations ​

tyrus
const doubled = g.mapEdges((w: i32): i32 => w * 2);
print(doubled.edge("b", "c") ?? 0);  // 4

const upper = g.mapNodes((n: string): string => n.toUpperCase());
print(upper.hasNode("A"));  // true
print(upper.edge("A", "C") ?? 0);  // 6

const reversed = g.reverse();
print(reversed.hasEdge("c", "b"));  // true
print(reversed.hasEdge("b", "c"));  // false

Rendering ​

tyrus
print(g.toString());
// Graph { "a" -> "b" [label="1"], "a" -> "c" [label="3"], "b" -> "c" [label="2"] }

print(g.toDot());
// digraph {
//     "a" -> "b" [label="1"];
//     "a" -> "c" [label="3"];
//     "b" -> "c" [label="2"];
// }

Numeric nodes and float weights ​

tyrus
const net = Graph<i32, f64>();
net.addEdge(1, 2, 1.5);
net.addEdge(2, 3, 2.5);

print(net.edge(1, 2) ?? 0.0);  // 1.5
print(net.degree(2));          // 1
print(net.nodes());            // [1, 2, 3]