# Module /graph

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

```tangu
(use /graph)
```

## Traits

### Finite-Graph

`Trait` in `/graph`

Extends: Graph

- `` — 
- `` — 
- `` — 
- `` — 
- `` — 
- `` — 
- `` — 
- `` — 
- `` — 
- `` — 
- `` — 

### Graph

`Trait` in `/graph`

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.

- `` — 
- `` — 
- `` — 
- `` — 
- `` — 

### Mutable-Graph

`Trait` in `/graph`

Extends: Finite-Graph

- `` — 
- `` — 
- `` — 
- `` — 
- `` — 
- `` — 

## Functions & symbols

### Csr-Graph

`(Csr-Graph) -> 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)` | no | Optional vertex list |
| `edges` | `(Array Any)` | no | Optional edge list |

```tangu
(use /graph [Csr-Graph])
(let g (Csr-Graph [[:a :b 2] [:b :c 3]]))
(assert-eq! (g :a :b) 2)
```

### Directed-Graph

`(Directed-Graph) -> Directed-Graph`

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `nodes` | `(Array Any)` | no | Optional initial vertices |
| `edges` | `Any` | no | Optional edge array `[[u v [w]] ...]`, adjacency `Map`, or `Finite-Graph` |

```tangu
(use /graph [Directed-Graph])
(let g (Directed-Graph [[:a :b 2.5] [:b :c 1.0]]))
(assert-eq! g:a [:b])
```

### Implicit-Graph

`(Implicit-Graph neighbor-fn) -> 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)` | no | Function `(Func [u] ...)` returning neighbors of `u` |
| `weight-fn` | `(Func [Any Any] Number)` | no | Optional function `(Func [u v] ...)` computing edge weight |

