UndirectedGraph<T> / DirectedGraph<T>
Guide: /guide/data-structures/graph
Generic Types
T
- type of collection elements
Implements interfaces
IGraph
Methods
constructor(): IGraph<T>
Creates empty instance
weight(): number
Sum of all edges in graph
vertices(): Array<T>
Only vertices itself without edges and weight info
verticesCount(): number
Count of vertices
edgesCount(): number
Count of all edges (it's different in directed and undirected graphs)
addVertex(value: T): IGraph<T>
Create new graph node
Params:
Name | Type | Required | Default | Description |
---|---|---|---|---|
value | T | + | - |
IsAlreadyExistsException
when vertex is already exists
Throws: removeVertex(value: T): IGraph<T>
Remove vertex and all related edges
Params:
Name | Type | Required | Default | Description |
---|---|---|---|---|
value | T | + | - |
IsNotFoundException
when vertex to remove was not found
Throws: hasVertex(value: T): boolean
Check if graph has given vertex
Params:
Name | Type | Required | Default | Description |
---|---|---|---|---|
value | T | + | - |
getVertexNeighbors(value: T): Array<T>
Get neighbor vertices of given vertex
addEdge(from: T, to: T, weight?: number): IGraph<T>
Add new edge between two given vertices
Params:
Name | Type | Required | Default | Description |
---|---|---|---|---|
from | T | + | - | Start vertex |
to | T | + | - | End vertex |
weight | number | - | 0 | Edge weight |
IsNotFoundException
when one of vertices was not found
Throws: Difference between directed and undirected graphs
In case of undirected graph, edges A->B and B->A are the same
Instead, in case of directed graph, they are not equal
When edge already exists
In case of already existed edge, its weight just will be updated
removeEdge(from: T, to: T): IGraph<T>
Remove edge between two given vertices
Params:
Name | Type | Required | Default | Description |
---|---|---|---|---|
from | T | + | - | Start vertex |
to | T | + | - | End vertex |
IsNotFoundException
when edge to remove was not found
Throws: hasEdge(from: T, to: T): boolean
Check if graph has edge between two given vertices
Params:
Name | Type | Required | Default | Description |
---|---|---|---|---|
from | T | + | - | Start vertex |
to | T | + | - | End vertex |
getEdgeWeight(from: T, to: T): number
Get weight of edge between two given vertices
Params:
Name | Type | Required | Default | Description |
---|---|---|---|---|
from | T | + | - | Start vertex |
to | T | + | - | End vertex |