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.
Files changed (101) hide show
  1. package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
  2. package/dist/data-structures/base/iterable-element-base.js +149 -107
  3. package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
  4. package/dist/data-structures/base/iterable-entry-base.js +59 -116
  5. package/dist/data-structures/base/linear-base.d.ts +250 -192
  6. package/dist/data-structures/base/linear-base.js +137 -274
  7. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  8. package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
  9. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  10. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  11. package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
  12. package/dist/data-structures/binary-tree/avl-tree.js +208 -195
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
  14. package/dist/data-structures/binary-tree/binary-tree.js +612 -879
  15. package/dist/data-structures/binary-tree/bst.d.ts +258 -306
  16. package/dist/data-structures/binary-tree/bst.js +505 -481
  17. package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  18. package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
  19. package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
  20. package/dist/data-structures/binary-tree/tree-counter.js +172 -203
  21. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  22. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
  23. package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
  24. package/dist/data-structures/graph/abstract-graph.js +267 -237
  25. package/dist/data-structures/graph/directed-graph.d.ts +108 -224
  26. package/dist/data-structures/graph/directed-graph.js +146 -233
  27. package/dist/data-structures/graph/map-graph.d.ts +49 -55
  28. package/dist/data-structures/graph/map-graph.js +56 -59
  29. package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
  30. package/dist/data-structures/graph/undirected-graph.js +129 -149
  31. package/dist/data-structures/hash/hash-map.d.ts +164 -338
  32. package/dist/data-structures/hash/hash-map.js +270 -457
  33. package/dist/data-structures/heap/heap.d.ts +214 -289
  34. package/dist/data-structures/heap/heap.js +340 -349
  35. package/dist/data-structures/heap/max-heap.d.ts +11 -47
  36. package/dist/data-structures/heap/max-heap.js +11 -66
  37. package/dist/data-structures/heap/min-heap.d.ts +12 -47
  38. package/dist/data-structures/heap/min-heap.js +11 -66
  39. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  40. package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
  41. package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  42. package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
  43. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  44. package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
  45. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  46. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
  47. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  48. package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
  49. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
  50. package/dist/data-structures/priority-queue/priority-queue.js +8 -83
  51. package/dist/data-structures/queue/deque.d.ts +227 -254
  52. package/dist/data-structures/queue/deque.js +309 -348
  53. package/dist/data-structures/queue/queue.d.ts +180 -201
  54. package/dist/data-structures/queue/queue.js +265 -248
  55. package/dist/data-structures/stack/stack.d.ts +124 -102
  56. package/dist/data-structures/stack/stack.js +181 -125
  57. package/dist/data-structures/trie/trie.d.ts +164 -165
  58. package/dist/data-structures/trie/trie.js +189 -172
  59. package/dist/interfaces/binary-tree.d.ts +56 -6
  60. package/dist/interfaces/graph.d.ts +16 -0
  61. package/dist/types/data-structures/base/base.d.ts +1 -1
  62. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
  63. package/dist/types/utils/utils.d.ts +6 -6
  64. package/dist/utils/utils.d.ts +110 -49
  65. package/dist/utils/utils.js +148 -73
  66. package/package.json +2 -2
  67. package/src/data-structures/base/iterable-element-base.ts +238 -115
  68. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  69. package/src/data-structures/base/linear-base.ts +271 -277
  70. package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
  71. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
  72. package/src/data-structures/binary-tree/avl-tree.ts +239 -206
  73. package/src/data-structures/binary-tree/binary-tree.ts +681 -905
  74. package/src/data-structures/binary-tree/bst.ts +568 -570
  75. package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
  76. package/src/data-structures/binary-tree/tree-counter.ts +199 -218
  77. package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
  78. package/src/data-structures/graph/abstract-graph.ts +339 -264
  79. package/src/data-structures/graph/directed-graph.ts +146 -236
  80. package/src/data-structures/graph/map-graph.ts +63 -60
  81. package/src/data-structures/graph/undirected-graph.ts +129 -152
  82. package/src/data-structures/hash/hash-map.ts +274 -496
  83. package/src/data-structures/heap/heap.ts +389 -402
  84. package/src/data-structures/heap/max-heap.ts +12 -76
  85. package/src/data-structures/heap/min-heap.ts +13 -76
  86. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  87. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  88. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  89. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  90. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  91. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  92. package/src/data-structures/queue/deque.ts +381 -357
  93. package/src/data-structures/queue/queue.ts +310 -264
  94. package/src/data-structures/stack/stack.ts +217 -131
  95. package/src/data-structures/trie/trie.ts +240 -175
  96. package/src/interfaces/binary-tree.ts +240 -6
  97. package/src/interfaces/graph.ts +37 -0
  98. package/src/types/data-structures/base/base.ts +5 -5
  99. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  100. package/src/types/utils/utils.ts +9 -5
  101. package/src/utils/utils.ts +152 -86