```tangu
(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

`(Matrix-Graph) -> Matrix-Graph`

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `nodes` | `(Array Any)` | no | Optional initial vertices |
| `edges` | `(Array Any)` | no | Optional edge list |

```tangu
(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

`(Undirected-Graph) -> Undirected-Graph`

Constructs a mutable undirected graph with symmetric edge storage.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `nodes` | `(Array Any)` | no | Optional initial vertices |
| `edges` | `Any` | no | Optional edge array `[[u v [w]] ...]`, adjacency `Map`, or `Finite-Graph` |

```tangu
(use /graph [Undirected-Graph has-edge?])
(let g (Undirected-Graph [[:a :b 3]]))
(assert (has-edge? g :b :a))
```

### a-star

`(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.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Graph` | no | Finite or infinite graph with non-negative edge weights |
| `start` | `Any` | no | Starting vertex |
| `goal` | `Any` | no | Target vertex or goal predicate `(Func [u] -> Bool)` |
| `heuristic-fn` | `Func` | no | Heuristic `(Func [u] -> Number)` or `(Func [u goal] -> Number)` |

```tangu
(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

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph to traverse |
| `u` | `Any` | no | Target vertex |

```tangu
(use /graph [ancestors-of])
(assert-eq! (ancestors-of {:a [:b] :b [:c]} :c) [:b :a])
```

### bellman-ford

`(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`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph (supports negative edge weights) |
| `start` | `Any` | no | Starting vertex |
| `goal` | `Any` | no | Optional target vertex |

```tangu
(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

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph |

```tangu
(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

`(bfs g start) -> Bfs-Iterator`

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Graph` | no | Finite or infinite graph |
| `start` | `Any` | no | Starting vertex |

```tangu
(use /graph [bfs])
(assert-eq! (Array (bfs {:a [:b :c] :b [:d]} :a)) [:a :b :c :d])
```

### connected-components-of

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph |

```tangu
(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

`(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).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph to inspect |
| `u` | `Any` | no | Vertex to inspect |

```tangu
(use /graph [Directed-Graph degree-of])
(let g (Directed-Graph [[:a :b] [:b :c]]))
(assert-eq! (degree-of g :b) 2)
```

### descendants-of

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Graph` | no | Graph to traverse |
| `u` | `Any` | no | Source vertex |

```tangu
(use /graph [descendants-of])
(assert-eq! (descendants-of {:a [:b] :b [:c]} :a) [:b :c])
```

### dfs

`(dfs g start) -> Dfs-Iterator`

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Graph` | no | Finite or infinite graph |
| `start` | `Any` | no | Starting vertex |

```tangu
(use /graph [dfs])
(assert-eq! (Array (dfs {:a [:b :c] :b [:d]} :a)) [:a :b :d :c])
```

### dijkstra

`(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 `()`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Graph` | no | Graph with non-negative edge weights |
| `start` | `Any` | no | Starting vertex |
| `goal` | `Any` | no | Optional target vertex or goal predicate `(Func [u] -> Bool)` |

```tangu
(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

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph to inspect |

```tangu
(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

`(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.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Graph` | no | Graph to query |
| `u` | `Any` | no | Source vertex |
| `v` | `Any` | no | Target vertex |

```tangu
(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

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph to inspect |

```tangu
(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

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Graph` | no | Underlying graph |
| `node-pred` | `(Func [Any] Bool)` | no | Vertex filter predicate or `{:nodes ... :edges ...}` map |
| `edge-pred` | `(Func [Any Any Any] Bool)` | no | Optional edge filter predicate `(Func [u v w] -> Bool)` |

```tangu
(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

`(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.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph |

```tangu
(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

`(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, ...`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph |

```tangu
(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?

`(has-cycle? g) -> Bool`

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph to inspect |

```tangu
(use /graph [has-cycle?])
(assert (has-cycle? {:a [:b] :b [:a]}))
```

### has-edge?

`(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`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Graph` | no | Graph to query |
| `u` | `Any` | no | Source vertex |
| `v` | `Any` | no | Target vertex |

```tangu
(use /graph [Directed-Graph has-edge?])
(let g (Directed-Graph [[:a :b]]))
(assert (has-edge? g :a :b))
```

### has-node?

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph to inspect |
| `u` | `Any` | no | Vertex to check |

```tangu
(use /graph [Directed-Graph has-node?])
(let g (Directed-Graph [[:a :b]]))
(assert (has-node? g :a))
```

### in-degree-of

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph to inspect |
| `u` | `Any` | no | Target vertex |

```tangu
(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

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph to inspect |
| `u` | `Any` | no | Target vertex |

```tangu
(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?

`(is-bipartite? g) -> Bool`

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph |

```tangu
(use /graph [Undirected-Graph is-bipartite?])
(assert (is-bipartite? (Undirected-Graph [[:a :b] [:b :c]])))
```

### is-dag?

`(is-dag? g) -> Bool`

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph to inspect |

```tangu
(use /graph [is-dag?])
(assert (is-dag? {:a [:b] :b [:c]}))
```

### is-directed?

`(is-directed? g) -> Bool`

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph to inspect |

```tangu
(use /graph [Undirected-Graph is-directed?])
(assert (not (is-directed? (Undirected-Graph))))
```

### maximum-flow-of

`(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 ...} ...}}`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Capacity network with non-negative edge weights |
| `source` | `Any` | no | Source vertex |
| `sink` | `Any` | no | Sink vertex |

```tangu
(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

`(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.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite weighted graph |

```tangu
(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

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Graph` | no | Any graph (Directed-Graph, Undirected-Graph, Implicit-Graph, Matrix-Graph, Csr-Graph, Map, Func, or custom Graph) |
| `u` | `Any` | no | Source vertex |

```tangu
(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

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph to inspect |

```tangu
(use /graph [Directed-Graph node-count-of])
(let g (Directed-Graph [[:a :b]]))
(assert-eq! (node-count-of g) 2)
```

### nodes-of

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph to inspect |

```tangu
(use /graph [Directed-Graph nodes-of])
(let g (Directed-Graph [[:a :b]]))
(assert-eq! (nodes-of g) [:a :b])
```

### out-degree-of

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Graph` | no | Graph to query |
| `u` | `Any` | no | Vertex to inspect |

```tangu
(use /graph [Directed-Graph out-degree-of])
(let g (Directed-Graph [[:a :b] [:a :c]]))
(assert-eq! (out-degree-of g :a) 2)
```

### pagerank

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph |
| `damping` | `Float` | no | Optional damping factor in `[0.0, 1.0]` (default `0.85`) or options map `{:damping 0.85 :iterations 100}` |
| `iterations` | `Int` | no | Optional maximum iterations (default `100`) |

```tangu
(use /graph [pagerank])
(let ranks (pagerank {:a [:b] :b [:c] :c [:a]}))
(assert (> ranks:a 0.3))
```

### path-to

`(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.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `sssp-result` | `(Map Symbol Any)` | no | `{:distances ... :predecessors ...}` map from 2-arg `dijkstra` or `bellman-ford` |
| `goal` | `Any` | no | Target vertex to reconstruct path to |

```tangu
(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

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Mutable-Graph` | no | Mutable graph to modify |
| `edges` | `(Array Any)` | no | Edges as `[u v]`, `[u v w]`, or `{:from u :to v :weight w}` |

```tangu
(use /graph [Directed-Graph put-all-edges])
(let g (Directed-Graph))
(put-all-edges g [[:a :b 1] [:b :c 2]])
```

### put-all-nodes

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Mutable-Graph` | no | Mutable graph to modify |
| `nodes` | `(Array Any)` | no | Vertices to insert |

```tangu
(use /graph [Directed-Graph put-all-nodes])
(let g (Directed-Graph))
(put-all-nodes g [:a :b :c])
```

### put-edge

`(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.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Mutable-Graph` | no | Mutable graph to modify |
| `u` | `Any` | no | Source vertex (auto-inserted if absent) |
| `v` | `Any` | no | Target vertex (auto-inserted if absent) |
| `weight` | `Any` | no | Optional numeric weight or metadata Map (defaults to 1) |

```tangu
(use /graph [Directed-Graph put-edge])
(let g (Directed-Graph))
(put-edge g :a :b 2.5)
```

### put-node

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Mutable-Graph` | no | Mutable graph to modify |
| `u` | `Any` | no | Vertex to insert |

```tangu
(use /graph [Directed-Graph put-node])
(let g (Directed-Graph))
(put-node g :a)
```

### remove-edge

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Mutable-Graph` | no | Mutable graph to modify |
| `u` | `Any` | no | Source vertex |
| `v` | `Any` | no | Target vertex |

```tangu
(use /graph [Directed-Graph remove-edge])
(let g (Directed-Graph [[:a :b]]))
(assert (remove-edge g :a :b))
```

### remove-node

`(remove-node g u) -> Bool`

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` | no | Mutable graph to modify |
| `u` | `Any` | no | Vertex to remove |

```tangu
(use /graph [Directed-Graph remove-node])
(let g (Directed-Graph [[:a :b]]))
(assert (remove-node g :a))
```

### reversed-of

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph to reverse |

```tangu
(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

`(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.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Graph` | no | Finite or infinite graph |
| `start` | `Any` | no | Starting vertex |
| `goal` | `Any` | no | Target vertex or goal predicate `(Func [u] -> Bool)` |

```tangu
(use /graph [shortest-path-of])
(assert-eq! (shortest-path-of {:a [:b] :b [:c]} :a :c) {:nodes [:a :b :c] :cost 2})
```

### sinks-of

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph to inspect |

```tangu
(use /graph [Directed-Graph sinks-of])
(let g (Directed-Graph [[:a :b] [:b :c]]))
(assert-eq! (sinks-of g) [:c])
```

### sources-of

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph to inspect |

```tangu
(use /graph [Directed-Graph sources-of])
(let g (Directed-Graph [[:a :b] [:b :c]]))
(assert-eq! (sources-of g) [:a])
```

### strongly-connected-components-of

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite directed graph |

```tangu
(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

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

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Graph` | no | Source graph |
| `nodes` | `(Array Any)` | no | Subset of vertices to induce subgraph on |

```tangu
(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

`(to-dot g) -> String`

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

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Finite graph to export |
| `graph-name` | `String` | no | Optional Graphviz graph identifier |

```tangu
(use /graph [Directed-Graph to-dot])
(let s (to-dot (Directed-Graph [[:a :b 2]]) "G"))
(assert (contains? s "digraph G"))
```

### topological-sort

`(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.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Finite-Graph` | no | Directed acyclic graph |

```tangu
(use /graph [topological-sort])
(assert-eq! (topological-sort {:a [:b] :b [:c]}) [:a :b :c])
```

### weighted-neighbors-of

`(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`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `g` | `Graph` | no | Graph to query |
| `u` | `Any` | no | Source vertex |

```tangu
(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}])
```
