avl-tree-typed 2.0.5 → 2.1.1
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/common/index.js +1 -1
- package/dist/constants/index.js +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 +602 -873
- 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 +1 -0
- package/dist/utils/number.js +1 -2
- package/dist/utils/utils.d.ts +1 -1
- package/dist/utils/utils.js +9 -8
- package/package.json +15 -15
- 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 +196 -217
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +188 -102
- package/src/data-structures/binary-tree/avl-tree.ts +237 -206
- package/src/data-structures/binary-tree/binary-tree.ts +665 -896
- package/src/data-structures/binary-tree/bst.ts +565 -572
- package/src/data-structures/binary-tree/red-black-tree.ts +157 -223
- package/src/data-structures/binary-tree/tree-counter.ts +195 -219
- package/src/data-structures/binary-tree/tree-multi-map.ts +127 -98
- 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 +2 -0
- package/src/utils/utils.ts +9 -14
|
@@ -1,32 +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.UndirectedGraph = exports.UndirectedEdge = exports.UndirectedVertex = void 0;
|
|
4
11
|
const abstract_graph_1 = require("./abstract-graph");
|
|
5
12
|
const utils_1 = require("../../utils");
|
|
6
13
|
class UndirectedVertex 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 network.
|
|
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.UndirectedVertex = UndirectedVertex;
|
|
19
19
|
class UndirectedEdge extends abstract_graph_1.AbstractEdge {
|
|
20
|
-
/**
|
|
21
|
-
* The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
|
|
22
|
-
* value.
|
|
23
|
-
* @param {VertexKey} v1 - The first vertex ID of the edge.
|
|
24
|
-
* @param {VertexKey} v2 - The parameter `v2` is a `VertexKey`, which represents the identifier of the second vertex in a
|
|
25
|
-
* graph edge.
|
|
26
|
-
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
|
27
|
-
* @param {E} [value] - The "value" parameter is an optional parameter of type E. It is used to store a value associated
|
|
28
|
-
* with the edge.
|
|
29
|
-
*/
|
|
30
20
|
constructor(v1, v2, weight, value) {
|
|
31
21
|
super(weight, value);
|
|
32
22
|
this.endpoints = [v1, v2];
|
|
@@ -34,14 +24,22 @@ class UndirectedEdge extends abstract_graph_1.AbstractEdge {
|
|
|
34
24
|
}
|
|
35
25
|
exports.UndirectedEdge = UndirectedEdge;
|
|
36
26
|
/**
|
|
37
|
-
*
|
|
27
|
+
* Undirected graph implementation.
|
|
28
|
+
* @template V - Vertex value type.
|
|
29
|
+
* @template E - Edge value type.
|
|
30
|
+
* @template VO - Concrete vertex class (extends AbstractVertex<V>).
|
|
31
|
+
* @template EO - Concrete edge class (extends AbstractEdge<E>).
|
|
32
|
+
* @remarks Time O(1), Space O(1)
|
|
33
|
+
* @example examples will be generated by unit test
|
|
38
34
|
*/
|
|
39
35
|
class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
40
36
|
/**
|
|
41
|
-
*
|
|
37
|
+
* Construct an undirected graph with runtime defaults.
|
|
38
|
+
* @param options - `GraphOptions<V>` (e.g. `vertexValueInitializer`, `defaultEdgeWeight`).
|
|
39
|
+
* @remarks Time O(1), Space O(1)
|
|
42
40
|
*/
|
|
43
|
-
constructor() {
|
|
44
|
-
super();
|
|
41
|
+
constructor(options) {
|
|
42
|
+
super(options);
|
|
45
43
|
this._edgeMap = new Map();
|
|
46
44
|
}
|
|
47
45
|
get edgeMap() {
|
|
@@ -51,40 +49,62 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
51
49
|
this._edgeMap = v;
|
|
52
50
|
}
|
|
53
51
|
/**
|
|
54
|
-
*
|
|
55
|
-
* @
|
|
56
|
-
*
|
|
57
|
-
* @
|
|
58
|
-
*
|
|
59
|
-
|
|
60
|
-
|
|
52
|
+
* Construct an undirected graph from keys with value initializer `v => v`.
|
|
53
|
+
* @template K - Vertex key type.
|
|
54
|
+
* @param keys - Iterable of vertex keys.
|
|
55
|
+
* @returns UndirectedGraph with all keys added.
|
|
56
|
+
* @remarks Time O(V), Space O(V)
|
|
57
|
+
*/
|
|
58
|
+
static fromKeys(keys) {
|
|
59
|
+
const g = new UndirectedGraph({
|
|
60
|
+
vertexValueInitializer: (k) => k
|
|
61
|
+
});
|
|
62
|
+
for (const k of keys)
|
|
63
|
+
g.addVertex(k);
|
|
64
|
+
return g;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Construct an undirected graph from `[key, value]` entries.
|
|
68
|
+
* @template V - Vertex value type.
|
|
69
|
+
* @param entries - Iterable of `[key, value]` pairs.
|
|
70
|
+
* @returns UndirectedGraph with all vertices added.
|
|
71
|
+
* @remarks Time O(V), Space O(V)
|
|
72
|
+
*/
|
|
73
|
+
static fromEntries(entries) {
|
|
74
|
+
const g = new UndirectedGraph();
|
|
75
|
+
for (const [k, v] of entries)
|
|
76
|
+
g.addVertex(k, v);
|
|
77
|
+
return g;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Create an undirected vertex instance. Does not insert into the graph.
|
|
81
|
+
* @param key - Vertex identifier.
|
|
82
|
+
* @param value - Optional payload.
|
|
83
|
+
* @returns Concrete vertex instance.
|
|
84
|
+
* @remarks Time O(1), Space O(1)
|
|
61
85
|
*/
|
|
62
86
|
createVertex(key, value) {
|
|
63
|
-
return new UndirectedVertex(key, value
|
|
87
|
+
return new UndirectedVertex(key, value);
|
|
64
88
|
}
|
|
65
89
|
/**
|
|
66
|
-
*
|
|
67
|
-
* @param
|
|
68
|
-
* @param
|
|
69
|
-
* @param
|
|
70
|
-
*
|
|
71
|
-
* @
|
|
72
|
-
*
|
|
73
|
-
* @returns a new instance of the `UndirectedEdge` class, which is casted as type `EO`.
|
|
90
|
+
* Create an undirected edge instance. Does not insert into the graph.
|
|
91
|
+
* @param v1 - One endpoint key.
|
|
92
|
+
* @param v2 - The other endpoint key.
|
|
93
|
+
* @param weight - Edge weight; defaults to `defaultEdgeWeight`.
|
|
94
|
+
* @param value - Edge payload.
|
|
95
|
+
* @returns Concrete edge instance.
|
|
96
|
+
* @remarks Time O(1), Space O(1)
|
|
74
97
|
*/
|
|
75
98
|
createEdge(v1, v2, weight, value) {
|
|
76
|
-
|
|
99
|
+
var _a;
|
|
100
|
+
return new UndirectedEdge(v1, v2, (_a = weight !== null && weight !== void 0 ? weight : this.options.defaultEdgeWeight) !== null && _a !== void 0 ? _a : 1, value);
|
|
77
101
|
}
|
|
78
102
|
/**
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
* @
|
|
84
|
-
* object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
85
|
-
* @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
86
|
-
* object), `undefined`, or `VertexKey` (vertex ID).
|
|
87
|
-
* @returns an edge (EO) or undefined.
|
|
103
|
+
* Get an undirected edge between two vertices, if present.
|
|
104
|
+
* @param v1 - One vertex or key.
|
|
105
|
+
* @param v2 - The other vertex or key.
|
|
106
|
+
* @returns Edge instance or `undefined`.
|
|
107
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
88
108
|
*/
|
|
89
109
|
getEdge(v1, v2) {
|
|
90
110
|
var _a;
|
|
@@ -99,14 +119,11 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
99
119
|
return edgeMap ? edgeMap[0] || undefined : undefined;
|
|
100
120
|
}
|
|
101
121
|
/**
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
* @
|
|
107
|
-
* @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
|
|
108
|
-
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
109
|
-
* @returns the removed edge (EO) if it exists, or undefined if either of the endpoints (VO) does not exist.
|
|
122
|
+
* Delete a single undirected edge between two vertices.
|
|
123
|
+
* @param v1 - One vertex or key.
|
|
124
|
+
* @param v2 - The other vertex or key.
|
|
125
|
+
* @returns Removed edge or `undefined`.
|
|
126
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
110
127
|
*/
|
|
111
128
|
deleteEdgeBetween(v1, v2) {
|
|
112
129
|
const vertex1 = this._getVertex(v1);
|
|
@@ -126,17 +143,11 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
126
143
|
return removed;
|
|
127
144
|
}
|
|
128
145
|
/**
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
* @
|
|
134
|
-
* either an edge object or a vertex key.
|
|
135
|
-
* @param {VertexKey} [otherSideVertexKey] - The parameter `otherSideVertexKey` is an optional
|
|
136
|
-
* parameter that represents the key of the vertex on the other side of the edge. It is used when the
|
|
137
|
-
* `edgeOrOneSideVertexKey` parameter is a vertex key, and it specifies the key of the vertex on the
|
|
138
|
-
* other side of the
|
|
139
|
-
* @returns The `deleteEdge` function returns either the deleted edge object (EO) or `undefined`.
|
|
146
|
+
* Delete an edge by instance or by a pair of keys.
|
|
147
|
+
* @param edgeOrOneSideVertexKey - Edge instance or one endpoint vertex/key.
|
|
148
|
+
* @param otherSideVertexKey - Required second endpoint when deleting by pair.
|
|
149
|
+
* @returns Removed edge or `undefined`.
|
|
150
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
140
151
|
*/
|
|
141
152
|
deleteEdge(edgeOrOneSideVertexKey, otherSideVertexKey) {
|
|
142
153
|
let oneSide, otherSide;
|
|
@@ -161,13 +172,10 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
161
172
|
}
|
|
162
173
|
}
|
|
163
174
|
/**
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
169
|
-
* (`VertexKey`).
|
|
170
|
-
* @returns The method is returning a boolean value.
|
|
175
|
+
* Delete a vertex and remove it from all neighbor lists.
|
|
176
|
+
* @param vertexOrKey - Vertex or key.
|
|
177
|
+
* @returns `true` if removed; otherwise `false`.
|
|
178
|
+
* @remarks Time O(deg), Space O(1)
|
|
171
179
|
*/
|
|
172
180
|
deleteVertex(vertexOrKey) {
|
|
173
181
|
let vertexKey;
|
|
@@ -180,6 +188,12 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
180
188
|
vertex = vertexOrKey;
|
|
181
189
|
vertexKey = this._getVertexKey(vertexOrKey);
|
|
182
190
|
}
|
|
191
|
+
/**
|
|
192
|
+
* All neighbors connected via undirected edges.
|
|
193
|
+
* @param vertexOrKey - Vertex or key.
|
|
194
|
+
* @returns Array of neighbor vertices.
|
|
195
|
+
* @remarks Time O(deg), Space O(deg)
|
|
196
|
+
*/
|
|
183
197
|
const neighbors = this.getNeighbors(vertexOrKey);
|
|
184
198
|
if (vertex) {
|
|
185
199
|
neighbors.forEach(neighbor => {
|
|
@@ -196,14 +210,10 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
196
210
|
return this._vertexMap.delete(vertexKey);
|
|
197
211
|
}
|
|
198
212
|
/**
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
* vertex.
|
|
204
|
-
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
205
|
-
* @returns The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of
|
|
206
|
-
* edgeMap connected to that vertex.
|
|
213
|
+
* Degree of a vertex (# of incident undirected edges).
|
|
214
|
+
* @param vertexOrKey - Vertex or key.
|
|
215
|
+
* @returns Non-negative integer.
|
|
216
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
207
217
|
*/
|
|
208
218
|
degreeOf(vertexOrKey) {
|
|
209
219
|
var _a;
|
|
@@ -216,13 +226,10 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
216
226
|
}
|
|
217
227
|
}
|
|
218
228
|
/**
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
*
|
|
223
|
-
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`. A `VertexKey` is a
|
|
224
|
-
* unique identifier for a vertex in a graph, while `VO` represents the type of the vertex.
|
|
225
|
-
* @returns an array of edgeMap.
|
|
229
|
+
* Incident undirected edges of a vertex.
|
|
230
|
+
* @param vertexOrKey - Vertex or key.
|
|
231
|
+
* @returns Array of incident edges.
|
|
232
|
+
* @remarks Time O(deg), Space O(deg)
|
|
226
233
|
*/
|
|
227
234
|
edgesOf(vertexOrKey) {
|
|
228
235
|
const vertex = this._getVertex(vertexOrKey);
|
|
@@ -234,11 +241,9 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
234
241
|
}
|
|
235
242
|
}
|
|
236
243
|
/**
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
* The function "edgeSet" returns an array of unique edgeMap from a set of edgeMap.
|
|
241
|
-
* @returns The method `edgeSet()` returns an array of type `EO[]`.
|
|
244
|
+
* Unique set of undirected edges across endpoints.
|
|
245
|
+
* @returns Array of edges.
|
|
246
|
+
* @remarks Time O(E), Space O(E)
|
|
242
247
|
*/
|
|
243
248
|
edgeSet() {
|
|
244
249
|
const edgeSet = new Set();
|
|
@@ -249,15 +254,6 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
249
254
|
});
|
|
250
255
|
return [...edgeSet];
|
|
251
256
|
}
|
|
252
|
-
/**
|
|
253
|
-
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
254
|
-
* Space Complexity: O(|E|)
|
|
255
|
-
*
|
|
256
|
-
* The function "getNeighbors" returns an array of neighboring endpoints for a given vertex or vertex ID.
|
|
257
|
-
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
258
|
-
* (`VertexKey`).
|
|
259
|
-
* @returns an array of vertexMap (VO[]).
|
|
260
|
-
*/
|
|
261
257
|
getNeighbors(vertexOrKey) {
|
|
262
258
|
const neighbors = [];
|
|
263
259
|
const vertex = this._getVertex(vertexOrKey);
|
|
@@ -273,14 +269,10 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
273
269
|
return neighbors;
|
|
274
270
|
}
|
|
275
271
|
/**
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
* it returns undefined.
|
|
281
|
-
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
282
|
-
* @returns The function `getEndsOfEdge` returns an array containing two endpoints `[VO, VO]` if the edge exists in the
|
|
283
|
-
* graph. If the edge does not exist, it returns `undefined`.
|
|
272
|
+
* Resolve an edge's two endpoints to vertex instances.
|
|
273
|
+
* @param edge - Edge instance.
|
|
274
|
+
* @returns `[v1, v2]` or `undefined` if either endpoint is missing.
|
|
275
|
+
* @remarks Time O(1), Space O(1)
|
|
284
276
|
*/
|
|
285
277
|
getEndsOfEdge(edge) {
|
|
286
278
|
if (!this.hasEdge(edge.endpoints[0], edge.endpoints[1])) {
|
|
@@ -296,46 +288,32 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
296
288
|
}
|
|
297
289
|
}
|
|
298
290
|
/**
|
|
299
|
-
*
|
|
300
|
-
* @
|
|
291
|
+
* Whether the graph has no vertices and no edges.
|
|
292
|
+
* @remarks Time O(1), Space O(1)
|
|
301
293
|
*/
|
|
302
294
|
isEmpty() {
|
|
303
295
|
return this.vertexMap.size === 0 && this.edgeMap.size === 0;
|
|
304
296
|
}
|
|
305
297
|
/**
|
|
306
|
-
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
* The clear function resets the vertex and edge maps to empty maps.
|
|
298
|
+
* Remove all vertices and edges.
|
|
299
|
+
* @remarks Time O(V + E), Space O(1)
|
|
310
300
|
*/
|
|
311
301
|
clear() {
|
|
312
302
|
this._vertexMap = new Map();
|
|
313
303
|
this._edgeMap = new Map();
|
|
314
304
|
}
|
|
315
305
|
/**
|
|
316
|
-
*
|
|
317
|
-
*
|
|
318
|
-
*
|
|
319
|
-
* cloned graph. The clone function returns a reference to this newly created,
|
|
320
|
-
* cloned UndirectedGraph object.
|
|
321
|
-
*
|
|
322
|
-
* @return A new instance of the undirectedgraph class
|
|
306
|
+
* Deep clone as the same concrete class.
|
|
307
|
+
* @returns A new graph of the same concrete class (`this` type).
|
|
308
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
323
309
|
*/
|
|
324
310
|
clone() {
|
|
325
|
-
|
|
326
|
-
cloned.vertexMap = new Map(this.vertexMap);
|
|
327
|
-
cloned.edgeMap = new Map(this.edgeMap);
|
|
328
|
-
return cloned;
|
|
311
|
+
return super.clone();
|
|
329
312
|
}
|
|
330
313
|
/**
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
*
|
|
334
|
-
* 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time
|
|
335
|
-
*
|
|
336
|
-
* The function `tarjan` implements the Tarjan's algorithm to find bridges and cut vertices in a
|
|
337
|
-
* graph.
|
|
338
|
-
* @returns The function `tarjan()` returns an object with the following properties:
|
|
314
|
+
* Tarjan-based bridge and articulation point detection.
|
|
315
|
+
* @returns `{ dfnMap, lowMap, bridges, cutVertices }`.
|
|
316
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
339
317
|
*/
|
|
340
318
|
tarjan() {
|
|
341
319
|
const dfnMap = new Map();
|
|
@@ -388,40 +366,42 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
388
366
|
};
|
|
389
367
|
}
|
|
390
368
|
/**
|
|
391
|
-
*
|
|
392
|
-
* @returns
|
|
369
|
+
* Get bridges discovered by `tarjan()`.
|
|
370
|
+
* @returns Array of edges that are bridges.
|
|
371
|
+
* @remarks Time O(B), Space O(1)
|
|
393
372
|
*/
|
|
394
373
|
getBridges() {
|
|
395
374
|
return this.tarjan().bridges;
|
|
396
375
|
}
|
|
397
376
|
/**
|
|
398
|
-
*
|
|
399
|
-
* @returns
|
|
377
|
+
* Get articulation points discovered by `tarjan()`.
|
|
378
|
+
* @returns Array of cut vertices.
|
|
379
|
+
* @remarks Time O(C), Space O(1)
|
|
400
380
|
*/
|
|
401
381
|
getCutVertices() {
|
|
402
382
|
return this.tarjan().cutVertices;
|
|
403
383
|
}
|
|
404
384
|
/**
|
|
405
|
-
*
|
|
406
|
-
* @returns
|
|
385
|
+
* DFN index map computed by `tarjan()`.
|
|
386
|
+
* @returns Map from vertex to DFN index.
|
|
387
|
+
* @remarks Time O(V), Space O(V)
|
|
407
388
|
*/
|
|
408
389
|
getDFNMap() {
|
|
409
390
|
return this.tarjan().dfnMap;
|
|
410
391
|
}
|
|
411
392
|
/**
|
|
412
|
-
*
|
|
413
|
-
* @returns
|
|
393
|
+
* LOW link map computed by `tarjan()`.
|
|
394
|
+
* @returns Map from vertex to LOW value.
|
|
395
|
+
* @remarks Time O(V), Space O(V)
|
|
414
396
|
*/
|
|
415
397
|
getLowMap() {
|
|
416
398
|
return this.tarjan().lowMap;
|
|
417
399
|
}
|
|
418
400
|
/**
|
|
419
|
-
*
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
*
|
|
423
|
-
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
424
|
-
* @returns a boolean value.
|
|
401
|
+
* Internal hook to attach an undirected edge into adjacency maps.
|
|
402
|
+
* @param edge - Edge instance.
|
|
403
|
+
* @returns `true` if both endpoints exist; otherwise `false`.
|
|
404
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
425
405
|
*/
|
|
426
406
|
_addEdge(edge) {
|
|
427
407
|
for (const end of edge.endpoints) {
|