Tanguv0.35.0
Collections55 exports
Raw Markdown

/graph

Directed and undirected graph data structures (Graph) with node/edge mutation and traversal helpers.

(use /graph)

Traits#

Finite-Graph#

trait

Extends: Graph

MethodSignatureDescription
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#

trait

Base 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.

MethodSignatureDescription
neighbors-of—
has-edge?—
out-degree-of—
edge-weight-of—
weighted-neighbors-of—

Mutable-Graph#

trait

Extends: Finite-Graph

MethodSignatureDescription
put-node—
put-edge—
remove-node—
remove-edge—
put-all-nodes—
put-all-edges—

Functions & symbols#

Csr-Graph#

constructor
(Csr-Graph) -> Csr-Graph

Constructs an immutable, cache-local Compressed Sparse Row (CSR) graph with precomputed CSR transpose for `O(1)` incoming neighbor slices.

ParameterTypeOptionalDescription
nodes(Array Any)requiredOptional vertex list
edges(Array Any)requiredOptional edge list
Returns:Any
(use /graph [Csr-Graph])
(let g (Csr-Graph [[:a :b 2] [:b :c 3]]))
(assert-eq! (g :a :b) 2)

Directed-Graph#

constructor
(Directed-Graph) -> Directed-Graph

Constructs a mutable directed graph backed by dual outgoing and incoming index-mapped adjacency tables.

ParameterTypeOptionalDescription
nodes(Array Any)requiredOptional initial vertices
edgesAnyrequiredOptional edge array `[[u v [w]] ...]`, adjacency `Map`, or `Finite-Graph`
Returns:Any
(use /graph [Directed-Graph])
(let g (Directed-Graph [[:a :b 2.5] [:b :c 1.0]]))
(assert-eq! g:a [:b])

Implicit-Graph#

constructor
(Implicit-Graph neighbor-fn) -> Implicit-Graph

Constructs a procedural or infinite graph computed on demand from `neighbor-fn` and optional `weight-fn`.

ParameterTypeOptionalDescription
neighbor-fn(Func [Any] Any)requiredFunction `(Func [u] ...)` returning neighbors of `u`
weight-fn(Func [Any Any] Number)requiredOptional function `(Func [u v] ...)` computing edge weight
Returns: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#

constructor
(Matrix-Graph) -> Matrix-Graph

Constructs a dense `N x N` adjacency-matrix graph.

ParameterTypeOptionalDescription
nodes(Array Any)requiredOptional initial vertices
edges(Array Any)requiredOptional edge list
Returns: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#

constructor
(Undirected-Graph) -> Undirected-Graph

Constructs a mutable undirected graph with symmetric edge storage.

ParameterTypeOptionalDescription
nodes(Array Any)requiredOptional initial vertices
edgesAnyrequiredOptional edge array `[[u v [w]] ...]`, adjacency `Map`, or `Finite-Graph`
Returns:Any
(use /graph [Undirected-Graph has-edge?])
(let g (Undirected-Graph [[:a :b 3]]))
(assert (has-edge? g :b :a))

a-star#

function
(a-star g start goal heuristic-fn) -> (Option (Map Symbol Any))

Finds the minimum-cost path from `start` to `goal` using A* search, returning `{:nodes [...] :cost c}` or `()` if unreachable.

ParameterTypeOptionalDescription
gGraphrequiredFinite or infinite graph with non-negative edge weights
startAnyrequiredStarting vertex
goalAnyrequiredTarget vertex or goal predicate `(Func [u] -> Bool)`
heuristic-fnFuncrequiredHeuristic `(Func [u] -> Number)` or `(Func [u goal] -> Number)`
Returns: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#

function
(ancestors-of g u) -> (Array Any)

Returns all transitive predecessor vertices that can reach `u` (excluding `u` itself).

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph to traverse
uAnyrequiredTarget vertex
Returns:Any
(use /graph [ancestors-of])
(assert-eq! (ancestors-of {:a [:b] :b [:c]} :c) [:b :a])

bellman-ford#

function
(bellman-ford g start) -> (Map Symbol Any)

Computes single-source shortest paths allowing negative edge weights. Errors if a negative-weight cycle is reachable from `start`.

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph (supports negative edge weights)
startAnyrequiredStarting vertex
goalAnyrequiredOptional target vertex
Returns: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#

function
(betweenness-centrality-of g) -> (Map Any Float)

Computes unweighted betweenness centrality for every vertex in `g` using Brandes' `O(V E)` algorithm.

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph
Returns: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#

function
(bfs g start) -> Bfs-Iterator

Returns a lazy Breadth-First Search iterator over vertices reachable from `start`.

ParameterTypeOptionalDescription
gGraphrequiredFinite or infinite graph
startAnyrequiredStarting vertex
Returns:Any
(use /graph [bfs])
(assert-eq! (Array (bfs {:a [:b :c] :b [:d]} :a)) [:a :b :c :d])

