Skip to content

Data Model & Lifecycle

This guide explains how the Graph, Vertex, and Edge classes structure their data, which properties are derived, and when to recompute metadata via infer().

GraphProperties

When instantiating new Graph(props, comparator), you can pass the following optional fields:

PropertyTypeDefaultNotes
uuidstringcrypto.randomUUID()Acts as the canonical identifier when serializing.
titlestring'new graph'Convenience label for UIs.
verticesSet<IVertex<T>>new Set()Pre-populate the graph; remember to call infer() afterwards.
edgesSet<IEdge<T>>new Set()When seeding edges manually ensure the source/target vertices exist in the set.
directedbooleanfalseDerived by isDirected(); mirrors whether any edge is marked directed.
mixedbooleanfalseTrue when both directed and undirected edges are present.
connectedbooleanfalseUpdated by isConnected().
connectedComponentsCountnumber0Populated by connectedComponents().
strongConnectedComponentsCountnumber0Populated by strongConnectedComponents().
cycleCountnumber0Updated by inferCycles().
hasCyclesbooleanfalseUpdated by checkForCycles().
hasNegativeCyclesbooleanfalseUpdated by checkForNegativeCycles().

VertexProperties

Vertices encapsulate metadata attached to each node:

PropertyTypeDefault
uuidstringcrypto.randomUUID()
titlestring'new vertex'
pointPointundefined
incomingEdgesSet<IEdge<T>>new Set()
outgoingEdgesSet<IEdge<T>>new Set()
typestringundefined
dataTundefined

Helper methods such as deg(), indeg(), outdeg(), getNeighbours(), and getEdgeTo() reflect these sets and stay in sync as edges are added/removed.

EdgeProperties

Edges capture relationships between two vertices:

PropertyTypeDefault
uuidstringcrypto.randomUUID()
titlestring'new edge'
from / toIVertex<T>required
directedbooleantrue
weightnumber0
typestringundefined
dataRecord<string, any>undefined

Calling graph.createEdge(a, b, title, directed, weight) ensures edge instances automatically register with the involved vertices. If you instantiate new Edge({ from: a, to: b }) directly, remember to call graph.addEdge(edge) so graph-level events fire.

Lifecycle & infer()

  • The constructor invokes infer() once to initialize derived flags (directed, mixed, hasCycles, etc.).
  • After bulk mutations (batch edge inserts, manual vertex manipulation) call graph.infer() to recompute isDirected() and checkForCycles().
  • Methods like isConnected() and connectedComponents() recompute their respective caches on demand; store the return value if you need the result later.

When to call what

ScenarioRecommended call
Added/removed many vertices or edges manuallygraph.infer()
Need to know density classificationgraph.density() followed by isDense(threshold)
Need connectivity metadatagraph.connectedComponents(); the method updates connectedComponentsCount.
Detect negative cycles after weight changesgraph.checkForNegativeCycles()

Remember that undirected edges are stored internally as two directed edges (forward + reverse). Keep this in mind when interpreting size() or iterating over graph.edges.

Built with VitePress – Released under the MIT License.