min-heap-typed 1.42.8 → 1.42.9
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/dist/data-structures/binary-tree/avl-tree.d.ts +88 -23
- package/dist/data-structures/binary-tree/avl-tree.js +88 -23
- package/dist/data-structures/binary-tree/binary-tree.d.ts +180 -74
- package/dist/data-structures/binary-tree/binary-tree.js +388 -201
- package/dist/data-structures/binary-tree/bst.d.ts +121 -66
- package/dist/data-structures/binary-tree/bst.js +121 -67
- package/dist/data-structures/binary-tree/rb-tree.d.ts +72 -5
- package/dist/data-structures/binary-tree/rb-tree.js +95 -18
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +82 -43
- package/dist/data-structures/binary-tree/tree-multimap.js +82 -43
- package/dist/data-structures/graph/abstract-graph.d.ts +139 -36
- package/dist/data-structures/graph/abstract-graph.js +147 -36
- package/dist/data-structures/graph/directed-graph.d.ts +126 -0
- package/dist/data-structures/graph/directed-graph.js +126 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/data-structures/graph/undirected-graph.js +63 -0
- package/dist/data-structures/heap/heap.d.ts +175 -12
- package/dist/data-structures/heap/heap.js +175 -12
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +203 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +203 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +182 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +182 -0
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +64 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +64 -0
- package/dist/data-structures/queue/deque.d.ts +113 -3
- package/dist/data-structures/queue/deque.js +113 -3
- package/dist/data-structures/queue/queue.d.ts +87 -0
- package/dist/data-structures/queue/queue.js +87 -0
- package/dist/data-structures/stack/stack.d.ts +42 -0
- package/dist/data-structures/stack/stack.js +42 -0
- package/dist/data-structures/trie/trie.d.ts +76 -0
- package/dist/data-structures/trie/trie.js +76 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +97 -23
- package/src/data-structures/binary-tree/binary-tree.ts +419 -204
- package/src/data-structures/binary-tree/bst.ts +130 -68
- package/src/data-structures/binary-tree/rb-tree.ts +106 -19
- package/src/data-structures/binary-tree/tree-multimap.ts +88 -44
- package/src/data-structures/graph/abstract-graph.ts +133 -7
- package/src/data-structures/graph/directed-graph.ts +145 -1
- package/src/data-structures/graph/undirected-graph.ts +72 -0
- package/src/data-structures/heap/heap.ts +201 -12
- package/src/data-structures/linked-list/doubly-linked-list.ts +232 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +208 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +74 -0
- package/src/data-structures/queue/deque.ts +127 -3
- package/src/data-structures/queue/queue.ts +99 -0
- package/src/data-structures/stack/stack.ts +48 -0
- package/src/data-structures/trie/trie.ts +87 -4
|
@@ -53,6 +53,13 @@ class AbstractGraph {
|
|
|
53
53
|
return this._vertices;
|
|
54
54
|
}
|
|
55
55
|
/**
|
|
56
|
+
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
57
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
58
|
+
*/
|
|
59
|
+
/**
|
|
60
|
+
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
61
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
62
|
+
*
|
|
56
63
|
* The function "getVertex" returns the vertex with the specified ID or null if it doesn't exist.
|
|
57
64
|
* @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
|
|
58
65
|
* the `_vertices` map.
|
|
@@ -63,6 +70,13 @@ class AbstractGraph {
|
|
|
63
70
|
return this._vertices.get(vertexKey) || null;
|
|
64
71
|
}
|
|
65
72
|
/**
|
|
73
|
+
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
74
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
75
|
+
*/
|
|
76
|
+
/**
|
|
77
|
+
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
78
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
79
|
+
*
|
|
66
80
|
* The function checks if a vertex exists in a graph.
|
|
67
81
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
68
82
|
* (`VertexKey`).
|
|
@@ -71,6 +85,10 @@ class AbstractGraph {
|
|
|
71
85
|
hasVertex(vertexOrKey) {
|
|
72
86
|
return this._vertices.has(this._getVertexKey(vertexOrKey));
|
|
73
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
90
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
91
|
+
*/
|
|
74
92
|
addVertex(keyOrVertex, value) {
|
|
75
93
|
if (keyOrVertex instanceof AbstractVertex) {
|
|
76
94
|
return this._addVertexOnly(keyOrVertex);
|
|
@@ -81,6 +99,13 @@ class AbstractGraph {
|
|
|
81
99
|
}
|
|
82
100
|
}
|
|
83
101
|
/**
|
|
102
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
103
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
104
|
+
*/
|
|
105
|
+
/**
|
|
106
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
107
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
108
|
+
*
|
|
84
109
|
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
85
110
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
86
111
|
* (`VertexKey`).
|
|
@@ -91,6 +116,13 @@ class AbstractGraph {
|
|
|
91
116
|
return this._vertices.delete(vertexKey);
|
|
92
117
|
}
|
|
93
118
|
/**
|
|
119
|
+
* Time Complexity: O(K), where K is the number of vertices to be removed.
|
|
120
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
121
|
+
*/
|
|
122
|
+
/**
|
|
123
|
+
* Time Complexity: O(K), where K is the number of vertices to be removed.
|
|
124
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
125
|
+
*
|
|
94
126
|
* The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
|
|
95
127
|
* @param {VO[] | VertexKey[]} vertices - The `vertices` parameter can be either an array of vertices (`VO[]`) or an array
|
|
96
128
|
* of vertex IDs (`VertexKey[]`).
|
|
@@ -105,6 +137,13 @@ class AbstractGraph {
|
|
|
105
137
|
return removed.length > 0;
|
|
106
138
|
}
|
|
107
139
|
/**
|
|
140
|
+
* Time Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
141
|
+
* Space Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
142
|
+
*/
|
|
143
|
+
/**
|
|
144
|
+
* Time Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
145
|
+
* Space Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
146
|
+
*
|
|
108
147
|
* The function checks if there is an edge between two vertices and returns a boolean value indicating the result.
|
|
109
148
|
* @param {VertexKey | VO} v1 - The parameter v1 can be either a VertexKey or a VO. A VertexKey represents the unique
|
|
110
149
|
* identifier of a vertex in a graph, while VO represents the type of the vertex object itself.
|
|
@@ -116,6 +155,10 @@ class AbstractGraph {
|
|
|
116
155
|
const edge = this.getEdge(v1, v2);
|
|
117
156
|
return !!edge;
|
|
118
157
|
}
|
|
158
|
+
/**
|
|
159
|
+
* Time Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
160
|
+
* Space Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
161
|
+
*/
|
|
119
162
|
addEdge(srcOrEdge, dest, weight, value) {
|
|
120
163
|
if (srcOrEdge instanceof AbstractEdge) {
|
|
121
164
|
return this._addEdgeOnly(srcOrEdge);
|
|
@@ -137,6 +180,13 @@ class AbstractGraph {
|
|
|
137
180
|
}
|
|
138
181
|
}
|
|
139
182
|
/**
|
|
183
|
+
* Time Complexity: O(1) - Constant time for Map and Edge operations.
|
|
184
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
185
|
+
*/
|
|
186
|
+
/**
|
|
187
|
+
* Time Complexity: O(1) - Constant time for Map and Edge operations.
|
|
188
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
189
|
+
*
|
|
140
190
|
* The function sets the weight of an edge between two vertices in a graph.
|
|
141
191
|
* @param {VertexKey | VO} srcOrKey - The `srcOrKey` parameter can be either a `VertexKey` or a `VO` object. It represents
|
|
142
192
|
* the source vertex of the edge.
|
|
@@ -158,6 +208,13 @@ class AbstractGraph {
|
|
|
158
208
|
}
|
|
159
209
|
}
|
|
160
210
|
/**
|
|
211
|
+
* Time Complexity: O(P), where P is the number of paths found (in the worst case, exploring all paths).
|
|
212
|
+
* Space Complexity: O(P) - Linear space, where P is the number of paths found.
|
|
213
|
+
*/
|
|
214
|
+
/**
|
|
215
|
+
* Time Complexity: O(P), where P is the number of paths found (in the worst case, exploring all paths).
|
|
216
|
+
* Space Complexity: O(P) - Linear space, where P is the number of paths found.
|
|
217
|
+
*
|
|
161
218
|
* The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
|
|
162
219
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
163
220
|
* It is the starting vertex for finding paths.
|
|
@@ -192,6 +249,13 @@ class AbstractGraph {
|
|
|
192
249
|
return paths;
|
|
193
250
|
}
|
|
194
251
|
/**
|
|
252
|
+
* Time Complexity: O(L), where L is the length of the path.
|
|
253
|
+
* Space Complexity: O(1) - Constant space.
|
|
254
|
+
*/
|
|
255
|
+
/**
|
|
256
|
+
* Time Complexity: O(L), where L is the length of the path.
|
|
257
|
+
* Space Complexity: O(1) - Constant space.
|
|
258
|
+
*
|
|
195
259
|
* The function calculates the sum of weights along a given path.
|
|
196
260
|
* @param {VO[]} path - An array of vertices (VO) representing a path in a graph.
|
|
197
261
|
* @returns The function `getPathSumWeight` returns the sum of the weights of the edges in the given path.
|
|
@@ -205,6 +269,13 @@ class AbstractGraph {
|
|
|
205
269
|
return sum;
|
|
206
270
|
}
|
|
207
271
|
/**
|
|
272
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
273
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
274
|
+
*/
|
|
275
|
+
/**
|
|
276
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
277
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
278
|
+
*
|
|
208
279
|
* The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
|
|
209
280
|
* weights or using a breadth-first search algorithm.
|
|
210
281
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex or its ID.
|
|
@@ -263,6 +334,13 @@ class AbstractGraph {
|
|
|
263
334
|
}
|
|
264
335
|
}
|
|
265
336
|
/**
|
|
337
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
338
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
339
|
+
*/
|
|
340
|
+
/**
|
|
341
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
342
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
343
|
+
*
|
|
266
344
|
* The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
|
|
267
345
|
* using a breadth-first search algorithm.
|
|
268
346
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
|
|
@@ -330,26 +408,32 @@ class AbstractGraph {
|
|
|
330
408
|
}
|
|
331
409
|
}
|
|
332
410
|
/**
|
|
333
|
-
*
|
|
411
|
+
* Dijkstra algorithm time: O(VE) space: O(VO + EO)
|
|
334
412
|
* /
|
|
335
413
|
|
|
336
414
|
/**
|
|
337
|
-
*
|
|
338
|
-
*
|
|
339
|
-
* a graph without using a heap data structure.
|
|
340
|
-
* @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
341
|
-
* vertex object or a vertex ID.
|
|
342
|
-
* @param {VO | VertexKey | null} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
343
|
-
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
344
|
-
* identifier. If no destination is provided, the value is set to `null`.
|
|
345
|
-
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
346
|
-
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
347
|
-
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
348
|
-
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
349
|
-
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
350
|
-
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
351
|
-
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
|
|
415
|
+
* Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
|
|
416
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
352
417
|
*/
|
|
418
|
+
/**
|
|
419
|
+
* Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
|
|
420
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
421
|
+
*
|
|
422
|
+
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
|
|
423
|
+
* a graph without using a heap data structure.
|
|
424
|
+
* @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
425
|
+
* vertex object or a vertex ID.
|
|
426
|
+
* @param {VO | VertexKey | null} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
427
|
+
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
428
|
+
* identifier. If no destination is provided, the value is set to `null`.
|
|
429
|
+
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
430
|
+
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
431
|
+
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
432
|
+
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
433
|
+
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
434
|
+
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
435
|
+
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
|
|
436
|
+
*/
|
|
353
437
|
dijkstraWithoutHeap(src, dest, getMinDist, genPaths) {
|
|
354
438
|
if (getMinDist === undefined)
|
|
355
439
|
getMinDist = false;
|
|
@@ -453,7 +537,7 @@ class AbstractGraph {
|
|
|
453
537
|
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
454
538
|
}
|
|
455
539
|
/**
|
|
456
|
-
*
|
|
540
|
+
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
457
541
|
*
|
|
458
542
|
* 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.
|
|
459
543
|
* 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.
|
|
@@ -462,22 +546,29 @@ class AbstractGraph {
|
|
|
462
546
|
* /
|
|
463
547
|
|
|
464
548
|
/**
|
|
465
|
-
*
|
|
466
|
-
*
|
|
467
|
-
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
468
|
-
* @param {VO | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
469
|
-
* start. It can be either a vertex object or a vertex ID.
|
|
470
|
-
* @param {VO | VertexKey | null} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
471
|
-
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
472
|
-
* will calculate the shortest paths to all other vertices from the source vertex.
|
|
473
|
-
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
474
|
-
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
475
|
-
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
476
|
-
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
477
|
-
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
478
|
-
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
479
|
-
* @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
|
|
549
|
+
* Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
|
|
550
|
+
* Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
|
|
480
551
|
*/
|
|
552
|
+
/**
|
|
553
|
+
* Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
|
|
554
|
+
* Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
|
|
555
|
+
*
|
|
556
|
+
* 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.
|
|
557
|
+
* The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
|
|
558
|
+
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
559
|
+
* @param {VO | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
560
|
+
* start. It can be either a vertex object or a vertex ID.
|
|
561
|
+
* @param {VO | VertexKey | null} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
562
|
+
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
563
|
+
* will calculate the shortest paths to all other vertices from the source vertex.
|
|
564
|
+
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
565
|
+
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
566
|
+
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
567
|
+
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
568
|
+
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
569
|
+
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
570
|
+
* @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
|
|
571
|
+
*/
|
|
481
572
|
dijkstra(src, dest, getMinDist, genPaths) {
|
|
482
573
|
var _a;
|
|
483
574
|
if (getMinDist === undefined)
|
|
@@ -581,12 +672,15 @@ class AbstractGraph {
|
|
|
581
672
|
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
582
673
|
}
|
|
583
674
|
/**
|
|
584
|
-
*
|
|
675
|
+
* Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
|
|
676
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
|
|
585
677
|
* one to rest pairs
|
|
586
678
|
* /
|
|
587
679
|
|
|
588
680
|
/**
|
|
589
|
-
*
|
|
681
|
+
* Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
|
|
682
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
|
|
683
|
+
*
|
|
590
684
|
* one to rest pairs
|
|
591
685
|
* 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.
|
|
592
686
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
@@ -701,13 +795,18 @@ class AbstractGraph {
|
|
|
701
795
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
702
796
|
*/
|
|
703
797
|
/**
|
|
704
|
-
*
|
|
798
|
+
* Time Complexity: O(V^3) - Cubic time (Floyd-Warshall algorithm).
|
|
799
|
+
* Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
|
|
800
|
+
* Not support graph with negative weight cycle
|
|
705
801
|
* all pairs
|
|
706
802
|
* 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.
|
|
707
803
|
* /
|
|
708
804
|
|
|
709
805
|
/**
|
|
710
|
-
*
|
|
806
|
+
* Time Complexity: O(V^3) - Cubic time (Floyd-Warshall algorithm).
|
|
807
|
+
* Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
|
|
808
|
+
*
|
|
809
|
+
* Not support graph with negative weight cycle
|
|
711
810
|
* all pairs
|
|
712
811
|
* 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.
|
|
713
812
|
* The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a
|
|
@@ -749,6 +848,8 @@ class AbstractGraph {
|
|
|
749
848
|
return { costs, predecessor };
|
|
750
849
|
}
|
|
751
850
|
/**
|
|
851
|
+
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
|
|
852
|
+
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
752
853
|
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
753
854
|
* Tarjan can find cycles in directed or undirected graph
|
|
754
855
|
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
@@ -757,6 +858,9 @@ class AbstractGraph {
|
|
|
757
858
|
* /
|
|
758
859
|
|
|
759
860
|
/**
|
|
861
|
+
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
|
|
862
|
+
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
863
|
+
*
|
|
760
864
|
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
761
865
|
* Tarjan can find cycles in directed or undirected graph
|
|
762
866
|
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
@@ -872,6 +976,13 @@ class AbstractGraph {
|
|
|
872
976
|
return { dfnMap, lowMap, bridges, cutVertexes, SCCs, cycles };
|
|
873
977
|
}
|
|
874
978
|
/**
|
|
979
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
980
|
+
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
981
|
+
*/
|
|
982
|
+
/**
|
|
983
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
984
|
+
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
985
|
+
*
|
|
875
986
|
* The function returns a map that associates each vertex object with its corresponding depth-first
|
|
876
987
|
* number.
|
|
877
988
|
* @returns A Map object with keys of type VO and values of type number.
|
|
@@ -66,6 +66,13 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
66
66
|
*/
|
|
67
67
|
createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO;
|
|
68
68
|
/**
|
|
69
|
+
* Time Complexity: O(|V|) where |V| is the number of vertices
|
|
70
|
+
* Space Complexity: O(1)
|
|
71
|
+
*/
|
|
72
|
+
/**
|
|
73
|
+
* Time Complexity: O(|V|) where |V| is the number of vertices
|
|
74
|
+
* Space Complexity: O(1)
|
|
75
|
+
*
|
|
69
76
|
* The `getEdge` function retrieves an edge between two vertices based on their source and destination IDs.
|
|
70
77
|
* @param {VO | VertexKey | null} srcOrKey - The source vertex or its ID. It can be either a vertex object or a vertex ID.
|
|
71
78
|
* @param {VO | VertexKey | null} destOrKey - The `destOrKey` parameter in the `getEdge` function represents the
|
|
@@ -75,6 +82,13 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
75
82
|
*/
|
|
76
83
|
getEdge(srcOrKey: VO | VertexKey | null, destOrKey: VO | VertexKey | null): EO | null;
|
|
77
84
|
/**
|
|
85
|
+
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
86
|
+
* Space Complexity: O(1)
|
|
87
|
+
*/
|
|
88
|
+
/**
|
|
89
|
+
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
90
|
+
* Space Complexity: O(1)
|
|
91
|
+
*
|
|
78
92
|
* The function removes an edge between two vertices in a graph and returns the removed edge.
|
|
79
93
|
* @param {VO | VertexKey} srcOrKey - The source vertex or its ID.
|
|
80
94
|
* @param {VO | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
|
|
@@ -82,6 +96,13 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
82
96
|
*/
|
|
83
97
|
deleteEdgeSrcToDest(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO | null;
|
|
84
98
|
/**
|
|
99
|
+
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
100
|
+
* Space Complexity: O(1)
|
|
101
|
+
*/
|
|
102
|
+
/**
|
|
103
|
+
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
104
|
+
* Space Complexity: O(1)
|
|
105
|
+
*
|
|
85
106
|
* The function removes an edge from a graph and returns the removed edge, or null if the edge was not found.
|
|
86
107
|
* @param {EO} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
|
|
87
108
|
* and `dest`, which represent the source and destination vertices of the edge, respectively.
|
|
@@ -89,6 +110,13 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
89
110
|
*/
|
|
90
111
|
deleteEdge(edge: EO): EO | null;
|
|
91
112
|
/**
|
|
113
|
+
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
114
|
+
* Space Complexity: O(1)
|
|
115
|
+
*/
|
|
116
|
+
/**
|
|
117
|
+
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
118
|
+
* Space Complexity: O(1)
|
|
119
|
+
*
|
|
92
120
|
* The function removes edges between two vertices and returns the removed edges.
|
|
93
121
|
* @param {VertexKey | VO} v1 - The parameter `v1` can be either a `VertexKey` or a `VO`. A `VertexKey` represents the
|
|
94
122
|
* unique identifier of a vertex in a graph, while `VO` represents the actual vertex object.
|
|
@@ -98,6 +126,13 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
98
126
|
*/
|
|
99
127
|
deleteEdgesBetween(v1: VertexKey | VO, v2: VertexKey | VO): EO[];
|
|
100
128
|
/**
|
|
129
|
+
* Time Complexity: O(1)
|
|
130
|
+
* Space Complexity: O(1)
|
|
131
|
+
*/
|
|
132
|
+
/**
|
|
133
|
+
* Time Complexity: O(1)
|
|
134
|
+
* Space Complexity: O(1)
|
|
135
|
+
*
|
|
101
136
|
* The function `incomingEdgesOf` returns an array of incoming edges for a given vertex or vertex ID.
|
|
102
137
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
103
138
|
* (`VertexKey`).
|
|
@@ -105,6 +140,13 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
105
140
|
*/
|
|
106
141
|
incomingEdgesOf(vertexOrKey: VO | VertexKey): EO[];
|
|
107
142
|
/**
|
|
143
|
+
* Time Complexity: O(1)
|
|
144
|
+
* Space Complexity: O(1)
|
|
145
|
+
*/
|
|
146
|
+
/**
|
|
147
|
+
* Time Complexity: O(1)
|
|
148
|
+
* Space Complexity: O(1)
|
|
149
|
+
*
|
|
108
150
|
* The function `outgoingEdgesOf` returns an array of outgoing edges from a given vertex or vertex ID.
|
|
109
151
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can accept either a vertex object (`VO`) or a vertex ID
|
|
110
152
|
* (`VertexKey`).
|
|
@@ -112,42 +154,91 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
112
154
|
*/
|
|
113
155
|
outgoingEdgesOf(vertexOrKey: VO | VertexKey): EO[];
|
|
114
156
|
/**
|
|
157
|
+
* Time Complexity: O(1)
|
|
158
|
+
* Space Complexity: O(1)
|
|
159
|
+
*/
|
|
160
|
+
/**
|
|
161
|
+
* Time Complexity: O(1)
|
|
162
|
+
* Space Complexity: O(1)
|
|
163
|
+
*
|
|
115
164
|
* The function "degreeOf" returns the total degree of a vertex, which is the sum of its out-degree and in-degree.
|
|
116
165
|
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
117
166
|
* @returns The sum of the out-degree and in-degree of the specified vertex or vertex ID.
|
|
118
167
|
*/
|
|
119
168
|
degreeOf(vertexOrKey: VertexKey | VO): number;
|
|
120
169
|
/**
|
|
170
|
+
* Time Complexity: O(1)
|
|
171
|
+
* Space Complexity: O(1)
|
|
172
|
+
*/
|
|
173
|
+
/**
|
|
174
|
+
* Time Complexity: O(1)
|
|
175
|
+
* Space Complexity: O(1)
|
|
176
|
+
*
|
|
121
177
|
* The function "inDegreeOf" returns the number of incoming edges for a given vertex.
|
|
122
178
|
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
123
179
|
* @returns The number of incoming edges of the specified vertex or vertex ID.
|
|
124
180
|
*/
|
|
125
181
|
inDegreeOf(vertexOrKey: VertexKey | VO): number;
|
|
126
182
|
/**
|
|
183
|
+
* Time Complexity: O(1)
|
|
184
|
+
* Space Complexity: O(1)
|
|
185
|
+
*/
|
|
186
|
+
/**
|
|
187
|
+
* Time Complexity: O(1)
|
|
188
|
+
* Space Complexity: O(1)
|
|
189
|
+
*
|
|
127
190
|
* The function `outDegreeOf` returns the number of outgoing edges from a given vertex.
|
|
128
191
|
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
129
192
|
* @returns The number of outgoing edges from the specified vertex or vertex ID.
|
|
130
193
|
*/
|
|
131
194
|
outDegreeOf(vertexOrKey: VertexKey | VO): number;
|
|
132
195
|
/**
|
|
196
|
+
* Time Complexity: O(1)
|
|
197
|
+
* Space Complexity: O(1)
|
|
198
|
+
*/
|
|
199
|
+
/**
|
|
200
|
+
* Time Complexity: O(1)
|
|
201
|
+
* Space Complexity: O(1)
|
|
202
|
+
*
|
|
133
203
|
* The function "edgesOf" returns an array of both outgoing and incoming edges of a given vertex or vertex ID.
|
|
134
204
|
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
135
205
|
* @returns The function `edgesOf` returns an array of edges.
|
|
136
206
|
*/
|
|
137
207
|
edgesOf(vertexOrKey: VertexKey | VO): EO[];
|
|
138
208
|
/**
|
|
209
|
+
* Time Complexity: O(1)
|
|
210
|
+
* Space Complexity: O(1)
|
|
211
|
+
*/
|
|
212
|
+
/**
|
|
213
|
+
* Time Complexity: O(1)
|
|
214
|
+
* Space Complexity: O(1)
|
|
215
|
+
*
|
|
139
216
|
* The function "getEdgeSrc" returns the source vertex of an edge, or null if the edge does not exist.
|
|
140
217
|
* @param {EO} e - The parameter "e" is of type EO, which represents an edge in a graph.
|
|
141
218
|
* @returns either a vertex object (VO) or null.
|
|
142
219
|
*/
|
|
143
220
|
getEdgeSrc(e: EO): VO | null;
|
|
144
221
|
/**
|
|
222
|
+
* Time Complexity: O(1)
|
|
223
|
+
* Space Complexity: O(1)
|
|
224
|
+
*/
|
|
225
|
+
/**
|
|
226
|
+
* Time Complexity: O(1)
|
|
227
|
+
* Space Complexity: O(1)
|
|
228
|
+
*
|
|
145
229
|
* The function "getEdgeDest" returns the destination vertex of an edge.
|
|
146
230
|
* @param {EO} e - The parameter "e" is of type "EO", which represents an edge in a graph.
|
|
147
231
|
* @returns either a vertex object of type VO or null.
|
|
148
232
|
*/
|
|
149
233
|
getEdgeDest(e: EO): VO | null;
|
|
150
234
|
/**
|
|
235
|
+
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
236
|
+
* Space Complexity: O(1)
|
|
237
|
+
*/
|
|
238
|
+
/**
|
|
239
|
+
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
240
|
+
* Space Complexity: O(1)
|
|
241
|
+
*
|
|
151
242
|
* The function `getDestinations` returns an array of destination vertices connected to a given vertex.
|
|
152
243
|
* @param {VO | VertexKey | null} vertex - The `vertex` parameter represents the starting vertex from which we want to
|
|
153
244
|
* find the destinations. It can be either a `VO` object, a `VertexKey` value, or `null`.
|
|
@@ -155,6 +246,13 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
155
246
|
*/
|
|
156
247
|
getDestinations(vertex: VO | VertexKey | null): VO[];
|
|
157
248
|
/**
|
|
249
|
+
* Time Complexity: O(|V| + |E|) where |V| is the number of vertices and |E| is the number of edges
|
|
250
|
+
* Space Complexity: O(|V|)
|
|
251
|
+
*/
|
|
252
|
+
/**
|
|
253
|
+
* Time Complexity: O(|V| + |E|) where |V| is the number of vertices and |E| is the number of edges
|
|
254
|
+
* Space Complexity: O(|V|)
|
|
255
|
+
*
|
|
158
256
|
* The `topologicalSort` function performs a topological sort on a graph and returns an array of vertices or vertex IDs
|
|
159
257
|
* in the sorted order, or null if the graph contains a cycle.
|
|
160
258
|
* @param {'vertex' | 'key'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
|
|
@@ -164,11 +262,25 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
164
262
|
*/
|
|
165
263
|
topologicalSort(propertyName?: 'vertex' | 'key'): Array<VO | VertexKey> | null;
|
|
166
264
|
/**
|
|
265
|
+
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
266
|
+
* Space Complexity: O(|E|)
|
|
267
|
+
*/
|
|
268
|
+
/**
|
|
269
|
+
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
270
|
+
* Space Complexity: O(|E|)
|
|
271
|
+
*
|
|
167
272
|
* The `edgeSet` function returns an array of all the edges in the graph.
|
|
168
273
|
* @returns The `edgeSet()` method returns an array of edges (`EO[]`).
|
|
169
274
|
*/
|
|
170
275
|
edgeSet(): EO[];
|
|
171
276
|
/**
|
|
277
|
+
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
278
|
+
* Space Complexity: O(1)
|
|
279
|
+
*/
|
|
280
|
+
/**
|
|
281
|
+
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
282
|
+
* Space Complexity: O(1)
|
|
283
|
+
*
|
|
172
284
|
* The function `getNeighbors` returns an array of neighboring vertices of a given vertex or vertex ID in a graph.
|
|
173
285
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
174
286
|
* (`VertexKey`).
|
|
@@ -176,6 +288,13 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
176
288
|
*/
|
|
177
289
|
getNeighbors(vertexOrKey: VO | VertexKey): VO[];
|
|
178
290
|
/**
|
|
291
|
+
* Time Complexity: O(1)
|
|
292
|
+
* Space Complexity: O(1)
|
|
293
|
+
*/
|
|
294
|
+
/**
|
|
295
|
+
* Time Complexity: O(1)
|
|
296
|
+
* Space Complexity: O(1)
|
|
297
|
+
*
|
|
179
298
|
* The function "getEndsOfEdge" returns the source and destination vertices of an edge if it exists in the graph,
|
|
180
299
|
* otherwise it returns null.
|
|
181
300
|
* @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph.
|
|
@@ -184,6 +303,13 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
184
303
|
*/
|
|
185
304
|
getEndsOfEdge(edge: EO): [VO, VO] | null;
|
|
186
305
|
/**
|
|
306
|
+
* Time Complexity: O(1)
|
|
307
|
+
* Space Complexity: O(1)
|
|
308
|
+
*/
|
|
309
|
+
/**
|
|
310
|
+
* Time Complexity: O(1)
|
|
311
|
+
* Space Complexity: O(1)
|
|
312
|
+
*
|
|
187
313
|
* The function `_addEdgeOnly` adds an edge to a graph if the source and destination vertices exist.
|
|
188
314
|
* @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph. It is the edge that
|
|
189
315
|
* needs to be added to the graph.
|