@@ -5,19 +5,13 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { TopologicalStatus, VertexKey } from '../../types';
8
+
9
+ import type { GraphOptions, TopologicalStatus, VertexKey } from '../../types';
9
10
  import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
10
11
  import { IGraph } from '../../interfaces';
11
12
  import { arrayRemove } from '../../utils';
12
13
 
13
14
  export class DirectedVertex<V = any> extends AbstractVertex<V> {
14
- /**
15
- * The constructor function initializes a vertex with an optional value.
16
- * @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
17
- * used to uniquely identify the vertex within a graph or data structure.
18
- * @param {V} [value] - The "value" parameter is an optional parameter of type V. It is used to initialize the value of the
19
- * vertex. If no value is provided, the vertex will be initialized with a default value.
20
- */
21
15
  constructor(key: VertexKey, value?: V) {
22
16
  super(key, value);
23
17
  }
@@ -27,17 +21,6 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
27
21
  src: VertexKey;
28
22
  dest: VertexKey;
29
23
 
30
- /**
31
- * The constructor function initializes the source and destination vertexMap of an edge, along with an optional weight
32
- * and value.
33
- * @param {VertexKey} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
34
- * a graph.
35
- * @param {VertexKey} dest - The `dest` parameter represents the destination vertex of an edge. It is of type
36
- * `VertexKey`, which is likely a unique identifier for a vertex in a graph.
37
- * @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
38
- * @param {E} [value] - The `value` parameter is an optional parameter of type `E`. It represents the value associated with
39
- * the edge.
40
- */
41
24
  constructor(src: VertexKey, dest: VertexKey, weight?: number, value?: E) {
42
25
  super(weight, value);
43
26
  this.src = src;
@@ -46,7 +29,13 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
46
29
  }
47
30
 
48
31
  /**
49
- *
32
+ * Directed graph implementation.
33
+ * @template V - Vertex value type.
34
+ * @template E - Edge value type.
35
+ * @template VO - Concrete vertex class (extends AbstractVertex<V>).
36
+ * @template EO - Concrete edge class (extends AbstractEdge<E>).
37
+ * @remarks Time O(1), Space O(1)
38
+ * @example examples will be generated by unit test
50
39
  */
51
40
  export class DirectedGraph<
52
41
  V = any,
@@ -58,10 +47,12 @@ export class DirectedGraph<
58
47
  implements IGraph<V, E, VO, EO>
59
48
  {
60
49
  /**
61
- * The constructor function initializes an instance of a class.
50
+ * Construct a directed graph with runtime defaults.
51
+ * @param options - `GraphOptions<V>` (e.g. `vertexValueInitializer`, `defaultEdgeWeight`).
52
+ * @remarks Time O(1), Space O(1)
62
53
  */
63
- constructor() {
64
- super();
54
+ constructor(options?: Partial<GraphOptions<V>>) {
55
+ super(options);
65
56
  }
66
57
 
67
58
  protected _outEdgeMap: Map<VO, EO[]> = new Map<VO, EO[]>();
@@ -85,42 +76,65 @@ export class DirectedGraph<
85
76
  }
86
77
 
87
78
  /**
88
- * The function creates a new vertex with an optional value and returns it.
89
- * @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is of type `VertexKey`, which
90
- * could be a number or a string depending on how you want to identify your vertexMap.
91
- * @param [value] - The 'value' parameter is an optional value that can be assigned to the vertex. If a value is provided,
92
- * it will be assigned to the 'value' property of the vertex. If no value is provided, the 'value' property will be
93
- * assigned the same value as the 'key' parameter
94
- * @returns a new instance of a DirectedVertex object, casted as type VO.
79
+ * Construct a directed graph from keys with value initializer `v => v`.
80
+ * @template K - Vertex key type.
81
+ * @param keys - Iterable of vertex keys.
82
+ * @returns DirectedGraph with all keys added.
83
+ * @remarks Time O(V), Space O(V)
95
84
  */
96
- createVertex(key: VertexKey, value?: V): VO {
85
+ static fromKeys<K extends VertexKey>(keys: Iterable<K>): DirectedGraph<K, any, DirectedVertex<K>, DirectedEdge<any>> {
86
+ const g: DirectedGraph<K, any, DirectedVertex<K>, DirectedEdge<any>> = new DirectedGraph<K, any>({
87
+ vertexValueInitializer: (k: VertexKey) => k as K
88
+ });
89
+ for (const k of keys) g.addVertex(k);
90
+ return g;
91
+ }
92
+
93
+ /**
94
+ * Construct a directed graph from `[key, value]` entries.
95
+ * @template V - Vertex value type.
96
+ * @param entries - Iterable of `[key, value]` pairs.
97
+ * @returns DirectedGraph with all vertices added.
98
+ * @remarks Time O(V), Space O(V)
99
+ */
100
+ static fromEntries<V>(
101
+ entries: Iterable<[VertexKey, V]>
102
+ ): DirectedGraph<V, any, DirectedVertex<V>, DirectedEdge<any>> {
103
+ const g: DirectedGraph<V, any, DirectedVertex<V>, DirectedEdge<any>> = new DirectedGraph<V, any>();
104
+ for (const [k, v] of entries) g.addVertex(k, v);
105
+ return g;
106
+ }
107
+
108
+ /**
109
+ * Create a directed vertex instance. Does not insert into the graph.
110
+ * @param key - Vertex identifier.
111
+ * @param value - Optional payload.
112
+ * @returns Concrete vertex instance.
113
+ * @remarks Time O(1), Space O(1)
114
+ */
115
+ createVertex(key: VertexKey, value?: VO['value']): VO {
97
116
  return new DirectedVertex(key, value) as VO;
98
117
  }
99
118
 
100
119
  /**
101
- * The function creates a directed edge between two vertexMap with an optional weight and value.
102
- * @param {VertexKey} src - The source vertex ID of the edge. It represents the starting point of the edge.
103
- * @param {VertexKey} dest - The `dest` parameter is the identifier of the destination vertex for the edge.
104
- * @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge. If no
105
- * weight is provided, it defaults to 1.
106
- * @param [value] - The 'value' parameter is an optional value that can be assigned to the edge. It can be of any type and
107
- * is used to store additional information or data associated with the edge.
108
- * @returns a new instance of a DirectedEdge object, casted as type EO.
120
+ * Create a directed edge instance. Does not insert into the graph.
121
+ * @param src - Source vertex key.
122
+ * @param dest - Destination vertex key.
123
+ * @param weight - Edge weight; defaults to `defaultEdgeWeight`.
124
+ * @param value - Edge payload.
125
+ * @returns Concrete edge instance.
126
+ * @remarks Time O(1), Space O(1)
109
127
  */
110
128
  createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO {
111
- return new DirectedEdge(src, dest, weight ?? 1, value) as EO;
129
+ return new DirectedEdge(src, dest, weight ?? this.options.defaultEdgeWeight ?? 1, value) as EO;
112
130
  }
113
131
 
114
132
  /**
115
- * Time Complexity: O(|V|) where |V| is the number of vertexMap
116
- * Space Complexity: O(1)
117
- *
118
- * The `getEdge` function retrieves an edge between two vertexMap based on their source and destination IDs.
119
- * @param {VO | VertexKey | undefined} srcOrKey - The source vertex or its ID. It can be either a vertex object or a vertex ID.
120
- * @param {VO | VertexKey | undefined} destOrKey - The `destOrKey` parameter in the `getEdge` function represents the
121
- * destination vertex of the edge. It can be either a vertex object (`VO`), a vertex ID (`VertexKey`), or `undefined` if the
122
- * destination is not specified.
123
- * @returns the first edge found between the source and destination vertexMap, or undefined if no such edge is found.
133
+ * Get the unique edge from `src` to `dest`, if present.
134
+ * @param srcOrKey - Source vertex or key.
135
+ * @param destOrKey - Destination vertex or key.
136
+ * @returns Edge instance or `undefined`.
137
+ * @remarks Time O(1) avg, Space O(1)
124
138
  */
125
139
  getEdge(srcOrKey: VO | VertexKey | undefined, destOrKey: VO | VertexKey | undefined): EO | undefined {
126
140
  let edgeMap: EO[] = [];
@@ -141,13 +155,11 @@ export class DirectedGraph<
141
155
  }
142
156
 
143
157
  /**
144
- * Time Complexity: O(|E|) where |E| is the number of edgeMap
145
- * Space Complexity: O(1)
146
- *
147
- * The function removes an edge between two vertexMap in a graph and returns the removed edge.
148
- * @param {VO | VertexKey} srcOrKey - The source vertex or its ID.
149
- * @param {VO | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
150
- * @returns the removed edge (EO) if it exists, or undefined if either the source or destination vertex does not exist.
158
+ * Delete edge `src -> dest` if present.
159
+ * @param srcOrKey - Source vertex or key.
160
+ * @param destOrKey - Destination vertex or key.
161
+ * @returns Removed edge or `undefined`.
162
+ * @remarks Time O(1) avg, Space O(1)
151
163
  */
152
164
  deleteEdgeSrcToDest(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO | undefined {
153
165
  const src: VO | undefined = this._getVertex(srcOrKey);
@@ -170,17 +182,11 @@ export class DirectedGraph<
170
182
  }
171
183
 
172
184
  /**
173
- * Time Complexity: O(E) where E is the number of edgeMap
174
- * Space Complexity: O(1)
175
- *
176
- * The `deleteEdge` function removes an edge from a graph and returns the removed edge.
177
- * @param {EO | VertexKey} edgeOrSrcVertexKey - The `edge` parameter can be either an `EO` object (edge object) or
178
- * a `VertexKey` (key of a vertex).
179
- * @param {VertexKey} [destVertexKey] - The `destVertexKey` parameter is an optional parameter that
180
- * represents the key of the destination vertex of the edge. It is used to specify the destination
181
- * vertex when the `edge` parameter is a vertex key. If `destVertexKey` is not provided, the function
182
- * assumes that the `edge`
183
- * @returns the removed edge (EO) or undefined if no edge was removed.
185
+ * Delete an edge by instance or by `(srcKey, destKey)`.
186
+ * @param edgeOrSrcVertexKey - Edge instance or source vertex/key.
187
+ * @param destVertexKey - Optional destination vertex/key when deleting by pair.
188
+ * @returns Removed edge or `undefined`.
189
+ * @remarks Time O(1) avg, Space O(1)
184
190
  */
185
191
  deleteEdge(edgeOrSrcVertexKey: EO | VertexKey, destVertexKey?: VertexKey): EO | undefined {
186
192
  let removed: EO | undefined = undefined;
@@ -212,15 +218,6 @@ export class DirectedGraph<
212
218
  return removed;
213
219
  }
214
220
 
215
- /**
216
- * Time Complexity: O(1) - Constant time for Map operations.
217
- * Space Complexity: O(1) - Constant space, as it creates only a few variables.
218
- *
219
- * The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
220
- * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
221
- * (`VertexKey`).
222
- * @returns The method is returning a boolean value.
223
- */
224
221
  deleteVertex(vertexOrKey: VO | VertexKey): boolean {
225
222
  let vertexKey: VertexKey;
226
223
  let vertex: VO | undefined;
@@ -233,9 +230,14 @@ export class DirectedGraph<
233
230
  }
234
231
 
235
232
  if (vertex) {
233
+ /**
234
+ * One-step neighbors following outgoing edges.
235
+ * @param vertexOrKey - Vertex or key.
236
+ * @returns Array of neighbor vertices.
237
+ * @remarks Time O(deg_out), Space O(deg_out)
238
+ */
236
239
  const neighbors = this.getNeighbors(vertex);
237
240
  for (const neighbor of neighbors) {
238
- // this._inEdgeMap.delete(neighbor);
239
241
  this.deleteEdgeSrcToDest(vertex, neighbor);
240
242
  }
241
243
  this._outEdgeMap.delete(vertex);
@@ -245,17 +247,6 @@ export class DirectedGraph<
245
247
  return this._vertexMap.delete(vertexKey);
246
248
  }
247
249
 
248
- /**
249
- * Time Complexity: O(|E|) where |E| is the number of edgeMap
250
- * Space Complexity: O(1)
251
- *
252
- * The function removes edgeMap between two vertexMap and returns the removed edgeMap.
253
- * @param {VertexKey | VO} v1 - The parameter `v1` can be either a `VertexKey` or a `VO`. A `VertexKey` represents the
254
- * unique identifier of a vertex in a graph, while `VO` represents the actual vertex object.
255
- * @param {VertexKey | VO} v2 - The parameter `v2` represents either a `VertexKey` or a `VO` object. It is used to specify
256
- * the second vertex in the edge that needs to be removed.
257
- * @returns an array of removed edgeMap (EO[]).
258
- */
259
250
  deleteEdgesBetween(v1: VertexKey | VO, v2: VertexKey | VO): EO[] {
260
251
  const removed: EO[] = [];
261
252
 
@@ -271,13 +262,10 @@ export class DirectedGraph<
271
262
  }
272
263
 
273
264
  /**
274
- * Time Complexity: O(1)
275
- * Space Complexity: O(1)
276
- *
277
- * The function `incomingEdgesOf` returns an array of incoming edgeMap for a given vertex or vertex ID.
278
- * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
279
- * (`VertexKey`).
280
- * @returns The method `incomingEdgesOf` returns an array of edgeMap (`EO[]`).
265
+ * Incoming edges of a vertex.
266
+ * @param vertexOrKey - Vertex or key.
267
+ * @returns Array of incoming edges.
268
+ * @remarks Time O(deg_in), Space O(deg_in)
281
269
  */
282
270
  incomingEdgesOf(vertexOrKey: VO | VertexKey): EO[] {
283
271
  const target = this._getVertex(vertexOrKey);
@@ -288,13 +276,10 @@ export class DirectedGraph<
288
276
  }
289
277
 
290
278
  /**
291
- * Time Complexity: O(1)
292
- * Space Complexity: O(1)
293
- *
294
- * The function `outgoingEdgesOf` returns an array of outgoing edgeMap from a given vertex or vertex ID.
295
- * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can accept either a vertex object (`VO`) or a vertex ID
296
- * (`VertexKey`).
297
- * @returns The method `outgoingEdgesOf` returns an array of edgeMap (`EO[]`).
279
+ * Outgoing edges of a vertex.
280
+ * @param vertexOrKey - Vertex or key.
281
+ * @returns Array of outgoing edges.
282
+ * @remarks Time O(deg_out), Space O(deg_out)
298
283
  */
299
284
  outgoingEdgesOf(vertexOrKey: VO | VertexKey): EO[] {
300
285
  const target = this._getVertex(vertexOrKey);
@@ -305,85 +290,58 @@ export class DirectedGraph<
305
290
  }
306
291
 
307
292
  /**
308
- * Time Complexity: O(1)
309
- * Space Complexity: O(1)
310
- *
311
- * The function "degreeOf" returns the total degree of a vertex, which is the sum of its out-degree and in-degree.
312
- * @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
313
- * @returns The sum of the out-degree and in-degree of the specified vertex or vertex ID.
293
+ * Degree (in + out) of a vertex.
294
+ * @param vertexOrKey - Vertex or key.
295
+ * @returns Non-negative integer.
296
+ * @remarks Time O(1) avg, Space O(1)
314
297
  */
315
298
  degreeOf(vertexOrKey: VertexKey | VO): number {
299
+ /**
300
+ * In-degree of a vertex.
301
+ * @param vertexOrKey - Vertex or key.
302
+ * @returns Non-negative integer.
303
+ * @remarks Time O(1) avg, Space O(1)
304
+ */
305
+ /**
306
+ * Out-degree of a vertex.
307
+ * @param vertexOrKey - Vertex or key.
308
+ * @returns Non-negative integer.
309
+ * @remarks Time O(1) avg, Space O(1)
310
+ */
316
311
  return this.outDegreeOf(vertexOrKey) + this.inDegreeOf(vertexOrKey);
317
312
  }
318
313
 
319
- /**
320
- * Time Complexity: O(1)
321
- * Space Complexity: O(1)
322
- *
323
- * The function "inDegreeOf" returns the number of incoming edgeMap for a given vertex.
324
- * @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
325
- * @returns The number of incoming edgeMap of the specified vertex or vertex ID.
326
- */
327
314
  inDegreeOf(vertexOrKey: VertexKey | VO): number {
328
315
  return this.incomingEdgesOf(vertexOrKey).length;
329
316
  }
330
317
 
331
- /**
332
- * Time Complexity: O(1)
333
- * Space Complexity: O(1)
334
- *
335
- * The function `outDegreeOf` returns the number of outgoing edgeMap from a given vertex.
336
- * @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
337
- * @returns The number of outgoing edgeMap from the specified vertex or vertex ID.
338
- */
339
318
  outDegreeOf(vertexOrKey: VertexKey | VO): number {
340
319
  return this.outgoingEdgesOf(vertexOrKey).length;
341
320
  }
342
321
 
343
322
  /**
344
- * Time Complexity: O(1)
345
- * Space Complexity: O(1)
346
- *
347
- * The function "edgesOf" returns an array of both outgoing and incoming edgeMap of a given vertex or vertex ID.
348
- * @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
349
- * @returns The function `edgesOf` returns an array of edgeMap.
323
+ * All incident edges of a vertex.
324
+ * @param vertexOrKey - Vertex or key.
325
+ * @returns Array of incident edges.
326
+ * @remarks Time O(deg_in + deg_out), Space O(deg_in + deg_out)
350
327
  */
351
328
  edgesOf(vertexOrKey: VertexKey | VO): EO[] {
352
329
  return [...this.outgoingEdgesOf(vertexOrKey), ...this.incomingEdgesOf(vertexOrKey)];
353
330
  }
354
331
 
355
- /**
356
- * Time Complexity: O(1)
357
- * Space Complexity: O(1)
358
- *
359
- * The function "getEdgeSrc" returns the source vertex of an edge, or undefined if the edge does not exist.
360
- * @param {EO} e - The parameter "e" is of type EO, which represents an edge in a graph.
361
- * @returns either a vertex object (VO) or undefined.
362
- */
363
332
  getEdgeSrc(e: EO): VO | undefined {
364
333
  return this._getVertex(e.src);
365
334
  }
366
335
 
367
- /**
368
- * Time Complexity: O(1)
369
- * Space Complexity: O(1)
370
- *
371
- * The function "getEdgeDest" returns the destination vertex of an edge.
372
- * @param {EO} e - The parameter "e" is of type "EO", which represents an edge in a graph.
373
- * @returns either a vertex object of type VO or undefined.
374
- */
375
336
  getEdgeDest(e: EO): VO | undefined {
376
337
  return this._getVertex(e.dest);
377
338
  }
378
339
 
379
340
  /**
380
- * Time Complexity: O(|E|) where |E| is the number of edgeMap
381
- * Space Complexity: O(1)
382
- *
383
- * The function `getDestinations` returns an array of destination vertexMap connected to a given vertex.
384
- * @param {VO | VertexKey | undefined} vertex - The `vertex` parameter represents the starting vertex from which we want to
385
- * find the destinations. It can be either a `VO` object, a `VertexKey` value, or `undefined`.
386
- * @returns an array of vertexMap (VO[]).
341
+ * Direct children reachable by one outgoing edge.
342
+ * @param vertex - Vertex or key.
343
+ * @returns Array of neighbor vertices.
344
+ * @remarks Time O(deg_out), Space O(deg_out)
387
345
  */
388
346
  getDestinations(vertex: VO | VertexKey | undefined): VO[] {
389
347
  if (vertex === undefined) {
@@ -401,20 +359,14 @@ export class DirectedGraph<
401
359
  }
402
360
 
403
361
  /**
404
- * Time Complexity: O(|V| + |E|) where |V| is the number of vertexMap and |E| is the number of edgeMap
405
- * Space Complexity: O(|V|)
406
- *
407
- * The `topologicalSort` function performs a topological sort on a graph and returns an array of vertexMap or vertex IDs
408
- * in the sorted order, or undefined if the graph contains a cycle.
409
- * @param {'vertex' | 'key'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
410
- * property to use for sorting the vertexMap. It can have two possible values: 'vertex' or 'key'. If 'vertex' is
411
- * specified, the vertexMap themselves will be used for sorting. If 'key' is specified, the ids of
412
- * @returns an array of vertexMap or vertex IDs in topological order. If there is a cycle in the graph, it returns undefined.
362
+ * Topological sort if DAG; returns `undefined` if a cycle exists.
363
+ * @param propertyName - `'key'` to map to keys; `'vertex'` to keep instances.
364
+ * @returns Array of keys/vertices, or `undefined` when cycle is found.
365
+ * @remarks Time O(V + E), Space O(V)
413
366
  */
414
367
  topologicalSort(propertyName?: 'vertex' | 'key'): Array<VO | VertexKey> | undefined {
415
368
  propertyName = propertyName ?? 'key';
416
- // When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
417
- // When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
369
+
418
370
  const statusMap: Map<VO | VertexKey, TopologicalStatus> = new Map<VO | VertexKey, TopologicalStatus>();
419
371
  for (const entry of this.vertexMap) {
420
372
  statusMap.set(entry[1], 0);
@@ -449,13 +401,6 @@ export class DirectedGraph<
449
401
  return sorted.reverse();
450
402
  }
451
403
 
452
- /**
453
- * Time Complexity: O(|E|) where |E| is the number of edgeMap
454
- * Space Complexity: O(|E|)
455
- *
456
- * The `edgeSet` function returns an array of all the edgeMap in the graph.
457
- * @returns The `edgeSet()` method returns an array of edgeMap (`EO[]`).
458
- */
459
404
  edgeSet(): EO[] {
460
405
  let edgeMap: EO[] = [];
461
406
  this._outEdgeMap.forEach(outEdges => {
@@ -464,15 +409,6 @@ export class DirectedGraph<
464
409
  return edgeMap;
465
410
  }
466
411
 
467
- /**
468
- * Time Complexity: O(|E|) where |E| is the number of edgeMap
469
- * Space Complexity: O(1)
470
- *
471
- * The function `getNeighbors` returns an array of neighboring vertexMap of a given vertex or vertex ID in a graph.
472
- * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
473
- * (`VertexKey`).
474
- * @returns an array of vertexMap (VO[]).
475
- */
476
412
  getNeighbors(vertexOrKey: VO | VertexKey): VO[] {
477
413
  const neighbors: VO[] = [];
478
414
  const vertex = this._getVertex(vertexOrKey);
@@ -480,7 +416,7 @@ export class DirectedGraph<
480
416
  const outEdges = this.outgoingEdgesOf(vertex);
481
417
  for (const outEdge of outEdges) {
482
418
  const neighbor = this._getVertex(outEdge.dest);
483
- // TODO after no-non-undefined-assertion not ensure the logic
419
+
484
420
  if (neighbor) {
485
421
  neighbors.push(neighbor);
486
422
  }
@@ -490,14 +426,10 @@ export class DirectedGraph<
490
426
  }
491
427
 
492
428
  /**
493
- * Time Complexity: O(1)
494
- * Space Complexity: O(1)
495
- *
496
- * The function "getEndsOfEdge" returns the source and destination vertexMap of an edge if it exists in the graph,
497
- * otherwise it returns undefined.
498
- * @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph.
499
- * @returns The function `getEndsOfEdge` returns an array containing two vertexMap `[VO, VO]` if the edge exists in the
500
- * graph. If the edge does not exist, it returns `undefined`.
429
+ * Resolve an edge's `[src, dest]` endpoints to vertex instances.
430
+ * @param edge - Edge instance.
431
+ * @returns `[src, dest]` or `undefined` if either endpoint is missing.
432
+ * @remarks Time O(1), Space O(1)
501
433
  */
502
434
  getEndsOfEdge(edge: EO): [VO, VO] | undefined {
503
435
  if (!this.hasEdge(edge.src, edge.dest)) {
@@ -513,19 +445,16 @@ export class DirectedGraph<
513
445
  }
514
446
 
515
447
  /**
516
- * The isEmpty function checks if the graph is empty.
517
- *
518
- * @return A boolean value
448
+ * Whether the graph has no vertices and no edges.
449
+ * @remarks Time O(1), Space O(1)
519
450
  */
520
451
  isEmpty(): boolean {
521
452
  return this.vertexMap.size === 0 && this.inEdgeMap.size === 0 && this.outEdgeMap.size === 0;
522
453
  }
523
454
 
524
455
  /**
525
- * Time Complexity: O(1)
526
- * Space Complexity: O(1)
527
- *
528
- * The clear function resets the vertex map, in-edge map, and out-edge map.
456
+ * Remove all vertices and edges.
457
+ * @remarks Time O(V + E), Space O(1)
529
458
  */
530
459
  clear() {
531
460
  this._vertexMap = new Map<VertexKey, VO>();
@@ -534,28 +463,18 @@ export class DirectedGraph<
534
463
  }
535
464
 
536
465
  /**
537
- * The clone function creates a new DirectedGraph object with the same vertices and edges as the original.
538
- *
539
- * @return A new instance of the directedgraph class
466
+ * Deep clone as the same concrete class.
467
+ * @returns A new graph of the same concrete class (`this` type).
468
+ * @remarks Time O(V + E), Space O(V + E)
540
469
  */
541
- clone(): DirectedGraph<V, E, VO, EO> {
542
- const cloned = new DirectedGraph<V, E, VO, EO>();
543
- cloned.vertexMap = new Map<VertexKey, VO>(this.vertexMap);
544
- cloned.inEdgeMap = new Map<VO, EO[]>(this.inEdgeMap);
545
- cloned.outEdgeMap = new Map<VO, EO[]>(this.outEdgeMap);
546
- return cloned;
470
+ override clone(): this {
471
+ return super.clone();
547
472
  }
548
473
 
549
474
  /**
550
- * Time Complexity: O(V + E)
551
- * Space Complexity: O(V)
552
- * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
553
- * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
554
- *
555
- * The function `tarjan` implements the Tarjan's algorithm to find strongly connected components in a
556
- * graph.
557
- * @returns The function `tarjan()` returns an object with three properties: `dfnMap`, `lowMap`, and
558
- * `SCCs`.
475
+ * Tarjan's algorithm for strongly connected components.
476
+ * @returns `{ dfnMap, lowMap, SCCs }`.
477
+ * @remarks Time O(V + E), Space O(V + E)
559
478
  */
560
479
  tarjan(): { dfnMap: Map<VO, number>; lowMap: Map<VO, number>; SCCs: Map<number, VO[]> } {
561
480
  const dfnMap = new Map<VO, number>();
@@ -609,45 +528,37 @@ export class DirectedGraph<
609
528
  }
610
529
 
611
530
  /**
612
- * Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
613
- * Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
614
- *
615
- * The function returns a map that associates each vertex object with its corresponding depth-first
616
- * number.
617
- * @returns A Map object with keys of type VO and values of type number.
531
+ * DFN index map computed by `tarjan()`.
532
+ * @returns Map from vertex to DFN index.
533
+ * @remarks Time O(V), Space O(V)
618
534
  */
619
535
  getDFNMap(): Map<VO, number> {
620
536
  return this.tarjan().dfnMap;
621
537
  }
622
538
 
623
539
  /**
624
- * The function returns a Map object that contains the low values of each vertex in a Tarjan
625
- * algorithm.
626
- * @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of
627
- * type `number`.
540
+ * LOW link map computed by `tarjan()`.
541
+ * @returns Map from vertex to LOW value.
542
+ * @remarks Time O(V), Space O(V)
628
543
  */
629
544
  getLowMap(): Map<VO, number> {
630
545
  return this.tarjan().lowMap;
631
546
  }
632
547
 
633
548
  /**
634
- * The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan
635
- * algorithm.
636
- * @returns a map where the keys are numbers and the values are arrays of VO objects.
549
+ * Strongly connected components computed by `tarjan()`.
550
+ * @returns Map from SCC id to vertices.
551
+ * @remarks Time O(#SCC + V), Space O(V)
637
552
  */
638
553
  getSCCs(): Map<number, VO[]> {
639
554
  return this.tarjan().SCCs;
640
555
  }
641
556
 
642
557
  /**
643
- * Time Complexity: O(1)
644
- * Space Complexity: O(1)
645
- *
646
- * The function `_addEdge` adds an edge to a graph if the source and destination vertexMap exist.
647
- * @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph. It is the edge that
648
- * needs to be added to the graph.
649
- * @returns a boolean value. It returns true if the edge was successfully added to the graph, and false if either the
650
- * source or destination vertex does not exist in the graph.
558
+ * Internal hook to attach a directed edge into adjacency maps.
559
+ * @param edge - Edge instance.
560
+ * @returns `true` if inserted; otherwise `false`.
561
+ * @remarks Time O(1) avg, Space O(1)
651
562
  */
652
563
  protected _addEdge(edge: EO): boolean {
653
564
  if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
@@ -657,7 +568,6 @@ export class DirectedGraph<
657
568
  const srcVertex = this._getVertex(edge.src);
658
569
  const destVertex = this._getVertex(edge.dest);
659
570
 
660
- // TODO after no-non-undefined-assertion not ensure the logic
661
571
  if (srcVertex && destVertex) {
662
572
  const srcOutEdges = this._outEdgeMap.get(srcVertex);
663
573
  if (srcOutEdges) {