# Graph class · @imqueue/pg-sequelize

Source: https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.graph/
Published: 2026-08-01
Author: @imqueue maintainers (https://github.com/imqueue)
Package: @imqueue/pg-sequelize 4.2.0 — generated reference, not hand-written

A directed, unweighted graph with depth-first traversal and cycle detection.

**Signature:**

```typescript
export declare class Graph<T> 
```

## Remarks

Small on purpose. It exists so model associations can be walked as a graph — `BaseModel.toGraph()` builds one — and the question worth asking of that graph is whether it has a cycle, because a cycle is a chain of `include`s that can be asked to include itself.

Directed, despite what this said for a long time: an edge is recorded only on the vertex it starts from, so `addEdge(a, b)` does not make `hasEdge(b, a)` true. The cycle detection is the standard directed-graph one, with a recursion stack alongside the visited set, and it would be wrong for an undirected graph.

Vertices are used as `Map` keys, so identity is what distinguishes them — model classes work, structurally equal objects do not.

## Example


```typescript
const graph = Lead.toGraph();

if (graph.isCycled()) {
    // some association path leads back to where it started
}
```

## Methods


| Method | Modifiers | Description |
| --- | --- | --- |
| [addEdge(fromVertex, toVertex)](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.graph.addedge/) |  | Adds edges from one vertex to others. |
| [addVertex(vertex)](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.graph.addvertex/) |  | Adds vertices with no edges. |
| [delEdge(fromVertex, toVertex)](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.graph.deledge/) |  | Removes edges from one vertex to others. |
| [delVertex(vertex)](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.graph.delvertex/) |  | Removes vertices along with the edges leading out of them. |
| [forEach(callback)](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.graph.foreach/) |  | Visits every vertex once, depth first. |
| [hasEdge(vertex, edge)](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.graph.hasedge/) |  | Whether one vertex points at another. |
| [hasVertex(vertex)](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.graph.hasvertex/) |  | Whether a vertex is in this graph. |
| [isCycled()](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.graph.iscycled/) |  | Whether any path in this graph leads back to where it started. |
| [path(vertex)](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.graph.path/) |  | Every vertex reachable from one vertex, in the order a depth-first walk finds them. |
| [vertices()](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.graph.vertices/) |  | The vertices in this graph, in insertion order. |
| [walk(vertex, callback, visited)](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.graph.walk/) |  | Walks depth first from one vertex. |

