Graph class
A directed, unweighted graph with depth-first traversal and cycle detection.
Signature:
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 includes 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
const graph = Lead.toGraph();
if (graph.isCycled()) {
// some association path leads back to where it started
}
Methods
|
Method |
Modifiers |
Description |
|---|---|---|
|
Adds edges from one vertex to others. | ||
|
Adds vertices with no edges. | ||
|
Removes edges from one vertex to others. | ||
|
Removes vertices along with the edges leading out of them. | ||
|
Visits every vertex once, depth first. | ||
|
Whether one vertex points at another. | ||
|
Whether a vertex is in this graph. | ||
|
Whether any path in this graph leads back to where it started. | ||
|
Every vertex reachable from one vertex, in the order a depth-first walk finds them. | ||
|
The vertices in this graph, in insertion order. | ||
|
Walks depth first from one vertex. |