priority-queue-typed 2.0.5 → 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 +598 -869
  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 +1 -0
  64. package/dist/utils/utils.d.ts +1 -1
  65. package/dist/utils/utils.js +2 -1
  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 +664 -893
  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 +2 -0
  101. package/src/utils/utils.ts +9 -14
@@ -1,3 +1,11 @@
1
+ /**
2
+ * data-structure-typed
3
+ *
4
+ * @author Pablo Zeng
5
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
7
+ */
8
+
1
9
  import type { MapGraphCoordinate, VertexKey } from '../../types';
2
10
  import { DirectedEdge, DirectedGraph, DirectedVertex } from './directed-graph';
3
11
 
@@ -5,18 +13,6 @@ export class MapVertex<V = any> extends DirectedVertex<V> {
5
13
  lat: number;
6
14
  long: number;
7
15
 
8
- /**
9
- * The constructor function initializes an object with an key, latitude, longitude, and an optional value.
10
- * @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex.
11
- * @param {number} lat - The "lat" parameter represents the latitude of a vertex. Latitude is a geographic coordinate
12
- * that specifies the north-south position of a point on the Earth's surface. It is measured in degrees, with positive
13
- * values representing points north of the equator and negative values representing points south of the equator.
14
- * @param {number} long - The "long" parameter represents the longitude of a location. Longitude is a geographic
15
- * coordinate that specifies the east-west position of a point on the Earth's surface. It is measured in degrees, with
16
- * values ranging from -180 to 180.
17
- * @param {V} [value] - The "value" parameter is an optional value of type V. It is not required to be provided when
18
- * creating an instance of the class.
19
- */
20
16
  constructor(key: VertexKey, value: V, lat: number, long: number) {
21
17
  super(key, value);
22
18
  this.lat = lat;
@@ -25,23 +21,19 @@ export class MapVertex<V = any> extends DirectedVertex<V> {
25
21
  }
26
22
 
27
23
  export class MapEdge<E = any> extends DirectedEdge<E> {
28
- /**
29
- * The constructor function initializes a new instance of a class with the given source, destination, weight, and
30
- * value.
31
- * @param {VertexKey} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
32
- * a graph.
33
- * @param {VertexKey} dest - The `dest` parameter is the identifier of the destination vertex for an edge.
34
- * @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
35
- * @param {E} [value] - The "value" parameter is an optional parameter of type E. It is used to store additional
36
- * information or data associated with the edge.
37
- */
38
24
  constructor(src: VertexKey, dest: VertexKey, weight?: number, value?: E) {
39
25
  super(src, dest, weight, value);
40
26
  }
41
27
  }
42
28
 
43
29
  /**
44
- *
30
+ * Directed graph variant carrying geospatial coordinates.
31
+ * @template V - Vertex value type.
32
+ * @template E - Edge value type.
33
+ * @template VO - Concrete vertex class (MapVertex<V>).
34
+ * @template EO - Concrete edge class (MapEdge<E>).
35
+ * @remarks Time O(1), Space O(1)
36
+ * @example examples will be generated by unit test
45
37
  */
46
38
  export class MapGraph<
47
39
  V = any,
@@ -50,13 +42,10 @@ export class MapGraph<
50
42
  EO extends MapEdge<E> = MapEdge<E>
51
43
  > extends DirectedGraph<V, E, VO, EO> {
52
44
  /**
53
- * The constructor function initializes the originCoord and bottomRight properties of a MapGraphCoordinate object.
54
- * @param {MapGraphCoordinate} originCoord - The `originCoord` parameter is a `MapGraphCoordinate` object that represents the
55
- * starting point or reference point of the map graph. It defines the coordinates of the top-left corner of the map
56
- * graph.
57
- * @param {MapGraphCoordinate} [bottomRight] - The `bottomRight` parameter is an optional parameter of type
58
- * `MapGraphCoordinate`. It represents the bottom right coordinate of a map graph. If this parameter is not provided,
59
- * it will default to `undefined`.
45
+ * Construct a MapGraph.
46
+ * @param originCoord - Origin coordinate `[lat, long]` used as default.
47
+ * @param bottomRight - Optional bottom-right coordinate for bounding boxes.
48
+ * @remarks Time O(1), Space O(1)
60
49
  */
61
50
  constructor(originCoord: MapGraphCoordinate, bottomRight?: MapGraphCoordinate) {
62
51
  super();
@@ -77,15 +66,13 @@ export class MapGraph<
77
66
  }
78
67
 
79
68
  /**
80
- * The function creates a new vertex with the given key, value, latitude, and longitude.
81
- * @param {VertexKey} key - The key parameter is the unique identifier for the vertex. It is of type VertexKey, which could
82
- * be a string or a number depending on how you define it in your code.
83
- * @param [value] - The `value` parameter is an optional value that can be assigned to the `value` property of the vertex. It
84
- * is of type `V`, which means it should be of the same type as the `value` property of the vertex class `VO`.
85
- * @param {number} lat - The `lat` parameter represents the latitude of the vertex. It is a number that specifies the
86
- * position of the vertex on the Earth's surface in the north-south direction.
87
- * @param {number} long - The `long` parameter represents the longitude coordinate of the vertex.
88
- * @returns The method is returning a new instance of the `MapVertex` class, casted as type `VO`.
69
+ * Create a map vertex with optional coordinates.
70
+ * @param key - Vertex identifier.
71
+ * @param value - Optional payload.
72
+ * @param lat - Latitude (defaults to `originCoord[0]`).
73
+ * @param long - Longitude (defaults to `originCoord[1]`).
74
+ * @returns MapVertex instance.
75
+ * @remarks Time O(1), Space O(1)
89
76
  */
90
77
  override createVertex(
91
78
  key: VertexKey,
@@ -97,33 +84,49 @@ export class MapGraph<
97
84
  }
98
85
 
99
86
  /**
100
- * The function creates a new instance of a MapEdge with the given source, destination, weight, and value.
101
- * @param {VertexKey} src - The source vertex ID of the edge. It represents the starting point of the edge.
102
- * @param {VertexKey} dest - The `dest` parameter is the identifier of the destination vertex for the edge being
103
- * created.
104
- * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. It
105
- * is used to assign a numerical value to the edge, which can be used in algorithms such as shortest path algorithms.
106
- * If the weight is not provided, it can be set to a default value or left undefined.
107
- * @param [value] - The `value` parameter is an optional value that can be assigned to the edge. It can be of any type,
108
- * depending on the specific implementation of the `MapEdge` class.
109
- * @returns a new instance of the `MapEdge` class, cast as type `EO`.
87
+ * Create a map edge (directed) with optional weight/value.
88
+ * @param src - Source key.
89
+ * @param dest - Destination key.
90
+ * @param weight - Edge weight.
91
+ * @param value - Edge payload.
92
+ * @returns MapEdge instance.
93
+ * @remarks Time O(1), Space O(1)
110
94
  */
111
95
  override createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO {
112
96
  return new MapEdge(src, dest, weight, value) as EO;
113
97
  }
114
98
 
115
99
  /**
116
- * The override function is used to override the default behavior of a function.
117
- * In this case, we are overriding the clone() function from Graph&lt;V, E&gt;.
118
- * The clone() function returns a new graph that is an exact copy of the original graph.
119
- *
120
- * @return A mapgraph&lt;v, e, vo, eo&gt;
100
+ * Deep clone as the same concrete class.
101
+ * @returns A new graph of the same concrete class (`this` type).
102
+ * @remarks Time O(V + E), Space O(V + E)
103
+ */
104
+ override clone(): this {
105
+ return super.clone();
106
+ }
107
+
108
+ /**
109
+ * Include `originCoord` and `bottomRight` so `clone()/filter()` preserve geospatial settings.
110
+ * @returns Options bag extending super snapshot.
111
+ * @remarks Time O(1), Space O(1)
112
+ */
113
+ protected override _snapshotOptions(): Record<string, unknown> {
114
+ return { ...(super._snapshotOptions() as any), originCoord: this.originCoord, bottomRight: this.bottomRight };
115
+ }
116
+
117
+ /**
118
+ * Re-create a same-species MapGraph instance from snapshot options.
119
+ * @param options - Snapshot options providing `originCoord`/`bottomRight`.
120
+ * @returns Empty MapGraph instance of `this` type.
121
+ * @remarks Time O(1), Space O(1)
121
122
  */
122
- override clone(): MapGraph<V, E, VO, EO> {
123
- const cloned = new MapGraph<V, E, VO, EO>(this.originCoord, this.bottomRight);
124
- cloned.vertexMap = new Map<VertexKey, VO>(this.vertexMap);
125
- cloned.inEdgeMap = new Map<VO, EO[]>(this.inEdgeMap);
126
- cloned.outEdgeMap = new Map<VO, EO[]>(this.outEdgeMap);
127
- return cloned;
123
+ protected override _createInstance(options?: Partial<Record<string, unknown>>): this {
124
+ const { originCoord, bottomRight } = (options || {}) as {
125
+ originCoord?: [number, number];
126
+ bottomRight?: [number, number] | undefined;
127
+ };
128
+ const oc = (originCoord ?? this.originCoord) as [number, number];
129
+ const br = (bottomRight ?? this.bottomRight) as [number, number] | undefined;
130
+ return new MapGraph<V, E, VO, EO>(oc, br) as this;
128
131
  }
129
132
  }
@@ -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 { VertexKey } from '../../types';
8
+
9
+ import type { GraphOptions, VertexKey } from '../../types';
9
10
  import { IGraph } from '../../interfaces';
10
11
  import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
11
12
  import { arrayRemove } from '../../utils';
12
13
 
13
14
  export class UndirectedVertex<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 network.
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
  }
@@ -26,16 +20,6 @@ export class UndirectedVertex<V = any> extends AbstractVertex<V> {
26
20
  export class UndirectedEdge<E = number> extends AbstractEdge<E> {
27
21
  endpoints: [VertexKey, VertexKey];
28
22
 
29
- /**
30
- * The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
31
- * value.
32
- * @param {VertexKey} v1 - The first vertex ID of the edge.
33
- * @param {VertexKey} v2 - The parameter `v2` is a `VertexKey`, which represents the identifier of the second vertex in a
34
- * graph edge.
35
- * @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
36
- * @param {E} [value] - The "value" parameter is an optional parameter of type E. It is used to store a value associated
37
- * with the edge.
38
- */
39
23
  constructor(v1: VertexKey, v2: VertexKey, weight?: number, value?: E) {
40
24
  super(weight, value);
41
25
  this.endpoints = [v1, v2];
@@ -43,7 +27,13 @@ export class UndirectedEdge<E = number> extends AbstractEdge<E> {
43
27
  }
44
28
 
45
29
  /**
46
- *
30
+ * Undirected graph implementation.
31
+ * @template V - Vertex value type.
32
+ * @template E - Edge value type.
33
+ * @template VO - Concrete vertex class (extends AbstractVertex<V>).
34
+ * @template EO - Concrete edge class (extends AbstractEdge<E>).
35
+ * @remarks Time O(1), Space O(1)
36
+ * @example examples will be generated by unit test
47
37
  */
48
38
  export class UndirectedGraph<
49
39
  V = any,
@@ -55,10 +45,12 @@ export class UndirectedGraph<
55
45
  implements IGraph<V, E, VO, EO>
56
46
  {
57
47
  /**
58
- * The constructor initializes a new Map object to store edgeMap.
48
+ * Construct an undirected graph with runtime defaults.
49
+ * @param options - `GraphOptions<V>` (e.g. `vertexValueInitializer`, `defaultEdgeWeight`).
50
+ * @remarks Time O(1), Space O(1)
59
51
  */
60
- constructor() {
61
- super();
52
+ constructor(options?: Partial<GraphOptions<V>>) {
53
+ super(options);
62
54
  this._edgeMap = new Map<VO, EO[]>();
63
55
  }
64
56
 
@@ -73,42 +65,67 @@ export class UndirectedGraph<
73
65
  }
74
66
 
75
67
  /**
76
- * The function creates a new vertex with an optional value and returns it.
77
- * @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is used to distinguish one
78
- * vertex from another in the graph.
79
- * @param [value] - The `value` parameter is an optional value that can be assigned to the vertex. If a value is provided,
80
- * it will be used as the value of the vertex. If no value is provided, the `key` parameter will be used as the value of
81
- * the vertex.
82
- * @returns The method is returning a new instance of the `UndirectedVertex` class, casted as type `VO`.
68
+ * Construct an undirected graph from keys with value initializer `v => v`.
69
+ * @template K - Vertex key type.
70
+ * @param keys - Iterable of vertex keys.
71
+ * @returns UndirectedGraph with all keys added.
72
+ * @remarks Time O(V), Space O(V)
83
73
  */
84
- override createVertex(key: VertexKey, value?: VO['value']): VO {
85
- return new UndirectedVertex(key, value ?? key) as VO;
74
+ static fromKeys<K extends VertexKey>(
75
+ keys: Iterable<K>
76
+ ): UndirectedGraph<K, any, UndirectedVertex<K>, UndirectedEdge<any>> {
77
+ const g: UndirectedGraph<K, any, UndirectedVertex<K>, UndirectedEdge<any>> = new UndirectedGraph<K, any>({
78
+ vertexValueInitializer: (k: VertexKey) => k as K
79
+ });
80
+ for (const k of keys) g.addVertex(k);
81
+ return g;
86
82
  }
87
83
 
88
84
  /**
89
- * The function creates an undirected edge between two vertexMap with an optional weight and value.
90
- * @param {VertexKey} v1 - The parameter `v1` represents the first vertex of the edge.
91
- * @param {VertexKey} v2 - The parameter `v2` represents the second vertex of the edge.
92
- * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
93
- * no weight is provided, it defaults to 1.
94
- * @param [value] - The `value` parameter is an optional value that can be assigned to the edge. It can be of any type and
95
- * is used to store additional information or data associated with the edge.
96
- * @returns a new instance of the `UndirectedEdge` class, which is casted as type `EO`.
85
+ * Construct an undirected graph from `[key, value]` entries.
86
+ * @template V - Vertex value type.
87
+ * @param entries - Iterable of `[key, value]` pairs.
88
+ * @returns UndirectedGraph with all vertices added.
89
+ * @remarks Time O(V), Space O(V)
90
+ */
91
+ static fromEntries<V>(
92
+ entries: Iterable<[VertexKey, V]>
93
+ ): UndirectedGraph<V, any, UndirectedVertex<V>, UndirectedEdge<any>> {
94
+ const g: UndirectedGraph<V, any, UndirectedVertex<V>, UndirectedEdge<any>> = new UndirectedGraph<V, any>();
95
+ for (const [k, v] of entries) g.addVertex(k, v);
96
+ return g;
97
+ }
98
+
99
+ /**
100
+ * Create an undirected vertex instance. Does not insert into the graph.
101
+ * @param key - Vertex identifier.
102
+ * @param value - Optional payload.
103
+ * @returns Concrete vertex instance.
104
+ * @remarks Time O(1), Space O(1)
105
+ */
106
+ createVertex(key: VertexKey, value?: VO['value']): VO {
107
+ return new UndirectedVertex(key, value) as VO;
108
+ }
109
+
110
+ /**
111
+ * Create an undirected edge instance. Does not insert into the graph.
112
+ * @param v1 - One endpoint key.
113
+ * @param v2 - The other endpoint key.
114
+ * @param weight - Edge weight; defaults to `defaultEdgeWeight`.
115
+ * @param value - Edge payload.
116
+ * @returns Concrete edge instance.
117
+ * @remarks Time O(1), Space O(1)
97
118
  */
98
119
  override createEdge(v1: VertexKey, v2: VertexKey, weight?: number, value?: EO['value']): EO {
99
- return new UndirectedEdge(v1, v2, weight ?? 1, value) as EO;
120
+ return new UndirectedEdge(v1, v2, weight ?? this.options.defaultEdgeWeight ?? 1, value) as EO;
100
121
  }
101
122
 
102
123
  /**
103
- * Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
104
- * Space Complexity: O(1)
105
- *
106
- * The function `getEdge` returns the first edge that connects two endpoints, or undefined if no such edge exists.
107
- * @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
108
- * object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
109
- * @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
110
- * object), `undefined`, or `VertexKey` (vertex ID).
111
- * @returns an edge (EO) or undefined.
124
+ * Get an undirected edge between two vertices, if present.
125
+ * @param v1 - One vertex or key.
126
+ * @param v2 - The other vertex or key.
127
+ * @returns Edge instance or `undefined`.
128
+ * @remarks Time O(1) avg, Space O(1)
112
129
  */
113
130
  getEdge(v1: VO | VertexKey | undefined, v2: VO | VertexKey | undefined): EO | undefined {
114
131
  let edgeMap: EO[] | undefined = [];
@@ -126,14 +143,11 @@ export class UndirectedGraph<
126
143
  }
127
144
 
128
145
  /**
129
- * Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
130
- * Space Complexity: O(1)
131
- *
132
- * The function removes an edge between two vertexMap in a graph and returns the removed edge.
133
- * @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
134
- * @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
135
- * (VertexKey). It represents the second vertex of the edge that needs to be removed.
136
- * @returns the removed edge (EO) if it exists, or undefined if either of the endpoints (VO) does not exist.
146
+ * Delete a single undirected edge between two vertices.
147
+ * @param v1 - One vertex or key.
148
+ * @param v2 - The other vertex or key.
149
+ * @returns Removed edge or `undefined`.
150
+ * @remarks Time O(1) avg, Space O(1)
137
151
  */
138
152
  deleteEdgeBetween(v1: VO | VertexKey, v2: VO | VertexKey): EO | undefined {
139
153
  const vertex1: VO | undefined = this._getVertex(v1);
@@ -156,17 +170,11 @@ export class UndirectedGraph<
156
170
  }
157
171
 
158
172
  /**
159
- * Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
160
- * Space Complexity: O(1)
161
- *
162
- * The function `deleteEdge` deletes an edge between two endpoints in a graph.
163
- * @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be
164
- * either an edge object or a vertex key.
165
- * @param {VertexKey} [otherSideVertexKey] - The parameter `otherSideVertexKey` is an optional
166
- * parameter that represents the key of the vertex on the other side of the edge. It is used when the
167
- * `edgeOrOneSideVertexKey` parameter is a vertex key, and it specifies the key of the vertex on the
168
- * other side of the
169
- * @returns The `deleteEdge` function returns either the deleted edge object (EO) or `undefined`.
173
+ * Delete an edge by instance or by a pair of keys.
174
+ * @param edgeOrOneSideVertexKey - Edge instance or one endpoint vertex/key.
175
+ * @param otherSideVertexKey - Required second endpoint when deleting by pair.
176
+ * @returns Removed edge or `undefined`.
177
+ * @remarks Time O(1) avg, Space O(1)
170
178
  */
171
179
  deleteEdge(edgeOrOneSideVertexKey: EO | VertexKey, otherSideVertexKey?: VertexKey): EO | undefined {
172
180
  let oneSide: VO | undefined, otherSide: VO | undefined;
@@ -190,13 +198,10 @@ export class UndirectedGraph<
190
198
  }
191
199
 
192
200
  /**
193
- * Time Complexity: O(1) - Constant time for Map operations.
194
- * Space Complexity: O(1) - Constant space, as it creates only a few variables.
195
- *
196
- * The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
197
- * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
198
- * (`VertexKey`).
199
- * @returns The method is returning a boolean value.
201
+ * Delete a vertex and remove it from all neighbor lists.
202
+ * @param vertexOrKey - Vertex or key.
203
+ * @returns `true` if removed; otherwise `false`.
204
+ * @remarks Time O(deg), Space O(1)
200
205
  */
201
206
  deleteVertex(vertexOrKey: VO | VertexKey): boolean {
202
207
  let vertexKey: VertexKey;
@@ -209,6 +214,12 @@ export class UndirectedGraph<
209
214
  vertexKey = this._getVertexKey(vertexOrKey);
210
215
  }
211
216
 
217
+ /**
218
+ * All neighbors connected via undirected edges.
219
+ * @param vertexOrKey - Vertex or key.
220
+ * @returns Array of neighbor vertices.
221
+ * @remarks Time O(deg), Space O(deg)
222
+ */
212
223
  const neighbors = this.getNeighbors(vertexOrKey);
213
224
 
214
225
  if (vertex) {
@@ -228,14 +239,10 @@ export class UndirectedGraph<
228
239
  }
229
240
 
230
241
  /**
231
- * Time Complexity: O(1)
232
- * Space Complexity: O(1)
233
- *
234
- * The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edgeMap connected to that
235
- * vertex.
236
- * @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
237
- * @returns The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of
238
- * edgeMap connected to that vertex.
242
+ * Degree of a vertex (# of incident undirected edges).
243
+ * @param vertexOrKey - Vertex or key.
244
+ * @returns Non-negative integer.
245
+ * @remarks Time O(1) avg, Space O(1)
239
246
  */
240
247
  degreeOf(vertexOrKey: VertexKey | VO): number {
241
248
  const vertex = this._getVertex(vertexOrKey);
@@ -247,13 +254,10 @@ export class UndirectedGraph<
247
254
  }
248
255
 
249
256
  /**
250
- * Time Complexity: O(1)
251
- * Space Complexity: O(1)
252
- *
253
- * The function returns the edgeMap of a given vertex or vertex ID.
254
- * @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`. A `VertexKey` is a
255
- * unique identifier for a vertex in a graph, while `VO` represents the type of the vertex.
256
- * @returns an array of edgeMap.
257
+ * Incident undirected edges of a vertex.
258
+ * @param vertexOrKey - Vertex or key.
259
+ * @returns Array of incident edges.
260
+ * @remarks Time O(deg), Space O(deg)
257
261
  */
258
262
  edgesOf(vertexOrKey: VertexKey | VO): EO[] {
259
263
  const vertex = this._getVertex(vertexOrKey);
@@ -265,11 +269,9 @@ export class UndirectedGraph<
265
269
  }
266
270
 
267
271
  /**
268
- * Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
269
- * Space Complexity: O(|E|)
270
- *
271
- * The function "edgeSet" returns an array of unique edgeMap from a set of edgeMap.
272
- * @returns The method `edgeSet()` returns an array of type `EO[]`.
272
+ * Unique set of undirected edges across endpoints.
273
+ * @returns Array of edges.
274
+ * @remarks Time O(E), Space O(E)
273
275
  */
274
276
  edgeSet(): EO[] {
275
277
  const edgeSet: Set<EO> = new Set();
@@ -281,15 +283,6 @@ export class UndirectedGraph<
281
283
  return [...edgeSet];
282
284
  }
283
285
 
284
- /**
285
- * Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
286
- * Space Complexity: O(|E|)
287
- *
288
- * The function "getNeighbors" returns an array of neighboring endpoints for a given vertex or vertex ID.
289
- * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
290
- * (`VertexKey`).
291
- * @returns an array of vertexMap (VO[]).
292
- */
293
286
  getNeighbors(vertexOrKey: VO | VertexKey): VO[] {
294
287
  const neighbors: VO[] = [];
295
288
  const vertex = this._getVertex(vertexOrKey);
@@ -306,14 +299,10 @@ export class UndirectedGraph<
306
299
  }
307
300
 
308
301
  /**
309
- * Time Complexity: O(1)
310
- * Space Complexity: O(1)
311
- *
312
- * The function "getEndsOfEdge" returns the endpoints at the ends of an edge if the edge exists in the graph, otherwise
313
- * it returns undefined.
314
- * @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
315
- * @returns The function `getEndsOfEdge` returns an array containing two endpoints `[VO, VO]` if the edge exists in the
316
- * graph. If the edge does not exist, it returns `undefined`.
302
+ * Resolve an edge's two endpoints to vertex instances.
303
+ * @param edge - Edge instance.
304
+ * @returns `[v1, v2]` or `undefined` if either endpoint is missing.
305
+ * @remarks Time O(1), Space O(1)
317
306
  */
318
307
  getEndsOfEdge(edge: EO): [VO, VO] | undefined {
319
308
  if (!this.hasEdge(edge.endpoints[0], edge.endpoints[1])) {
@@ -329,18 +318,16 @@ export class UndirectedGraph<
329
318
  }
330
319
 
331
320
  /**
332
- * The isEmpty function checks if the graph is empty.
333
- * @return True if the graph is empty and false otherwise
321
+ * Whether the graph has no vertices and no edges.
322
+ * @remarks Time O(1), Space O(1)
334
323
  */
335
324
  isEmpty(): boolean {
336
325
  return this.vertexMap.size === 0 && this.edgeMap.size === 0;
337
326
  }
338
327
 
339
328
  /**
340
- * Time Complexity: O(1)
341
- * Space Complexity: O(1)
342
- *
343
- * The clear function resets the vertex and edge maps to empty maps.
329
+ * Remove all vertices and edges.
330
+ * @remarks Time O(V + E), Space O(1)
344
331
  */
345
332
  clear() {
346
333
  this._vertexMap = new Map<VertexKey, VO>();
@@ -348,30 +335,18 @@ export class UndirectedGraph<
348
335
  }
349
336
 
350
337
  /**
351
- * The clone function creates a new UndirectedGraph object and copies the
352
- * vertexMap and edgeMap from this graph to the new one. This is done by
353
- * assigning each of these properties to their respective counterparts in the
354
- * cloned graph. The clone function returns a reference to this newly created,
355
- * cloned UndirectedGraph object.
356
- *
357
- * @return A new instance of the undirectedgraph class
338
+ * Deep clone as the same concrete class.
339
+ * @returns A new graph of the same concrete class (`this` type).
340
+ * @remarks Time O(V + E), Space O(V + E)
358
341
  */
359
- clone(): UndirectedGraph<V, E, VO, EO> {
360
- const cloned = new UndirectedGraph<V, E, VO, EO>();
361
- cloned.vertexMap = new Map<VertexKey, VO>(this.vertexMap);
362
- cloned.edgeMap = new Map<VO, EO[]>(this.edgeMap);
363
- return cloned;
342
+ override clone(): this {
343
+ return super.clone();
364
344
  }
365
345
 
366
346
  /**
367
- * Time Complexity: O(V + E)
368
- * Space Complexity: O(V)
369
- * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
370
- * 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time
371
- *
372
- * The function `tarjan` implements the Tarjan's algorithm to find bridges and cut vertices in a
373
- * graph.
374
- * @returns The function `tarjan()` returns an object with the following properties:
347
+ * Tarjan-based bridge and articulation point detection.
348
+ * @returns `{ dfnMap, lowMap, bridges, cutVertices }`.
349
+ * @remarks Time O(V + E), Space O(V + E)
375
350
  */
376
351
  tarjan(): { dfnMap: Map<VO, number>; lowMap: Map<VO, number>; bridges: EO[]; cutVertices: VO[] } {
377
352
  const dfnMap = new Map<VO, number>();
@@ -433,44 +408,46 @@ export class UndirectedGraph<
433
408
  }
434
409
 
435
410
  /**
436
- * The function "getBridges" returns an array of bridges in a graph using the Tarjan's algorithm.
437
- * @returns The function `getBridges()` is returning the bridges found using the Tarjan's algorithm.
411
+ * Get bridges discovered by `tarjan()`.
412
+ * @returns Array of edges that are bridges.
413
+ * @remarks Time O(B), Space O(1)
438
414
  */
439
415
  getBridges() {
440
416
  return this.tarjan().bridges;
441
417
  }
442
418
 
443
419
  /**
444
- * The function "getCutVertices" returns an array of cut vertices using the Tarjan's algorithm.
445
- * @returns the cut vertices found using the Tarjan's algorithm.
420
+ * Get articulation points discovered by `tarjan()`.
421
+ * @returns Array of cut vertices.
422
+ * @remarks Time O(C), Space O(1)
446
423
  */
447
424
  getCutVertices() {
448
425
  return this.tarjan().cutVertices;
449
426
  }
450
427
 
451
428
  /**
452
- * The function returns the dfnMap property of the result of the tarjan() function.
453
- * @returns the `dfnMap` property of the result of calling the `tarjan()` function.
429
+ * DFN index map computed by `tarjan()`.
430
+ * @returns Map from vertex to DFN index.
431
+ * @remarks Time O(V), Space O(V)
454
432
  */
455
433
  getDFNMap() {
456
434
  return this.tarjan().dfnMap;
457
435
  }
458
436
 
459
437
  /**
460
- * The function returns the lowMap property of the result of the tarjan() function.
461
- * @returns the lowMap property of the result of calling the tarjan() function.
438
+ * LOW link map computed by `tarjan()`.
439
+ * @returns Map from vertex to LOW value.
440
+ * @remarks Time O(V), Space O(V)
462
441
  */
463
442
  getLowMap() {
464
443
  return this.tarjan().lowMap;
465
444
  }
466
445
 
467
446
  /**
468
- * Time Complexity: O(1)
469
- * Space Complexity: O(1)
470
- *
471
- * The function adds an edge to the graph by updating the adjacency list with the vertexMap of the edge.
472
- * @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
473
- * @returns a boolean value.
447
+ * Internal hook to attach an undirected edge into adjacency maps.
448
+ * @param edge - Edge instance.
449
+ * @returns `true` if both endpoints exist; otherwise `false`.
450
+ * @remarks Time O(1) avg, Space O(1)
474
451
  */
475
452
  protected _addEdge(edge: EO): boolean {
476
453
  for (const end of edge.endpoints) {