Class: Graph<T>
Defined in: graph.ts:118 Weighted Graph with Adjacency List representation Supports both directed and undirected graphs with weighted edges. Implements common graph algorithms including BFS, DFS, Dijkstra’s shortest path, A* search, and topological sort. Time Complexity:- addVertex: O(1)
- addEdge: O(1)
- removeVertex: O(V + E)
- removeEdge: O(E)
- BFS/DFS: O(V + E)
- Dijkstra/A*: O((V + E) log V) with priority queue
Example
Type Parameters
T
T
Type of vertex identifiers
Constructors
Constructor
new Graph<Defined in: graph.ts:127 Creates a new GraphT>(options?):Graph<T>
Parameters
options?
GraphOptions = {}
Configuration options
Returns
Graph<T>
Throws
Error if options validation failsAccessors
edgeCount
Get Signature
get edgeCount(): number
Defined in: graph.ts:147
Returns the total number of edges in the graph
Returns
number
vertexCount
Get Signature
get vertexCount(): number
Defined in: graph.ts:140
Returns the number of vertices in the graph
Returns
number
Methods
addEdge()
addEdge(Defined in: graph.ts:197 Adds an edge between two verticessource,target,weight?,metadata?):this
Parameters
source
T
target
T
weight?
number = 1
metadata?
Record<string, unknown>
Returns
this
This graph for chaining
addVertex()
addVertex(Defined in: graph.ts:175 Adds a vertex to the graphvertex,metadata?):this
Parameters
vertex
T
metadata?
Record<string, unknown>
Returns
this
This graph for chaining
addVertices()
addVertices(Defined in: graph.ts:186 Adds multiple vertices at oncevertices):this
Parameters
vertices
T[]
Returns
this
This graph for chaining
astar()
astar(Defined in: graph.ts:434 Finds the shortest path using A* search with a heuristic functionstart,end,h): {cost:number;path:T[]; } |null
Parameters
start
T
Starting vertex
end
T
Ending vertex
h
(a, b) => number
Heuristic function estimating cost from a vertex to end
Returns
{cost: number; path: T[]; } | null
Path and cost, or null if no path exists
bfs()
bfs(Defined in: graph.ts:288 Breadth-First Search traversalstart):TraversalResult<T>
Parameters
start
T
Starting vertex
Returns
TraversalResult<T>
Traversal result with vertices, parents, and distances
clear()
clear(): void
Defined in: graph.ts:625
Clears all vertices and edges
Returns
void
dfs()
dfs(Defined in: graph.ts:329 Depth-First Search traversalstart):TraversalResult<T>
Parameters
start
T
Starting vertex
Returns
TraversalResult<T>
Traversal result with vertices and parents
findAllPaths()
findAllPaths(Defined in: graph.ts:478 Finds all paths between two vertices (limited by max depth)start,end,maxDepth?):T[][]
Parameters
start
T
Starting vertex
end
T
Ending vertex
maxDepth?
number = 10
Maximum path depth (default: 10)
Returns
T[][]
Array of all paths found
findShortestPath()
findShortestPath(Defined in: graph.ts:365 Finds the shortest path between two vertices using Dijkstra’s algorithmstart,end):PathResult<T>
Parameters
start
T
Starting vertex
end
T
Ending vertex
Returns
PathResult<T>
Path result with vertices and total distance
getConnectedComponents()
getConnectedComponents(): T[][]
Defined in: graph.ts:594
Gets connected components in an undirected graph
Returns
T[][]
Array of connected components (each component is an array of vertices)
getNeighbors()
getNeighbors(Defined in: graph.ts:264 Gets all neighbors of a vertexvertex):Edge<T>[]
Parameters
vertex
T
Returns
Edge<T>[]
getVertexMetadata()
getVertexMetadata(Defined in: graph.ts:278 Gets vertex metadatavertex):Record<string,unknown> |undefined
Parameters
vertex
T
Returns
Record<string, unknown> | undefined
getVertices()
getVertices(): T[]
Defined in: graph.ts:271
Gets all vertices in the graph
Returns
T[]
hasCycle()
hasCycle(): boolean
Defined in: graph.ts:560
Detects if the graph contains a cycle
Returns
boolean
hasEdge()
hasEdge(Defined in: graph.ts:165 Checks if an edge exists between two verticessource,target):boolean
Parameters
source
T
target
T
Returns
boolean
hasVertex()
hasVertex(Defined in: graph.ts:158 Checks if the graph has a vertexvertex):boolean
Parameters
vertex
T
Returns
boolean
removeEdge()
removeEdge(Defined in: graph.ts:244 Removes an edge between two verticessource,target):boolean
Parameters
source
T
target
T
Returns
boolean
True if edge was removed
removeVertex()
removeVertex(Defined in: graph.ts:229 Removes a vertex and all its edgesvertex):boolean
Parameters
vertex
T
Returns
boolean
True if vertex was removed
toAdjacencyMatrix()
toAdjacencyMatrix(): object
Defined in: graph.ts:632
Converts graph to adjacency matrix representation
Returns
object
matrix
matrix: number[][]
vertices
vertices: T[]
topologicalSort()
topologicalSort():Defined in: graph.ts:516 Performs topological sort on a directed acyclic graph (DAG)T[] |null
Returns
T[] | null
Topologically sorted vertices or null if cycle detected