min-heap-typed 2.0.4 → 2.1.0
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/base/iterable-element-base.d.ts +186 -83
- package/dist/data-structures/base/iterable-element-base.js +149 -107
- package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
- package/dist/data-structures/base/iterable-entry-base.js +59 -116
- package/dist/data-structures/base/linear-base.d.ts +250 -192
- package/dist/data-structures/base/linear-base.js +137 -274
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
- package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
- package/dist/data-structures/binary-tree/avl-tree.js +208 -195
- package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
- package/dist/data-structures/binary-tree/binary-tree.js +612 -879
- package/dist/data-structures/binary-tree/bst.d.ts +258 -306
- package/dist/data-structures/binary-tree/bst.js +505 -481
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
- package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
- package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
- package/dist/data-structures/binary-tree/tree-counter.js +172 -203
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
- package/dist/data-structures/graph/abstract-graph.js +267 -237
- package/dist/data-structures/graph/directed-graph.d.ts +108 -224
- package/dist/data-structures/graph/directed-graph.js +146 -233
- package/dist/data-structures/graph/map-graph.d.ts +49 -55
- package/dist/data-structures/graph/map-graph.js +56 -59
- package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
- package/dist/data-structures/graph/undirected-graph.js +129 -149
- package/dist/data-structures/hash/hash-map.d.ts +164 -338
- package/dist/data-structures/hash/hash-map.js +270 -457
- package/dist/data-structures/heap/heap.d.ts +214 -289
- package/dist/data-structures/heap/heap.js +340 -349
- package/dist/data-structures/heap/max-heap.d.ts +11 -47
- package/dist/data-structures/heap/max-heap.js +11 -66
- package/dist/data-structures/heap/min-heap.d.ts +12 -47
- package/dist/data-structures/heap/min-heap.js +11 -66
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
- package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
- package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
- package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
- package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
- package/dist/data-structures/priority-queue/priority-queue.js +8 -83
- package/dist/data-structures/queue/deque.d.ts +227 -254
- package/dist/data-structures/queue/deque.js +309 -348
- package/dist/data-structures/queue/queue.d.ts +180 -201
- package/dist/data-structures/queue/queue.js +265 -248
- package/dist/data-structures/stack/stack.d.ts +124 -102
- package/dist/data-structures/stack/stack.js +181 -125
- package/dist/data-structures/trie/trie.d.ts +164 -165
- package/dist/data-structures/trie/trie.js +189 -172
- package/dist/interfaces/binary-tree.d.ts +56 -6
- package/dist/interfaces/graph.d.ts +16 -0
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
- package/dist/types/utils/utils.d.ts +6 -6
- package/dist/utils/utils.d.ts +110 -49
- package/dist/utils/utils.js +148 -73
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +238 -115
- package/src/data-structures/base/iterable-entry-base.ts +96 -120
- package/src/data-structures/base/linear-base.ts +271 -277
- package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
- package/src/data-structures/binary-tree/avl-tree.ts +239 -206
- package/src/data-structures/binary-tree/binary-tree.ts +681 -905
- package/src/data-structures/binary-tree/bst.ts +568 -570
- package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
- package/src/data-structures/binary-tree/tree-counter.ts +199 -218
- package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
- package/src/data-structures/graph/abstract-graph.ts +339 -264
- package/src/data-structures/graph/directed-graph.ts +146 -236
- package/src/data-structures/graph/map-graph.ts +63 -60
- package/src/data-structures/graph/undirected-graph.ts +129 -152
- package/src/data-structures/hash/hash-map.ts +274 -496
- package/src/data-structures/heap/heap.ts +389 -402
- package/src/data-structures/heap/max-heap.ts +12 -76
- package/src/data-structures/heap/min-heap.ts +13 -76
- package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
- package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
- package/src/data-structures/priority-queue/priority-queue.ts +3 -92
- package/src/data-structures/queue/deque.ts +381 -357
- package/src/data-structures/queue/queue.ts +310 -264
- package/src/data-structures/stack/stack.ts +217 -131
- package/src/data-structures/trie/trie.ts +240 -175
- package/src/interfaces/binary-tree.ts +240 -6
- package/src/interfaces/graph.ts +37 -0
- package/src/types/data-structures/base/base.ts +5 -5
- package/src/types/data-structures/graph/abstract-graph.ts +5 -0
- package/src/types/utils/utils.ts +9 -5
- package/src/utils/utils.ts +152 -86
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* data-structure-typed
|
|
4
|
+
*
|
|
5
|
+
* @author Pablo Zeng
|
|
6
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
7
|
+
* @license MIT License
|
|
8
|
+
*/
|
|
2
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
10
|
exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
|
|
4
11
|
const utils_1 = require("../../utils");
|
|
@@ -6,13 +13,6 @@ const base_1 = require("../base");
|
|
|
6
13
|
const heap_1 = require("../heap");
|
|
7
14
|
const queue_1 = require("../queue");
|
|
8
15
|
class AbstractVertex {
|
|
9
|
-
/**
|
|
10
|
-
* The function is a protected constructor that takes an key and an optional value as parameters.
|
|
11
|
-
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
|
12
|
-
* used to uniquely identify the vertex object.
|
|
13
|
-
* @param {V} [value] - The parameter "value" is an optional parameter of type V. It is used to assign a value to the
|
|
14
|
-
* vertex. If no value is provided, it will be set to undefined.
|
|
15
|
-
*/
|
|
16
16
|
constructor(key, value) {
|
|
17
17
|
this.key = key;
|
|
18
18
|
this.value = value;
|
|
@@ -20,15 +20,6 @@ class AbstractVertex {
|
|
|
20
20
|
}
|
|
21
21
|
exports.AbstractVertex = AbstractVertex;
|
|
22
22
|
class AbstractEdge {
|
|
23
|
-
/**
|
|
24
|
-
* The above function is a protected constructor that initializes the weight, value, and hash code properties of an
|
|
25
|
-
* object.
|
|
26
|
-
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the object. If
|
|
27
|
-
* a value is provided, it will be assigned to the `_weight` property. If no value is provided, the default value of 1
|
|
28
|
-
* will be assigned.
|
|
29
|
-
* @param {VO} [value] - The `value` parameter is of type `VO`, which means it can be any type. It is an optional parameter,
|
|
30
|
-
* meaning it can be omitted when creating an instance of the class.
|
|
31
|
-
*/
|
|
32
23
|
constructor(weight, value) {
|
|
33
24
|
this.weight = weight !== undefined ? weight : 1;
|
|
34
25
|
this.value = value;
|
|
@@ -39,10 +30,30 @@ class AbstractEdge {
|
|
|
39
30
|
}
|
|
40
31
|
}
|
|
41
32
|
exports.AbstractEdge = AbstractEdge;
|
|
33
|
+
/**
|
|
34
|
+
* Abstract graph over vertices and edges.
|
|
35
|
+
* @template V - Vertex value type.
|
|
36
|
+
* @template E - Edge value type.
|
|
37
|
+
* @template VO - Concrete vertex subclass (extends AbstractVertex<V>).
|
|
38
|
+
* @template EO - Concrete edge subclass (extends AbstractEdge<E>).
|
|
39
|
+
* @remarks Time O(1), Space O(1)
|
|
40
|
+
* @example examples will be generated by unit test
|
|
41
|
+
*/
|
|
42
42
|
class AbstractGraph extends base_1.IterableEntryBase {
|
|
43
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Construct a graph with runtime defaults.
|
|
45
|
+
* @param options - `GraphOptions<V>` in `options.graph` (e.g. `vertexValueInitializer`, `defaultEdgeWeight`).
|
|
46
|
+
* @remarks Time O(1), Space O(1)
|
|
47
|
+
*/
|
|
48
|
+
constructor(options) {
|
|
44
49
|
super();
|
|
50
|
+
this._options = { defaultEdgeWeight: 1 };
|
|
45
51
|
this._vertexMap = new Map();
|
|
52
|
+
const graph = options === null || options === void 0 ? void 0 : options.graph;
|
|
53
|
+
this._options = Object.assign({ defaultEdgeWeight: 1 }, (graph !== null && graph !== void 0 ? graph : {}));
|
|
54
|
+
}
|
|
55
|
+
get options() {
|
|
56
|
+
return this._options;
|
|
46
57
|
}
|
|
47
58
|
get vertexMap() {
|
|
48
59
|
return this._vertexMap;
|
|
@@ -54,33 +65,29 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
54
65
|
return this._vertexMap.size;
|
|
55
66
|
}
|
|
56
67
|
/**
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
* @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
|
|
62
|
-
* the `_vertexMap` map.
|
|
63
|
-
* @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertexMap`
|
|
64
|
-
* map. If the vertex does not exist, it returns `undefined`.
|
|
68
|
+
* Get vertex instance by key.
|
|
69
|
+
* @param vertexKey - Vertex key.
|
|
70
|
+
* @returns Vertex instance or `undefined`.
|
|
71
|
+
* @remarks Time O(1), Space O(1)
|
|
65
72
|
*/
|
|
66
73
|
getVertex(vertexKey) {
|
|
67
74
|
return this._vertexMap.get(vertexKey) || undefined;
|
|
68
75
|
}
|
|
69
76
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
75
|
-
* (`VertexKey`).
|
|
76
|
-
* @returns a boolean value.
|
|
77
|
+
* Whether a vertex exists.
|
|
78
|
+
* @param vertexOrKey - Vertex or key.
|
|
79
|
+
* @returns `true` if present, otherwise `false`.
|
|
80
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
77
81
|
*/
|
|
78
82
|
hasVertex(vertexOrKey) {
|
|
79
83
|
return this._vertexMap.has(this._getVertexKey(vertexOrKey));
|
|
80
84
|
}
|
|
81
85
|
/**
|
|
82
|
-
*
|
|
83
|
-
*
|
|
86
|
+
* Add a vertex by key/value or by pre-built vertex.
|
|
87
|
+
* @param keyOrVertex - Vertex key or existing vertex instance.
|
|
88
|
+
* @param value - Optional payload.
|
|
89
|
+
* @returns `true` if inserted; `false` when key already exists.
|
|
90
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
84
91
|
*/
|
|
85
92
|
addVertex(keyOrVertex, value) {
|
|
86
93
|
if (keyOrVertex instanceof AbstractVertex) {
|
|
@@ -91,19 +98,21 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
91
98
|
return this._addVertex(newVertex);
|
|
92
99
|
}
|
|
93
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Type guard: check if a value is a valid vertex key.
|
|
103
|
+
* @param potentialKey - Value to test.
|
|
104
|
+
* @returns `true` if string/number; else `false`.
|
|
105
|
+
* @remarks Time O(1), Space O(1)
|
|
106
|
+
*/
|
|
94
107
|
isVertexKey(potentialKey) {
|
|
95
108
|
const potentialKeyType = typeof potentialKey;
|
|
96
109
|
return potentialKeyType === 'string' || potentialKeyType === 'number';
|
|
97
110
|
}
|
|
98
111
|
/**
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
* @param {VO[] | VertexKey[]} vertexMap - The `vertexMap` parameter can be either an array of vertexMap (`VO[]`) or an array
|
|
104
|
-
* of vertex IDs (`VertexKey[]`).
|
|
105
|
-
* @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertexMap
|
|
106
|
-
* were removed.
|
|
112
|
+
* Delete multiple vertices.
|
|
113
|
+
* @param vertexMap - Array of vertices or keys.
|
|
114
|
+
* @returns `true` if any vertex was removed.
|
|
115
|
+
* @remarks Time O(sum(deg)), Space O(1)
|
|
107
116
|
*/
|
|
108
117
|
removeManyVertices(vertexMap) {
|
|
109
118
|
const removed = [];
|
|
@@ -113,23 +122,24 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
113
122
|
return removed.length > 0;
|
|
114
123
|
}
|
|
115
124
|
/**
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
* @
|
|
121
|
-
* identifier of a vertex in a graph, while VO represents the type of the vertex object itself.
|
|
122
|
-
* @param {VertexKey | VO} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
|
|
123
|
-
* `VertexKey` or a `VO` type, which represents the type of the vertex.
|
|
124
|
-
* @returns A boolean value is being returned.
|
|
125
|
+
* Whether an edge exists between two vertices.
|
|
126
|
+
* @param v1 - Endpoint A vertex or key.
|
|
127
|
+
* @param v2 - Endpoint B vertex or key.
|
|
128
|
+
* @returns `true` if present; otherwise `false`.
|
|
129
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
125
130
|
*/
|
|
126
131
|
hasEdge(v1, v2) {
|
|
127
132
|
const edge = this.getEdge(v1, v2);
|
|
128
133
|
return !!edge;
|
|
129
134
|
}
|
|
130
135
|
/**
|
|
131
|
-
*
|
|
132
|
-
*
|
|
136
|
+
* Add an edge by instance or by `(src, dest, weight?, value?)`.
|
|
137
|
+
* @param srcOrEdge - Edge instance or source vertex/key.
|
|
138
|
+
* @param dest - Destination vertex/key (when adding by pair).
|
|
139
|
+
* @param weight - Edge weight.
|
|
140
|
+
* @param value - Edge payload.
|
|
141
|
+
* @returns `true` if inserted; otherwise `false`.
|
|
142
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
133
143
|
*/
|
|
134
144
|
addEdge(srcOrEdge, dest, weight, value) {
|
|
135
145
|
if (srcOrEdge instanceof AbstractEdge) {
|
|
@@ -152,18 +162,12 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
152
162
|
}
|
|
153
163
|
}
|
|
154
164
|
/**
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
* @
|
|
160
|
-
*
|
|
161
|
-
* @param {VertexKey | VO} destOrKey - The `destOrKey` parameter represents the destination vertex of the edge. It can be
|
|
162
|
-
* either a `VertexKey` or a vertex object `VO`.
|
|
163
|
-
* @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrKey)
|
|
164
|
-
* and the destination vertex (destOrKey).
|
|
165
|
-
* @returns a boolean value. If the edge exists between the source and destination vertexMap, the function will update
|
|
166
|
-
* the weight of the edge and return true. If the edge does not exist, the function will return false.
|
|
165
|
+
* Set the weight of an existing edge.
|
|
166
|
+
* @param srcOrKey - Source vertex or key.
|
|
167
|
+
* @param destOrKey - Destination vertex or key.
|
|
168
|
+
* @param weight - New weight.
|
|
169
|
+
* @returns `true` if updated; otherwise `false`.
|
|
170
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
167
171
|
*/
|
|
168
172
|
setEdgeWeight(srcOrKey, destOrKey, weight) {
|
|
169
173
|
const edge = this.getEdge(srcOrKey, destOrKey);
|
|
@@ -176,15 +180,12 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
176
180
|
}
|
|
177
181
|
}
|
|
178
182
|
/**
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
* @
|
|
184
|
-
*
|
|
185
|
-
* @param {VO | VertexKey} v2 - The parameter `v2` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
186
|
-
* @param limit - The count of limitation of result array.
|
|
187
|
-
* @returns The function `getAllPathsBetween` returns an array of arrays of vertexMap (`VO[][]`).
|
|
183
|
+
* Enumerate simple paths up to a limit.
|
|
184
|
+
* @param v1 - Source vertex or key.
|
|
185
|
+
* @param v2 - Destination vertex or key.
|
|
186
|
+
* @param limit - Maximum number of paths to collect.
|
|
187
|
+
* @returns Array of paths (each path is an array of vertices).
|
|
188
|
+
* @remarks Time O(paths) worst-case exponential, Space O(V + paths)
|
|
188
189
|
*/
|
|
189
190
|
getAllPathsBetween(v1, v2, limit = 1000) {
|
|
190
191
|
const paths = [];
|
|
@@ -213,12 +214,10 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
213
214
|
return paths;
|
|
214
215
|
}
|
|
215
216
|
/**
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
* @param {VO[]} path - An array of vertexMap (VO) representing a path in a graph.
|
|
221
|
-
* @returns The function `getPathSumWeight` returns the sum of the weights of the edgeMap in the given path.
|
|
217
|
+
* Sum the weights along a vertex path.
|
|
218
|
+
* @param path - Sequence of vertices.
|
|
219
|
+
* @returns Path weight sum (0 if empty or edge missing).
|
|
220
|
+
* @remarks Time O(L), Space O(1) where L is path length
|
|
222
221
|
*/
|
|
223
222
|
getPathSumWeight(path) {
|
|
224
223
|
var _a;
|
|
@@ -229,21 +228,12 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
229
228
|
return sum;
|
|
230
229
|
}
|
|
231
230
|
/**
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
* @
|
|
238
|
-
* @param {VO | VertexKey} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
|
|
239
|
-
* you want to find the minimum cost or weight from the source vertex `v1`.
|
|
240
|
-
* @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edgeMap have weights.
|
|
241
|
-
* If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
|
|
242
|
-
* the edgeMap. If isWeight is set to false or not provided, the function will calculate the
|
|
243
|
-
* @returns The function `getMinCostBetween` returns a number representing the minimum cost between two vertexMap (`v1`
|
|
244
|
-
* and `v2`). If the `isWeight` parameter is `true`, it calculates the minimum weight among all paths between the
|
|
245
|
-
* vertexMap. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
|
|
246
|
-
* minimum number of
|
|
231
|
+
* Minimum hops/weight between two vertices.
|
|
232
|
+
* @param v1 - Source vertex or key.
|
|
233
|
+
* @param v2 - Destination vertex or key.
|
|
234
|
+
* @param isWeight - If `true`, compare by path weight; otherwise by hop count.
|
|
235
|
+
* @returns Minimum cost or `undefined` if missing/unreachable.
|
|
236
|
+
* @remarks Time O((V + E) log V) weighted / O(V + E) unweighted, Space O(V + E)
|
|
247
237
|
*/
|
|
248
238
|
getMinCostBetween(v1, v2, isWeight) {
|
|
249
239
|
if (isWeight === undefined)
|
|
@@ -257,7 +247,6 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
257
247
|
return min;
|
|
258
248
|
}
|
|
259
249
|
else {
|
|
260
|
-
// BFS
|
|
261
250
|
const vertex2 = this._getVertex(v2);
|
|
262
251
|
const vertex1 = this._getVertex(v1);
|
|
263
252
|
if (!(vertex1 && vertex2)) {
|
|
@@ -268,12 +257,11 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
268
257
|
visited.set(vertex1, true);
|
|
269
258
|
let cost = 0;
|
|
270
259
|
while (queue.length > 0) {
|
|
271
|
-
for (let i = 0
|
|
260
|
+
for (let i = 0, layerSize = queue.length; i < layerSize; i++) {
|
|
272
261
|
const cur = queue.shift();
|
|
273
262
|
if (cur === vertex2) {
|
|
274
263
|
return cost;
|
|
275
264
|
}
|
|
276
|
-
// TODO consider optimizing to AbstractGraph
|
|
277
265
|
if (cur !== undefined) {
|
|
278
266
|
const neighbors = this.getNeighbors(cur);
|
|
279
267
|
for (const neighbor of neighbors) {
|
|
@@ -290,23 +278,13 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
290
278
|
}
|
|
291
279
|
}
|
|
292
280
|
/**
|
|
293
|
-
*
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
-
*
|
|
298
|
-
* @
|
|
299
|
-
*
|
|
300
|
-
* @param {VO | VertexKey} v2 - VO | VertexKey - The second vertex or vertex ID between which we want to find the minimum
|
|
301
|
-
* path.
|
|
302
|
-
* @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edgeMap in finding the
|
|
303
|
-
* minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
|
|
304
|
-
* to false, the function will use breadth-first search (BFS) to find the minimum path.
|
|
305
|
-
* @param isDFS - If set to true, it enforces the use of getAllPathsBetween to first obtain all possible paths,
|
|
306
|
-
* followed by iterative computation of the shortest path. This approach may result in exponential time complexity,
|
|
307
|
-
* so the default method is to use the Dijkstra algorithm to obtain the shortest weighted path.
|
|
308
|
-
* @returns The function `getMinPathBetween` returns an array of vertexMap (`VO[]`) representing the minimum path between
|
|
309
|
-
* two vertexMap (`v1` and `v2`). If there is no path between the vertexMap, it returns `undefined`.
|
|
281
|
+
* Minimum path (as vertex sequence) between two vertices.
|
|
282
|
+
* @param v1 - Source vertex or key.
|
|
283
|
+
* @param v2 - Destination vertex or key.
|
|
284
|
+
* @param isWeight - If `true`, compare by path weight; otherwise by hop count.
|
|
285
|
+
* @param isDFS - For weighted mode only: if `true`, brute-force all paths; if `false`, use Dijkstra.
|
|
286
|
+
* @returns Vertex sequence, or `undefined`/empty when unreachable depending on branch.
|
|
287
|
+
* @remarks Time O((V + E) log V) weighted / O(V + E) unweighted, Space O(V + E)
|
|
310
288
|
*/
|
|
311
289
|
getMinPathBetween(v1, v2, isWeight, isDFS = false) {
|
|
312
290
|
var _a, _b;
|
|
@@ -329,11 +307,19 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
329
307
|
return allPaths[minIndex] || undefined;
|
|
330
308
|
}
|
|
331
309
|
else {
|
|
310
|
+
/**
|
|
311
|
+
* Dijkstra (binary-heap) shortest paths for non-negative weights.
|
|
312
|
+
* @param src - Source vertex or key.
|
|
313
|
+
* @param dest - Optional destination for early stop.
|
|
314
|
+
* @param getMinDist - If `true`, compute global minimum distance.
|
|
315
|
+
* @param genPaths - If `true`, also generate path arrays.
|
|
316
|
+
* @returns Result bag or `undefined` if source missing.
|
|
317
|
+
* @remarks Time O((V + E) log V), Space O(V + E)
|
|
318
|
+
*/
|
|
332
319
|
return (_b = (_a = this.dijkstra(v1, v2, true, true)) === null || _a === void 0 ? void 0 : _a.minPath) !== null && _b !== void 0 ? _b : [];
|
|
333
320
|
}
|
|
334
321
|
}
|
|
335
322
|
else {
|
|
336
|
-
// DFS
|
|
337
323
|
let minPath = [];
|
|
338
324
|
const vertex1 = this._getVertex(v1);
|
|
339
325
|
const vertex2 = this._getVertex(v2);
|
|
@@ -360,23 +346,13 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
360
346
|
}
|
|
361
347
|
}
|
|
362
348
|
/**
|
|
363
|
-
*
|
|
364
|
-
*
|
|
365
|
-
*
|
|
366
|
-
*
|
|
367
|
-
*
|
|
368
|
-
* @
|
|
369
|
-
*
|
|
370
|
-
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
371
|
-
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
372
|
-
* identifier. If no destination is provided, the value is set to `undefined`.
|
|
373
|
-
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
374
|
-
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
375
|
-
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
376
|
-
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
377
|
-
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
378
|
-
* shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
|
|
379
|
-
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
|
|
349
|
+
* Dijkstra without heap (array-based selection).
|
|
350
|
+
* @param src - Source vertex or key.
|
|
351
|
+
* @param dest - Optional destination for early stop.
|
|
352
|
+
* @param getMinDist - If `true`, compute global minimum distance.
|
|
353
|
+
* @param genPaths - If `true`, also generate path arrays.
|
|
354
|
+
* @returns Result bag or `undefined` if source missing.
|
|
355
|
+
* @remarks Time O(V^2 + E), Space O(V + E)
|
|
380
356
|
*/
|
|
381
357
|
dijkstraWithoutHeap(src, dest = undefined, getMinDist = false, genPaths = false) {
|
|
382
358
|
let minDist = Number.MAX_SAFE_INTEGER;
|
|
@@ -386,7 +362,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
386
362
|
const vertexMap = this._vertexMap;
|
|
387
363
|
const distMap = new Map();
|
|
388
364
|
const seen = new Set();
|
|
389
|
-
const preMap = new Map();
|
|
365
|
+
const preMap = new Map();
|
|
390
366
|
const srcVertex = this._getVertex(src);
|
|
391
367
|
const destVertex = dest ? this._getVertex(dest) : undefined;
|
|
392
368
|
if (!srcVertex) {
|
|
@@ -449,7 +425,6 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
449
425
|
if (edge) {
|
|
450
426
|
const curFromMap = distMap.get(cur);
|
|
451
427
|
const neighborFromMap = distMap.get(neighbor);
|
|
452
|
-
// TODO after no-non-undefined-assertion not ensure the logic
|
|
453
428
|
if (curFromMap !== undefined && neighborFromMap !== undefined) {
|
|
454
429
|
if (edge.weight + curFromMap < neighborFromMap) {
|
|
455
430
|
distMap.set(neighbor, edge.weight + curFromMap);
|
|
@@ -475,26 +450,6 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
475
450
|
getPaths(minDest);
|
|
476
451
|
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
477
452
|
}
|
|
478
|
-
/**
|
|
479
|
-
* Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
|
|
480
|
-
* Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
|
|
481
|
-
*
|
|
482
|
-
* 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.
|
|
483
|
-
* The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
|
|
484
|
-
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
485
|
-
* @param {VO | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
486
|
-
* start. It can be either a vertex object or a vertex ID.
|
|
487
|
-
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
488
|
-
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
489
|
-
* will calculate the shortest paths to all other vertexMap from the source vertex.
|
|
490
|
-
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
491
|
-
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
492
|
-
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
493
|
-
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
494
|
-
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
495
|
-
* shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
|
|
496
|
-
* @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
|
|
497
|
-
*/
|
|
498
453
|
dijkstra(src, dest = undefined, getMinDist = false, genPaths = false) {
|
|
499
454
|
var _a;
|
|
500
455
|
let minDist = Number.MAX_SAFE_INTEGER;
|
|
@@ -504,7 +459,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
504
459
|
const vertexMap = this._vertexMap;
|
|
505
460
|
const distMap = new Map();
|
|
506
461
|
const seen = new Set();
|
|
507
|
-
const preMap = new Map();
|
|
462
|
+
const preMap = new Map();
|
|
508
463
|
const srcVertex = this._getVertex(src);
|
|
509
464
|
const destVertex = dest ? this._getVertex(dest) : undefined;
|
|
510
465
|
if (!srcVertex)
|
|
@@ -518,11 +473,6 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
518
473
|
heap.add({ key: 0, value: srcVertex });
|
|
519
474
|
distMap.set(srcVertex, 0);
|
|
520
475
|
preMap.set(srcVertex, undefined);
|
|
521
|
-
/**
|
|
522
|
-
* The function `getPaths` retrieves all paths from vertexMap to a specified minimum vertex.
|
|
523
|
-
* @param {VO | undefined} minV - The parameter `minV` is of type `VO | undefined`. It represents the minimum vertex value or
|
|
524
|
-
* undefined.
|
|
525
|
-
*/
|
|
526
476
|
const getPaths = (minV) => {
|
|
527
477
|
for (const vertex of vertexMap) {
|
|
528
478
|
const vertexOrKey = vertex[1];
|
|
@@ -562,7 +512,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
562
512
|
const weight = (_a = this.getEdge(cur, neighbor)) === null || _a === void 0 ? void 0 : _a.weight;
|
|
563
513
|
if (typeof weight === 'number') {
|
|
564
514
|
const distSrcToNeighbor = distMap.get(neighbor);
|
|
565
|
-
if (distSrcToNeighbor) {
|
|
515
|
+
if (distSrcToNeighbor !== undefined) {
|
|
566
516
|
if (dist + weight < distSrcToNeighbor) {
|
|
567
517
|
heap.add({ key: dist + weight, value: neighbor });
|
|
568
518
|
preMap.set(neighbor, cur);
|
|
@@ -592,22 +542,13 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
592
542
|
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
593
543
|
}
|
|
594
544
|
/**
|
|
595
|
-
*
|
|
596
|
-
*
|
|
597
|
-
*
|
|
598
|
-
*
|
|
599
|
-
*
|
|
600
|
-
*
|
|
601
|
-
*
|
|
602
|
-
* @param {VO | VertexKey} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
|
|
603
|
-
* start calculating the shortest paths. It can be either a vertex object or a vertex ID.
|
|
604
|
-
* @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
|
|
605
|
-
* @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
|
|
606
|
-
* calculate the minimum distance from the source vertex to all other vertexMap in the graph. If `getMin` is set to
|
|
607
|
-
* `true`, the algorithm will find the minimum distance and update the `min` variable with the minimum
|
|
608
|
-
* @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all vertexMap from the source
|
|
609
|
-
* vertex.
|
|
610
|
-
* @returns The function `bellmanFord` returns an object with the following properties:
|
|
545
|
+
* Bellman-Ford single-source shortest paths with option to scan negative cycles.
|
|
546
|
+
* @param src - Source vertex or key.
|
|
547
|
+
* @param scanNegativeCycle - If `true`, also detect negative cycles.
|
|
548
|
+
* @param getMin - If `true`, compute global minimum distance.
|
|
549
|
+
* @param genPath - If `true`, generate path arrays via predecessor map.
|
|
550
|
+
* @returns Result bag including distances, predecessors, and optional cycle flag.
|
|
551
|
+
* @remarks Time O(V * E), Space O(V + E)
|
|
611
552
|
*/
|
|
612
553
|
bellmanFord(src, scanNegativeCycle, getMin, genPath) {
|
|
613
554
|
if (getMin === undefined)
|
|
@@ -617,10 +558,9 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
617
558
|
const srcVertex = this._getVertex(src);
|
|
618
559
|
const paths = [];
|
|
619
560
|
const distMap = new Map();
|
|
620
|
-
const preMap = new Map();
|
|
561
|
+
const preMap = new Map();
|
|
621
562
|
let min = Number.MAX_SAFE_INTEGER;
|
|
622
563
|
let minPath = [];
|
|
623
|
-
// TODO
|
|
624
564
|
let hasNegativeCycle;
|
|
625
565
|
if (scanNegativeCycle)
|
|
626
566
|
hasNegativeCycle = false;
|
|
@@ -696,31 +636,9 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
696
636
|
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
697
637
|
}
|
|
698
638
|
/**
|
|
699
|
-
*
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
703
|
-
* 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.
|
|
704
|
-
*/
|
|
705
|
-
/**
|
|
706
|
-
* BellmanFord time:O(VE) space:O(VO)
|
|
707
|
-
* one to rest pairs
|
|
708
|
-
* 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 edgeMap for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edgeMap, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
709
|
-
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
710
|
-
*/
|
|
711
|
-
/**
|
|
712
|
-
* Time Complexity: O(V^3) - Cubic time (Floyd-Warshall algorithm).
|
|
713
|
-
* Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
|
|
714
|
-
*
|
|
715
|
-
* Not support graph with negative weight cycle
|
|
716
|
-
* all pairs
|
|
717
|
-
* 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 edgeMap, and it can simultaneously compute shortest paths between any two nodes.
|
|
718
|
-
* The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertexMap in a
|
|
719
|
-
* graph.
|
|
720
|
-
* @returns The function `floydWarshall()` returns an object with two properties: `costs` and `predecessor`. The `costs`
|
|
721
|
-
* property is a 2D array of numbers representing the shortest path costs between vertexMap in a graph. The
|
|
722
|
-
* `predecessor` property is a 2D array of vertexMap (or `undefined`) representing the predecessor vertexMap in the shortest
|
|
723
|
-
* path between vertexMap in the
|
|
639
|
+
* Floyd–Warshall all-pairs shortest paths.
|
|
640
|
+
* @returns `{ costs, predecessor }` matrices.
|
|
641
|
+
* @remarks Time O(V^3), Space O(V^2)
|
|
724
642
|
*/
|
|
725
643
|
floydWarshall() {
|
|
726
644
|
var _a;
|
|
@@ -728,7 +646,6 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
728
646
|
const n = idAndVertices.length;
|
|
729
647
|
const costs = [];
|
|
730
648
|
const predecessor = [];
|
|
731
|
-
// successors
|
|
732
649
|
for (let i = 0; i < n; i++) {
|
|
733
650
|
costs[i] = [];
|
|
734
651
|
predecessor[i] = [];
|
|
@@ -754,8 +671,10 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
754
671
|
return { costs, predecessor };
|
|
755
672
|
}
|
|
756
673
|
/**
|
|
757
|
-
*
|
|
758
|
-
*
|
|
674
|
+
* Enumerate simple cycles (may be expensive).
|
|
675
|
+
* @param isInclude2Cycle - If `true`, include 2-cycles when graph semantics allow.
|
|
676
|
+
* @returns Array of cycles (each as array of vertex keys).
|
|
677
|
+
* @remarks Time exponential in worst-case, Space O(V + E)
|
|
759
678
|
*/
|
|
760
679
|
getCycles(isInclude2Cycle = false) {
|
|
761
680
|
const cycles = [];
|
|
@@ -780,7 +699,6 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
780
699
|
for (const vertex of this.vertexMap.values()) {
|
|
781
700
|
dfs(vertex, [], visited);
|
|
782
701
|
}
|
|
783
|
-
// Use a set to eliminate duplicate cycles
|
|
784
702
|
const uniqueCycles = new Map();
|
|
785
703
|
for (const cycle of cycles) {
|
|
786
704
|
const sorted = [...cycle].sort().toString();
|
|
@@ -790,24 +708,22 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
790
708
|
uniqueCycles.set(sorted, cycle);
|
|
791
709
|
}
|
|
792
710
|
}
|
|
793
|
-
|
|
711
|
+
/**
|
|
712
|
+
* Map entries to an array via callback.
|
|
713
|
+
* @param callback - `(key, value, index, self) => T`.
|
|
714
|
+
* @param thisArg - Optional `this` for callback.
|
|
715
|
+
* @returns Mapped results.
|
|
716
|
+
* @remarks Time O(V), Space O(V)
|
|
717
|
+
*/
|
|
794
718
|
return [...uniqueCycles].map(cycleString => cycleString[1]);
|
|
795
719
|
}
|
|
796
720
|
/**
|
|
797
|
-
*
|
|
798
|
-
*
|
|
799
|
-
*
|
|
800
|
-
*
|
|
801
|
-
*
|
|
802
|
-
* @
|
|
803
|
-
* `value`, `key`, `index`, and `this`. It is used to determine whether an element should be included
|
|
804
|
-
* in the filtered array. The callback function should return `true` if the element should be
|
|
805
|
-
* included, and `
|
|
806
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
807
|
-
* specify the value of `this` within the `predicate` function. It is used when you want to bind a
|
|
808
|
-
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
|
|
809
|
-
* @returns The `filter` method returns an array of key-value pairs `[VertexKey, V | undefined][]`
|
|
810
|
-
* that satisfy the given predicate function.
|
|
721
|
+
* Induced-subgraph filter: keep vertices where `predicate(key, value)` is true,
|
|
722
|
+
* and only keep edges whose endpoints both survive.
|
|
723
|
+
* @param predicate - `(key, value, index, self) => boolean`.
|
|
724
|
+
* @param thisArg - Optional `this` for callback.
|
|
725
|
+
* @returns A new graph of the same concrete class (`this` type).
|
|
726
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
811
727
|
*/
|
|
812
728
|
filter(predicate, thisArg) {
|
|
813
729
|
const filtered = [];
|
|
@@ -818,21 +734,23 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
818
734
|
}
|
|
819
735
|
index++;
|
|
820
736
|
}
|
|
821
|
-
return filtered;
|
|
737
|
+
return this._createLike(filtered, this._snapshotOptions());
|
|
822
738
|
}
|
|
823
739
|
/**
|
|
824
|
-
*
|
|
825
|
-
*
|
|
826
|
-
*
|
|
827
|
-
* The `map` function iterates over the elements of a collection and applies a callback function to
|
|
828
|
-
* each element, returning an array of the results.
|
|
829
|
-
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
830
|
-
* map. It takes four arguments:
|
|
831
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
832
|
-
* specify the value of `this` within the callback function. If `thisArg` is provided, it will be
|
|
833
|
-
* used as the `this` value when calling the callback function. If `thisArg` is not provided, `
|
|
834
|
-
* @returns The `map` function is returning an array of type `T[]`.
|
|
740
|
+
* Preserve the old behavior: return filtered entries as an array.
|
|
741
|
+
* @remarks Time O(V), Space O(V)
|
|
835
742
|
*/
|
|
743
|
+
filterEntries(predicate, thisArg) {
|
|
744
|
+
const filtered = [];
|
|
745
|
+
let index = 0;
|
|
746
|
+
for (const [key, value] of this) {
|
|
747
|
+
if (predicate.call(thisArg, key, value, index, this)) {
|
|
748
|
+
filtered.push([key, value]);
|
|
749
|
+
}
|
|
750
|
+
index++;
|
|
751
|
+
}
|
|
752
|
+
return filtered;
|
|
753
|
+
}
|
|
836
754
|
map(callback, thisArg) {
|
|
837
755
|
const mapped = [];
|
|
838
756
|
let index = 0;
|
|
@@ -842,23 +760,135 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
842
760
|
}
|
|
843
761
|
return mapped;
|
|
844
762
|
}
|
|
763
|
+
/**
|
|
764
|
+
* Create a deep clone of the graph with the same species.
|
|
765
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
766
|
+
*/
|
|
767
|
+
/**
|
|
768
|
+
* Create a deep clone of the graph with the same species.
|
|
769
|
+
* @returns A new graph of the same concrete class (`this` type).
|
|
770
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
771
|
+
*/
|
|
772
|
+
clone() {
|
|
773
|
+
return this._createLike(undefined, this._snapshotOptions());
|
|
774
|
+
}
|
|
775
|
+
// ===== Same-species factory & cloning helpers =====
|
|
776
|
+
/**
|
|
777
|
+
* Internal iterator over `[key, value]` entries in insertion order.
|
|
778
|
+
* @returns Iterator of `[VertexKey, V | undefined]`.
|
|
779
|
+
* @remarks Time O(V), Space O(1)
|
|
780
|
+
*/
|
|
845
781
|
*_getIterator() {
|
|
846
782
|
for (const vertex of this._vertexMap.values()) {
|
|
847
783
|
yield [vertex.key, vertex.value];
|
|
848
784
|
}
|
|
849
785
|
}
|
|
786
|
+
/**
|
|
787
|
+
* Capture configuration needed to reproduce the current graph.
|
|
788
|
+
* Currently the graph has no runtime options, so we return an empty object.
|
|
789
|
+
*/
|
|
790
|
+
/**
|
|
791
|
+
* Capture configuration needed to reproduce the current graph.
|
|
792
|
+
* @returns Options bag (opaque to callers).
|
|
793
|
+
* @remarks Time O(1), Space O(1)
|
|
794
|
+
*/
|
|
795
|
+
_snapshotOptions() {
|
|
796
|
+
return { graph: Object.assign({}, this._options) };
|
|
797
|
+
}
|
|
798
|
+
/**
|
|
799
|
+
* Create an empty graph instance of the same concrete species (Directed/Undirected/etc).
|
|
800
|
+
* @remarks Time O(1), Space O(1)
|
|
801
|
+
*/
|
|
802
|
+
/**
|
|
803
|
+
* Create an empty graph instance of the same concrete species.
|
|
804
|
+
* @param _options - Snapshot options from `_snapshotOptions()`.
|
|
805
|
+
* @returns A new empty graph instance of `this` type.
|
|
806
|
+
* @remarks Time O(1), Space O(1)
|
|
807
|
+
*/
|
|
808
|
+
_createInstance(_options) {
|
|
809
|
+
const Ctor = this.constructor;
|
|
810
|
+
const instance = new Ctor();
|
|
811
|
+
const graph = _options === null || _options === void 0 ? void 0 : _options.graph;
|
|
812
|
+
if (graph)
|
|
813
|
+
instance._options = Object.assign(Object.assign({}, instance._options), graph);
|
|
814
|
+
else
|
|
815
|
+
instance._options = Object.assign(Object.assign({}, instance._options), this._options);
|
|
816
|
+
return instance;
|
|
817
|
+
}
|
|
818
|
+
/**
|
|
819
|
+
* Create a same-species graph populated with the given entries.
|
|
820
|
+
* Also preserves edges between kept vertices from the source graph.
|
|
821
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
822
|
+
*/
|
|
823
|
+
/**
|
|
824
|
+
* Create a same-species graph populated with entries; preserves edges among kept vertices.
|
|
825
|
+
* @param iter - Optional entries to seed the new graph.
|
|
826
|
+
* @param options - Snapshot options.
|
|
827
|
+
* @returns A new graph of `this` type.
|
|
828
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
829
|
+
*/
|
|
830
|
+
_createLike(iter, options) {
|
|
831
|
+
const g = this._createInstance(options);
|
|
832
|
+
// 1) Add vertices
|
|
833
|
+
if (iter) {
|
|
834
|
+
for (const [k, v] of iter) {
|
|
835
|
+
g.addVertex(k, v);
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
else {
|
|
839
|
+
for (const [k, v] of this) {
|
|
840
|
+
g.addVertex(k, v);
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
// 2) Add edges whose endpoints exist in the new graph
|
|
844
|
+
const edges = this.edgeSet();
|
|
845
|
+
for (const e of edges) {
|
|
846
|
+
const ends = this.getEndsOfEdge(e);
|
|
847
|
+
if (!ends)
|
|
848
|
+
continue;
|
|
849
|
+
const [va, vb] = ends;
|
|
850
|
+
const ka = va.key;
|
|
851
|
+
const kb = vb.key;
|
|
852
|
+
const hasA = g.hasVertex ? g.hasVertex(ka) : false;
|
|
853
|
+
const hasB = g.hasVertex ? g.hasVertex(kb) : false;
|
|
854
|
+
if (hasA && hasB) {
|
|
855
|
+
const w = e.weight;
|
|
856
|
+
const val = e.value;
|
|
857
|
+
const newEdge = g.createEdge(ka, kb, w, val);
|
|
858
|
+
g._addEdge(newEdge);
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
return g;
|
|
862
|
+
}
|
|
863
|
+
/**
|
|
864
|
+
* Insert a pre-built vertex into the graph.
|
|
865
|
+
* @param newVertex - Concrete vertex instance.
|
|
866
|
+
* @returns `true` if inserted; `false` if key already exists.
|
|
867
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
868
|
+
*/
|
|
850
869
|
_addVertex(newVertex) {
|
|
851
870
|
if (this.hasVertex(newVertex)) {
|
|
852
871
|
return false;
|
|
853
|
-
// throw (new Error('Duplicated vertex key is not allowed'));
|
|
854
872
|
}
|
|
855
873
|
this._vertexMap.set(newVertex.key, newVertex);
|
|
856
874
|
return true;
|
|
857
875
|
}
|
|
876
|
+
/**
|
|
877
|
+
* Resolve a vertex key or instance to the concrete vertex instance.
|
|
878
|
+
* @param vertexOrKey - Vertex key or existing vertex.
|
|
879
|
+
* @returns Vertex instance or `undefined`.
|
|
880
|
+
* @remarks Time O(1), Space O(1)
|
|
881
|
+
*/
|
|
858
882
|
_getVertex(vertexOrKey) {
|
|
859
883
|
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
860
884
|
return this._vertexMap.get(vertexKey) || undefined;
|
|
861
885
|
}
|
|
886
|
+
/**
|
|
887
|
+
* Resolve a vertex key from a key or vertex instance.
|
|
888
|
+
* @param vertexOrKey - Vertex key or existing vertex.
|
|
889
|
+
* @returns The vertex key.
|
|
890
|
+
* @remarks Time O(1), Space O(1)
|
|
891
|
+
*/
|
|
862
892
|
_getVertexKey(vertexOrKey) {
|
|
863
893
|
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|
|
864
894
|
}
|