connected-components-of#

function
(connected-components-of g) -> (Array (Array Any))

Returns the connected (or weakly connected) components of `g` as an Array of vertex Arrays.

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph
Returns: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#

function
(degree-of g u) -> Int

Returns the total degree of vertex `u` (`out-degree + in-degree` for directed graphs, or incident degree for undirected graphs).

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph to inspect
uAnyrequiredVertex to inspect
Returns:Any
(use /graph [Directed-Graph degree-of])
(let g (Directed-Graph [[:a :b] [:b :c]]))
(assert-eq! (degree-of g :b) 2)

descendants-of#

function
(descendants-of g u) -> (Array Any)

Returns all vertices reachable from `u` (excluding `u` itself) in BFS order.

ParameterTypeOptionalDescription
gGraphrequiredGraph to traverse
uAnyrequiredSource vertex
Returns:Any
(use /graph [descendants-of])
(assert-eq! (descendants-of {:a [:b] :b [:c]} :a) [:b :c])

dfs#

function
(dfs g start) -> Dfs-Iterator

Returns a lazy preorder Depth-First Search iterator over vertices reachable from `start`.

ParameterTypeOptionalDescription
gGraphrequiredFinite or infinite graph
startAnyrequiredStarting vertex
Returns:Any
(use /graph [dfs])
(assert-eq! (Array (dfs {:a [:b :c] :b [:d]} :a)) [:a :b :d :c])

dijkstra#

function
(dijkstra g start) -> (Map Symbol Any)

Computes shortest paths from `start` using Dijkstra's algorithm. With 2 arguments returns `{:distances {...} :predecessors {...}}`; with 3 arguments returns `{:nodes [...] :cost c}` or `()`.

ParameterTypeOptionalDescription
gGraphrequiredGraph with non-negative edge weights
startAnyrequiredStarting vertex
goalAnyrequiredOptional target vertex or goal predicate `(Func [u] -> Bool)`
Returns: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#

function
(edge-count-of g) -> Int

Returns the number of edges in finite graph `g`.

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph to inspect
Returns: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#

function
(edge-weight-of g u v) -> Any

Returns the weight or metadata Map of the edge `(u, v)`, or `()` if the edge does not exist.

ParameterTypeOptionalDescription
gGraphrequiredGraph to query
uAnyrequiredSource vertex
vAnyrequiredTarget vertex
Returns: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#

function
(edges-of g) -> (Array (Map Symbol Any))

Returns all edges in finite graph `g` as `{:from u :to v :weight w}` maps.

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph to inspect
Returns: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#

function
(filtered-of g node-pred) -> Filtered-Graph

Returns a lazy view of `g` retaining only vertices and edges that satisfy the given predicates.

ParameterTypeOptionalDescription
gGraphrequiredUnderlying graph
node-pred(Func [Any] Bool)requiredVertex filter predicate or `{:nodes ... :edges ...}` map
edge-pred(Func [Any Any Any] Bool)requiredOptional edge filter predicate `(Func [u v w] -> Bool)`
Returns: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#

function
(floyd-warshall g) -> (Map Any (Map Any Number))

Computes all-pairs shortest path distances in `O(V^3)` time, returning `{u {v cost ...} ...}`. Errors if `g` contains a negative-weight cycle.

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph
Returns: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#

function
(greedy-color g) -> (Map Any Int)

Colors vertices of `g` using the Welsh-Powell largest-degree-first heuristic, returning `{u color-int ...}` with non-negative integer colors `0, 1, ...`.

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph
Returns: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?#

function
(has-cycle? g) -> Bool

Returns `true` if finite graph `g` contains at least one cycle.

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph to inspect
Returns:Any
(use /graph [has-cycle?])
(assert (has-cycle? {:a [:b] :b [:a]}))

has-edge?#

function
(has-edge? g u v) -> Bool

Returns `true` if there is a directed edge from `u` to `v` (or an undirected edge between `u` and `v`).

ParameterTypeOptionalDescription
gGraphrequiredGraph to query
uAnyrequiredSource vertex
vAnyrequiredTarget vertex
Returns:Any
(use /graph [Directed-Graph has-edge?])
(let g (Directed-Graph [[:a :b]]))
(assert (has-edge? g :a :b))

has-node?#

function
(has-node? g u) -> Bool

Returns `true` if vertex `u` belongs to finite graph `g`.

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph to inspect
uAnyrequiredVertex to check
Returns:Any
(use /graph [Directed-Graph has-node?])
(let g (Directed-Graph [[:a :b]]))
(assert (has-node? g :a))

in-degree-of#

function
(in-degree-of g u) -> Int

Returns the number of incoming edges to vertex `u`.

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph to inspect
uAnyrequiredTarget vertex
Returns: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#

