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
|
@@ -68,17 +68,23 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
/**
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
* @
|
|
71
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
72
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
77
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
78
|
+
*
|
|
79
|
+
* The `add` function adds a new node to the tree multimap, updating the count if the key already
|
|
80
|
+
* exists, and balances the tree if necessary.
|
|
81
|
+
* @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
|
|
82
|
+
* following types:
|
|
83
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
84
|
+
* being added to the tree. It is an optional parameter, so it can be omitted if not needed.
|
|
85
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
86
|
+
* times the key-value pair should be added to the multimap. If not provided, the default value is 1.
|
|
87
|
+
* @returns a node (`N`) or `undefined`.
|
|
82
88
|
*/
|
|
83
89
|
override add(keyOrNode: BTNKey | N | null | undefined, value?: V, count = 1): N | undefined {
|
|
84
90
|
if (keyOrNode === null) return undefined;
|
|
@@ -150,12 +156,24 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
150
156
|
}
|
|
151
157
|
|
|
152
158
|
/**
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
*
|
|
159
|
+
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
160
|
+
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
161
|
+
*/
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
165
|
+
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
166
|
+
*
|
|
167
|
+
* The function adds a new node to a binary tree, either as the left child or the right child of a
|
|
168
|
+
* given parent node.
|
|
169
|
+
* @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be
|
|
170
|
+
* added to the binary tree. It can be of type `N` (which represents a node in the binary tree) or
|
|
171
|
+
* `undefined` if there is no node to add.
|
|
172
|
+
* @param {BTNKey | N | undefined} parent - The `parent` parameter represents the parent node to
|
|
173
|
+
* which the new node will be added as a child. It can be either a node object (`N`) or a key value
|
|
174
|
+
* (`BTNKey`).
|
|
175
|
+
* @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
|
|
176
|
+
* added, or `undefined` if no node was added.
|
|
159
177
|
*/
|
|
160
178
|
protected override _addTo(newNode: N | undefined, parent: BTNKey | N | undefined): N | undefined {
|
|
161
179
|
parent = this.ensureNotKey(parent);
|
|
@@ -184,14 +202,22 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
184
202
|
}
|
|
185
203
|
|
|
186
204
|
/**
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
*
|
|
205
|
+
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
|
|
206
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
207
|
+
*/
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
|
|
211
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
212
|
+
*
|
|
213
|
+
* The function `addMany` takes an array of keys or nodes and adds them to the TreeMultimap,
|
|
214
|
+
* returning an array of the inserted nodes.
|
|
215
|
+
* @param {(BTNKey | N | undefined)[]} keysOrNodes - An array of keys or nodes. Each element can be
|
|
216
|
+
* of type BTNKey, N, or undefined.
|
|
217
|
+
* @param {V[]} [data] - The `data` parameter is an optional array of values that correspond to the
|
|
218
|
+
* keys or nodes being added. It is used to associate data with each key or node being added to the
|
|
219
|
+
* TreeMultimap. If provided, the length of the `data` array should be the same as the length of the
|
|
220
|
+
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
195
221
|
*/
|
|
196
222
|
override addMany(keysOrNodes: (BTNKey | N | undefined)[], data?: V[]): (N | undefined)[] {
|
|
197
223
|
const inserted: (N | undefined)[] = [];
|
|
@@ -215,10 +241,18 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
215
241
|
}
|
|
216
242
|
|
|
217
243
|
/**
|
|
218
|
-
*
|
|
219
|
-
*
|
|
244
|
+
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
|
|
245
|
+
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
|
|
246
|
+
*/
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
|
|
250
|
+
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
|
|
251
|
+
*
|
|
252
|
+
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
253
|
+
* tree using either a recursive or iterative approach.
|
|
220
254
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
221
|
-
* type of iteration to use when building
|
|
255
|
+
* type of iteration to use when building the balanced binary search tree. It can have two possible
|
|
222
256
|
* values:
|
|
223
257
|
* @returns a boolean value.
|
|
224
258
|
*/
|
|
@@ -261,20 +295,28 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
261
295
|
}
|
|
262
296
|
|
|
263
297
|
/**
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
*
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
* `
|
|
298
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
|
|
299
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
300
|
+
*/
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
|
|
304
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
305
|
+
*
|
|
306
|
+
* The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
|
|
307
|
+
* account the count of the node and balancing the tree if necessary.
|
|
308
|
+
* @param identifier - The identifier is the value or key that is used to identify the node that
|
|
309
|
+
* needs to be deleted from the binary tree. It can be of any type that is returned by the callback
|
|
310
|
+
* function.
|
|
311
|
+
* @param {C} callback - The `callback` parameter is a function that is used to determine if a node
|
|
312
|
+
* should be deleted. It is optional and defaults to a default callback function. The `callback`
|
|
313
|
+
* function takes one parameter, which is the identifier of the node, and returns a value that is
|
|
314
|
+
* used to identify the node to
|
|
273
315
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
274
316
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
275
317
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
276
318
|
* decremented by 1 and
|
|
277
|
-
* @returns
|
|
319
|
+
* @returns an array of `BiTreeDeleteResult<N>`.
|
|
278
320
|
*/
|
|
279
321
|
override delete<C extends BTNCallback<N>>(
|
|
280
322
|
identifier: ReturnType<C>,
|
|
@@ -344,11 +386,13 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
344
386
|
}
|
|
345
387
|
|
|
346
388
|
/**
|
|
347
|
-
* The function swaps the
|
|
348
|
-
* @param {N} srcNode - The
|
|
349
|
-
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
389
|
+
* The `_swap` function swaps the key, value, count, and height properties between two nodes.
|
|
390
|
+
* @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
391
|
+
* which the values will be swapped. It can be of type `BTNKey`, `N`, or `undefined`.
|
|
392
|
+
* @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
|
|
393
|
+
* node where the values from the source node will be swapped to.
|
|
394
|
+
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
395
|
+
* if either `srcNode` or `destNode` is undefined.
|
|
352
396
|
*/
|
|
353
397
|
protected _swap(srcNode: BTNKey | N | undefined, destNode:BTNKey | N | undefined): N | undefined{
|
|
354
398
|
srcNode = this.ensureNotKey(srcNode);
|
|
@@ -374,4 +418,4 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
374
418
|
}
|
|
375
419
|
return undefined;
|
|
376
420
|
}
|
|
377
|
-
}
|
|
421
|
+
}
|
|
@@ -105,6 +105,14 @@ export abstract class AbstractGraph<
|
|
|
105
105
|
abstract getEndsOfEdge(edge: EO): [VO, VO] | null;
|
|
106
106
|
|
|
107
107
|
/**
|
|
108
|
+
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
109
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
110
|
+
*/
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
114
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
115
|
+
*
|
|
108
116
|
* The function "getVertex" returns the vertex with the specified ID or null if it doesn't exist.
|
|
109
117
|
* @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
|
|
110
118
|
* the `_vertices` map.
|
|
@@ -116,6 +124,14 @@ export abstract class AbstractGraph<
|
|
|
116
124
|
}
|
|
117
125
|
|
|
118
126
|
/**
|
|
127
|
+
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
128
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
129
|
+
*/
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
133
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
134
|
+
*
|
|
119
135
|
* The function checks if a vertex exists in a graph.
|
|
120
136
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
121
137
|
* (`VertexKey`).
|
|
@@ -129,6 +145,11 @@ export abstract class AbstractGraph<
|
|
|
129
145
|
|
|
130
146
|
addVertex(key: VertexKey, value?: V): boolean;
|
|
131
147
|
|
|
148
|
+
/**
|
|
149
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
150
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
151
|
+
*/
|
|
152
|
+
|
|
132
153
|
addVertex(keyOrVertex: VertexKey | VO, value?: V): boolean {
|
|
133
154
|
if (keyOrVertex instanceof AbstractVertex) {
|
|
134
155
|
return this._addVertexOnly(keyOrVertex);
|
|
@@ -139,6 +160,14 @@ export abstract class AbstractGraph<
|
|
|
139
160
|
}
|
|
140
161
|
|
|
141
162
|
/**
|
|
163
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
164
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
165
|
+
*/
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
169
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
170
|
+
*
|
|
142
171
|
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
143
172
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
144
173
|
* (`VertexKey`).
|
|
@@ -150,6 +179,14 @@ export abstract class AbstractGraph<
|
|
|
150
179
|
}
|
|
151
180
|
|
|
152
181
|
/**
|
|
182
|
+
* Time Complexity: O(K), where K is the number of vertices to be removed.
|
|
183
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
184
|
+
*/
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Time Complexity: O(K), where K is the number of vertices to be removed.
|
|
188
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
189
|
+
*
|
|
153
190
|
* The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
|
|
154
191
|
* @param {VO[] | VertexKey[]} vertices - The `vertices` parameter can be either an array of vertices (`VO[]`) or an array
|
|
155
192
|
* of vertex IDs (`VertexKey[]`).
|
|
@@ -165,6 +202,14 @@ export abstract class AbstractGraph<
|
|
|
165
202
|
}
|
|
166
203
|
|
|
167
204
|
/**
|
|
205
|
+
* Time Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
206
|
+
* Space Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
207
|
+
*/
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Time Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
211
|
+
* Space Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
212
|
+
*
|
|
168
213
|
* The function checks if there is an edge between two vertices and returns a boolean value indicating the result.
|
|
169
214
|
* @param {VertexKey | VO} v1 - The parameter v1 can be either a VertexKey or a VO. A VertexKey represents the unique
|
|
170
215
|
* identifier of a vertex in a graph, while VO represents the type of the vertex object itself.
|
|
@@ -181,6 +226,11 @@ export abstract class AbstractGraph<
|
|
|
181
226
|
|
|
182
227
|
addEdge(src: VO | VertexKey, dest: VO | VertexKey, weight?: number, value?: E): boolean;
|
|
183
228
|
|
|
229
|
+
/**
|
|
230
|
+
* Time Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
231
|
+
* Space Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
232
|
+
*/
|
|
233
|
+
|
|
184
234
|
addEdge(srcOrEdge: VO | VertexKey | EO, dest?: VO | VertexKey, weight?: number, value?: E): boolean {
|
|
185
235
|
if (srcOrEdge instanceof AbstractEdge) {
|
|
186
236
|
return this._addEdgeOnly(srcOrEdge);
|
|
@@ -198,6 +248,14 @@ export abstract class AbstractGraph<
|
|
|
198
248
|
}
|
|
199
249
|
|
|
200
250
|
/**
|
|
251
|
+
* Time Complexity: O(1) - Constant time for Map and Edge operations.
|
|
252
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
253
|
+
*/
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Time Complexity: O(1) - Constant time for Map and Edge operations.
|
|
257
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
258
|
+
*
|
|
201
259
|
* The function sets the weight of an edge between two vertices in a graph.
|
|
202
260
|
* @param {VertexKey | VO} srcOrKey - The `srcOrKey` parameter can be either a `VertexKey` or a `VO` object. It represents
|
|
203
261
|
* the source vertex of the edge.
|
|
@@ -219,6 +277,14 @@ export abstract class AbstractGraph<
|
|
|
219
277
|
}
|
|
220
278
|
|
|
221
279
|
/**
|
|
280
|
+
* Time Complexity: O(P), where P is the number of paths found (in the worst case, exploring all paths).
|
|
281
|
+
* Space Complexity: O(P) - Linear space, where P is the number of paths found.
|
|
282
|
+
*/
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Time Complexity: O(P), where P is the number of paths found (in the worst case, exploring all paths).
|
|
286
|
+
* Space Complexity: O(P) - Linear space, where P is the number of paths found.
|
|
287
|
+
*
|
|
222
288
|
* The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
|
|
223
289
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
224
290
|
* It is the starting vertex for finding paths.
|
|
@@ -258,6 +324,14 @@ export abstract class AbstractGraph<
|
|
|
258
324
|
}
|
|
259
325
|
|
|
260
326
|
/**
|
|
327
|
+
* Time Complexity: O(L), where L is the length of the path.
|
|
328
|
+
* Space Complexity: O(1) - Constant space.
|
|
329
|
+
*/
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Time Complexity: O(L), where L is the length of the path.
|
|
333
|
+
* Space Complexity: O(1) - Constant space.
|
|
334
|
+
*
|
|
261
335
|
* The function calculates the sum of weights along a given path.
|
|
262
336
|
* @param {VO[]} path - An array of vertices (VO) representing a path in a graph.
|
|
263
337
|
* @returns The function `getPathSumWeight` returns the sum of the weights of the edges in the given path.
|
|
@@ -271,6 +345,14 @@ export abstract class AbstractGraph<
|
|
|
271
345
|
}
|
|
272
346
|
|
|
273
347
|
/**
|
|
348
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
349
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
350
|
+
*/
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
354
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
355
|
+
*
|
|
274
356
|
* The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
|
|
275
357
|
* weights or using a breadth-first search algorithm.
|
|
276
358
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex or its ID.
|
|
@@ -330,6 +412,14 @@ export abstract class AbstractGraph<
|
|
|
330
412
|
}
|
|
331
413
|
|
|
332
414
|
/**
|
|
415
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
416
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
417
|
+
*/
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
421
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
422
|
+
*
|
|
333
423
|
* The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
|
|
334
424
|
* using a breadth-first search algorithm.
|
|
335
425
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
|
|
@@ -398,11 +488,18 @@ export abstract class AbstractGraph<
|
|
|
398
488
|
}
|
|
399
489
|
|
|
400
490
|
/**
|
|
401
|
-
*
|
|
491
|
+
* Dijkstra algorithm time: O(VE) space: O(VO + EO)
|
|
402
492
|
* /
|
|
403
493
|
|
|
404
494
|
/**
|
|
405
|
-
*
|
|
495
|
+
* Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
|
|
496
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
497
|
+
*/
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
|
|
501
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
502
|
+
*
|
|
406
503
|
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
|
|
407
504
|
* a graph without using a heap data structure.
|
|
408
505
|
* @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
@@ -533,7 +630,7 @@ export abstract class AbstractGraph<
|
|
|
533
630
|
}
|
|
534
631
|
|
|
535
632
|
/**
|
|
536
|
-
*
|
|
633
|
+
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
537
634
|
*
|
|
538
635
|
* 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.
|
|
539
636
|
* 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.
|
|
@@ -542,6 +639,14 @@ export abstract class AbstractGraph<
|
|
|
542
639
|
* /
|
|
543
640
|
|
|
544
641
|
/**
|
|
642
|
+
* Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
|
|
643
|
+
* Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
|
|
644
|
+
*/
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
|
|
648
|
+
* Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
|
|
649
|
+
*
|
|
545
650
|
* 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.
|
|
546
651
|
* The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
|
|
547
652
|
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
@@ -670,12 +775,15 @@ export abstract class AbstractGraph<
|
|
|
670
775
|
}
|
|
671
776
|
|
|
672
777
|
/**
|
|
673
|
-
*
|
|
778
|
+
* Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
|
|
779
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
|
|
674
780
|
* one to rest pairs
|
|
675
781
|
* /
|
|
676
782
|
|
|
677
783
|
/**
|
|
678
|
-
*
|
|
784
|
+
* Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
|
|
785
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
|
|
786
|
+
*
|
|
679
787
|
* one to rest pairs
|
|
680
788
|
* 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.
|
|
681
789
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
@@ -795,13 +903,18 @@ export abstract class AbstractGraph<
|
|
|
795
903
|
*/
|
|
796
904
|
|
|
797
905
|
/**
|
|
798
|
-
*
|
|
906
|
+
* Time Complexity: O(V^3) - Cubic time (Floyd-Warshall algorithm).
|
|
907
|
+
* Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
|
|
908
|
+
* Not support graph with negative weight cycle
|
|
799
909
|
* all pairs
|
|
800
910
|
* 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.
|
|
801
911
|
* /
|
|
802
912
|
|
|
803
913
|
/**
|
|
804
|
-
*
|
|
914
|
+
* Time Complexity: O(V^3) - Cubic time (Floyd-Warshall algorithm).
|
|
915
|
+
* Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
|
|
916
|
+
*
|
|
917
|
+
* Not support graph with negative weight cycle
|
|
805
918
|
* all pairs
|
|
806
919
|
* 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.
|
|
807
920
|
* The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a
|
|
@@ -847,6 +960,8 @@ export abstract class AbstractGraph<
|
|
|
847
960
|
}
|
|
848
961
|
|
|
849
962
|
/**
|
|
963
|
+
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
|
|
964
|
+
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
850
965
|
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
851
966
|
* Tarjan can find cycles in directed or undirected graph
|
|
852
967
|
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
@@ -855,6 +970,9 @@ export abstract class AbstractGraph<
|
|
|
855
970
|
* /
|
|
856
971
|
|
|
857
972
|
/**
|
|
973
|
+
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
|
|
974
|
+
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
975
|
+
*
|
|
858
976
|
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
859
977
|
* Tarjan can find cycles in directed or undirected graph
|
|
860
978
|
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
@@ -983,6 +1101,14 @@ export abstract class AbstractGraph<
|
|
|
983
1101
|
}
|
|
984
1102
|
|
|
985
1103
|
/**
|
|
1104
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
1105
|
+
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
1106
|
+
*/
|
|
1107
|
+
|
|
1108
|
+
/**
|
|
1109
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
1110
|
+
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
1111
|
+
*
|
|
986
1112
|
* The function returns a map that associates each vertex object with its corresponding depth-first
|
|
987
1113
|
* number.
|
|
988
1114
|
* @returns A Map object with keys of type VO and values of type number.
|