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:
NameTypeRequiredDefaultDescription
valueT+-
Throws: IsAlreadyExistsException when vertex is already exists



removeVertex(value: T): IGraph<T>

Remove vertex and all related edges

Params:
NameTypeRequiredDefaultDescription
valueT+-
Throws: IsNotFoundException when vertex to remove was not found



hasVertex(value: T): boolean

Check if graph has given vertex

Params:
NameTypeRequiredDefaultDescription
valueT+-



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:
NameTypeRequiredDefaultDescription
fromT+-Start vertex
toT+-End vertex
weightnumber-0Edge weight
Throws: IsNotFoundException when one of vertices was not found

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:
NameTypeRequiredDefaultDescription
fromT+-Start vertex
toT+-End vertex
Throws: IsNotFoundException when edge to remove was not found



hasEdge(from: T, to: T): boolean

Check if graph has edge between two given vertices

Params:
NameTypeRequiredDefaultDescription
fromT+-Start vertex
toT+-End vertex



getEdgeWeight(from: T, to: T): number

Get weight of edge between two given vertices

Params:
NameTypeRequiredDefaultDescription
fromT+-Start vertex
toT+-End vertex