function
(in-neighbors-of g u) -> (Array Any)

Returns all predecessor vertices `p` that have a directed edge `(p, u)` in `g`.

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph to inspect
uAnyrequiredTarget vertex
Returns: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?#

function
(is-bipartite? g) -> Bool

Returns `true` if `g` is 2-colorable (bipartite).

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph
Returns:Any
(use /graph [Undirected-Graph is-bipartite?])
(assert (is-bipartite? (Undirected-Graph [[:a :b] [:b :c]])))

is-dag?#

function
(is-dag? g) -> Bool

Returns `true` if `g` is a directed acyclic graph (directed and free of cycles).

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph to inspect
Returns:Any
(use /graph [is-dag?])
(assert (is-dag? {:a [:b] :b [:c]}))

is-directed?#

function
(is-directed? g) -> Bool

Returns `true` if `g` is a directed graph, or `false` if `g` is undirected.

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph to inspect
Returns:Any
(use /graph [Undirected-Graph is-directed?])
(assert (not (is-directed? (Undirected-Graph))))

maximum-flow-of#

function
(maximum-flow-of g source sink) -> (Map Symbol Any)

Computes maximum `source -> sink` flow using the Edmonds-Karp algorithm, returning `{:flow total-flow :flows {u {v flow ...} ...}}`.

ParameterTypeOptionalDescription
gFinite-GraphrequiredCapacity network with non-negative edge weights
sourceAnyrequiredSource vertex
sinkAnyrequiredSink vertex
Returns: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#

function
(minimum-spanning-tree-of g) -> (Array (Map Symbol Any))

Computes a minimum spanning tree (or forest) of `g` using Kruskal's algorithm, returning `[{:from u :to v :weight w} ...]` sorted by ascending weight.

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite weighted graph
Returns: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#

function
(neighbors-of g u) -> (Array Any)

Returns the outgoing neighbors of vertex `u` in graph `g`.

ParameterTypeOptionalDescription
gGraphrequiredAny graph (Directed-Graph, Undirected-Graph, Implicit-Graph, Matrix-Graph, Csr-Graph, Map, Func, or custom Graph)
uAnyrequiredSource vertex
Returns: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#

function
(node-count-of g) -> Int

Returns the total number of vertices in finite graph `g`.

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph to inspect
Returns:Any
(use /graph [Directed-Graph node-count-of])
(let g (Directed-Graph [[:a :b]]))
(assert-eq! (node-count-of g) 2)

nodes-of#

function
(nodes-of g) -> (Array Any)

Returns all vertices of finite graph `g` in insertion order.

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph to inspect
Returns:Any
(use /graph [Directed-Graph nodes-of])
(let g (Directed-Graph [[:a :b]]))
(assert-eq! (nodes-of g) [:a :b])

out-degree-of#

function
(out-degree-of g u) -> Int

Returns the number of outgoing edges from vertex `u`.

ParameterTypeOptionalDescription
gGraphrequiredGraph to query
uAnyrequiredVertex to inspect
Returns: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#

function
(pagerank g) -> (Map Any Float)

Computes PageRank centrality scores for all vertices in `g` via power iteration with dangling-node redistribution.

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph
dampingFloatrequiredOptional damping factor in `[0.0, 1.0]` (default `0.85`) or options map `{:damping 0.85 :iterations 100}`
iterationsIntrequiredOptional maximum iterations (default `100`)
Returns:Any
(use /graph [pagerank])
(let ranks (pagerank {:a [:b] :b [:c] :c [:a]}))
(assert (> ranks:a 0.3))

path-to#

function
(path-to sssp-result goal) -> (Option (Map Symbol Any))

Reconstructs `{:nodes [start ... goal] :cost c}` from a single-source shortest-path result map, or `()` if `goal` was unreachable.

ParameterTypeOptionalDescription
sssp-result(Map Symbol Any)required`{:distances ... :predecessors ...}` map from 2-arg `dijkstra` or `bellman-ford`
goalAnyrequiredTarget vertex to reconstruct path to
Returns: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#

function
(put-all-edges g edges) -> Mutable-Graph

Inserts all edges from `edges` into mutable graph `g` and returns `g`.

ParameterTypeOptionalDescription
gMutable-GraphrequiredMutable graph to modify
edges(Array Any)requiredEdges as `[u v]`, `[u v w]`, or `{:from u :to v :weight w}`
Returns: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#

function
(put-all-nodes g nodes) -> Mutable-Graph

Inserts all vertices from `nodes` into mutable graph `g` and returns `g`.

ParameterTypeOptionalDescription
gMutable-GraphrequiredMutable graph to modify
nodes(Array Any)requiredVertices to insert
Returns:Any
(use /graph [Directed-Graph put-all-nodes])
(let g (Directed-Graph))
(put-all-nodes g [:a :b :c])

