/graph
Directed and undirected graph data structures (Graph) with node/edge mutation and traversal helpers.
Traits#
Finite-Graph#
traitExtends: Graph
| Method | Signature | Description |
|---|---|---|
nodes-of | | — |
is-directed? | | — |
has-node? | | — |
node-count-of | | — |
edges-of | | — |
edge-count-of | | — |
in-neighbors-of | | — |
in-degree-of | | — |
degree-of | | — |
sources-of | | — |
sinks-of | | — |
Graph#
traitBase capability trait for local graph navigation (`neighbors-of`). Satisfied by finite graphs, infinite procedural `Implicit-Graph`s, `Func` closures, `Map`s, and custom `Struct`s.
| Method | Signature | Description |
|---|---|---|
neighbors-of | | — |
has-edge? | | — |
out-degree-of | | — |
edge-weight-of | | — |
weighted-neighbors-of | | — |
Mutable-Graph#
traitExtends: Finite-Graph
| Method | Signature | Description |
|---|---|---|
put-node | | — |
put-edge | | — |
remove-node | | — |
remove-edge | | — |
put-all-nodes | | — |
put-all-edges | | — |
Functions & symbols#
Csr-Graph#
Constructs an immutable, cache-local Compressed Sparse Row (CSR) graph with precomputed CSR transpose for `O(1)` incoming neighbor slices.
| Parameter | Type | Optional | Description |
|---|---|---|---|
nodes | (Array Any) | required | Optional vertex list |
edges | (Array Any) | required | Optional edge list |
Any(use /graph [Csr-Graph])
(let g (Csr-Graph [[:a :b 2] [:b :c 3]]))
(assert-eq! (g :a :b) 2)Directed-Graph#
Constructs a mutable directed graph backed by dual outgoing and incoming index-mapped adjacency tables.
| Parameter | Type | Optional | Description |
|---|---|---|---|
nodes | (Array Any) | required | Optional initial vertices |
edges | Any | required | Optional edge array `[[u v [w]] ...]`, adjacency `Map`, or `Finite-Graph` |
Any(use /graph [Directed-Graph])
(let g (Directed-Graph [[:a :b 2.5] [:b :c 1.0]]))
(assert-eq! g:a [:b])Implicit-Graph#
Constructs a procedural or infinite graph computed on demand from `neighbor-fn` and optional `weight-fn`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
neighbor-fn | (Func [Any] Any) | required | Function `(Func [u] ...)` returning neighbors of `u` |
weight-fn | (Func [Any Any] Number) | required | Optional function `(Func [u v] ...)` computing edge weight |
Any(use /graph [Implicit-Graph bfs])
(let g (Implicit-Graph (Func [n] [(+ n 1) (* n 2)])))
(assert-eq! (Array (take 4 (bfs g 1))) [1 2 3 4])Matrix-Graph#
Constructs a dense `N x N` adjacency-matrix graph.
| Parameter | Type | Optional | Description |
|---|---|---|---|
nodes | (Array Any) | required | Optional initial vertices |
edges | (Array Any) | required | Optional edge list |
Any(use /graph [Matrix-Graph put-edge])
(let g (Matrix-Graph [:a :b]))
(put-edge g :a :b 4.0)
(assert-eq! (g :a :b) 4)Undirected-Graph#
Constructs a mutable undirected graph with symmetric edge storage.
| Parameter | Type | Optional | Description |
|---|---|---|---|
nodes | (Array Any) | required | Optional initial vertices |
edges | Any | required | Optional edge array `[[u v [w]] ...]`, adjacency `Map`, or `Finite-Graph` |
Any(use /graph [Undirected-Graph has-edge?])
(let g (Undirected-Graph [[:a :b 3]]))
(assert (has-edge? g :b :a))a-star#
Finds the minimum-cost path from `start` to `goal` using A* search, returning `{:nodes [...] :cost c}` or `()` if unreachable.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Graph | required | Finite or infinite graph with non-negative edge weights |
start | Any | required | Starting vertex |
goal | Any | required | Target vertex or goal predicate `(Func [u] -> Bool)` |
heuristic-fn | Func | required | Heuristic `(Func [u] -> Number)` or `(Func [u goal] -> Number)` |
Any(use /graph [a-star])
(let res (a-star {:a {:b 1} :b {:c 1}} :a :c (Func [u] 0)))
(assert-eq! res {:nodes [:a :b :c] :cost 2})ancestors-of#
Returns all transitive predecessor vertices that can reach `u` (excluding `u` itself).
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph to traverse |
u | Any | required | Target vertex |
Any(use /graph [ancestors-of])
(assert-eq! (ancestors-of {:a [:b] :b [:c]} :c) [:b :a])bellman-ford#
Computes single-source shortest paths allowing negative edge weights. Errors if a negative-weight cycle is reachable from `start`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph (supports negative edge weights) |
start | Any | required | Starting vertex |
goal | Any | required | Optional target vertex |
Any(use /graph [Directed-Graph bellman-ford])
(let g (Directed-Graph [[:a :b 4] [:a :c 2] [:b :c -3]]))
(assert-eq! (bellman-ford g :a :c) {:nodes [:a :b :c] :cost 1})betweenness-centrality-of#
Computes unweighted betweenness centrality for every vertex in `g` using Brandes' `O(V E)` algorithm.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph |
Any(use /graph [Undirected-Graph betweenness-centrality-of])
(let bc (betweenness-centrality-of (Undirected-Graph [[:a :b] [:b :c]])))
(assert-eq! bc:b 1.0)bfs#
Returns a lazy Breadth-First Search iterator over vertices reachable from `start`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Graph | required | Finite or infinite graph |
start | Any | required | Starting vertex |
Any(use /graph [bfs])
(assert-eq! (Array (bfs {:a [:b :c] :b [:d]} :a)) [:a :b :c :d])connected-components-of#
Returns the connected (or weakly connected) components of `g` as an Array of vertex Arrays.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph |
Any(use /graph [Undirected-Graph connected-components-of])
(let g (Undirected-Graph [[:a :b] [:c :d]]))
(assert-eq! (connected-components-of g) [[:a :b] [:c :d]])degree-of#
Returns the total degree of vertex `u` (`out-degree + in-degree` for directed graphs, or incident degree for undirected graphs).
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph to inspect |
u | Any | required | Vertex to inspect |
Any(use /graph [Directed-Graph degree-of])
(let g (Directed-Graph [[:a :b] [:b :c]]))
(assert-eq! (degree-of g :b) 2)descendants-of#
Returns all vertices reachable from `u` (excluding `u` itself) in BFS order.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Graph | required | Graph to traverse |
u | Any | required | Source vertex |
Any(use /graph [descendants-of])
(assert-eq! (descendants-of {:a [:b] :b [:c]} :a) [:b :c])dfs#
Returns a lazy preorder Depth-First Search iterator over vertices reachable from `start`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Graph | required | Finite or infinite graph |
start | Any | required | Starting vertex |
Any(use /graph [dfs])
(assert-eq! (Array (dfs {:a [:b :c] :b [:d]} :a)) [:a :b :d :c])dijkstra#
Computes shortest paths from `start` using Dijkstra's algorithm. With 2 arguments returns `{:distances {...} :predecessors {...}}`; with 3 arguments returns `{:nodes [...] :cost c}` or `()`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Graph | required | Graph with non-negative edge weights |
start | Any | required | Starting vertex |
goal | Any | required | Optional target vertex or goal predicate `(Func [u] -> Bool)` |
Any(use /graph [dijkstra])
(assert-eq! (dijkstra {:a {:b 2 :c 5} :b {:c 1}} :a :c) {:nodes [:a :b :c] :cost 3})edge-count-of#
Returns the number of edges in finite graph `g`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph to inspect |
Any(use /graph [Directed-Graph edge-count-of])
(let g (Directed-Graph [[:a :b] [:b :c]]))
(assert-eq! (edge-count-of g) 2)edge-weight-of#
Returns the weight or metadata Map of the edge `(u, v)`, or `()` if the edge does not exist.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Graph | required | Graph to query |
u | Any | required | Source vertex |
v | Any | required | Target vertex |
Any(use /graph [Directed-Graph edge-weight-of])
(let g (Directed-Graph [[:a :b 2.5]]))
(assert-eq! (edge-weight-of g :a :b) 2.5)edges-of#
Returns all edges in finite graph `g` as `{:from u :to v :weight w}` maps.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph to inspect |
Any(use /graph [Directed-Graph edges-of])
(let g (Directed-Graph [[:a :b 5]]))
(assert-eq! (edges-of g) [{:from :a :to :b :weight 5}])filtered-of#
Returns a lazy view of `g` retaining only vertices and edges that satisfy the given predicates.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Graph | required | Underlying graph |
node-pred | (Func [Any] Bool) | required | Vertex filter predicate or `{:nodes ... :edges ...}` map |
edge-pred | (Func [Any Any Any] Bool) | required | Optional edge filter predicate `(Func [u v w] -> Bool)` |
Any(use /graph [Directed-Graph filtered-of nodes-of])
(let g (Directed-Graph [[:a :b] [:b :c]]))
(let view (filtered-of g (Func [u] (!= u :c))))
(assert-eq! (nodes-of view) [:a :b])floyd-warshall#
Computes all-pairs shortest path distances in `O(V^3)` time, returning `{u {v cost ...} ...}`. Errors if `g` contains a negative-weight cycle.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph |
Any(use /graph [Directed-Graph floyd-warshall])
(let d (floyd-warshall (Directed-Graph [[:a :b 2] [:b :c 3]])))
(assert-eq! d:a:c 5)greedy-color#
Colors vertices of `g` using the Welsh-Powell largest-degree-first heuristic, returning `{u color-int ...}` with non-negative integer colors `0, 1, ...`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph |
Any(use /graph [Undirected-Graph greedy-color])
(let c (greedy-color (Undirected-Graph [[:a :b] [:b :c] [:c :a]])))
(assert (!= c:a c:b))has-cycle?#
Returns `true` if finite graph `g` contains at least one cycle.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph to inspect |
Any(use /graph [has-cycle?])
(assert (has-cycle? {:a [:b] :b [:a]}))has-edge?#
Returns `true` if there is a directed edge from `u` to `v` (or an undirected edge between `u` and `v`).
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Graph | required | Graph to query |
u | Any | required | Source vertex |
v | Any | required | Target vertex |
Any(use /graph [Directed-Graph has-edge?])
(let g (Directed-Graph [[:a :b]]))
(assert (has-edge? g :a :b))has-node?#
Returns `true` if vertex `u` belongs to finite graph `g`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph to inspect |
u | Any | required | Vertex to check |
Any(use /graph [Directed-Graph has-node?])
(let g (Directed-Graph [[:a :b]]))
(assert (has-node? g :a))in-degree-of#
Returns the number of incoming edges to vertex `u`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph to inspect |
u | Any | required | Target vertex |
Any(use /graph [Directed-Graph in-degree-of])
(let g (Directed-Graph [[:a :c] [:b :c]]))
(assert-eq! (in-degree-of g :c) 2)in-neighbors-of#
Returns all predecessor vertices `p` that have a directed edge `(p, u)` in `g`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph to inspect |
u | Any | required | Target vertex |
Any(use /graph [Directed-Graph in-neighbors-of])
(let g (Directed-Graph [[:a :c] [:b :c]]))
(assert-eq! (in-neighbors-of g :c) [:a :b])is-bipartite?#
Returns `true` if `g` is 2-colorable (bipartite).
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph |
Any(use /graph [Undirected-Graph is-bipartite?])
(assert (is-bipartite? (Undirected-Graph [[:a :b] [:b :c]])))is-dag?#
Returns `true` if `g` is a directed acyclic graph (directed and free of cycles).
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph to inspect |
Any(use /graph [is-dag?])
(assert (is-dag? {:a [:b] :b [:c]}))is-directed?#
Returns `true` if `g` is a directed graph, or `false` if `g` is undirected.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph to inspect |
Any(use /graph [Undirected-Graph is-directed?])
(assert (not (is-directed? (Undirected-Graph))))maximum-flow-of#
Computes maximum `source -> sink` flow using the Edmonds-Karp algorithm, returning `{:flow total-flow :flows {u {v flow ...} ...}}`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Capacity network with non-negative edge weights |
source | Any | required | Source vertex |
sink | Any | required | Sink vertex |
Any(use /graph [Directed-Graph maximum-flow-of])
(let g (Directed-Graph [[:s :a 10] [:a :t 7]]))
(assert-eq! (maximum-flow-of g :s :t):flow 7)minimum-spanning-tree-of#
Computes a minimum spanning tree (or forest) of `g` using Kruskal's algorithm, returning `[{:from u :to v :weight w} ...]` sorted by ascending weight.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite weighted graph |
Any(use /graph [Undirected-Graph minimum-spanning-tree-of])
(let g (Undirected-Graph [[:a :b 3] [:b :c 1] [:a :c 2]]))
(assert-eq! (count-of (minimum-spanning-tree-of g)) 2)neighbors-of#
Returns the outgoing neighbors of vertex `u` in graph `g`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Graph | required | Any graph (Directed-Graph, Undirected-Graph, Implicit-Graph, Matrix-Graph, Csr-Graph, Map, Func, or custom Graph) |
u | Any | required | Source vertex |
Any(use /graph [Directed-Graph neighbors-of])
(let g (Directed-Graph [[:a :b] [:a :c]]))
(assert-eq! (neighbors-of g :a) [:b :c])node-count-of#
Returns the total number of vertices in finite graph `g`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph to inspect |
Any(use /graph [Directed-Graph node-count-of])
(let g (Directed-Graph [[:a :b]]))
(assert-eq! (node-count-of g) 2)nodes-of#
Returns all vertices of finite graph `g` in insertion order.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph to inspect |
Any(use /graph [Directed-Graph nodes-of])
(let g (Directed-Graph [[:a :b]]))
(assert-eq! (nodes-of g) [:a :b])out-degree-of#
Returns the number of outgoing edges from vertex `u`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Graph | required | Graph to query |
u | Any | required | Vertex to inspect |
Any(use /graph [Directed-Graph out-degree-of])
(let g (Directed-Graph [[:a :b] [:a :c]]))
(assert-eq! (out-degree-of g :a) 2)pagerank#
Computes PageRank centrality scores for all vertices in `g` via power iteration with dangling-node redistribution.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph |
damping | Float | required | Optional damping factor in `[0.0, 1.0]` (default `0.85`) or options map `{:damping 0.85 :iterations 100}` |
iterations | Int | required | Optional maximum iterations (default `100`) |
Any(use /graph [pagerank])
(let ranks (pagerank {:a [:b] :b [:c] :c [:a]}))
(assert (> ranks:a 0.3))path-to#
Reconstructs `{:nodes [start ... goal] :cost c}` from a single-source shortest-path result map, or `()` if `goal` was unreachable.
| Parameter | Type | Optional | Description |
|---|---|---|---|
sssp-result | (Map Symbol Any) | required | `{:distances ... :predecessors ...}` map from 2-arg `dijkstra` or `bellman-ford` |
goal | Any | required | Target vertex to reconstruct path to |
Any(use /graph [dijkstra path-to])
(let sssp (dijkstra {:a {:b 2} :b {:c 3}} :a))
(assert-eq! (path-to sssp :c) {:nodes [:a :b :c] :cost 5})put-all-edges#
Inserts all edges from `edges` into mutable graph `g` and returns `g`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Mutable-Graph | required | Mutable graph to modify |
edges | (Array Any) | required | Edges as `[u v]`, `[u v w]`, or `{:from u :to v :weight w}` |
Any(use /graph [Directed-Graph put-all-edges])
(let g (Directed-Graph))
(put-all-edges g [[:a :b 1] [:b :c 2]])put-all-nodes#
Inserts all vertices from `nodes` into mutable graph `g` and returns `g`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Mutable-Graph | required | Mutable graph to modify |
nodes | (Array Any) | required | Vertices to insert |
Any(use /graph [Directed-Graph put-all-nodes])
(let g (Directed-Graph))
(put-all-nodes g [:a :b :c])put-edge#
Inserts or updates edge `(u, v)` with optional `weight` (default `1`) in mutable graph `g`, automatically inserting `u` and `v` if absent.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Mutable-Graph | required | Mutable graph to modify |
u | Any | required | Source vertex (auto-inserted if absent) |
v | Any | required | Target vertex (auto-inserted if absent) |
weight | Any | required | Optional numeric weight or metadata Map (defaults to 1) |
Any(use /graph [Directed-Graph put-edge])
(let g (Directed-Graph))
(put-edge g :a :b 2.5)put-node#
Inserts vertex `u` into mutable graph `g` if not already present and returns `g`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Mutable-Graph | required | Mutable graph to modify |
u | Any | required | Vertex to insert |
Any(use /graph [Directed-Graph put-node])
(let g (Directed-Graph))
(put-node g :a)remove-edge#
Removes edge `(u, v)` from mutable graph `g`. Returns `true` if the edge existed.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Mutable-Graph | required | Mutable graph to modify |
u | Any | required | Source vertex |
v | Any | required | Target vertex |
Any(use /graph [Directed-Graph remove-edge])
(let g (Directed-Graph [[:a :b]]))
(assert (remove-edge g :a :b))remove-node#
Removes vertex `u` and all its incident edges from mutable graph `g`. Returns `true` if `u` was present.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Mutable-Graph | required | Mutable graph to modify |
u | Any | required | Vertex to remove |
Any(use /graph [Directed-Graph remove-node])
(let g (Directed-Graph [[:a :b]]))
(assert (remove-node g :a))reversed-of#
Returns an `O(1)` zero-copy view of `g` with all directed edges flipped.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph to reverse |
Any(use /graph [Directed-Graph reversed-of neighbors-of])
(let g (Directed-Graph [[:a :b]]))
(assert-eq! (neighbors-of (reversed-of g) :b) [:a])shortest-path-of#
Finds a minimum-hop unweighted path from `start` to `goal` via BFS, returning `{:nodes [...] :cost k}` or `()` if unreachable.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Graph | required | Finite or infinite graph |
start | Any | required | Starting vertex |
goal | Any | required | Target vertex or goal predicate `(Func [u] -> Bool)` |
Any(use /graph [shortest-path-of])
(assert-eq! (shortest-path-of {:a [:b] :b [:c]} :a :c) {:nodes [:a :b :c] :cost 2})sinks-of#
Returns all vertices in `g` with out-degree `0`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph to inspect |
Any(use /graph [Directed-Graph sinks-of])
(let g (Directed-Graph [[:a :b] [:b :c]]))
(assert-eq! (sinks-of g) [:c])sources-of#
Returns all vertices in `g` with in-degree `0`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph to inspect |
Any(use /graph [Directed-Graph sources-of])
(let g (Directed-Graph [[:a :b] [:b :c]]))
(assert-eq! (sources-of g) [:a])strongly-connected-components-of#
Computes the strongly connected components of `g` using Tarjan's `O(V + E)` algorithm.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite directed graph |
Any(use /graph [strongly-connected-components-of])
(let sccs (strongly-connected-components-of {:a [:b] :b [:a :c] :c []}))
(assert-eq! (count-of sccs) 2)subgraph-of#
Materializes the induced subgraph of `g` containing only `nodes` and the edges between them.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Graph | required | Source graph |
nodes | (Array Any) | required | Subset of vertices to induce subgraph on |
Any(use /graph [Directed-Graph subgraph-of node-count-of])
(let g (Directed-Graph [[:a :b] [:b :c]]))
(let sub (subgraph-of g [:a :b]))
(assert-eq! (node-count-of sub) 2)to-dot#
Serializes finite graph `g` to a Graphviz DOT string (`digraph` for directed graphs, `graph` for undirected graphs).
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Finite graph to export |
graph-name | String | required | Optional Graphviz graph identifier |
Any(use /graph [Directed-Graph to-dot])
(let s (to-dot (Directed-Graph [[:a :b 2]]) "G"))
(assert (contains? s "digraph G"))topological-sort#
Returns a topological ordering of vertices in directed acyclic graph `g` using Kahn's algorithm. Errors if `g` is undirected or contains a cycle.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Finite-Graph | required | Directed acyclic graph |
Any(use /graph [topological-sort])
(assert-eq! (topological-sort {:a [:b] :b [:c]}) [:a :b :c])weighted-neighbors-of#
Returns an Array of `{:to v :weight w}` maps for all outgoing edges from vertex `u`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
g | Graph | required | Graph to query |
u | Any | required | Source vertex |
Any(use /graph [Directed-Graph weighted-neighbors-of])
(let g (Directed-Graph [[:a :b 3]]))
(assert-eq! (weighted-neighbors-of g :a) [{:to :b :weight 3}])