data-structure-typed 1.18.8 → 1.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +169 -168
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +182 -148
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +288 -316
- package/dist/data-structures/binary-tree/avl-tree.d.ts +22 -14
- package/dist/data-structures/binary-tree/avl-tree.js +23 -17
- package/dist/data-structures/binary-tree/binary-tree.d.ts +12 -26
- package/dist/data-structures/binary-tree/binary-tree.js +11 -26
- package/dist/data-structures/binary-tree/bst.d.ts +62 -74
- package/dist/data-structures/binary-tree/bst.js +72 -96
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -14
- package/dist/data-structures/binary-tree/rb-tree.js +5 -17
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +186 -17
- package/dist/data-structures/binary-tree/tree-multiset.js +712 -28
- package/dist/data-structures/graph/abstract-graph.d.ts +107 -49
- package/dist/data-structures/graph/abstract-graph.js +104 -55
- package/dist/data-structures/graph/directed-graph.d.ts +95 -94
- package/dist/data-structures/graph/directed-graph.js +95 -95
- package/dist/data-structures/graph/undirected-graph.d.ts +62 -61
- package/dist/data-structures/graph/undirected-graph.js +62 -61
- package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +10 -15
- package/dist/data-structures/interfaces/avl-tree.d.ts +2 -2
- package/dist/data-structures/interfaces/binary-tree.d.ts +1 -1
- package/dist/data-structures/interfaces/bst.d.ts +3 -4
- package/dist/data-structures/interfaces/rb-tree.d.ts +1 -2
- package/dist/data-structures/interfaces/tree-multiset.d.ts +3 -3
- package/dist/data-structures/types/abstract-binary-tree.d.ts +1 -1
- package/dist/data-structures/types/tree-multiset.d.ts +3 -3
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/types/index.d.ts +1 -0
- package/dist/utils/types/index.js +1 -0
- package/dist/utils/types/utils.d.ts +0 -18
- package/dist/utils/types/validate-type.d.ts +19 -0
- package/dist/utils/types/validate-type.js +2 -0
- package/dist/utils/utils.d.ts +3 -8
- package/dist/utils/utils.js +1 -83
- package/dist/utils/validate-type.d.ts +45 -0
- package/dist/utils/validate-type.js +58 -0
- package/package.json +6 -2
- package/tsconfig.json +0 -17
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import type { DijkstraResult, VertexId } from '../types';
|
|
2
2
|
import { IAbstractGraph } from '../interfaces';
|
|
3
3
|
export declare abstract class AbstractVertex<T = number> {
|
|
4
|
+
/**
|
|
5
|
+
* The function is a protected constructor that takes an id and an optional value as parameters.
|
|
6
|
+
* @param {VertexId} id - The `id` parameter is of type `VertexId` and represents the identifier of the vertex. It is
|
|
7
|
+
* used to uniquely identify the vertex object.
|
|
8
|
+
* @param {T} [val] - The parameter "val" is an optional parameter of type T. It is used to assign a value to the
|
|
9
|
+
* vertex. If no value is provided, it will be set to undefined.
|
|
10
|
+
*/
|
|
4
11
|
protected constructor(id: VertexId, val?: T);
|
|
5
12
|
private _id;
|
|
6
13
|
get id(): VertexId;
|
|
@@ -10,6 +17,15 @@ export declare abstract class AbstractVertex<T = number> {
|
|
|
10
17
|
set val(value: T | undefined);
|
|
11
18
|
}
|
|
12
19
|
export declare abstract class AbstractEdge<T = number> {
|
|
20
|
+
/**
|
|
21
|
+
* The above function is a protected constructor that initializes the weight, value, and hash code properties of an
|
|
22
|
+
* object.
|
|
23
|
+
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the object. If
|
|
24
|
+
* a value is provided, it will be assigned to the `_weight` property. If no value is provided, the default value of 1
|
|
25
|
+
* will be assigned.
|
|
26
|
+
* @param {T} [val] - The `val` parameter is of type `T`, which means it can be any type. It is an optional parameter,
|
|
27
|
+
* meaning it can be omitted when creating an instance of the class.
|
|
28
|
+
*/
|
|
13
29
|
protected constructor(weight?: number, val?: T);
|
|
14
30
|
private _val;
|
|
15
31
|
get val(): T | undefined;
|
|
@@ -19,6 +35,15 @@ export declare abstract class AbstractEdge<T = number> {
|
|
|
19
35
|
set weight(v: number);
|
|
20
36
|
protected _hashCode: string;
|
|
21
37
|
get hashCode(): string;
|
|
38
|
+
/**
|
|
39
|
+
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
40
|
+
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
41
|
+
*/
|
|
42
|
+
/**
|
|
43
|
+
* The function sets the value of the _hashCode property to the provided string.
|
|
44
|
+
* @param {string} v - The parameter "v" is of type string and represents the value that will be assigned to the
|
|
45
|
+
* "_hashCode" property.
|
|
46
|
+
*/
|
|
22
47
|
protected _setHashCode(v: string): void;
|
|
23
48
|
}
|
|
24
49
|
export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E extends AbstractEdge<any>> implements IAbstractGraph<V, E> {
|
|
@@ -41,22 +66,35 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
41
66
|
*/
|
|
42
67
|
abstract createEdge(srcOrV1: VertexId | string, destOrV2: VertexId | string, weight?: number, val?: E): E;
|
|
43
68
|
abstract removeEdge(edge: E): E | null;
|
|
69
|
+
abstract getEdge(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
|
|
70
|
+
abstract degreeOf(vertexOrId: V | VertexId): number;
|
|
71
|
+
abstract edgeSet(): E[];
|
|
72
|
+
abstract edgesOf(vertexOrId: V | VertexId): E[];
|
|
73
|
+
abstract getNeighbors(vertexOrId: V | VertexId): V[];
|
|
74
|
+
abstract getEndsOfEdge(edge: E): [V, V] | null;
|
|
75
|
+
protected abstract _addEdgeOnly(edge: E): boolean;
|
|
76
|
+
/**
|
|
77
|
+
* The function "getVertex" returns the vertex with the specified ID or null if it doesn't exist.
|
|
78
|
+
* @param {VertexId} vertexId - The `vertexId` parameter is the identifier of the vertex that you want to retrieve from
|
|
79
|
+
* the `_vertices` map.
|
|
80
|
+
* @returns The method `getVertex` returns the vertex with the specified `vertexId` if it exists in the `_vertices`
|
|
81
|
+
* map. If the vertex does not exist, it returns `null`.
|
|
82
|
+
*/
|
|
44
83
|
getVertex(vertexId: VertexId): V | null;
|
|
45
84
|
/**
|
|
46
85
|
* The function checks if a vertex exists in a graph.
|
|
47
|
-
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can
|
|
86
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
48
87
|
* (`VertexId`).
|
|
49
|
-
* @returns
|
|
88
|
+
* @returns a boolean value.
|
|
50
89
|
*/
|
|
51
90
|
hasVertex(vertexOrId: V | VertexId): boolean;
|
|
52
|
-
abstract getEdge(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
|
|
53
91
|
addVertex(vertex: V): boolean;
|
|
54
92
|
addVertex(id: VertexId, val?: V['val']): boolean;
|
|
55
93
|
/**
|
|
56
94
|
* The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
57
95
|
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
58
96
|
* (`VertexId`).
|
|
59
|
-
* @returns The method
|
|
97
|
+
* @returns The method is returning a boolean value.
|
|
60
98
|
*/
|
|
61
99
|
removeVertex(vertexOrId: V | VertexId): boolean;
|
|
62
100
|
/**
|
|
@@ -67,17 +105,13 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
67
105
|
* were removed.
|
|
68
106
|
*/
|
|
69
107
|
removeAllVertices(vertices: V[] | VertexId[]): boolean;
|
|
70
|
-
abstract degreeOf(vertexOrId: V | VertexId): number;
|
|
71
|
-
abstract edgeSet(): E[];
|
|
72
|
-
abstract edgesOf(vertexOrId: V | VertexId): E[];
|
|
73
108
|
/**
|
|
74
|
-
* The function checks if there is an edge between two vertices
|
|
75
|
-
* @param {VertexId | V} v1 - The parameter v1 can be either a VertexId or a V. A VertexId represents the
|
|
76
|
-
* a vertex in a graph, while V represents the type of the vertex itself.
|
|
77
|
-
* @param {VertexId | V} v2 - The parameter `v2` represents the second vertex in
|
|
78
|
-
* or a `V` type.
|
|
79
|
-
* @returns
|
|
80
|
-
* vertices `v1` and `v2`, and `false` otherwise.
|
|
109
|
+
* The function checks if there is an edge between two vertices and returns a boolean value indicating the result.
|
|
110
|
+
* @param {VertexId | V} v1 - The parameter v1 can be either a VertexId or a V. A VertexId represents the unique
|
|
111
|
+
* identifier of a vertex in a graph, while V represents the type of the vertex object itself.
|
|
112
|
+
* @param {VertexId | V} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
|
|
113
|
+
* `VertexId` or a `V` type, which represents the type of the vertex.
|
|
114
|
+
* @returns A boolean value is being returned.
|
|
81
115
|
*/
|
|
82
116
|
hasEdge(v1: VertexId | V, v2: VertexId | V): boolean;
|
|
83
117
|
addEdge(edge: E): boolean;
|
|
@@ -94,15 +128,12 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
94
128
|
* the weight of the edge and return true. If the edge does not exist, the function will return false.
|
|
95
129
|
*/
|
|
96
130
|
setEdgeWeight(srcOrId: VertexId | V, destOrId: VertexId | V, weight: number): boolean;
|
|
97
|
-
abstract getNeighbors(vertexOrId: V | VertexId): V[];
|
|
98
131
|
/**
|
|
99
132
|
* The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
|
|
100
133
|
* @param {V | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
|
|
101
134
|
* It is the starting vertex for finding paths.
|
|
102
|
-
* @param {V | VertexId} v2 - The parameter `v2` represents
|
|
103
|
-
*
|
|
104
|
-
* @returns an array of arrays of vertices (V[][]). Each inner array represents a path between the given vertices (v1
|
|
105
|
-
* and v2).
|
|
135
|
+
* @param {V | VertexId} v2 - The parameter `v2` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
|
|
136
|
+
* @returns The function `getAllPathsBetween` returns an array of arrays of vertices (`V[][]`).
|
|
106
137
|
*/
|
|
107
138
|
getAllPathsBetween(v1: V | VertexId, v2: V | VertexId): V[][];
|
|
108
139
|
/**
|
|
@@ -114,32 +145,37 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
114
145
|
/**
|
|
115
146
|
* The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
|
|
116
147
|
* weights or using a breadth-first search algorithm.
|
|
117
|
-
* @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex or
|
|
118
|
-
* @param {V | VertexId} v2 - The parameter `v2` represents the
|
|
119
|
-
*
|
|
148
|
+
* @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex or its ID.
|
|
149
|
+
* @param {V | VertexId} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
|
|
150
|
+
* you want to find the minimum cost or weight from the source vertex `v1`.
|
|
120
151
|
* @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edges have weights.
|
|
121
152
|
* If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
|
|
122
153
|
* the edges. If isWeight is set to false or not provided, the function will calculate the
|
|
123
154
|
* @returns The function `getMinCostBetween` returns a number representing the minimum cost between two vertices (`v1`
|
|
124
|
-
* and `v2`)
|
|
125
|
-
* If `isWeight` is `false` or not provided, it
|
|
126
|
-
*
|
|
155
|
+
* and `v2`). If the `isWeight` parameter is `true`, it calculates the minimum weight among all paths between the
|
|
156
|
+
* vertices. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
|
|
157
|
+
* minimum number of
|
|
127
158
|
*/
|
|
128
159
|
getMinCostBetween(v1: V | VertexId, v2: V | VertexId, isWeight?: boolean): number | null;
|
|
129
160
|
/**
|
|
130
161
|
* The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
|
|
131
162
|
* using a breadth-first search algorithm.
|
|
132
|
-
* @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex
|
|
133
|
-
*
|
|
134
|
-
*
|
|
163
|
+
* @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
|
|
164
|
+
* object (`V`) or a vertex ID (`VertexId`).
|
|
165
|
+
* @param {V | VertexId} v2 - V | VertexId - The second vertex or vertex ID between which we want to find the minimum
|
|
166
|
+
* path.
|
|
135
167
|
* @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edges in finding the
|
|
136
168
|
* minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
|
|
137
|
-
* to false, the function will use breadth-first search (BFS) to find the minimum path.
|
|
169
|
+
* to false, the function will use breadth-first search (BFS) to find the minimum path.
|
|
138
170
|
* @returns The function `getMinPathBetween` returns an array of vertices (`V[]`) representing the minimum path between
|
|
139
|
-
* two vertices (`v1` and `v2`). If no path
|
|
171
|
+
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `null`.
|
|
140
172
|
*/
|
|
141
173
|
getMinPathBetween(v1: V | VertexId, v2: V | VertexId, isWeight?: boolean): V[] | null;
|
|
142
174
|
/**
|
|
175
|
+
* Dijkstra algorithm time: O(VE) space: O(V + E)
|
|
176
|
+
* /
|
|
177
|
+
|
|
178
|
+
/**
|
|
143
179
|
* Dijkstra algorithm time: O(VE) space: O(V + E)
|
|
144
180
|
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
|
|
145
181
|
* a graph without using a heap data structure.
|
|
@@ -159,6 +195,14 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
159
195
|
dijkstraWithoutHeap(src: V | VertexId, dest?: V | VertexId | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<V>;
|
|
160
196
|
/**
|
|
161
197
|
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
198
|
+
*
|
|
199
|
+
* Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
|
|
200
|
+
* Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
|
|
201
|
+
* The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
|
|
202
|
+
*
|
|
203
|
+
* /
|
|
204
|
+
|
|
205
|
+
/**
|
|
162
206
|
* Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
|
|
163
207
|
* The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
|
|
164
208
|
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
@@ -177,11 +221,30 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
177
221
|
*/
|
|
178
222
|
dijkstra(src: V | VertexId, dest?: V | VertexId | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<V>;
|
|
179
223
|
/**
|
|
224
|
+
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
225
|
+
* /
|
|
226
|
+
|
|
227
|
+
/**
|
|
180
228
|
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
181
229
|
* Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
|
|
182
230
|
*/
|
|
183
|
-
abstract getEndsOfEdge(edge: E): [V, V] | null;
|
|
184
231
|
/**
|
|
232
|
+
* BellmanFord time:O(VE) space:O(V)
|
|
233
|
+
* one to rest pairs
|
|
234
|
+
* The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
235
|
+
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
236
|
+
*/
|
|
237
|
+
/**
|
|
238
|
+
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
239
|
+
* all pairs
|
|
240
|
+
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
|
|
241
|
+
*/
|
|
242
|
+
/**
|
|
243
|
+
* BellmanFord time:O(VE) space:O(V)
|
|
244
|
+
* one to rest pairs
|
|
245
|
+
* /
|
|
246
|
+
|
|
247
|
+
/**
|
|
185
248
|
* BellmanFord time:O(VE) space:O(V)
|
|
186
249
|
* one to rest pairs
|
|
187
250
|
* The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
@@ -206,6 +269,11 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
206
269
|
minPath: V[];
|
|
207
270
|
};
|
|
208
271
|
/**
|
|
272
|
+
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
273
|
+
* all pairs
|
|
274
|
+
* /
|
|
275
|
+
|
|
276
|
+
/**
|
|
209
277
|
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
210
278
|
* all pairs
|
|
211
279
|
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
|
|
@@ -221,6 +289,14 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
221
289
|
predecessor: (V | null)[][];
|
|
222
290
|
};
|
|
223
291
|
/**
|
|
292
|
+
* Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs.
|
|
293
|
+
* Tarjan can find cycles in directed or undirected graph
|
|
294
|
+
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
295
|
+
* Tarjan solve the bi-connected components of undirected graphs;
|
|
296
|
+
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
297
|
+
* /
|
|
298
|
+
|
|
299
|
+
/**
|
|
224
300
|
* Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs.
|
|
225
301
|
* Tarjan can find cycles in directed or undirected graph
|
|
226
302
|
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
@@ -249,26 +325,8 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
249
325
|
SCCs: Map<number, V[]>;
|
|
250
326
|
cycles: Map<number, V[]>;
|
|
251
327
|
};
|
|
252
|
-
/**
|
|
253
|
-
* Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
|
|
254
|
-
* Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
|
|
255
|
-
* The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
|
|
256
|
-
*/
|
|
257
328
|
protected _addVertexOnly(newVertex: V): boolean;
|
|
258
|
-
protected abstract _addEdgeOnly(edge: E): boolean;
|
|
259
|
-
/**
|
|
260
|
-
* BellmanFord time:O(VE) space:O(V)
|
|
261
|
-
* one to rest pairs
|
|
262
|
-
* The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
263
|
-
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
264
|
-
*/
|
|
265
329
|
protected _getVertex(vertexOrId: VertexId | V): V | null;
|
|
266
|
-
/**
|
|
267
|
-
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
268
|
-
* all pairs
|
|
269
|
-
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
|
|
270
|
-
*/
|
|
271
330
|
protected _getVertexId(vertexOrId: V | VertexId): VertexId;
|
|
272
|
-
/**--- start find cycles --- */
|
|
273
331
|
protected _setVertices(value: Map<VertexId, V>): void;
|
|
274
332
|
}
|
|
@@ -47,6 +47,13 @@ exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
|
|
|
47
47
|
var utils_1 = require("../../utils");
|
|
48
48
|
var priority_queue_1 = require("../priority-queue");
|
|
49
49
|
var AbstractVertex = /** @class */ (function () {
|
|
50
|
+
/**
|
|
51
|
+
* The function is a protected constructor that takes an id and an optional value as parameters.
|
|
52
|
+
* @param {VertexId} id - The `id` parameter is of type `VertexId` and represents the identifier of the vertex. It is
|
|
53
|
+
* used to uniquely identify the vertex object.
|
|
54
|
+
* @param {T} [val] - The parameter "val" is an optional parameter of type T. It is used to assign a value to the
|
|
55
|
+
* vertex. If no value is provided, it will be set to undefined.
|
|
56
|
+
*/
|
|
50
57
|
function AbstractVertex(id, val) {
|
|
51
58
|
this._id = id;
|
|
52
59
|
this._val = val;
|
|
@@ -75,6 +82,15 @@ var AbstractVertex = /** @class */ (function () {
|
|
|
75
82
|
}());
|
|
76
83
|
exports.AbstractVertex = AbstractVertex;
|
|
77
84
|
var AbstractEdge = /** @class */ (function () {
|
|
85
|
+
/**
|
|
86
|
+
* The above function is a protected constructor that initializes the weight, value, and hash code properties of an
|
|
87
|
+
* object.
|
|
88
|
+
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the object. If
|
|
89
|
+
* a value is provided, it will be assigned to the `_weight` property. If no value is provided, the default value of 1
|
|
90
|
+
* will be assigned.
|
|
91
|
+
* @param {T} [val] - The `val` parameter is of type `T`, which means it can be any type. It is an optional parameter,
|
|
92
|
+
* meaning it can be omitted when creating an instance of the class.
|
|
93
|
+
*/
|
|
78
94
|
function AbstractEdge(weight, val) {
|
|
79
95
|
this._weight = weight !== undefined ? weight : 1;
|
|
80
96
|
this._val = val;
|
|
@@ -107,28 +123,24 @@ var AbstractEdge = /** @class */ (function () {
|
|
|
107
123
|
enumerable: false,
|
|
108
124
|
configurable: true
|
|
109
125
|
});
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
126
|
+
/**
|
|
127
|
+
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
128
|
+
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
129
|
+
*/
|
|
130
|
+
/**
|
|
131
|
+
* The function sets the value of the _hashCode property to the provided string.
|
|
132
|
+
* @param {string} v - The parameter "v" is of type string and represents the value that will be assigned to the
|
|
133
|
+
* "_hashCode" property.
|
|
134
|
+
*/
|
|
119
135
|
AbstractEdge.prototype._setHashCode = function (v) {
|
|
120
136
|
this._hashCode = v;
|
|
121
137
|
};
|
|
122
138
|
return AbstractEdge;
|
|
123
139
|
}());
|
|
124
140
|
exports.AbstractEdge = AbstractEdge;
|
|
125
|
-
// Connected Component === Largest Connected Sub-Graph
|
|
126
141
|
var AbstractGraph = /** @class */ (function () {
|
|
127
142
|
function AbstractGraph() {
|
|
128
143
|
this._vertices = new Map();
|
|
129
|
-
// unionFind() {}
|
|
130
|
-
/**--- end find cycles --- */
|
|
131
|
-
// Minimum Spanning Tree
|
|
132
144
|
}
|
|
133
145
|
Object.defineProperty(AbstractGraph.prototype, "vertices", {
|
|
134
146
|
get: function () {
|
|
@@ -137,14 +149,21 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
137
149
|
enumerable: false,
|
|
138
150
|
configurable: true
|
|
139
151
|
});
|
|
152
|
+
/**
|
|
153
|
+
* The function "getVertex" returns the vertex with the specified ID or null if it doesn't exist.
|
|
154
|
+
* @param {VertexId} vertexId - The `vertexId` parameter is the identifier of the vertex that you want to retrieve from
|
|
155
|
+
* the `_vertices` map.
|
|
156
|
+
* @returns The method `getVertex` returns the vertex with the specified `vertexId` if it exists in the `_vertices`
|
|
157
|
+
* map. If the vertex does not exist, it returns `null`.
|
|
158
|
+
*/
|
|
140
159
|
AbstractGraph.prototype.getVertex = function (vertexId) {
|
|
141
160
|
return this._vertices.get(vertexId) || null;
|
|
142
161
|
};
|
|
143
162
|
/**
|
|
144
163
|
* The function checks if a vertex exists in a graph.
|
|
145
|
-
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can
|
|
164
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
146
165
|
* (`VertexId`).
|
|
147
|
-
* @returns
|
|
166
|
+
* @returns a boolean value.
|
|
148
167
|
*/
|
|
149
168
|
AbstractGraph.prototype.hasVertex = function (vertexOrId) {
|
|
150
169
|
return this._vertices.has(this._getVertexId(vertexOrId));
|
|
@@ -162,7 +181,7 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
162
181
|
* The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
163
182
|
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
164
183
|
* (`VertexId`).
|
|
165
|
-
* @returns The method
|
|
184
|
+
* @returns The method is returning a boolean value.
|
|
166
185
|
*/
|
|
167
186
|
AbstractGraph.prototype.removeVertex = function (vertexOrId) {
|
|
168
187
|
var vertexId = this._getVertexId(vertexOrId);
|
|
@@ -194,13 +213,12 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
194
213
|
return removed.length > 0;
|
|
195
214
|
};
|
|
196
215
|
/**
|
|
197
|
-
* The function checks if there is an edge between two vertices
|
|
198
|
-
* @param {VertexId | V} v1 - The parameter v1 can be either a VertexId or a V. A VertexId represents the
|
|
199
|
-
* a vertex in a graph, while V represents the type of the vertex itself.
|
|
200
|
-
* @param {VertexId | V} v2 - The parameter `v2` represents the second vertex in
|
|
201
|
-
* or a `V` type.
|
|
202
|
-
* @returns
|
|
203
|
-
* vertices `v1` and `v2`, and `false` otherwise.
|
|
216
|
+
* The function checks if there is an edge between two vertices and returns a boolean value indicating the result.
|
|
217
|
+
* @param {VertexId | V} v1 - The parameter v1 can be either a VertexId or a V. A VertexId represents the unique
|
|
218
|
+
* identifier of a vertex in a graph, while V represents the type of the vertex object itself.
|
|
219
|
+
* @param {VertexId | V} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
|
|
220
|
+
* `VertexId` or a `V` type, which represents the type of the vertex.
|
|
221
|
+
* @returns A boolean value is being returned.
|
|
204
222
|
*/
|
|
205
223
|
AbstractGraph.prototype.hasEdge = function (v1, v2) {
|
|
206
224
|
var edge = this.getEdge(v1, v2);
|
|
@@ -251,10 +269,8 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
251
269
|
* The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
|
|
252
270
|
* @param {V | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
|
|
253
271
|
* It is the starting vertex for finding paths.
|
|
254
|
-
* @param {V | VertexId} v2 - The parameter `v2` represents
|
|
255
|
-
*
|
|
256
|
-
* @returns an array of arrays of vertices (V[][]). Each inner array represents a path between the given vertices (v1
|
|
257
|
-
* and v2).
|
|
272
|
+
* @param {V | VertexId} v2 - The parameter `v2` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
|
|
273
|
+
* @returns The function `getAllPathsBetween` returns an array of arrays of vertices (`V[][]`).
|
|
258
274
|
*/
|
|
259
275
|
AbstractGraph.prototype.getAllPathsBetween = function (v1, v2) {
|
|
260
276
|
var _this = this;
|
|
@@ -312,16 +328,16 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
312
328
|
/**
|
|
313
329
|
* The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
|
|
314
330
|
* weights or using a breadth-first search algorithm.
|
|
315
|
-
* @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex or
|
|
316
|
-
* @param {V | VertexId} v2 - The parameter `v2` represents the
|
|
317
|
-
*
|
|
331
|
+
* @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex or its ID.
|
|
332
|
+
* @param {V | VertexId} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
|
|
333
|
+
* you want to find the minimum cost or weight from the source vertex `v1`.
|
|
318
334
|
* @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edges have weights.
|
|
319
335
|
* If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
|
|
320
336
|
* the edges. If isWeight is set to false or not provided, the function will calculate the
|
|
321
337
|
* @returns The function `getMinCostBetween` returns a number representing the minimum cost between two vertices (`v1`
|
|
322
|
-
* and `v2`)
|
|
323
|
-
* If `isWeight` is `false` or not provided, it
|
|
324
|
-
*
|
|
338
|
+
* and `v2`). If the `isWeight` parameter is `true`, it calculates the minimum weight among all paths between the
|
|
339
|
+
* vertices. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
|
|
340
|
+
* minimum number of
|
|
325
341
|
*/
|
|
326
342
|
AbstractGraph.prototype.getMinCostBetween = function (v1, v2, isWeight) {
|
|
327
343
|
var e_3, _a, e_4, _b;
|
|
@@ -391,14 +407,15 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
391
407
|
/**
|
|
392
408
|
* The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
|
|
393
409
|
* using a breadth-first search algorithm.
|
|
394
|
-
* @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex
|
|
395
|
-
*
|
|
396
|
-
*
|
|
410
|
+
* @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
|
|
411
|
+
* object (`V`) or a vertex ID (`VertexId`).
|
|
412
|
+
* @param {V | VertexId} v2 - V | VertexId - The second vertex or vertex ID between which we want to find the minimum
|
|
413
|
+
* path.
|
|
397
414
|
* @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edges in finding the
|
|
398
415
|
* minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
|
|
399
|
-
* to false, the function will use breadth-first search (BFS) to find the minimum path.
|
|
416
|
+
* to false, the function will use breadth-first search (BFS) to find the minimum path.
|
|
400
417
|
* @returns The function `getMinPathBetween` returns an array of vertices (`V[]`) representing the minimum path between
|
|
401
|
-
* two vertices (`v1` and `v2`). If no path
|
|
418
|
+
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `null`.
|
|
402
419
|
*/
|
|
403
420
|
AbstractGraph.prototype.getMinPathBetween = function (v1, v2, isWeight) {
|
|
404
421
|
var e_5, _a;
|
|
@@ -473,6 +490,10 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
473
490
|
}
|
|
474
491
|
};
|
|
475
492
|
/**
|
|
493
|
+
* Dijkstra algorithm time: O(VE) space: O(V + E)
|
|
494
|
+
* /
|
|
495
|
+
|
|
496
|
+
/**
|
|
476
497
|
* Dijkstra algorithm time: O(VE) space: O(V + E)
|
|
477
498
|
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
|
|
478
499
|
* a graph without using a heap data structure.
|
|
@@ -635,6 +656,14 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
635
656
|
};
|
|
636
657
|
/**
|
|
637
658
|
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
659
|
+
*
|
|
660
|
+
* Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
|
|
661
|
+
* Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
|
|
662
|
+
* The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
|
|
663
|
+
*
|
|
664
|
+
* /
|
|
665
|
+
|
|
666
|
+
/**
|
|
638
667
|
* Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
|
|
639
668
|
* The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
|
|
640
669
|
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
@@ -781,7 +810,31 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
781
810
|
}
|
|
782
811
|
return { distMap: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
|
|
783
812
|
};
|
|
813
|
+
/**
|
|
814
|
+
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
815
|
+
* /
|
|
816
|
+
|
|
817
|
+
/**
|
|
818
|
+
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
819
|
+
* Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
|
|
820
|
+
*/
|
|
821
|
+
/**
|
|
822
|
+
* BellmanFord time:O(VE) space:O(V)
|
|
823
|
+
* one to rest pairs
|
|
824
|
+
* The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
825
|
+
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
826
|
+
*/
|
|
827
|
+
/**
|
|
828
|
+
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
829
|
+
* all pairs
|
|
830
|
+
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
|
|
831
|
+
*/
|
|
784
832
|
/**
|
|
833
|
+
* BellmanFord time:O(VE) space:O(V)
|
|
834
|
+
* one to rest pairs
|
|
835
|
+
* /
|
|
836
|
+
|
|
837
|
+
/**
|
|
785
838
|
* BellmanFord time:O(VE) space:O(V)
|
|
786
839
|
* one to rest pairs
|
|
787
840
|
* The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
@@ -894,6 +947,11 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
894
947
|
return { hasNegativeCycle: hasNegativeCycle, distMap: distMap, preMap: preMap, paths: paths, min: min, minPath: minPath };
|
|
895
948
|
};
|
|
896
949
|
/**
|
|
950
|
+
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
951
|
+
* all pairs
|
|
952
|
+
* /
|
|
953
|
+
|
|
954
|
+
/**
|
|
897
955
|
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
898
956
|
* all pairs
|
|
899
957
|
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
|
|
@@ -936,6 +994,14 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
936
994
|
return { costs: costs, predecessor: predecessor };
|
|
937
995
|
};
|
|
938
996
|
/**
|
|
997
|
+
* Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs.
|
|
998
|
+
* Tarjan can find cycles in directed or undirected graph
|
|
999
|
+
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
1000
|
+
* Tarjan solve the bi-connected components of undirected graphs;
|
|
1001
|
+
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
1002
|
+
* /
|
|
1003
|
+
|
|
1004
|
+
/**
|
|
939
1005
|
* Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs.
|
|
940
1006
|
* Tarjan can find cycles in directed or undirected graph
|
|
941
1007
|
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
@@ -1062,11 +1128,6 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
1062
1128
|
}
|
|
1063
1129
|
return { dfnMap: dfnMap, lowMap: lowMap, bridges: bridges, articulationPoints: articulationPoints, SCCs: SCCs, cycles: cycles };
|
|
1064
1130
|
};
|
|
1065
|
-
/**
|
|
1066
|
-
* Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
|
|
1067
|
-
* Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
|
|
1068
|
-
* The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
|
|
1069
|
-
*/
|
|
1070
1131
|
AbstractGraph.prototype._addVertexOnly = function (newVertex) {
|
|
1071
1132
|
if (this.hasVertex(newVertex)) {
|
|
1072
1133
|
return false;
|
|
@@ -1075,25 +1136,13 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
1075
1136
|
this._vertices.set(newVertex.id, newVertex);
|
|
1076
1137
|
return true;
|
|
1077
1138
|
};
|
|
1078
|
-
/**
|
|
1079
|
-
* BellmanFord time:O(VE) space:O(V)
|
|
1080
|
-
* one to rest pairs
|
|
1081
|
-
* The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
1082
|
-
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
1083
|
-
*/
|
|
1084
1139
|
AbstractGraph.prototype._getVertex = function (vertexOrId) {
|
|
1085
1140
|
var vertexId = this._getVertexId(vertexOrId);
|
|
1086
1141
|
return this._vertices.get(vertexId) || null;
|
|
1087
1142
|
};
|
|
1088
|
-
/**
|
|
1089
|
-
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
1090
|
-
* all pairs
|
|
1091
|
-
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
|
|
1092
|
-
*/
|
|
1093
1143
|
AbstractGraph.prototype._getVertexId = function (vertexOrId) {
|
|
1094
1144
|
return vertexOrId instanceof AbstractVertex ? vertexOrId.id : vertexOrId;
|
|
1095
1145
|
};
|
|
1096
|
-
/**--- start find cycles --- */
|
|
1097
1146
|
AbstractGraph.prototype._setVertices = function (value) {
|
|
1098
1147
|
this._vertices = value;
|
|
1099
1148
|
};
|