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
|
@@ -50,17 +50,22 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
50
50
|
return new TreeMultimapNode(key, value, count);
|
|
51
51
|
}
|
|
52
52
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
53
|
+
* 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.
|
|
54
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
55
|
+
*/
|
|
56
|
+
/**
|
|
57
|
+
* 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.
|
|
58
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
59
|
+
*
|
|
60
|
+
* The `add` function adds a new node to the tree multimap, updating the count if the key already
|
|
61
|
+
* exists, and balances the tree if necessary.
|
|
62
|
+
* @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
|
|
63
|
+
* following types:
|
|
64
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
65
|
+
* being added to the tree. It is an optional parameter, so it can be omitted if not needed.
|
|
66
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
67
|
+
* times the key-value pair should be added to the multimap. If not provided, the default value is 1.
|
|
68
|
+
* @returns a node (`N`) or `undefined`.
|
|
64
69
|
*/
|
|
65
70
|
add(keyOrNode, value, count = 1) {
|
|
66
71
|
if (keyOrNode === null)
|
|
@@ -142,12 +147,23 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
142
147
|
return inserted;
|
|
143
148
|
}
|
|
144
149
|
/**
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
*
|
|
150
|
-
*
|
|
150
|
+
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
151
|
+
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
152
|
+
*/
|
|
153
|
+
/**
|
|
154
|
+
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
155
|
+
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
156
|
+
*
|
|
157
|
+
* The function adds a new node to a binary tree, either as the left child or the right child of a
|
|
158
|
+
* given parent node.
|
|
159
|
+
* @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be
|
|
160
|
+
* added to the binary tree. It can be of type `N` (which represents a node in the binary tree) or
|
|
161
|
+
* `undefined` if there is no node to add.
|
|
162
|
+
* @param {BTNKey | N | undefined} parent - The `parent` parameter represents the parent node to
|
|
163
|
+
* which the new node will be added as a child. It can be either a node object (`N`) or a key value
|
|
164
|
+
* (`BTNKey`).
|
|
165
|
+
* @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
|
|
166
|
+
* added, or `undefined` if no node was added.
|
|
151
167
|
*/
|
|
152
168
|
_addTo(newNode, parent) {
|
|
153
169
|
parent = this.ensureNotKey(parent);
|
|
@@ -177,14 +193,21 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
177
193
|
}
|
|
178
194
|
}
|
|
179
195
|
/**
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
196
|
+
* 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.
|
|
197
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
198
|
+
*/
|
|
199
|
+
/**
|
|
200
|
+
* 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.
|
|
201
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
202
|
+
*
|
|
203
|
+
* The function `addMany` takes an array of keys or nodes and adds them to the TreeMultimap,
|
|
204
|
+
* returning an array of the inserted nodes.
|
|
205
|
+
* @param {(BTNKey | N | undefined)[]} keysOrNodes - An array of keys or nodes. Each element can be
|
|
206
|
+
* of type BTNKey, N, or undefined.
|
|
207
|
+
* @param {V[]} [data] - The `data` parameter is an optional array of values that correspond to the
|
|
208
|
+
* keys or nodes being added. It is used to associate data with each key or node being added to the
|
|
209
|
+
* TreeMultimap. If provided, the length of the `data` array should be the same as the length of the
|
|
210
|
+
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
188
211
|
*/
|
|
189
212
|
addMany(keysOrNodes, data) {
|
|
190
213
|
const inserted = [];
|
|
@@ -203,10 +226,17 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
203
226
|
return inserted;
|
|
204
227
|
}
|
|
205
228
|
/**
|
|
206
|
-
*
|
|
207
|
-
*
|
|
229
|
+
* 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.
|
|
230
|
+
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
|
|
231
|
+
*/
|
|
232
|
+
/**
|
|
233
|
+
* 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.
|
|
234
|
+
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
|
|
235
|
+
*
|
|
236
|
+
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
237
|
+
* tree using either a recursive or iterative approach.
|
|
208
238
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
209
|
-
* type of iteration to use when building
|
|
239
|
+
* type of iteration to use when building the balanced binary search tree. It can have two possible
|
|
210
240
|
* values:
|
|
211
241
|
* @returns a boolean value.
|
|
212
242
|
*/
|
|
@@ -247,20 +277,27 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
247
277
|
}
|
|
248
278
|
}
|
|
249
279
|
/**
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
*
|
|
280
|
+
* 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.
|
|
281
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
282
|
+
*/
|
|
283
|
+
/**
|
|
284
|
+
* 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.
|
|
285
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
286
|
+
*
|
|
287
|
+
* The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
|
|
288
|
+
* account the count of the node and balancing the tree if necessary.
|
|
289
|
+
* @param identifier - The identifier is the value or key that is used to identify the node that
|
|
290
|
+
* needs to be deleted from the binary tree. It can be of any type that is returned by the callback
|
|
291
|
+
* function.
|
|
292
|
+
* @param {C} callback - The `callback` parameter is a function that is used to determine if a node
|
|
293
|
+
* should be deleted. It is optional and defaults to a default callback function. The `callback`
|
|
294
|
+
* function takes one parameter, which is the identifier of the node, and returns a value that is
|
|
295
|
+
* used to identify the node to
|
|
259
296
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
260
297
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
261
298
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
262
299
|
* decremented by 1 and
|
|
263
|
-
* @returns
|
|
300
|
+
* @returns an array of `BiTreeDeleteResult<N>`.
|
|
264
301
|
*/
|
|
265
302
|
delete(identifier, callback = this._defaultOneParamCallback, ignoreCount = false) {
|
|
266
303
|
var _a;
|
|
@@ -328,11 +365,13 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
328
365
|
this._count = 0;
|
|
329
366
|
}
|
|
330
367
|
/**
|
|
331
|
-
* The function swaps the
|
|
332
|
-
* @param {N} srcNode - The
|
|
333
|
-
*
|
|
334
|
-
*
|
|
335
|
-
*
|
|
368
|
+
* The `_swap` function swaps the key, value, count, and height properties between two nodes.
|
|
369
|
+
* @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
370
|
+
* which the values will be swapped. It can be of type `BTNKey`, `N`, or `undefined`.
|
|
371
|
+
* @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
|
|
372
|
+
* node where the values from the source node will be swapped to.
|
|
373
|
+
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
374
|
+
* if either `srcNode` or `destNode` is undefined.
|
|
336
375
|
*/
|
|
337
376
|
_swap(srcNode, destNode) {
|
|
338
377
|
srcNode = this.ensureNotKey(srcNode);
|
|
@@ -55,6 +55,13 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
55
55
|
abstract getNeighbors(vertexOrKey: VO | VertexKey): VO[];
|
|
56
56
|
abstract getEndsOfEdge(edge: EO): [VO, VO] | null;
|
|
57
57
|
/**
|
|
58
|
+
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
59
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
60
|
+
*/
|
|
61
|
+
/**
|
|
62
|
+
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
63
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
64
|
+
*
|
|
58
65
|
* The function "getVertex" returns the vertex with the specified ID or null if it doesn't exist.
|
|
59
66
|
* @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
|
|
60
67
|
* the `_vertices` map.
|
|
@@ -63,6 +70,13 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
63
70
|
*/
|
|
64
71
|
getVertex(vertexKey: VertexKey): VO | null;
|
|
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`).
|
|
@@ -72,6 +86,13 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
72
86
|
addVertex(vertex: VO): boolean;
|
|
73
87
|
addVertex(key: VertexKey, value?: V): boolean;
|
|
74
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
|
+
*/
|
|
92
|
+
/**
|
|
93
|
+
* Time Complexity: O(1) - Constant time for Map operations.
|
|
94
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
95
|
+
*
|
|
75
96
|
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
76
97
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
77
98
|
* (`VertexKey`).
|
|
@@ -79,6 +100,13 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
79
100
|
*/
|
|
80
101
|
deleteVertex(vertexOrKey: VO | VertexKey): boolean;
|
|
81
102
|
/**
|
|
103
|
+
* Time Complexity: O(K), where K is the number of vertices to be removed.
|
|
104
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
105
|
+
*/
|
|
106
|
+
/**
|
|
107
|
+
* Time Complexity: O(K), where K is the number of vertices to be removed.
|
|
108
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
109
|
+
*
|
|
82
110
|
* The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
|
|
83
111
|
* @param {VO[] | VertexKey[]} vertices - The `vertices` parameter can be either an array of vertices (`VO[]`) or an array
|
|
84
112
|
* of vertex IDs (`VertexKey[]`).
|
|
@@ -87,6 +115,13 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
87
115
|
*/
|
|
88
116
|
removeManyVertices(vertices: VO[] | VertexKey[]): boolean;
|
|
89
117
|
/**
|
|
118
|
+
* Time Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
119
|
+
* Space Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
120
|
+
*/
|
|
121
|
+
/**
|
|
122
|
+
* Time Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
123
|
+
* Space Complexity: O(1) - Depends on the implementation in the concrete class.
|
|
124
|
+
*
|
|
90
125
|
* The function checks if there is an edge between two vertices and returns a boolean value indicating the result.
|
|
91
126
|
* @param {VertexKey | VO} v1 - The parameter v1 can be either a VertexKey or a VO. A VertexKey represents the unique
|
|
92
127
|
* identifier of a vertex in a graph, while VO represents the type of the vertex object itself.
|
|
@@ -98,6 +133,13 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
98
133
|
addEdge(edge: EO): boolean;
|
|
99
134
|
addEdge(src: VO | VertexKey, dest: VO | VertexKey, weight?: number, value?: E): boolean;
|
|
100
135
|
/**
|
|
136
|
+
* Time Complexity: O(1) - Constant time for Map and Edge operations.
|
|
137
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
138
|
+
*/
|
|
139
|
+
/**
|
|
140
|
+
* Time Complexity: O(1) - Constant time for Map and Edge operations.
|
|
141
|
+
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
142
|
+
*
|
|
101
143
|
* The function sets the weight of an edge between two vertices in a graph.
|
|
102
144
|
* @param {VertexKey | VO} srcOrKey - The `srcOrKey` parameter can be either a `VertexKey` or a `VO` object. It represents
|
|
103
145
|
* the source vertex of the edge.
|
|
@@ -110,6 +152,13 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
110
152
|
*/
|
|
111
153
|
setEdgeWeight(srcOrKey: VertexKey | VO, destOrKey: VertexKey | VO, weight: number): boolean;
|
|
112
154
|
/**
|
|
155
|
+
* Time Complexity: O(P), where P is the number of paths found (in the worst case, exploring all paths).
|
|
156
|
+
* Space Complexity: O(P) - Linear space, where P is the number of paths found.
|
|
157
|
+
*/
|
|
158
|
+
/**
|
|
159
|
+
* Time Complexity: O(P), where P is the number of paths found (in the worst case, exploring all paths).
|
|
160
|
+
* Space Complexity: O(P) - Linear space, where P is the number of paths found.
|
|
161
|
+
*
|
|
113
162
|
* The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
|
|
114
163
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
115
164
|
* It is the starting vertex for finding paths.
|
|
@@ -119,12 +168,26 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
119
168
|
*/
|
|
120
169
|
getAllPathsBetween(v1: VO | VertexKey, v2: VO | VertexKey, limit?: number): VO[][];
|
|
121
170
|
/**
|
|
171
|
+
* Time Complexity: O(L), where L is the length of the path.
|
|
172
|
+
* Space Complexity: O(1) - Constant space.
|
|
173
|
+
*/
|
|
174
|
+
/**
|
|
175
|
+
* Time Complexity: O(L), where L is the length of the path.
|
|
176
|
+
* Space Complexity: O(1) - Constant space.
|
|
177
|
+
*
|
|
122
178
|
* The function calculates the sum of weights along a given path.
|
|
123
179
|
* @param {VO[]} path - An array of vertices (VO) representing a path in a graph.
|
|
124
180
|
* @returns The function `getPathSumWeight` returns the sum of the weights of the edges in the given path.
|
|
125
181
|
*/
|
|
126
182
|
getPathSumWeight(path: VO[]): number;
|
|
127
183
|
/**
|
|
184
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
185
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
186
|
+
*/
|
|
187
|
+
/**
|
|
188
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
189
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
190
|
+
*
|
|
128
191
|
* The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
|
|
129
192
|
* weights or using a breadth-first search algorithm.
|
|
130
193
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex or its ID.
|
|
@@ -140,6 +203,13 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
140
203
|
*/
|
|
141
204
|
getMinCostBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean): number | null;
|
|
142
205
|
/**
|
|
206
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
207
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
208
|
+
*/
|
|
209
|
+
/**
|
|
210
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
211
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
|
|
212
|
+
*
|
|
143
213
|
* The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
|
|
144
214
|
* using a breadth-first search algorithm.
|
|
145
215
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
|
|
@@ -157,29 +227,35 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
157
227
|
*/
|
|
158
228
|
getMinPathBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean, isDFS?: boolean): VO[] | null;
|
|
159
229
|
/**
|
|
160
|
-
*
|
|
230
|
+
* Dijkstra algorithm time: O(VE) space: O(VO + EO)
|
|
161
231
|
* /
|
|
162
232
|
|
|
163
233
|
/**
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
* a graph without using a heap data structure.
|
|
167
|
-
* @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
168
|
-
* vertex object or a vertex ID.
|
|
169
|
-
* @param {VO | VertexKey | null} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
170
|
-
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
171
|
-
* identifier. If no destination is provided, the value is set to `null`.
|
|
172
|
-
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
173
|
-
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
174
|
-
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
175
|
-
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
176
|
-
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
177
|
-
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
178
|
-
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
|
|
234
|
+
* Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
|
|
235
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
179
236
|
*/
|
|
237
|
+
/**
|
|
238
|
+
* Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
|
|
239
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
240
|
+
*
|
|
241
|
+
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
|
|
242
|
+
* a graph without using a heap data structure.
|
|
243
|
+
* @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
244
|
+
* vertex object or a vertex ID.
|
|
245
|
+
* @param {VO | VertexKey | null} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
246
|
+
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
247
|
+
* identifier. If no destination is provided, the value is set to `null`.
|
|
248
|
+
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
249
|
+
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
250
|
+
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
251
|
+
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
252
|
+
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
253
|
+
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
254
|
+
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
|
|
255
|
+
*/
|
|
180
256
|
dijkstraWithoutHeap(src: VO | VertexKey, dest?: VO | VertexKey | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<VO>;
|
|
181
257
|
/**
|
|
182
|
-
*
|
|
258
|
+
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
183
259
|
*
|
|
184
260
|
* 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.
|
|
185
261
|
* 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.
|
|
@@ -188,30 +264,40 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
188
264
|
* /
|
|
189
265
|
|
|
190
266
|
/**
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
194
|
-
* @param {VO | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
195
|
-
* start. It can be either a vertex object or a vertex ID.
|
|
196
|
-
* @param {VO | VertexKey | null} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
197
|
-
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
198
|
-
* will calculate the shortest paths to all other vertices from the source vertex.
|
|
199
|
-
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
200
|
-
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
201
|
-
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
202
|
-
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
203
|
-
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
204
|
-
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
205
|
-
* @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
|
|
267
|
+
* Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
|
|
268
|
+
* Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
|
|
206
269
|
*/
|
|
270
|
+
/**
|
|
271
|
+
* Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
|
|
272
|
+
* Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
|
|
273
|
+
*
|
|
274
|
+
* 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.
|
|
275
|
+
* The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
|
|
276
|
+
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
277
|
+
* @param {VO | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
278
|
+
* start. It can be either a vertex object or a vertex ID.
|
|
279
|
+
* @param {VO | VertexKey | null} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
280
|
+
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
281
|
+
* will calculate the shortest paths to all other vertices from the source vertex.
|
|
282
|
+
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
283
|
+
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
284
|
+
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
285
|
+
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
286
|
+
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
287
|
+
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
288
|
+
* @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
|
|
289
|
+
*/
|
|
207
290
|
dijkstra(src: VO | VertexKey, dest?: VO | VertexKey | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<VO>;
|
|
208
291
|
/**
|
|
209
|
-
*
|
|
292
|
+
* Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
|
|
293
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
|
|
210
294
|
* one to rest pairs
|
|
211
295
|
* /
|
|
212
296
|
|
|
213
297
|
/**
|
|
214
|
-
*
|
|
298
|
+
* Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
|
|
299
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
|
|
300
|
+
*
|
|
215
301
|
* one to rest pairs
|
|
216
302
|
* 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.
|
|
217
303
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
@@ -249,13 +335,18 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
249
335
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
250
336
|
*/
|
|
251
337
|
/**
|
|
252
|
-
*
|
|
338
|
+
* Time Complexity: O(V^3) - Cubic time (Floyd-Warshall algorithm).
|
|
339
|
+
* Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
|
|
340
|
+
* Not support graph with negative weight cycle
|
|
253
341
|
* all pairs
|
|
254
342
|
* 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.
|
|
255
343
|
* /
|
|
256
344
|
|
|
257
345
|
/**
|
|
258
|
-
*
|
|
346
|
+
* Time Complexity: O(V^3) - Cubic time (Floyd-Warshall algorithm).
|
|
347
|
+
* Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
|
|
348
|
+
*
|
|
349
|
+
* Not support graph with negative weight cycle
|
|
259
350
|
* all pairs
|
|
260
351
|
* 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.
|
|
261
352
|
* The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a
|
|
@@ -270,6 +361,8 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
270
361
|
predecessor: (VO | null)[][];
|
|
271
362
|
};
|
|
272
363
|
/**
|
|
364
|
+
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
|
|
365
|
+
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
273
366
|
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
274
367
|
* Tarjan can find cycles in directed or undirected graph
|
|
275
368
|
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
@@ -278,6 +371,9 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
278
371
|
* /
|
|
279
372
|
|
|
280
373
|
/**
|
|
374
|
+
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
|
|
375
|
+
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
376
|
+
*
|
|
281
377
|
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
282
378
|
* Tarjan can find cycles in directed or undirected graph
|
|
283
379
|
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
@@ -307,6 +403,13 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
307
403
|
cycles: Map<number, VO[]>;
|
|
308
404
|
};
|
|
309
405
|
/**
|
|
406
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
407
|
+
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
408
|
+
*/
|
|
409
|
+
/**
|
|
410
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
411
|
+
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
412
|
+
*
|
|
310
413
|
* The function returns a map that associates each vertex object with its corresponding depth-first
|
|
311
414
|
* number.
|
|
312
415
|
* @returns A Map object with keys of type VO and values of type number.
|