data-structure-typed 1.39.3 → 1.39.5
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/CHANGELOG.md +2 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +4 -2
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +5 -13
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +17 -25
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.js +6 -6
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js +2 -2
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +88 -88
- package/dist/cjs/data-structures/graph/abstract-graph.js +41 -41
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +63 -63
- package/dist/cjs/data-structures/graph/directed-graph.js +36 -36
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +10 -10
- package/dist/cjs/data-structures/graph/map-graph.js +7 -7
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +38 -38
- package/dist/cjs/data-structures/graph/undirected-graph.js +21 -21
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/cjs/data-structures/queue/queue.js +3 -3
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/interfaces/graph.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +4 -2
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +5 -13
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +17 -25
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/bst.js +6 -6
- package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/tree-multiset.js +2 -2
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +88 -88
- package/dist/mjs/data-structures/graph/abstract-graph.js +41 -41
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +63 -63
- package/dist/mjs/data-structures/graph/directed-graph.js +36 -36
- package/dist/mjs/data-structures/graph/map-graph.d.ts +10 -10
- package/dist/mjs/data-structures/graph/map-graph.js +7 -7
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +38 -38
- package/dist/mjs/data-structures/graph/undirected-graph.js +21 -21
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/mjs/data-structures/queue/queue.js +3 -3
- package/dist/mjs/interfaces/graph.d.ts +3 -3
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -5
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-tree.ts +19 -28
- package/src/data-structures/binary-tree/bst.ts +6 -6
- package/src/data-structures/binary-tree/tree-multiset.ts +2 -2
- package/src/data-structures/graph/abstract-graph.ts +135 -133
- package/src/data-structures/graph/directed-graph.ts +92 -87
- package/src/data-structures/graph/map-graph.ts +17 -20
- package/src/data-structures/graph/undirected-graph.ts +56 -54
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/graph.ts +3 -3
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +0 -1
- package/test/unit/data-structures/graph/directed-graph.test.ts +20 -15
- package/test/unit/data-structures/graph/map-graph.test.ts +23 -23
- package/test/unit/data-structures/graph/undirected-graph.test.ts +2 -2
- package/test/unit/data-structures/queue/queue.test.ts +8 -8
|
@@ -16,20 +16,20 @@ export declare abstract class AbstractVertex<V = any> {
|
|
|
16
16
|
get val(): V | undefined;
|
|
17
17
|
set val(value: V | undefined);
|
|
18
18
|
}
|
|
19
|
-
export declare abstract class AbstractEdge<
|
|
19
|
+
export declare abstract class AbstractEdge<VO = any> {
|
|
20
20
|
/**
|
|
21
21
|
* The above function is a protected constructor that initializes the weight, value, and hash code properties of an
|
|
22
22
|
* object.
|
|
23
23
|
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the object. If
|
|
24
24
|
* a value is provided, it will be assigned to the `_weight` property. If no value is provided, the default value of 1
|
|
25
25
|
* will be assigned.
|
|
26
|
-
* @param {
|
|
26
|
+
* @param {VO} [val] - The `val` parameter is of type `VO`, which means it can be any type. It is an optional parameter,
|
|
27
27
|
* meaning it can be omitted when creating an instance of the class.
|
|
28
28
|
*/
|
|
29
|
-
protected constructor(weight?: number, val?:
|
|
29
|
+
protected constructor(weight?: number, val?: VO);
|
|
30
30
|
private _val;
|
|
31
|
-
get val():
|
|
32
|
-
set val(value:
|
|
31
|
+
get val(): VO | undefined;
|
|
32
|
+
set val(value: VO | undefined);
|
|
33
33
|
private _weight;
|
|
34
34
|
get weight(): number;
|
|
35
35
|
set weight(v: number);
|
|
@@ -46,16 +46,16 @@ export declare abstract class AbstractEdge<V = any> {
|
|
|
46
46
|
*/
|
|
47
47
|
protected _setHashCode(v: string): void;
|
|
48
48
|
}
|
|
49
|
-
export declare abstract class AbstractGraph<V extends AbstractVertex<
|
|
49
|
+
export declare abstract class AbstractGraph<V = any, E = any, VO extends AbstractVertex<V> = AbstractVertex<V>, EO extends AbstractEdge<E> = AbstractEdge<E>> implements IGraph<V, E, VO, EO> {
|
|
50
50
|
private _vertices;
|
|
51
|
-
get vertices(): Map<VertexKey,
|
|
51
|
+
get vertices(): Map<VertexKey, VO>;
|
|
52
52
|
/**
|
|
53
53
|
* 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.
|
|
54
54
|
* 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.
|
|
55
55
|
* @param key
|
|
56
56
|
* @param val
|
|
57
57
|
*/
|
|
58
|
-
abstract createVertex(key: VertexKey, val?: V):
|
|
58
|
+
abstract createVertex(key: VertexKey, val?: V): VO;
|
|
59
59
|
/**
|
|
60
60
|
* 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.
|
|
61
61
|
* 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.
|
|
@@ -64,14 +64,14 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
64
64
|
* @param weight
|
|
65
65
|
* @param val
|
|
66
66
|
*/
|
|
67
|
-
abstract createEdge(srcOrV1: VertexKey | string, destOrV2: VertexKey | string, weight?: number, val?: E):
|
|
68
|
-
abstract deleteEdge(edge:
|
|
69
|
-
abstract getEdge(srcOrKey:
|
|
70
|
-
abstract degreeOf(vertexOrKey:
|
|
71
|
-
abstract edgeSet():
|
|
72
|
-
abstract edgesOf(vertexOrKey:
|
|
73
|
-
abstract getNeighbors(vertexOrKey:
|
|
74
|
-
abstract getEndsOfEdge(edge:
|
|
67
|
+
abstract createEdge(srcOrV1: VertexKey | string, destOrV2: VertexKey | string, weight?: number, val?: E): EO;
|
|
68
|
+
abstract deleteEdge(edge: EO): EO | null;
|
|
69
|
+
abstract getEdge(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO | null;
|
|
70
|
+
abstract degreeOf(vertexOrKey: VO | VertexKey): number;
|
|
71
|
+
abstract edgeSet(): EO[];
|
|
72
|
+
abstract edgesOf(vertexOrKey: VO | VertexKey): EO[];
|
|
73
|
+
abstract getNeighbors(vertexOrKey: VO | VertexKey): VO[];
|
|
74
|
+
abstract getEndsOfEdge(edge: EO): [VO, VO] | null;
|
|
75
75
|
/**
|
|
76
76
|
* The function "getVertex" returns the vertex with the specified ID or null if it doesn't exist.
|
|
77
77
|
* @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
|
|
@@ -79,73 +79,73 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
79
79
|
* @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertices`
|
|
80
80
|
* map. If the vertex does not exist, it returns `null`.
|
|
81
81
|
*/
|
|
82
|
-
getVertex(vertexKey: VertexKey):
|
|
82
|
+
getVertex(vertexKey: VertexKey): VO | null;
|
|
83
83
|
/**
|
|
84
84
|
* The function checks if a vertex exists in a graph.
|
|
85
|
-
* @param {
|
|
85
|
+
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
86
86
|
* (`VertexKey`).
|
|
87
87
|
* @returns a boolean value.
|
|
88
88
|
*/
|
|
89
|
-
hasVertex(vertexOrKey:
|
|
90
|
-
addVertex(vertex:
|
|
91
|
-
addVertex(key: VertexKey, val?: V
|
|
89
|
+
hasVertex(vertexOrKey: VO | VertexKey): boolean;
|
|
90
|
+
addVertex(vertex: VO): boolean;
|
|
91
|
+
addVertex(key: VertexKey, val?: V): boolean;
|
|
92
92
|
/**
|
|
93
93
|
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
94
|
-
* @param {
|
|
94
|
+
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
95
95
|
* (`VertexKey`).
|
|
96
96
|
* @returns The method is returning a boolean value.
|
|
97
97
|
*/
|
|
98
|
-
deleteVertex(vertexOrKey:
|
|
98
|
+
deleteVertex(vertexOrKey: VO | VertexKey): boolean;
|
|
99
99
|
/**
|
|
100
100
|
* The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
|
|
101
|
-
* @param {
|
|
101
|
+
* @param {VO[] | VertexKey[]} vertices - The `vertices` parameter can be either an array of vertices (`VO[]`) or an array
|
|
102
102
|
* of vertex IDs (`VertexKey[]`).
|
|
103
103
|
* @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
|
|
104
104
|
* were removed.
|
|
105
105
|
*/
|
|
106
|
-
removeManyVertices(vertices:
|
|
106
|
+
removeManyVertices(vertices: VO[] | VertexKey[]): boolean;
|
|
107
107
|
/**
|
|
108
108
|
* The function checks if there is an edge between two vertices and returns a boolean value indicating the result.
|
|
109
|
-
* @param {VertexKey |
|
|
110
|
-
* identifier of a vertex in a graph, while
|
|
111
|
-
* @param {VertexKey |
|
|
112
|
-
* `VertexKey` or a `
|
|
109
|
+
* @param {VertexKey | VO} v1 - The parameter v1 can be either a VertexKey or a VO. A VertexKey represents the unique
|
|
110
|
+
* identifier of a vertex in a graph, while VO represents the type of the vertex object itself.
|
|
111
|
+
* @param {VertexKey | VO} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
|
|
112
|
+
* `VertexKey` or a `VO` type, which represents the type of the vertex.
|
|
113
113
|
* @returns A boolean value is being returned.
|
|
114
114
|
*/
|
|
115
|
-
hasEdge(v1: VertexKey |
|
|
116
|
-
addEdge(edge:
|
|
117
|
-
addEdge(src:
|
|
115
|
+
hasEdge(v1: VertexKey | VO, v2: VertexKey | VO): boolean;
|
|
116
|
+
addEdge(edge: EO): boolean;
|
|
117
|
+
addEdge(src: VO | VertexKey, dest: VO | VertexKey, weight?: number, val?: E): boolean;
|
|
118
118
|
/**
|
|
119
119
|
* The function sets the weight of an edge between two vertices in a graph.
|
|
120
|
-
* @param {VertexKey |
|
|
120
|
+
* @param {VertexKey | VO} srcOrKey - The `srcOrKey` parameter can be either a `VertexKey` or a `VO` object. It represents
|
|
121
121
|
* the source vertex of the edge.
|
|
122
|
-
* @param {VertexKey |
|
|
123
|
-
* either a `VertexKey` or a vertex object `
|
|
122
|
+
* @param {VertexKey | VO} destOrKey - The `destOrKey` parameter represents the destination vertex of the edge. It can be
|
|
123
|
+
* either a `VertexKey` or a vertex object `VO`.
|
|
124
124
|
* @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrKey)
|
|
125
125
|
* and the destination vertex (destOrKey).
|
|
126
126
|
* @returns a boolean value. If the edge exists between the source and destination vertices, the function will update
|
|
127
127
|
* the weight of the edge and return true. If the edge does not exist, the function will return false.
|
|
128
128
|
*/
|
|
129
|
-
setEdgeWeight(srcOrKey: VertexKey |
|
|
129
|
+
setEdgeWeight(srcOrKey: VertexKey | VO, destOrKey: VertexKey | VO, weight: number): boolean;
|
|
130
130
|
/**
|
|
131
131
|
* The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
|
|
132
|
-
* @param {
|
|
132
|
+
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
133
133
|
* It is the starting vertex for finding paths.
|
|
134
|
-
* @param {
|
|
135
|
-
* @returns The function `getAllPathsBetween` returns an array of arrays of vertices (`
|
|
134
|
+
* @param {VO | VertexKey} v2 - The parameter `v2` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
135
|
+
* @returns The function `getAllPathsBetween` returns an array of arrays of vertices (`VO[][]`).
|
|
136
136
|
*/
|
|
137
|
-
getAllPathsBetween(v1:
|
|
137
|
+
getAllPathsBetween(v1: VO | VertexKey, v2: VO | VertexKey): VO[][];
|
|
138
138
|
/**
|
|
139
139
|
* The function calculates the sum of weights along a given path.
|
|
140
|
-
* @param {
|
|
140
|
+
* @param {VO[]} path - An array of vertices (VO) representing a path in a graph.
|
|
141
141
|
* @returns The function `getPathSumWeight` returns the sum of the weights of the edges in the given path.
|
|
142
142
|
*/
|
|
143
|
-
getPathSumWeight(path:
|
|
143
|
+
getPathSumWeight(path: VO[]): number;
|
|
144
144
|
/**
|
|
145
145
|
* The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
|
|
146
146
|
* weights or using a breadth-first search algorithm.
|
|
147
|
-
* @param {
|
|
148
|
-
* @param {
|
|
147
|
+
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex or its ID.
|
|
148
|
+
* @param {VO | VertexKey} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
|
|
149
149
|
* you want to find the minimum cost or weight from the source vertex `v1`.
|
|
150
150
|
* @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edges have weights.
|
|
151
151
|
* If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
|
|
@@ -155,32 +155,32 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
155
155
|
* vertices. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
|
|
156
156
|
* minimum number of
|
|
157
157
|
*/
|
|
158
|
-
getMinCostBetween(v1:
|
|
158
|
+
getMinCostBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean): number | null;
|
|
159
159
|
/**
|
|
160
160
|
* The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
|
|
161
161
|
* using a breadth-first search algorithm.
|
|
162
|
-
* @param {
|
|
163
|
-
* object (`
|
|
164
|
-
* @param {
|
|
162
|
+
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
|
|
163
|
+
* object (`VO`) or a vertex ID (`VertexKey`).
|
|
164
|
+
* @param {VO | VertexKey} v2 - VO | VertexKey - The second vertex or vertex ID between which we want to find the minimum
|
|
165
165
|
* path.
|
|
166
166
|
* @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edges in finding the
|
|
167
167
|
* minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
|
|
168
168
|
* to false, the function will use breadth-first search (BFS) to find the minimum path.
|
|
169
|
-
* @returns The function `getMinPathBetween` returns an array of vertices (`
|
|
169
|
+
* @returns The function `getMinPathBetween` returns an array of vertices (`VO[]`) representing the minimum path between
|
|
170
170
|
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `null`.
|
|
171
171
|
*/
|
|
172
|
-
getMinPathBetween(v1:
|
|
172
|
+
getMinPathBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean): VO[] | null;
|
|
173
173
|
/**
|
|
174
|
-
* Dijkstra algorithm time: O(VE) space: O(
|
|
174
|
+
* Dijkstra algorithm time: O(VE) space: O(VO + EO)
|
|
175
175
|
* /
|
|
176
176
|
|
|
177
177
|
/**
|
|
178
|
-
* Dijkstra algorithm time: O(VE) space: O(
|
|
178
|
+
* Dijkstra algorithm time: O(VE) space: O(VO + EO)
|
|
179
179
|
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
|
|
180
180
|
* a graph without using a heap data structure.
|
|
181
|
-
* @param {
|
|
181
|
+
* @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
182
182
|
* vertex object or a vertex ID.
|
|
183
|
-
* @param {
|
|
183
|
+
* @param {VO | VertexKey | null} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
184
184
|
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
185
185
|
* identifier. If no destination is provided, the value is set to `null`.
|
|
186
186
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
@@ -189,15 +189,15 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
189
189
|
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
190
190
|
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
191
191
|
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
192
|
-
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<
|
|
192
|
+
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
|
|
193
193
|
*/
|
|
194
|
-
dijkstraWithoutHeap(src:
|
|
194
|
+
dijkstraWithoutHeap(src: VO | VertexKey, dest?: VO | VertexKey | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<VO>;
|
|
195
195
|
/**
|
|
196
|
-
* Dijkstra algorithm time: O(logVE) space: O(
|
|
196
|
+
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
197
197
|
*
|
|
198
198
|
* 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.
|
|
199
199
|
* 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.
|
|
200
|
-
* 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(
|
|
200
|
+
* 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(VO^3), where VO is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
|
|
201
201
|
*
|
|
202
202
|
* /
|
|
203
203
|
|
|
@@ -205,9 +205,9 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
205
205
|
* 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.
|
|
206
206
|
* The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
|
|
207
207
|
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
208
|
-
* @param {
|
|
208
|
+
* @param {VO | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
209
209
|
* start. It can be either a vertex object or a vertex ID.
|
|
210
|
-
* @param {
|
|
210
|
+
* @param {VO | VertexKey | null} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
211
211
|
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
212
212
|
* will calculate the shortest paths to all other vertices from the source vertex.
|
|
213
213
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
@@ -216,21 +216,21 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
216
216
|
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
217
217
|
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
218
218
|
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
219
|
-
* @returns The function `dijkstra` returns an object of type `DijkstraResult<
|
|
219
|
+
* @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
|
|
220
220
|
*/
|
|
221
|
-
dijkstra(src:
|
|
221
|
+
dijkstra(src: VO | VertexKey, dest?: VO | VertexKey | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<VO>;
|
|
222
222
|
/**
|
|
223
|
-
* BellmanFord time:O(VE) space:O(
|
|
223
|
+
* BellmanFord time:O(VE) space:O(VO)
|
|
224
224
|
* one to rest pairs
|
|
225
225
|
* /
|
|
226
226
|
|
|
227
227
|
/**
|
|
228
|
-
* BellmanFord time:O(VE) space:O(
|
|
228
|
+
* BellmanFord time:O(VE) space:O(VO)
|
|
229
229
|
* one to rest pairs
|
|
230
230
|
* 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.
|
|
231
231
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
232
232
|
* all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.
|
|
233
|
-
* @param {
|
|
233
|
+
* @param {VO | VertexKey} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
|
|
234
234
|
* start calculating the shortest paths. It can be either a vertex object or a vertex ID.
|
|
235
235
|
* @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
|
|
236
236
|
* @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
|
|
@@ -240,40 +240,40 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
240
240
|
* vertex.
|
|
241
241
|
* @returns The function `bellmanFord` returns an object with the following properties:
|
|
242
242
|
*/
|
|
243
|
-
bellmanFord(src:
|
|
243
|
+
bellmanFord(src: VO | VertexKey, scanNegativeCycle?: boolean, getMin?: boolean, genPath?: boolean): {
|
|
244
244
|
hasNegativeCycle: boolean | undefined;
|
|
245
|
-
distMap: Map<
|
|
246
|
-
preMap: Map<
|
|
247
|
-
paths:
|
|
245
|
+
distMap: Map<VO, number>;
|
|
246
|
+
preMap: Map<VO, VO>;
|
|
247
|
+
paths: VO[][];
|
|
248
248
|
min: number;
|
|
249
|
-
minPath:
|
|
249
|
+
minPath: VO[];
|
|
250
250
|
};
|
|
251
251
|
/**
|
|
252
|
-
* Dijkstra algorithm time: O(logVE) space: O(
|
|
252
|
+
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
253
253
|
* /
|
|
254
254
|
|
|
255
255
|
/**
|
|
256
|
-
* Dijkstra algorithm time: O(logVE) space: O(
|
|
256
|
+
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
257
257
|
* 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.
|
|
258
258
|
*/
|
|
259
259
|
/**
|
|
260
|
-
* BellmanFord time:O(VE) space:O(
|
|
260
|
+
* BellmanFord time:O(VE) space:O(VO)
|
|
261
261
|
* one to rest pairs
|
|
262
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
263
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
264
264
|
*/
|
|
265
265
|
/**
|
|
266
|
-
* Floyd algorithm time: O(
|
|
266
|
+
* Floyd algorithm time: O(VO^3) space: O(VO^2), not support graph with negative weight cycle
|
|
267
267
|
* all pairs
|
|
268
268
|
* 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.
|
|
269
269
|
*/
|
|
270
270
|
/**
|
|
271
|
-
* Floyd algorithm time: O(
|
|
271
|
+
* Floyd algorithm time: O(VO^3) space: O(VO^2), not support graph with negative weight cycle
|
|
272
272
|
* all pairs
|
|
273
273
|
* /
|
|
274
274
|
|
|
275
275
|
/**
|
|
276
|
-
* Floyd algorithm time: O(
|
|
276
|
+
* Floyd algorithm time: O(VO^3) space: O(VO^2), not support graph with negative weight cycle
|
|
277
277
|
* all pairs
|
|
278
278
|
* 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.
|
|
279
279
|
* The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a
|
|
@@ -285,7 +285,7 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
285
285
|
*/
|
|
286
286
|
floyd(): {
|
|
287
287
|
costs: number[][];
|
|
288
|
-
predecessor: (
|
|
288
|
+
predecessor: (VO | null)[][];
|
|
289
289
|
};
|
|
290
290
|
/**
|
|
291
291
|
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
@@ -317,16 +317,16 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
317
317
|
* @returns The function `tarjan` returns an object with the following properties:
|
|
318
318
|
*/
|
|
319
319
|
tarjan(needArticulationPoints?: boolean, needBridges?: boolean, needSCCs?: boolean, needCycles?: boolean): {
|
|
320
|
-
dfnMap: Map<
|
|
321
|
-
lowMap: Map<
|
|
322
|
-
bridges:
|
|
323
|
-
articulationPoints:
|
|
324
|
-
SCCs: Map<number,
|
|
325
|
-
cycles: Map<number,
|
|
320
|
+
dfnMap: Map<VO, number>;
|
|
321
|
+
lowMap: Map<VO, number>;
|
|
322
|
+
bridges: EO[];
|
|
323
|
+
articulationPoints: VO[];
|
|
324
|
+
SCCs: Map<number, VO[]>;
|
|
325
|
+
cycles: Map<number, VO[]>;
|
|
326
326
|
};
|
|
327
|
-
protected abstract _addEdgeOnly(edge:
|
|
328
|
-
protected _addVertexOnly(newVertex:
|
|
329
|
-
protected _getVertex(vertexOrKey: VertexKey |
|
|
330
|
-
protected _getVertexKey(vertexOrKey:
|
|
331
|
-
protected _setVertices(value: Map<VertexKey,
|
|
327
|
+
protected abstract _addEdgeOnly(edge: EO): boolean;
|
|
328
|
+
protected _addVertexOnly(newVertex: VO): boolean;
|
|
329
|
+
protected _getVertex(vertexOrKey: VertexKey | VO): VO | null;
|
|
330
|
+
protected _getVertexKey(vertexOrKey: VO | VertexKey): VertexKey;
|
|
331
|
+
protected _setVertices(value: Map<VertexKey, VO>): void;
|
|
332
332
|
}
|
|
@@ -46,7 +46,7 @@ class AbstractEdge {
|
|
|
46
46
|
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the object. If
|
|
47
47
|
* a value is provided, it will be assigned to the `_weight` property. If no value is provided, the default value of 1
|
|
48
48
|
* will be assigned.
|
|
49
|
-
* @param {
|
|
49
|
+
* @param {VO} [val] - The `val` parameter is of type `VO`, which means it can be any type. It is an optional parameter,
|
|
50
50
|
* meaning it can be omitted when creating an instance of the class.
|
|
51
51
|
*/
|
|
52
52
|
constructor(weight, val) {
|
|
@@ -103,7 +103,7 @@ class AbstractGraph {
|
|
|
103
103
|
}
|
|
104
104
|
/**
|
|
105
105
|
* The function checks if a vertex exists in a graph.
|
|
106
|
-
* @param {
|
|
106
|
+
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
107
107
|
* (`VertexKey`).
|
|
108
108
|
* @returns a boolean value.
|
|
109
109
|
*/
|
|
@@ -121,7 +121,7 @@ class AbstractGraph {
|
|
|
121
121
|
}
|
|
122
122
|
/**
|
|
123
123
|
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
124
|
-
* @param {
|
|
124
|
+
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
125
125
|
* (`VertexKey`).
|
|
126
126
|
* @returns The method is returning a boolean value.
|
|
127
127
|
*/
|
|
@@ -131,7 +131,7 @@ class AbstractGraph {
|
|
|
131
131
|
}
|
|
132
132
|
/**
|
|
133
133
|
* The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
|
|
134
|
-
* @param {
|
|
134
|
+
* @param {VO[] | VertexKey[]} vertices - The `vertices` parameter can be either an array of vertices (`VO[]`) or an array
|
|
135
135
|
* of vertex IDs (`VertexKey[]`).
|
|
136
136
|
* @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
|
|
137
137
|
* were removed.
|
|
@@ -145,10 +145,10 @@ class AbstractGraph {
|
|
|
145
145
|
}
|
|
146
146
|
/**
|
|
147
147
|
* The function checks if there is an edge between two vertices and returns a boolean value indicating the result.
|
|
148
|
-
* @param {VertexKey |
|
|
149
|
-
* identifier of a vertex in a graph, while
|
|
150
|
-
* @param {VertexKey |
|
|
151
|
-
* `VertexKey` or a `
|
|
148
|
+
* @param {VertexKey | VO} v1 - The parameter v1 can be either a VertexKey or a VO. A VertexKey represents the unique
|
|
149
|
+
* identifier of a vertex in a graph, while VO represents the type of the vertex object itself.
|
|
150
|
+
* @param {VertexKey | VO} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
|
|
151
|
+
* `VertexKey` or a `VO` type, which represents the type of the vertex.
|
|
152
152
|
* @returns A boolean value is being returned.
|
|
153
153
|
*/
|
|
154
154
|
hasEdge(v1, v2) {
|
|
@@ -177,10 +177,10 @@ class AbstractGraph {
|
|
|
177
177
|
}
|
|
178
178
|
/**
|
|
179
179
|
* The function sets the weight of an edge between two vertices in a graph.
|
|
180
|
-
* @param {VertexKey |
|
|
180
|
+
* @param {VertexKey | VO} srcOrKey - The `srcOrKey` parameter can be either a `VertexKey` or a `VO` object. It represents
|
|
181
181
|
* the source vertex of the edge.
|
|
182
|
-
* @param {VertexKey |
|
|
183
|
-
* either a `VertexKey` or a vertex object `
|
|
182
|
+
* @param {VertexKey | VO} destOrKey - The `destOrKey` parameter represents the destination vertex of the edge. It can be
|
|
183
|
+
* either a `VertexKey` or a vertex object `VO`.
|
|
184
184
|
* @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrKey)
|
|
185
185
|
* and the destination vertex (destOrKey).
|
|
186
186
|
* @returns a boolean value. If the edge exists between the source and destination vertices, the function will update
|
|
@@ -198,10 +198,10 @@ class AbstractGraph {
|
|
|
198
198
|
}
|
|
199
199
|
/**
|
|
200
200
|
* The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
|
|
201
|
-
* @param {
|
|
201
|
+
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
202
202
|
* It is the starting vertex for finding paths.
|
|
203
|
-
* @param {
|
|
204
|
-
* @returns The function `getAllPathsBetween` returns an array of arrays of vertices (`
|
|
203
|
+
* @param {VO | VertexKey} v2 - The parameter `v2` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
204
|
+
* @returns The function `getAllPathsBetween` returns an array of arrays of vertices (`VO[][]`).
|
|
205
205
|
*/
|
|
206
206
|
getAllPathsBetween(v1, v2) {
|
|
207
207
|
const paths = [];
|
|
@@ -230,7 +230,7 @@ class AbstractGraph {
|
|
|
230
230
|
}
|
|
231
231
|
/**
|
|
232
232
|
* The function calculates the sum of weights along a given path.
|
|
233
|
-
* @param {
|
|
233
|
+
* @param {VO[]} path - An array of vertices (VO) representing a path in a graph.
|
|
234
234
|
* @returns The function `getPathSumWeight` returns the sum of the weights of the edges in the given path.
|
|
235
235
|
*/
|
|
236
236
|
getPathSumWeight(path) {
|
|
@@ -243,8 +243,8 @@ class AbstractGraph {
|
|
|
243
243
|
/**
|
|
244
244
|
* The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
|
|
245
245
|
* weights or using a breadth-first search algorithm.
|
|
246
|
-
* @param {
|
|
247
|
-
* @param {
|
|
246
|
+
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex or its ID.
|
|
247
|
+
* @param {VO | VertexKey} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
|
|
248
248
|
* you want to find the minimum cost or weight from the source vertex `v1`.
|
|
249
249
|
* @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edges have weights.
|
|
250
250
|
* If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
|
|
@@ -301,14 +301,14 @@ class AbstractGraph {
|
|
|
301
301
|
/**
|
|
302
302
|
* The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
|
|
303
303
|
* using a breadth-first search algorithm.
|
|
304
|
-
* @param {
|
|
305
|
-
* object (`
|
|
306
|
-
* @param {
|
|
304
|
+
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
|
|
305
|
+
* object (`VO`) or a vertex ID (`VertexKey`).
|
|
306
|
+
* @param {VO | VertexKey} v2 - VO | VertexKey - The second vertex or vertex ID between which we want to find the minimum
|
|
307
307
|
* path.
|
|
308
308
|
* @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edges in finding the
|
|
309
309
|
* minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
|
|
310
310
|
* to false, the function will use breadth-first search (BFS) to find the minimum path.
|
|
311
|
-
* @returns The function `getMinPathBetween` returns an array of vertices (`
|
|
311
|
+
* @returns The function `getMinPathBetween` returns an array of vertices (`VO[]`) representing the minimum path between
|
|
312
312
|
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `null`.
|
|
313
313
|
*/
|
|
314
314
|
getMinPathBetween(v1, v2, isWeight) {
|
|
@@ -358,16 +358,16 @@ class AbstractGraph {
|
|
|
358
358
|
}
|
|
359
359
|
}
|
|
360
360
|
/**
|
|
361
|
-
* Dijkstra algorithm time: O(VE) space: O(
|
|
361
|
+
* Dijkstra algorithm time: O(VE) space: O(VO + EO)
|
|
362
362
|
* /
|
|
363
363
|
|
|
364
364
|
/**
|
|
365
|
-
* Dijkstra algorithm time: O(VE) space: O(
|
|
365
|
+
* Dijkstra algorithm time: O(VE) space: O(VO + EO)
|
|
366
366
|
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
|
|
367
367
|
* a graph without using a heap data structure.
|
|
368
|
-
* @param {
|
|
368
|
+
* @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
369
369
|
* vertex object or a vertex ID.
|
|
370
|
-
* @param {
|
|
370
|
+
* @param {VO | VertexKey | null} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
371
371
|
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
372
372
|
* identifier. If no destination is provided, the value is set to `null`.
|
|
373
373
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
@@ -376,7 +376,7 @@ class AbstractGraph {
|
|
|
376
376
|
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
377
377
|
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
378
378
|
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
379
|
-
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<
|
|
379
|
+
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
|
|
380
380
|
*/
|
|
381
381
|
dijkstraWithoutHeap(src, dest, getMinDist, genPaths) {
|
|
382
382
|
if (getMinDist === undefined)
|
|
@@ -481,11 +481,11 @@ class AbstractGraph {
|
|
|
481
481
|
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
482
482
|
}
|
|
483
483
|
/**
|
|
484
|
-
* Dijkstra algorithm time: O(logVE) space: O(
|
|
484
|
+
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
485
485
|
*
|
|
486
486
|
* 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.
|
|
487
487
|
* 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.
|
|
488
|
-
* 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(
|
|
488
|
+
* 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(VO^3), where VO is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
|
|
489
489
|
*
|
|
490
490
|
* /
|
|
491
491
|
|
|
@@ -493,9 +493,9 @@ class AbstractGraph {
|
|
|
493
493
|
* 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.
|
|
494
494
|
* The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
|
|
495
495
|
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
496
|
-
* @param {
|
|
496
|
+
* @param {VO | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
497
497
|
* start. It can be either a vertex object or a vertex ID.
|
|
498
|
-
* @param {
|
|
498
|
+
* @param {VO | VertexKey | null} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
499
499
|
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
500
500
|
* will calculate the shortest paths to all other vertices from the source vertex.
|
|
501
501
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
@@ -504,7 +504,7 @@ class AbstractGraph {
|
|
|
504
504
|
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
505
505
|
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
506
506
|
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
507
|
-
* @returns The function `dijkstra` returns an object of type `DijkstraResult<
|
|
507
|
+
* @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
|
|
508
508
|
*/
|
|
509
509
|
dijkstra(src, dest, getMinDist, genPaths) {
|
|
510
510
|
if (getMinDist === undefined)
|
|
@@ -536,7 +536,7 @@ class AbstractGraph {
|
|
|
536
536
|
preMap.set(srcVertex, null);
|
|
537
537
|
/**
|
|
538
538
|
* The function `getPaths` retrieves all paths from vertices to a specified minimum vertex.
|
|
539
|
-
* @param {
|
|
539
|
+
* @param {VO | null} minV - The parameter `minV` is of type `VO | null`. It represents the minimum vertex value or
|
|
540
540
|
* null.
|
|
541
541
|
*/
|
|
542
542
|
const getPaths = (minV) => {
|
|
@@ -608,17 +608,17 @@ class AbstractGraph {
|
|
|
608
608
|
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
609
609
|
}
|
|
610
610
|
/**
|
|
611
|
-
* BellmanFord time:O(VE) space:O(
|
|
611
|
+
* BellmanFord time:O(VE) space:O(VO)
|
|
612
612
|
* one to rest pairs
|
|
613
613
|
* /
|
|
614
614
|
|
|
615
615
|
/**
|
|
616
|
-
* BellmanFord time:O(VE) space:O(
|
|
616
|
+
* BellmanFord time:O(VE) space:O(VO)
|
|
617
617
|
* one to rest pairs
|
|
618
618
|
* 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.
|
|
619
619
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
620
620
|
* all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.
|
|
621
|
-
* @param {
|
|
621
|
+
* @param {VO | VertexKey} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
|
|
622
622
|
* start calculating the shortest paths. It can be either a vertex object or a vertex ID.
|
|
623
623
|
* @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
|
|
624
624
|
* @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
|
|
@@ -714,31 +714,31 @@ class AbstractGraph {
|
|
|
714
714
|
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
715
715
|
}
|
|
716
716
|
/**
|
|
717
|
-
* Dijkstra algorithm time: O(logVE) space: O(
|
|
717
|
+
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
718
718
|
* /
|
|
719
719
|
|
|
720
720
|
/**
|
|
721
|
-
* Dijkstra algorithm time: O(logVE) space: O(
|
|
721
|
+
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
722
722
|
* 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.
|
|
723
723
|
*/
|
|
724
724
|
/**
|
|
725
|
-
* BellmanFord time:O(VE) space:O(
|
|
725
|
+
* BellmanFord time:O(VE) space:O(VO)
|
|
726
726
|
* one to rest pairs
|
|
727
727
|
* 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.
|
|
728
728
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
729
729
|
*/
|
|
730
730
|
/**
|
|
731
|
-
* Floyd algorithm time: O(
|
|
731
|
+
* Floyd algorithm time: O(VO^3) space: O(VO^2), not support graph with negative weight cycle
|
|
732
732
|
* all pairs
|
|
733
733
|
* 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.
|
|
734
734
|
*/
|
|
735
735
|
/**
|
|
736
|
-
* Floyd algorithm time: O(
|
|
736
|
+
* Floyd algorithm time: O(VO^3) space: O(VO^2), not support graph with negative weight cycle
|
|
737
737
|
* all pairs
|
|
738
738
|
* /
|
|
739
739
|
|
|
740
740
|
/**
|
|
741
|
-
* Floyd algorithm time: O(
|
|
741
|
+
* Floyd algorithm time: O(VO^3) space: O(VO^2), not support graph with negative weight cycle
|
|
742
742
|
* all pairs
|
|
743
743
|
* 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.
|
|
744
744
|
* The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a
|