put-edge#

function
(put-edge g u v) -> Mutable-Graph

Inserts or updates edge `(u, v)` with optional `weight` (default `1`) in mutable graph `g`, automatically inserting `u` and `v` if absent.

ParameterTypeOptionalDescription
gMutable-GraphrequiredMutable graph to modify
uAnyrequiredSource vertex (auto-inserted if absent)
vAnyrequiredTarget vertex (auto-inserted if absent)
weightAnyrequiredOptional numeric weight or metadata Map (defaults to 1)
Returns:Any
(use /graph [Directed-Graph put-edge])
(let g (Directed-Graph))
(put-edge g :a :b 2.5)

put-node#

function
(put-node g u) -> Mutable-Graph

Inserts vertex `u` into mutable graph `g` if not already present and returns `g`.

ParameterTypeOptionalDescription
gMutable-GraphrequiredMutable graph to modify
uAnyrequiredVertex to insert
Returns:Any
(use /graph [Directed-Graph put-node])
(let g (Directed-Graph))
(put-node g :a)

remove-edge#

function
(remove-edge g u v) -> Bool

Removes edge `(u, v)` from mutable graph `g`. Returns `true` if the edge existed.

ParameterTypeOptionalDescription
gMutable-GraphrequiredMutable graph to modify
uAnyrequiredSource vertex
vAnyrequiredTarget vertex
Returns:Any
(use /graph [Directed-Graph remove-edge])
(let g (Directed-Graph [[:a :b]]))
(assert (remove-edge g :a :b))

remove-node#

function
(remove-node g u) -> Bool

Removes vertex `u` and all its incident edges from mutable graph `g`. Returns `true` if `u` was present.

ParameterTypeOptionalDescription
gMutable-GraphrequiredMutable graph to modify
uAnyrequiredVertex to remove
Returns:Any
(use /graph [Directed-Graph remove-node])
(let g (Directed-Graph [[:a :b]]))
(assert (remove-node g :a))

reversed-of#

function
(reversed-of g) -> Reversed-Graph

Returns an `O(1)` zero-copy view of `g` with all directed edges flipped.

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph to reverse
Returns: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#

function
(shortest-path-of g start goal) -> (Option (Map Symbol Any))

Finds a minimum-hop unweighted path from `start` to `goal` via BFS, returning `{:nodes [...] :cost k}` or `()` if unreachable.

ParameterTypeOptionalDescription
gGraphrequiredFinite or infinite graph
startAnyrequiredStarting vertex
goalAnyrequiredTarget vertex or goal predicate `(Func [u] -> Bool)`
Returns: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#

function
(sinks-of g) -> (Array Any)

Returns all vertices in `g` with out-degree `0`.

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph to inspect
Returns:Any
(use /graph [Directed-Graph sinks-of])
(let g (Directed-Graph [[:a :b] [:b :c]]))
(assert-eq! (sinks-of g) [:c])

sources-of#

function
(sources-of g) -> (Array Any)

Returns all vertices in `g` with in-degree `0`.

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph to inspect
Returns: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#

function
(strongly-connected-components-of g) -> (Array (Array Any))

Computes the strongly connected components of `g` using Tarjan's `O(V + E)` algorithm.

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite directed graph
Returns: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#

function
(subgraph-of g nodes) -> Finite-Graph

Materializes the induced subgraph of `g` containing only `nodes` and the edges between them.

ParameterTypeOptionalDescription
gGraphrequiredSource graph
nodes(Array Any)requiredSubset of vertices to induce subgraph on
Returns: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#

function
(to-dot g) -> String

Serializes finite graph `g` to a Graphviz DOT string (`digraph` for directed graphs, `graph` for undirected graphs).

ParameterTypeOptionalDescription
gFinite-GraphrequiredFinite graph to export
graph-nameStringrequiredOptional Graphviz graph identifier
Returns:Any
(use /graph [Directed-Graph to-dot])
(let s (to-dot (Directed-Graph [[:a :b 2]]) "G"))
(assert (contains? s "digraph G"))

topological-sort#

function
(topological-sort g) -> (Array Any)

Returns a topological ordering of vertices in directed acyclic graph `g` using Kahn's algorithm. Errors if `g` is undirected or contains a cycle.

ParameterTypeOptionalDescription
gFinite-GraphrequiredDirected acyclic graph
Returns:Any
(use /graph [topological-sort])
(assert-eq! (topological-sort {:a [:b] :b [:c]}) [:a :b :c])

weighted-neighbors-of#

function
(weighted-neighbors-of g u) -> (Array (Map Symbol Any))

Returns an Array of `{:to v :weight w}` maps for all outgoing edges from vertex `u`.

ParameterTypeOptionalDescription
gGraphrequiredGraph to query
uAnyrequiredSource vertex
Returns: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}])