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
| Syntax | Description |
|---|---|
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
| Method | Signature | Returns | Description |
|---|---|---|---|
addEdge | (source: N, target: N, weight: W) => void | void | Add an edge; creates the nodes if absent, replaces the weight if the ordered pair already has an edge |
addNode | (node: N) => void | void | Add an isolated node |
removeNode | (node: N) => boolean | boolean | Remove a node and all its incident edges; returns true if the node existed |
removeEdge | (source: N, target: N) => boolean | boolean | Remove the edge between source and target; the nodes remain |
hasNode | (node: N) => boolean | boolean | Whether the node exists |
hasEdge | (source: N, target: N) => boolean | boolean | Whether 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) => number | number | The out-degree of a node |
nodeCount | () => number | number | The total number of nodes |
edgeCount | () => number | number | The total number of edges |
isEmpty | () => boolean | boolean | Whether 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 | () => string | string | The graph rendered as Graphviz DOT |
toString | () => string | string | A 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
| Operation | Time Complexity |
|---|---|
addEdge / addNode | O(1) amortized, O(1) expected |
hasNode / edge / hasEdge | O(1) expected |
removeNode | O(V + E) |
removeEdge | O(deg(source)) |
neighbors / degree | O(deg(node)) |
nodes / nodeCount / isEmpty | O(V) / O(1) |
edges / edgeCount | O(E) / O(1) |
reverse / mapEdges / mapNodes | O(V + E) |
toDot / toString | O(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()); // falseQueries
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")); // falseTransformations
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")); // falseRendering
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]