Protected
_inProtected
_outProtected
_verticesThe parameter edge
is of type E
, which represents an edge in the graph. It contains
information about the source vertex (src
) and the destination vertex (dest
) of the edge.
The addEdge
function returns a boolean value. It returns true
if the edge was successfully added to the
graph, and false
if either the source or destination vertices of the edge are not present in the graph.
The addVertex function adds a new vertex to a graph if it does not already exist.
The parameter "newVertex" is of type V, which represents a vertex in a graph.
The method is returning a boolean value. If the newVertex is already contained in the graph, it will return false. Otherwise, it will add the newVertex to the graph and return true.
BellmanFord time:O(VE) space:O(V)
one to rest pairs
The bellmanFord
function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.
The src
parameter is the source vertex from which the Bellman-Ford algorithm will
start calculating the shortest paths. It can be either a vertex object or a vertex ID.
Optional
scanNegativeCycle: booleanA boolean flag indicating whether to scan for negative cycles in the graph.
Optional
getMin: booleanThe getMin
parameter is a boolean flag that determines whether the algorithm should
calculate the minimum distance from the source vertex to all other vertices in the graph. If getMin
is set to
true
, the algorithm will find the minimum distance and update the min
variable with the minimum
Optional
genPath: booleanA boolean flag indicating whether to generate paths for all vertices from the source vertex.
The function bellmanFord
returns an object with the following properties:
The function checks if there is an edge between two vertices in a graph.
The parameter v1 can be either a VertexId or a V. A VertexId represents the identifier of a vertex in a graph, while V represents the type of the vertex itself.
The parameter v2
represents the second vertex in an edge. It can be either a VertexId
or a V
type.
The function containsEdge
returns a boolean value. It returns true
if there is an edge between the
vertices v1
and v2
, and false
otherwise.
The function "degreeOf" returns the total degree of a vertex, which is the sum of its out-degree and in-degree.
The parameter vertexOrId
can be either a VertexId
or a V
.
the sum of the out-degree and in-degree of the specified vertex or vertex ID.
Dijkstra algorithm time: O(logVE) space: O(V + E)
The dijkstra
function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
The src
parameter represents the source vertex from which the Dijkstra algorithm will
start. It can be either a vertex object or a vertex ID.
Optional
dest: null | VertexId | VThe dest
parameter is the destination vertex or vertex ID. It specifies the
vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
will calculate the shortest paths to all other vertices from the source vertex.
Optional
getMinDist: booleanThe getMinDist
parameter is a boolean flag that determines whether the minimum
distance from the source vertex to the destination vertex should be calculated and returned in the result. If
getMinDist
is set to true
, the minDist
property in the result will contain the minimum distance
Optional
genPaths: booleanThe genPaths
parameter is a boolean flag that determines whether or not to generate
paths in the Dijkstra algorithm. If genPaths
is set to true
, the algorithm will calculate and return the
shortest paths from the source vertex to all other vertices in the graph. If genPaths @returns The function
dijkstrareturns an object of type
DijkstraResult
Dijkstra algorithm time: O(VE) space: O(V + E)
The function dijkstraWithoutHeap
implements Dijkstra's algorithm to find the shortest path between two vertices in
a graph without using a heap data structure.
The source vertex from which to start the Dijkstra's algorithm. It can be either a vertex object or a vertex ID.
Optional
dest: null | VertexId | VThe dest
parameter in the dijkstraWithoutHeap
function is an optional
parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
identifier. If no destination is provided, the value is set to null
.
Optional
getMinDist: booleanThe getMinDist
parameter is a boolean flag that determines whether the minimum
distance from the source vertex to the destination vertex should be calculated and returned in the result. If
getMinDist
is set to true
, the minDist
property in the result will contain the minimum distance
Optional
genPaths: booleanThe genPaths
parameter is a boolean flag that determines whether or not to generate
paths in the Dijkstra algorithm. If genPaths
is set to true
, the algorithm will calculate and return the
shortest paths from the source vertex to all other vertices in the graph. If genPaths @returns The function
dijkstraWithoutHeapreturns an object of type
DijkstraResult
Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle all pairs The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a graph.
The function floyd()
returns an object with two properties: costs
and predecessor
. The costs
property is a 2D array of numbers representing the shortest path costs between vertices in a graph. The
predecessor
property is a 2D array of vertices (or null
) representing the predecessor vertices in the shortest
path between vertices in the
The function getAllPathsBetween
finds all paths between two vertices in a graph using depth-first search.
The parameter v1
represents either a vertex object (V
) or a vertex ID (VertexId
).
It is the starting vertex for finding paths.
The parameter v2
represents the destination vertex or its ID. It is the vertex that we
want to find paths to from the starting vertex v1
.
an array of arrays of vertices (V[][]). Each inner array represents a path between the given vertices (v1 and v2).
The function getDestinations
returns an array of destination vertices connected to a given vertex.
The vertex
parameter represents the starting vertex from which we want to
find the destinations. It can be either a V
object, a VertexId
(which is a unique identifier for a vertex), or
null
if we want to find destinations from all vertices.
an array of vertices (V[]).
The function getEdge
returns the first edge between two vertices, given their source and destination.
The srcOrId
parameter can be either a vertex object (V
), a vertex ID
(VertexId
), or null
. It represents the source vertex of the edge.
The destOrId
parameter is either a vertex object (V
), a vertex ID
(VertexId
), or null
. It represents the destination vertex of the edge.
an edge (E) or null.
The function "getEndsOfEdge" returns the source and destination vertices of an edge if it exists in the graph, otherwise it returns null.
The parameter "edge" is of type E, which represents an edge in a graph.
an array containing two vertices [V, V] if the edge is found in the graph. If the edge is not found, it returns null.
The function getMinCostBetween
calculates the minimum cost between two vertices in a graph, either based on edge
weights or using a breadth-first search algorithm.
The parameter v1
represents the starting vertex or vertex ID of the graph.
The parameter v2
represents the second vertex in the graph. It can be either a vertex
object or a vertex ID.
Optional
isWeight: booleanisWeight is an optional parameter that indicates whether the graph edges have weights. If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of the edges. If isWeight is set to false or not provided, the function will calculate the
The function getMinCostBetween
returns a number representing the minimum cost between two vertices (v1
and v2
) in a graph. If the isWeight
parameter is true
, it calculates the minimum weight between the vertices.
If isWeight
is false
or not provided, it calculates the minimum number of edges between the vertices. If the
vertices are not
The function getMinPathBetween
returns the minimum path between two vertices in a graph, either based on weight or
using a breadth-first search algorithm.
The parameter v1
represents the starting vertex or its ID.
The parameter v2
represents the destination vertex or its ID. It is the vertex that we
want to find the minimum path to from the source vertex v1
.
Optional
isWeight: booleanA boolean flag indicating whether to consider the weight of edges in finding the minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set to false, the function will use breadth-first search (BFS) to find the minimum path. If
The function getMinPathBetween
returns an array of vertices (V[]
) representing the minimum path between
two vertices (v1
and v2
). If no path is found, it returns null
.
The function getVertex
returns the vertex object associated with a given vertex ID or vertex object, or null if it
does not exist.
The parameter vertexOrId
can be either a VertexId
or a V
.
The function getVertex
returns the vertex object (V
) corresponding to the given vertexOrId
parameter.
If the vertex is found in the _vertices
map, it is returned. Otherwise, null
is returned.
The function getVertexId
returns the id of a vertex, whether it is passed as an instance of AbstractVertex
or as
a VertexId
.
The parameter vertexOrId
can be either a vertex object (V
) or a vertex ID
(VertexId
).
the id of the vertex.
The function incomingEdgesOf
returns an array of incoming edges for a given vertex or vertex ID.
The parameter vertexOrId
can be either a vertex object (V
) or a vertex ID
(VertexId
).
The method incomingEdgesOf
returns an array of edges (E[]
).
The function outgoingEdgesOf
returns an array of outgoing edges from a given vertex or vertex ID.
The parameter vertexOrId
can accept either a vertex object (V
) or a vertex ID
(VertexId
).
The method outgoingEdgesOf
returns an array of outgoing edges from a given vertex or vertex ID.
The function removeAllEdges removes all edges between two vertices.
The src
parameter represents the source vertex from which the edges will be removed.
It can be either a VertexId
or a V
type, which represents the identifier or object of the vertex.
The dest
parameter represents the destination vertex of an edge. It can be either a
VertexId
or a vertex object V
.
An empty array is being returned.
The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
The vertices
parameter can be either an array of vertices (V[]
) or an array
of vertex IDs (VertexId[]
).
a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices were removed.
The removeEdge function removes an edge from a graph and returns the removed edge, or null if the edge was not found.
The edge
parameter is an object that represents an edge in a graph. It has two properties: src
and dest
, which represent the source and destination vertices of the edge, respectively.
The method removeEdge
returns the removed edge (E
) if it exists, or null
if the edge does not exist.
The function removes an edge between two vertices in a directed graph and returns the removed edge.
The source vertex or its ID.
The destOrId
parameter in the removeEdgeBetween
function represents the
destination vertex of the edge that needs to be removed. It can be either a vertex object (V
) or a vertex ID
(VertexId
).
The function removeEdgeBetween
returns the removed edge (E
) if the edge between the source and
destination vertices is successfully removed. If either the source or destination vertex is not found, or if the
edge does not exist, it returns null
.
The removeVertex
function removes a vertex from a graph by its ID or by the vertex object itself.
The parameter vertexOrId
can be either a vertex object (V
) or a vertex ID
(VertexId
).
The method removeVertex
returns a boolean value.
The function sets the weight of an edge between two vertices in a graph.
The srcOrId
parameter can be either a VertexId
or a V
object. It represents
the source vertex of the edge.
The destOrId
parameter represents the destination vertex of the edge. It can be
either a VertexId
or a vertex object V
.
The weight parameter represents the weight of the edge between the source vertex (srcOrId) and the destination vertex (destOrId).
a boolean value. If the edge exists between the source and destination vertices, the function will update the weight of the edge and return true. If the edge does not exist, the function will return false.
Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs.
Tarjan can find cycles in directed or undirected graph
Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
Tarjan solve the bi-connected components of undirected graphs;
Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
The tarjan
function is used to perform various graph analysis tasks such as finding articulation points, bridges,
strongly connected components (SCCs), and cycles in a graph.
Optional
needArticulationPoints: booleanA boolean value indicating whether or not to calculate and return the articulation points in the graph. Articulation points are the vertices in a graph whose removal would increase the number of connected components in the graph.
Optional
needBridges: booleanA boolean flag indicating whether the algorithm should find and return the bridges (edges whose removal would increase the number of connected components in the graph).
Optional
needSCCs: booleanA boolean value indicating whether the Strongly Connected Components (SCCs) of the graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the SCCs will not be calculated or returned.
Optional
needCycles: booleanA boolean flag indicating whether the algorithm should find cycles in the graph. If set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values are arrays of vertices that form cycles within the SCCs.
The function tarjan
returns an object with the following properties:
when stored with adjacency list time: O(V+E)
when stored with adjacency matrix time: O(V^2)
The topologicalSort
function performs a topological sort on a directed graph and returns the sorted vertices in
reverse order, or null if the graph contains a cycle.
The function topologicalSort()
returns an array of vertices in topological order if there is no cycle in
the graph. If there is a cycle in the graph, it returns null
.
Generated using TypeDoc
The
addEdge
function adds an edge to a graph if the source and destination vertices exist.