queue-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/README.md +1 -1
- 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,33 +1,22 @@
|
|
|
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.DirectedGraph = exports.DirectedEdge = exports.DirectedVertex = void 0;
|
|
4
11
|
const abstract_graph_1 = require("./abstract-graph");
|
|
5
12
|
const utils_1 = require("../../utils");
|
|
6
13
|
class DirectedVertex extends abstract_graph_1.AbstractVertex {
|
|
7
|
-
/**
|
|
8
|
-
* The constructor function initializes a vertex with an optional value.
|
|
9
|
-
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
|
10
|
-
* used to uniquely identify the vertex within a graph or data structure.
|
|
11
|
-
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It is used to initialize the value of the
|
|
12
|
-
* vertex. If no value is provided, the vertex will be initialized with a default value.
|
|
13
|
-
*/
|
|
14
14
|
constructor(key, value) {
|
|
15
15
|
super(key, value);
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
exports.DirectedVertex = DirectedVertex;
|
|
19
19
|
class DirectedEdge extends abstract_graph_1.AbstractEdge {
|
|
20
|
-
/**
|
|
21
|
-
* The constructor function initializes the source and destination vertexMap of an edge, along with an optional weight
|
|
22
|
-
* and value.
|
|
23
|
-
* @param {VertexKey} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
|
|
24
|
-
* a graph.
|
|
25
|
-
* @param {VertexKey} dest - The `dest` parameter represents the destination vertex of an edge. It is of type
|
|
26
|
-
* `VertexKey`, which is likely a unique identifier for a vertex in a graph.
|
|
27
|
-
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
|
28
|
-
* @param {E} [value] - The `value` parameter is an optional parameter of type `E`. It represents the value associated with
|
|
29
|
-
* the edge.
|
|
30
|
-
*/
|
|
31
20
|
constructor(src, dest, weight, value) {
|
|
32
21
|
super(weight, value);
|
|
33
22
|
this.src = src;
|
|
@@ -36,14 +25,22 @@ class DirectedEdge extends abstract_graph_1.AbstractEdge {
|
|
|
36
25
|
}
|
|
37
26
|
exports.DirectedEdge = DirectedEdge;
|
|
38
27
|
/**
|
|
39
|
-
*
|
|
28
|
+
* Directed graph implementation.
|
|
29
|
+
* @template V - Vertex value type.
|
|
30
|
+
* @template E - Edge value type.
|
|
31
|
+
* @template VO - Concrete vertex class (extends AbstractVertex<V>).
|
|
32
|
+
* @template EO - Concrete edge class (extends AbstractEdge<E>).
|
|
33
|
+
* @remarks Time O(1), Space O(1)
|
|
34
|
+
* @example examples will be generated by unit test
|
|
40
35
|
*/
|
|
41
36
|
class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
42
37
|
/**
|
|
43
|
-
*
|
|
38
|
+
* Construct a directed graph with runtime defaults.
|
|
39
|
+
* @param options - `GraphOptions<V>` (e.g. `vertexValueInitializer`, `defaultEdgeWeight`).
|
|
40
|
+
* @remarks Time O(1), Space O(1)
|
|
44
41
|
*/
|
|
45
|
-
constructor() {
|
|
46
|
-
super();
|
|
42
|
+
constructor(options) {
|
|
43
|
+
super(options);
|
|
47
44
|
this._outEdgeMap = new Map();
|
|
48
45
|
this._inEdgeMap = new Map();
|
|
49
46
|
}
|
|
@@ -60,40 +57,62 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
60
57
|
this._inEdgeMap = v;
|
|
61
58
|
}
|
|
62
59
|
/**
|
|
63
|
-
*
|
|
64
|
-
* @
|
|
65
|
-
*
|
|
66
|
-
* @
|
|
67
|
-
*
|
|
68
|
-
|
|
69
|
-
|
|
60
|
+
* Construct a directed graph from keys with value initializer `v => v`.
|
|
61
|
+
* @template K - Vertex key type.
|
|
62
|
+
* @param keys - Iterable of vertex keys.
|
|
63
|
+
* @returns DirectedGraph with all keys added.
|
|
64
|
+
* @remarks Time O(V), Space O(V)
|
|
65
|
+
*/
|
|
66
|
+
static fromKeys(keys) {
|
|
67
|
+
const g = new DirectedGraph({
|
|
68
|
+
vertexValueInitializer: (k) => k
|
|
69
|
+
});
|
|
70
|
+
for (const k of keys)
|
|
71
|
+
g.addVertex(k);
|
|
72
|
+
return g;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Construct a directed graph from `[key, value]` entries.
|
|
76
|
+
* @template V - Vertex value type.
|
|
77
|
+
* @param entries - Iterable of `[key, value]` pairs.
|
|
78
|
+
* @returns DirectedGraph with all vertices added.
|
|
79
|
+
* @remarks Time O(V), Space O(V)
|
|
80
|
+
*/
|
|
81
|
+
static fromEntries(entries) {
|
|
82
|
+
const g = new DirectedGraph();
|
|
83
|
+
for (const [k, v] of entries)
|
|
84
|
+
g.addVertex(k, v);
|
|
85
|
+
return g;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Create a directed vertex instance. Does not insert into the graph.
|
|
89
|
+
* @param key - Vertex identifier.
|
|
90
|
+
* @param value - Optional payload.
|
|
91
|
+
* @returns Concrete vertex instance.
|
|
92
|
+
* @remarks Time O(1), Space O(1)
|
|
70
93
|
*/
|
|
71
94
|
createVertex(key, value) {
|
|
72
95
|
return new DirectedVertex(key, value);
|
|
73
96
|
}
|
|
74
97
|
/**
|
|
75
|
-
*
|
|
76
|
-
* @param
|
|
77
|
-
* @param
|
|
78
|
-
* @param
|
|
79
|
-
*
|
|
80
|
-
* @
|
|
81
|
-
*
|
|
82
|
-
* @returns a new instance of a DirectedEdge object, casted as type EO.
|
|
98
|
+
* Create a directed edge instance. Does not insert into the graph.
|
|
99
|
+
* @param src - Source vertex key.
|
|
100
|
+
* @param dest - Destination vertex key.
|
|
101
|
+
* @param weight - Edge weight; defaults to `defaultEdgeWeight`.
|
|
102
|
+
* @param value - Edge payload.
|
|
103
|
+
* @returns Concrete edge instance.
|
|
104
|
+
* @remarks Time O(1), Space O(1)
|
|
83
105
|
*/
|
|
84
106
|
createEdge(src, dest, weight, value) {
|
|
85
|
-
|
|
107
|
+
var _a;
|
|
108
|
+
return new DirectedEdge(src, dest, (_a = weight !== null && weight !== void 0 ? weight : this.options.defaultEdgeWeight) !== null && _a !== void 0 ? _a : 1, value);
|
|
86
109
|
}
|
|
87
110
|
/**
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
* @
|
|
93
|
-
* @param {VO | VertexKey | undefined} destOrKey - The `destOrKey` parameter in the `getEdge` function represents the
|
|
94
|
-
* destination vertex of the edge. It can be either a vertex object (`VO`), a vertex ID (`VertexKey`), or `undefined` if the
|
|
95
|
-
* destination is not specified.
|
|
96
|
-
* @returns the first edge found between the source and destination vertexMap, or undefined if no such edge is found.
|
|
111
|
+
* Get the unique edge from `src` to `dest`, if present.
|
|
112
|
+
* @param srcOrKey - Source vertex or key.
|
|
113
|
+
* @param destOrKey - Destination vertex or key.
|
|
114
|
+
* @returns Edge instance or `undefined`.
|
|
115
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
97
116
|
*/
|
|
98
117
|
getEdge(srcOrKey, destOrKey) {
|
|
99
118
|
let edgeMap = [];
|
|
@@ -110,13 +129,11 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
110
129
|
return edgeMap[0] || undefined;
|
|
111
130
|
}
|
|
112
131
|
/**
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
* @
|
|
118
|
-
* @param {VO | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
|
|
119
|
-
* @returns the removed edge (EO) if it exists, or undefined if either the source or destination vertex does not exist.
|
|
132
|
+
* Delete edge `src -> dest` if present.
|
|
133
|
+
* @param srcOrKey - Source vertex or key.
|
|
134
|
+
* @param destOrKey - Destination vertex or key.
|
|
135
|
+
* @returns Removed edge or `undefined`.
|
|
136
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
120
137
|
*/
|
|
121
138
|
deleteEdgeSrcToDest(srcOrKey, destOrKey) {
|
|
122
139
|
const src = this._getVertex(srcOrKey);
|
|
@@ -136,17 +153,11 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
136
153
|
return removed;
|
|
137
154
|
}
|
|
138
155
|
/**
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
* @
|
|
144
|
-
* a `VertexKey` (key of a vertex).
|
|
145
|
-
* @param {VertexKey} [destVertexKey] - The `destVertexKey` parameter is an optional parameter that
|
|
146
|
-
* represents the key of the destination vertex of the edge. It is used to specify the destination
|
|
147
|
-
* vertex when the `edge` parameter is a vertex key. If `destVertexKey` is not provided, the function
|
|
148
|
-
* assumes that the `edge`
|
|
149
|
-
* @returns the removed edge (EO) or undefined if no edge was removed.
|
|
156
|
+
* Delete an edge by instance or by `(srcKey, destKey)`.
|
|
157
|
+
* @param edgeOrSrcVertexKey - Edge instance or source vertex/key.
|
|
158
|
+
* @param destVertexKey - Optional destination vertex/key when deleting by pair.
|
|
159
|
+
* @returns Removed edge or `undefined`.
|
|
160
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
150
161
|
*/
|
|
151
162
|
deleteEdge(edgeOrSrcVertexKey, destVertexKey) {
|
|
152
163
|
let removed = undefined;
|
|
@@ -176,15 +187,6 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
176
187
|
}
|
|
177
188
|
return removed;
|
|
178
189
|
}
|
|
179
|
-
/**
|
|
180
|
-
* Time Complexity: O(1) - Constant time for Map operations.
|
|
181
|
-
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
182
|
-
*
|
|
183
|
-
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
184
|
-
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
185
|
-
* (`VertexKey`).
|
|
186
|
-
* @returns The method is returning a boolean value.
|
|
187
|
-
*/
|
|
188
190
|
deleteVertex(vertexOrKey) {
|
|
189
191
|
let vertexKey;
|
|
190
192
|
let vertex;
|
|
@@ -197,9 +199,14 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
197
199
|
vertexKey = this._getVertexKey(vertexOrKey);
|
|
198
200
|
}
|
|
199
201
|
if (vertex) {
|
|
202
|
+
/**
|
|
203
|
+
* One-step neighbors following outgoing edges.
|
|
204
|
+
* @param vertexOrKey - Vertex or key.
|
|
205
|
+
* @returns Array of neighbor vertices.
|
|
206
|
+
* @remarks Time O(deg_out), Space O(deg_out)
|
|
207
|
+
*/
|
|
200
208
|
const neighbors = this.getNeighbors(vertex);
|
|
201
209
|
for (const neighbor of neighbors) {
|
|
202
|
-
// this._inEdgeMap.delete(neighbor);
|
|
203
210
|
this.deleteEdgeSrcToDest(vertex, neighbor);
|
|
204
211
|
}
|
|
205
212
|
this._outEdgeMap.delete(vertex);
|
|
@@ -207,17 +214,6 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
207
214
|
}
|
|
208
215
|
return this._vertexMap.delete(vertexKey);
|
|
209
216
|
}
|
|
210
|
-
/**
|
|
211
|
-
* Time Complexity: O(|E|) where |E| is the number of edgeMap
|
|
212
|
-
* Space Complexity: O(1)
|
|
213
|
-
*
|
|
214
|
-
* The function removes edgeMap between two vertexMap and returns the removed edgeMap.
|
|
215
|
-
* @param {VertexKey | VO} v1 - The parameter `v1` can be either a `VertexKey` or a `VO`. A `VertexKey` represents the
|
|
216
|
-
* unique identifier of a vertex in a graph, while `VO` represents the actual vertex object.
|
|
217
|
-
* @param {VertexKey | VO} v2 - The parameter `v2` represents either a `VertexKey` or a `VO` object. It is used to specify
|
|
218
|
-
* the second vertex in the edge that needs to be removed.
|
|
219
|
-
* @returns an array of removed edgeMap (EO[]).
|
|
220
|
-
*/
|
|
221
217
|
deleteEdgesBetween(v1, v2) {
|
|
222
218
|
const removed = [];
|
|
223
219
|
if (v1 && v2) {
|
|
@@ -231,13 +227,10 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
231
227
|
return removed;
|
|
232
228
|
}
|
|
233
229
|
/**
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
239
|
-
* (`VertexKey`).
|
|
240
|
-
* @returns The method `incomingEdgesOf` returns an array of edgeMap (`EO[]`).
|
|
230
|
+
* Incoming edges of a vertex.
|
|
231
|
+
* @param vertexOrKey - Vertex or key.
|
|
232
|
+
* @returns Array of incoming edges.
|
|
233
|
+
* @remarks Time O(deg_in), Space O(deg_in)
|
|
241
234
|
*/
|
|
242
235
|
incomingEdgesOf(vertexOrKey) {
|
|
243
236
|
const target = this._getVertex(vertexOrKey);
|
|
@@ -247,13 +240,10 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
247
240
|
return [];
|
|
248
241
|
}
|
|
249
242
|
/**
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can accept either a vertex object (`VO`) or a vertex ID
|
|
255
|
-
* (`VertexKey`).
|
|
256
|
-
* @returns The method `outgoingEdgesOf` returns an array of edgeMap (`EO[]`).
|
|
243
|
+
* Outgoing edges of a vertex.
|
|
244
|
+
* @param vertexOrKey - Vertex or key.
|
|
245
|
+
* @returns Array of outgoing edges.
|
|
246
|
+
* @remarks Time O(deg_out), Space O(deg_out)
|
|
257
247
|
*/
|
|
258
248
|
outgoingEdgesOf(vertexOrKey) {
|
|
259
249
|
const target = this._getVertex(vertexOrKey);
|
|
@@ -263,79 +253,52 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
263
253
|
return [];
|
|
264
254
|
}
|
|
265
255
|
/**
|
|
266
|
-
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
271
|
-
* @returns The sum of the out-degree and in-degree of the specified vertex or vertex ID.
|
|
256
|
+
* Degree (in + out) of a vertex.
|
|
257
|
+
* @param vertexOrKey - Vertex or key.
|
|
258
|
+
* @returns Non-negative integer.
|
|
259
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
272
260
|
*/
|
|
273
261
|
degreeOf(vertexOrKey) {
|
|
262
|
+
/**
|
|
263
|
+
* In-degree of a vertex.
|
|
264
|
+
* @param vertexOrKey - Vertex or key.
|
|
265
|
+
* @returns Non-negative integer.
|
|
266
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
267
|
+
*/
|
|
268
|
+
/**
|
|
269
|
+
* Out-degree of a vertex.
|
|
270
|
+
* @param vertexOrKey - Vertex or key.
|
|
271
|
+
* @returns Non-negative integer.
|
|
272
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
273
|
+
*/
|
|
274
274
|
return this.outDegreeOf(vertexOrKey) + this.inDegreeOf(vertexOrKey);
|
|
275
275
|
}
|
|
276
|
-
/**
|
|
277
|
-
* Time Complexity: O(1)
|
|
278
|
-
* Space Complexity: O(1)
|
|
279
|
-
*
|
|
280
|
-
* The function "inDegreeOf" returns the number of incoming edgeMap for a given vertex.
|
|
281
|
-
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
282
|
-
* @returns The number of incoming edgeMap of the specified vertex or vertex ID.
|
|
283
|
-
*/
|
|
284
276
|
inDegreeOf(vertexOrKey) {
|
|
285
277
|
return this.incomingEdgesOf(vertexOrKey).length;
|
|
286
278
|
}
|
|
287
|
-
/**
|
|
288
|
-
* Time Complexity: O(1)
|
|
289
|
-
* Space Complexity: O(1)
|
|
290
|
-
*
|
|
291
|
-
* The function `outDegreeOf` returns the number of outgoing edgeMap from a given vertex.
|
|
292
|
-
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
293
|
-
* @returns The number of outgoing edgeMap from the specified vertex or vertex ID.
|
|
294
|
-
*/
|
|
295
279
|
outDegreeOf(vertexOrKey) {
|
|
296
280
|
return this.outgoingEdgesOf(vertexOrKey).length;
|
|
297
281
|
}
|
|
298
282
|
/**
|
|
299
|
-
*
|
|
300
|
-
*
|
|
301
|
-
*
|
|
302
|
-
*
|
|
303
|
-
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
304
|
-
* @returns The function `edgesOf` returns an array of edgeMap.
|
|
283
|
+
* All incident edges of a vertex.
|
|
284
|
+
* @param vertexOrKey - Vertex or key.
|
|
285
|
+
* @returns Array of incident edges.
|
|
286
|
+
* @remarks Time O(deg_in + deg_out), Space O(deg_in + deg_out)
|
|
305
287
|
*/
|
|
306
288
|
edgesOf(vertexOrKey) {
|
|
307
289
|
return [...this.outgoingEdgesOf(vertexOrKey), ...this.incomingEdgesOf(vertexOrKey)];
|
|
308
290
|
}
|
|
309
|
-
/**
|
|
310
|
-
* Time Complexity: O(1)
|
|
311
|
-
* Space Complexity: O(1)
|
|
312
|
-
*
|
|
313
|
-
* The function "getEdgeSrc" returns the source vertex of an edge, or undefined if the edge does not exist.
|
|
314
|
-
* @param {EO} e - The parameter "e" is of type EO, which represents an edge in a graph.
|
|
315
|
-
* @returns either a vertex object (VO) or undefined.
|
|
316
|
-
*/
|
|
317
291
|
getEdgeSrc(e) {
|
|
318
292
|
return this._getVertex(e.src);
|
|
319
293
|
}
|
|
320
|
-
/**
|
|
321
|
-
* Time Complexity: O(1)
|
|
322
|
-
* Space Complexity: O(1)
|
|
323
|
-
*
|
|
324
|
-
* The function "getEdgeDest" returns the destination vertex of an edge.
|
|
325
|
-
* @param {EO} e - The parameter "e" is of type "EO", which represents an edge in a graph.
|
|
326
|
-
* @returns either a vertex object of type VO or undefined.
|
|
327
|
-
*/
|
|
328
294
|
getEdgeDest(e) {
|
|
329
295
|
return this._getVertex(e.dest);
|
|
330
296
|
}
|
|
331
297
|
/**
|
|
332
|
-
*
|
|
333
|
-
*
|
|
334
|
-
*
|
|
335
|
-
*
|
|
336
|
-
* @param {VO | VertexKey | undefined} vertex - The `vertex` parameter represents the starting vertex from which we want to
|
|
337
|
-
* find the destinations. It can be either a `VO` object, a `VertexKey` value, or `undefined`.
|
|
338
|
-
* @returns an array of vertexMap (VO[]).
|
|
298
|
+
* Direct children reachable by one outgoing edge.
|
|
299
|
+
* @param vertex - Vertex or key.
|
|
300
|
+
* @returns Array of neighbor vertices.
|
|
301
|
+
* @remarks Time O(deg_out), Space O(deg_out)
|
|
339
302
|
*/
|
|
340
303
|
getDestinations(vertex) {
|
|
341
304
|
if (vertex === undefined) {
|
|
@@ -352,20 +315,13 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
352
315
|
return destinations;
|
|
353
316
|
}
|
|
354
317
|
/**
|
|
355
|
-
*
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
359
|
-
* in the sorted order, or undefined if the graph contains a cycle.
|
|
360
|
-
* @param {'vertex' | 'key'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
|
|
361
|
-
* property to use for sorting the vertexMap. It can have two possible values: 'vertex' or 'key'. If 'vertex' is
|
|
362
|
-
* specified, the vertexMap themselves will be used for sorting. If 'key' is specified, the ids of
|
|
363
|
-
* @returns an array of vertexMap or vertex IDs in topological order. If there is a cycle in the graph, it returns undefined.
|
|
318
|
+
* Topological sort if DAG; returns `undefined` if a cycle exists.
|
|
319
|
+
* @param propertyName - `'key'` to map to keys; `'vertex'` to keep instances.
|
|
320
|
+
* @returns Array of keys/vertices, or `undefined` when cycle is found.
|
|
321
|
+
* @remarks Time O(V + E), Space O(V)
|
|
364
322
|
*/
|
|
365
323
|
topologicalSort(propertyName) {
|
|
366
324
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'key';
|
|
367
|
-
// When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
|
|
368
|
-
// When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
|
|
369
325
|
const statusMap = new Map();
|
|
370
326
|
for (const entry of this.vertexMap) {
|
|
371
327
|
statusMap.set(entry[1], 0);
|
|
@@ -398,13 +354,6 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
398
354
|
sorted = sorted.map(vertex => (vertex instanceof DirectedVertex ? vertex.key : vertex));
|
|
399
355
|
return sorted.reverse();
|
|
400
356
|
}
|
|
401
|
-
/**
|
|
402
|
-
* Time Complexity: O(|E|) where |E| is the number of edgeMap
|
|
403
|
-
* Space Complexity: O(|E|)
|
|
404
|
-
*
|
|
405
|
-
* The `edgeSet` function returns an array of all the edgeMap in the graph.
|
|
406
|
-
* @returns The `edgeSet()` method returns an array of edgeMap (`EO[]`).
|
|
407
|
-
*/
|
|
408
357
|
edgeSet() {
|
|
409
358
|
let edgeMap = [];
|
|
410
359
|
this._outEdgeMap.forEach(outEdges => {
|
|
@@ -412,15 +361,6 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
412
361
|
});
|
|
413
362
|
return edgeMap;
|
|
414
363
|
}
|
|
415
|
-
/**
|
|
416
|
-
* Time Complexity: O(|E|) where |E| is the number of edgeMap
|
|
417
|
-
* Space Complexity: O(1)
|
|
418
|
-
*
|
|
419
|
-
* The function `getNeighbors` returns an array of neighboring vertexMap of a given vertex or vertex ID in a graph.
|
|
420
|
-
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
421
|
-
* (`VertexKey`).
|
|
422
|
-
* @returns an array of vertexMap (VO[]).
|
|
423
|
-
*/
|
|
424
364
|
getNeighbors(vertexOrKey) {
|
|
425
365
|
const neighbors = [];
|
|
426
366
|
const vertex = this._getVertex(vertexOrKey);
|
|
@@ -428,7 +368,6 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
428
368
|
const outEdges = this.outgoingEdgesOf(vertex);
|
|
429
369
|
for (const outEdge of outEdges) {
|
|
430
370
|
const neighbor = this._getVertex(outEdge.dest);
|
|
431
|
-
// TODO after no-non-undefined-assertion not ensure the logic
|
|
432
371
|
if (neighbor) {
|
|
433
372
|
neighbors.push(neighbor);
|
|
434
373
|
}
|
|
@@ -437,14 +376,10 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
437
376
|
return neighbors;
|
|
438
377
|
}
|
|
439
378
|
/**
|
|
440
|
-
*
|
|
441
|
-
*
|
|
442
|
-
*
|
|
443
|
-
*
|
|
444
|
-
* otherwise it returns undefined.
|
|
445
|
-
* @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph.
|
|
446
|
-
* @returns The function `getEndsOfEdge` returns an array containing two vertexMap `[VO, VO]` if the edge exists in the
|
|
447
|
-
* graph. If the edge does not exist, it returns `undefined`.
|
|
379
|
+
* Resolve an edge's `[src, dest]` endpoints to vertex instances.
|
|
380
|
+
* @param edge - Edge instance.
|
|
381
|
+
* @returns `[src, dest]` or `undefined` if either endpoint is missing.
|
|
382
|
+
* @remarks Time O(1), Space O(1)
|
|
448
383
|
*/
|
|
449
384
|
getEndsOfEdge(edge) {
|
|
450
385
|
if (!this.hasEdge(edge.src, edge.dest)) {
|
|
@@ -460,18 +395,15 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
460
395
|
}
|
|
461
396
|
}
|
|
462
397
|
/**
|
|
463
|
-
*
|
|
464
|
-
*
|
|
465
|
-
* @return A boolean value
|
|
398
|
+
* Whether the graph has no vertices and no edges.
|
|
399
|
+
* @remarks Time O(1), Space O(1)
|
|
466
400
|
*/
|
|
467
401
|
isEmpty() {
|
|
468
402
|
return this.vertexMap.size === 0 && this.inEdgeMap.size === 0 && this.outEdgeMap.size === 0;
|
|
469
403
|
}
|
|
470
404
|
/**
|
|
471
|
-
*
|
|
472
|
-
*
|
|
473
|
-
*
|
|
474
|
-
* The clear function resets the vertex map, in-edge map, and out-edge map.
|
|
405
|
+
* Remove all vertices and edges.
|
|
406
|
+
* @remarks Time O(V + E), Space O(1)
|
|
475
407
|
*/
|
|
476
408
|
clear() {
|
|
477
409
|
this._vertexMap = new Map();
|
|
@@ -479,27 +411,17 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
479
411
|
this._outEdgeMap = new Map();
|
|
480
412
|
}
|
|
481
413
|
/**
|
|
482
|
-
*
|
|
483
|
-
*
|
|
484
|
-
* @
|
|
414
|
+
* Deep clone as the same concrete class.
|
|
415
|
+
* @returns A new graph of the same concrete class (`this` type).
|
|
416
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
485
417
|
*/
|
|
486
418
|
clone() {
|
|
487
|
-
|
|
488
|
-
cloned.vertexMap = new Map(this.vertexMap);
|
|
489
|
-
cloned.inEdgeMap = new Map(this.inEdgeMap);
|
|
490
|
-
cloned.outEdgeMap = new Map(this.outEdgeMap);
|
|
491
|
-
return cloned;
|
|
419
|
+
return super.clone();
|
|
492
420
|
}
|
|
493
421
|
/**
|
|
494
|
-
*
|
|
495
|
-
*
|
|
496
|
-
*
|
|
497
|
-
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
498
|
-
*
|
|
499
|
-
* The function `tarjan` implements the Tarjan's algorithm to find strongly connected components in a
|
|
500
|
-
* graph.
|
|
501
|
-
* @returns The function `tarjan()` returns an object with three properties: `dfnMap`, `lowMap`, and
|
|
502
|
-
* `SCCs`.
|
|
422
|
+
* Tarjan's algorithm for strongly connected components.
|
|
423
|
+
* @returns `{ dfnMap, lowMap, SCCs }`.
|
|
424
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
503
425
|
*/
|
|
504
426
|
tarjan() {
|
|
505
427
|
const dfnMap = new Map();
|
|
@@ -543,42 +465,34 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
543
465
|
return { dfnMap, lowMap, SCCs };
|
|
544
466
|
}
|
|
545
467
|
/**
|
|
546
|
-
*
|
|
547
|
-
*
|
|
548
|
-
*
|
|
549
|
-
* The function returns a map that associates each vertex object with its corresponding depth-first
|
|
550
|
-
* number.
|
|
551
|
-
* @returns A Map object with keys of type VO and values of type number.
|
|
468
|
+
* DFN index map computed by `tarjan()`.
|
|
469
|
+
* @returns Map from vertex to DFN index.
|
|
470
|
+
* @remarks Time O(V), Space O(V)
|
|
552
471
|
*/
|
|
553
472
|
getDFNMap() {
|
|
554
473
|
return this.tarjan().dfnMap;
|
|
555
474
|
}
|
|
556
475
|
/**
|
|
557
|
-
*
|
|
558
|
-
*
|
|
559
|
-
* @
|
|
560
|
-
* type `number`.
|
|
476
|
+
* LOW link map computed by `tarjan()`.
|
|
477
|
+
* @returns Map from vertex to LOW value.
|
|
478
|
+
* @remarks Time O(V), Space O(V)
|
|
561
479
|
*/
|
|
562
480
|
getLowMap() {
|
|
563
481
|
return this.tarjan().lowMap;
|
|
564
482
|
}
|
|
565
483
|
/**
|
|
566
|
-
*
|
|
567
|
-
*
|
|
568
|
-
* @
|
|
484
|
+
* Strongly connected components computed by `tarjan()`.
|
|
485
|
+
* @returns Map from SCC id to vertices.
|
|
486
|
+
* @remarks Time O(#SCC + V), Space O(V)
|
|
569
487
|
*/
|
|
570
488
|
getSCCs() {
|
|
571
489
|
return this.tarjan().SCCs;
|
|
572
490
|
}
|
|
573
491
|
/**
|
|
574
|
-
*
|
|
575
|
-
*
|
|
576
|
-
*
|
|
577
|
-
*
|
|
578
|
-
* @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph. It is the edge that
|
|
579
|
-
* needs to be added to the graph.
|
|
580
|
-
* @returns a boolean value. It returns true if the edge was successfully added to the graph, and false if either the
|
|
581
|
-
* source or destination vertex does not exist in the graph.
|
|
492
|
+
* Internal hook to attach a directed edge into adjacency maps.
|
|
493
|
+
* @param edge - Edge instance.
|
|
494
|
+
* @returns `true` if inserted; otherwise `false`.
|
|
495
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
582
496
|
*/
|
|
583
497
|
_addEdge(edge) {
|
|
584
498
|
if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
|
|
@@ -586,7 +500,6 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
586
500
|
}
|
|
587
501
|
const srcVertex = this._getVertex(edge.src);
|
|
588
502
|
const destVertex = this._getVertex(edge.dest);
|
|
589
|
-
// TODO after no-non-undefined-assertion not ensure the logic
|
|
590
503
|
if (srcVertex && destVertex) {
|
|
591
504
|
const srcOutEdges = this._outEdgeMap.get(srcVertex);
|
|
592
505
|
if (srcOutEdges) {
|