bst-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.
- 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 +598 -869
- 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/utils.d.ts +1 -1
- package/dist/utils/utils.js +2 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +238 -115
- package/src/data-structures/base/iterable-entry-base.ts +96 -120
- package/src/data-structures/base/linear-base.ts +271 -277
- package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
- package/src/data-structures/binary-tree/avl-tree.ts +239 -206
- package/src/data-structures/binary-tree/binary-tree.ts +664 -893
- package/src/data-structures/binary-tree/bst.ts +568 -570
- package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
- package/src/data-structures/binary-tree/tree-counter.ts +199 -218
- package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
- package/src/data-structures/graph/abstract-graph.ts +339 -264
- package/src/data-structures/graph/directed-graph.ts +146 -236
- package/src/data-structures/graph/map-graph.ts +63 -60
- package/src/data-structures/graph/undirected-graph.ts +129 -152
- package/src/data-structures/hash/hash-map.ts +274 -496
- package/src/data-structures/heap/heap.ts +389 -402
- package/src/data-structures/heap/max-heap.ts +12 -76
- package/src/data-structures/heap/min-heap.ts +13 -76
- package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
- package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
- package/src/data-structures/priority-queue/priority-queue.ts +3 -92
- package/src/data-structures/queue/deque.ts +381 -357
- package/src/data-structures/queue/queue.ts +310 -264
- package/src/data-structures/stack/stack.ts +217 -131
- package/src/data-structures/trie/trie.ts +240 -175
- package/src/interfaces/binary-tree.ts +240 -6
- package/src/interfaces/graph.ts +37 -0
- package/src/types/data-structures/base/base.ts +5 -5
- package/src/types/data-structures/graph/abstract-graph.ts +5 -0
- package/src/types/utils/utils.ts +2 -0
- 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
|
-
*
|
|
54
|
-
* @param
|
|
55
|
-
*
|
|
56
|
-
*
|
|
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
|
-
*
|
|
81
|
-
* @param
|
|
82
|
-
*
|
|
83
|
-
* @param
|
|
84
|
-
*
|
|
85
|
-
* @
|
|
86
|
-
*
|
|
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
|
-
*
|
|
101
|
-
* @param
|
|
102
|
-
* @param
|
|
103
|
-
*
|
|
104
|
-
* @param
|
|
105
|
-
*
|
|
106
|
-
*
|
|
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
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
|
|
120
|
-
|
|
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
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
-
|
|
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
|
-
*
|
|
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
|
-
*
|
|
77
|
-
* @
|
|
78
|
-
*
|
|
79
|
-
* @
|
|
80
|
-
*
|
|
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
|
-
|
|
85
|
-
|
|
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
|
-
*
|
|
90
|
-
* @
|
|
91
|
-
* @param
|
|
92
|
-
* @
|
|
93
|
-
*
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
* @
|
|
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
|
-
*
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
* @
|
|
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
|
-
*
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
* @
|
|
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
|
-
*
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
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
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
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
|
-
*
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
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
|
-
*
|
|
269
|
-
*
|
|
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
|
-
*
|
|
310
|
-
*
|
|
311
|
-
*
|
|
312
|
-
*
|
|
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
|
-
*
|
|
333
|
-
* @
|
|
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
|
-
*
|
|
341
|
-
*
|
|
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
|
-
*
|
|
352
|
-
*
|
|
353
|
-
*
|
|
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():
|
|
360
|
-
|
|
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
|
-
*
|
|
368
|
-
*
|
|
369
|
-
*
|
|
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
|
-
*
|
|
437
|
-
* @returns
|
|
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
|
-
*
|
|
445
|
-
* @returns
|
|
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
|
-
*
|
|
453
|
-
* @returns
|
|
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
|
-
*
|
|
461
|
-
* @returns
|
|
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
|
-
*
|
|
469
|
-
*
|
|
470
|
-
*
|
|
471
|
-
*
|
|
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) {
|