data-structure-typed 1.34.7 → 1.34.9
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/.eslintrc.js +1 -0
- package/CHANGELOG.md +1 -1
- package/CONTRIBUTING.md +0 -0
- package/README.md +8 -8
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/aa-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +314 -457
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +64 -90
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/b-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
- package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +12 -31
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.js +157 -244
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js +20 -44
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/segment-tree.js +80 -122
- package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/splay-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +168 -239
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/binary-tree/two-three-tree.js.map +1 -1
- package/dist/data-structures/graph/abstract-graph.js +351 -585
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.js +152 -282
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/map-graph.js +47 -88
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.js +91 -180
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/hash/coordinate-map.js +23 -45
- package/dist/data-structures/hash/coordinate-map.js.map +1 -1
- package/dist/data-structures/hash/coordinate-set.js +20 -42
- package/dist/data-structures/hash/coordinate-set.js.map +1 -1
- package/dist/data-structures/hash/hash-map.js +85 -247
- package/dist/data-structures/hash/hash-map.js.map +1 -1
- package/dist/data-structures/hash/hash-table.js +87 -128
- package/dist/data-structures/hash/hash-table.js.map +1 -1
- package/dist/data-structures/hash/pair.js +2 -5
- package/dist/data-structures/hash/pair.js.map +1 -1
- package/dist/data-structures/hash/tree-map.js +2 -5
- package/dist/data-structures/hash/tree-map.js.map +1 -1
- package/dist/data-structures/hash/tree-set.js +2 -5
- package/dist/data-structures/hash/tree-set.js.map +1 -1
- package/dist/data-structures/heap/heap.js +56 -80
- package/dist/data-structures/heap/heap.js.map +1 -1
- package/dist/data-structures/heap/max-heap.js +8 -26
- package/dist/data-structures/heap/max-heap.js.map +1 -1
- package/dist/data-structures/heap/min-heap.js +8 -26
- package/dist/data-structures/heap/min-heap.js.map +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +149 -218
- package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +119 -218
- package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +50 -70
- package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/data-structures/matrix/matrix.js +7 -8
- package/dist/data-structures/matrix/matrix.js.map +1 -1
- package/dist/data-structures/matrix/matrix2d.js +57 -70
- package/dist/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/data-structures/matrix/navigator.js +18 -37
- package/dist/data-structures/matrix/navigator.js.map +1 -1
- package/dist/data-structures/matrix/vector2d.js +63 -84
- package/dist/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +13 -41
- package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +13 -41
- package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +93 -139
- package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/data-structures/queue/deque.js +82 -128
- package/dist/data-structures/queue/deque.js.map +1 -1
- package/dist/data-structures/queue/queue.js +57 -157
- package/dist/data-structures/queue/queue.js.map +1 -1
- package/dist/data-structures/stack/stack.js +21 -22
- package/dist/data-structures/stack/stack.js.map +1 -1
- package/dist/data-structures/tree/tree.js +33 -46
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/dist/data-structures/trie/trie.js +93 -200
- package/dist/data-structures/trie/trie.js.map +1 -1
- package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
- package/dist/utils/utils.js +22 -107
- package/dist/utils/utils.js.map +1 -1
- package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +124 -123
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +136 -216
- package/lib/data-structures/binary-tree/avl-tree.d.ts +19 -10
- package/lib/data-structures/binary-tree/avl-tree.js +35 -13
- package/lib/data-structures/binary-tree/binary-tree.d.ts +6 -6
- package/lib/data-structures/binary-tree/binary-tree.js +7 -7
- package/lib/data-structures/binary-tree/bst.d.ts +34 -34
- package/lib/data-structures/binary-tree/bst.js +86 -89
- package/lib/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/lib/data-structures/binary-tree/rb-tree.js +4 -4
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +40 -42
- package/lib/data-structures/binary-tree/tree-multiset.js +76 -86
- package/lib/data-structures/graph/abstract-graph.d.ts +62 -62
- package/lib/data-structures/graph/abstract-graph.js +86 -86
- package/lib/data-structures/graph/directed-graph.d.ts +51 -51
- package/lib/data-structures/graph/directed-graph.js +63 -63
- package/lib/data-structures/graph/map-graph.d.ts +13 -13
- package/lib/data-structures/graph/map-graph.js +12 -12
- package/lib/data-structures/graph/undirected-graph.d.ts +30 -30
- package/lib/data-structures/graph/undirected-graph.js +32 -32
- package/lib/data-structures/priority-queue/priority-queue.d.ts +3 -3
- package/lib/data-structures/priority-queue/priority-queue.js +3 -3
- package/lib/data-structures/tree/tree.d.ts +4 -4
- package/lib/data-structures/tree/tree.js +6 -6
- package/lib/interfaces/abstract-binary-tree.d.ts +37 -42
- package/lib/interfaces/abstract-graph.d.ts +13 -13
- package/lib/interfaces/avl-tree.d.ts +6 -4
- package/lib/interfaces/bst.d.ts +10 -9
- package/lib/interfaces/directed-graph.d.ts +5 -5
- package/lib/interfaces/rb-tree.d.ts +2 -2
- package/lib/interfaces/undirected-graph.d.ts +2 -2
- package/lib/types/data-structures/abstract-binary-tree.d.ts +3 -3
- package/lib/types/data-structures/abstract-binary-tree.js +0 -1
- package/lib/types/data-structures/abstract-graph.d.ts +2 -2
- package/lib/types/data-structures/bst.d.ts +2 -2
- package/lib/types/data-structures/tree-multiset.d.ts +1 -1
- package/lib/types/utils/validate-type.d.ts +8 -8
- package/package.json +6 -6
- package/scripts/rename_clear_files.sh +29 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +241 -293
- package/src/data-structures/binary-tree/avl-tree.ts +43 -14
- package/src/data-structures/binary-tree/binary-tree.ts +8 -8
- package/src/data-structures/binary-tree/bst.ts +101 -96
- package/src/data-structures/binary-tree/rb-tree.ts +9 -9
- package/src/data-structures/binary-tree/tree-multiset.ts +82 -94
- package/src/data-structures/graph/abstract-graph.ts +114 -109
- package/src/data-structures/graph/directed-graph.ts +77 -77
- package/src/data-structures/graph/map-graph.ts +20 -15
- package/src/data-structures/graph/undirected-graph.ts +39 -39
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/tree/tree.ts +7 -7
- package/src/interfaces/abstract-binary-tree.ts +37 -50
- package/src/interfaces/abstract-graph.ts +13 -13
- package/src/interfaces/avl-tree.ts +6 -4
- package/src/interfaces/bst.ts +9 -9
- package/src/interfaces/directed-graph.ts +5 -5
- package/src/interfaces/rb-tree.ts +2 -2
- package/src/interfaces/undirected-graph.ts +2 -2
- package/src/types/data-structures/abstract-binary-tree.ts +3 -4
- package/src/types/data-structures/abstract-graph.ts +2 -2
- package/src/types/data-structures/bst.ts +2 -2
- package/src/types/data-structures/tree-multiset.ts +1 -1
- package/src/types/utils/validate-type.ts +10 -10
- package/test/integration/avl-tree.test.ts +24 -24
- package/test/integration/bst.test.ts +71 -71
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +28 -28
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +4 -12
- package/test/unit/data-structures/binary-tree/bst.test.ts +79 -79
- package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +80 -80
- package/test/unit/data-structures/graph/directed-graph.test.ts +8 -8
- package/test/unit/data-structures/graph/map-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/overall.test.ts +2 -2
- package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +4 -5
- package/test/unit/data-structures/tree/tree.test.ts +2 -2
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -7,30 +7,30 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import {arrayRemove, uuidV4} from '../../utils';
|
|
9
9
|
import {PriorityQueue} from '../priority-queue';
|
|
10
|
-
import type {DijkstraResult,
|
|
10
|
+
import type {DijkstraResult, VertexKey} from '../../types';
|
|
11
11
|
import {IAbstractGraph} from '../../interfaces';
|
|
12
12
|
|
|
13
13
|
export abstract class AbstractVertex<V = any> {
|
|
14
14
|
/**
|
|
15
|
-
* The function is a protected constructor that takes an
|
|
16
|
-
* @param {
|
|
15
|
+
* The function is a protected constructor that takes an key and an optional value as parameters.
|
|
16
|
+
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
|
17
17
|
* used to uniquely identify the vertex object.
|
|
18
18
|
* @param {V} [val] - The parameter "val" is an optional parameter of type V. It is used to assign a value to the
|
|
19
19
|
* vertex. If no value is provided, it will be set to undefined.
|
|
20
20
|
*/
|
|
21
|
-
protected constructor(
|
|
22
|
-
this.
|
|
21
|
+
protected constructor(key: VertexKey, val?: V) {
|
|
22
|
+
this._key = key;
|
|
23
23
|
this._val = val;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
private
|
|
26
|
+
private _key: VertexKey;
|
|
27
27
|
|
|
28
|
-
get
|
|
29
|
-
return this.
|
|
28
|
+
get key(): VertexKey {
|
|
29
|
+
return this._key;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
set
|
|
33
|
-
this.
|
|
32
|
+
set key(v: VertexKey) {
|
|
33
|
+
this._key = v;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
private _val: V | undefined;
|
|
@@ -106,19 +106,19 @@ export abstract class AbstractGraph<
|
|
|
106
106
|
E extends AbstractEdge<any> = AbstractEdge<any>
|
|
107
107
|
> implements IAbstractGraph<V, E>
|
|
108
108
|
{
|
|
109
|
-
private _vertices: Map<
|
|
109
|
+
private _vertices: Map<VertexKey, V> = new Map<VertexKey, V>();
|
|
110
110
|
|
|
111
|
-
get vertices(): Map<
|
|
111
|
+
get vertices(): Map<VertexKey, V> {
|
|
112
112
|
return this._vertices;
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
/**
|
|
116
116
|
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
117
117
|
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
118
|
-
* @param
|
|
118
|
+
* @param key
|
|
119
119
|
* @param val
|
|
120
120
|
*/
|
|
121
|
-
abstract createVertex(
|
|
121
|
+
abstract createVertex(key: VertexKey, val?: V): V;
|
|
122
122
|
|
|
123
123
|
/**
|
|
124
124
|
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
@@ -128,75 +128,75 @@ export abstract class AbstractGraph<
|
|
|
128
128
|
* @param weight
|
|
129
129
|
* @param val
|
|
130
130
|
*/
|
|
131
|
-
abstract createEdge(srcOrV1:
|
|
131
|
+
abstract createEdge(srcOrV1: VertexKey | string, destOrV2: VertexKey | string, weight?: number, val?: E): E;
|
|
132
132
|
|
|
133
133
|
abstract removeEdge(edge: E): E | null;
|
|
134
134
|
|
|
135
|
-
abstract getEdge(
|
|
135
|
+
abstract getEdge(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null;
|
|
136
136
|
|
|
137
|
-
abstract degreeOf(
|
|
137
|
+
abstract degreeOf(vertexOrKey: V | VertexKey): number;
|
|
138
138
|
|
|
139
139
|
abstract edgeSet(): E[];
|
|
140
140
|
|
|
141
|
-
abstract edgesOf(
|
|
141
|
+
abstract edgesOf(vertexOrKey: V | VertexKey): E[];
|
|
142
142
|
|
|
143
|
-
abstract getNeighbors(
|
|
143
|
+
abstract getNeighbors(vertexOrKey: V | VertexKey): V[];
|
|
144
144
|
|
|
145
145
|
abstract getEndsOfEdge(edge: E): [V, V] | null;
|
|
146
146
|
|
|
147
147
|
/**
|
|
148
148
|
* The function "getVertex" returns the vertex with the specified ID or null if it doesn't exist.
|
|
149
|
-
* @param {
|
|
149
|
+
* @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
|
|
150
150
|
* the `_vertices` map.
|
|
151
|
-
* @returns The method `getVertex` returns the vertex with the specified `
|
|
151
|
+
* @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertices`
|
|
152
152
|
* map. If the vertex does not exist, it returns `null`.
|
|
153
153
|
*/
|
|
154
|
-
getVertex(
|
|
155
|
-
return this._vertices.get(
|
|
154
|
+
getVertex(vertexKey: VertexKey): V | null {
|
|
155
|
+
return this._vertices.get(vertexKey) || null;
|
|
156
156
|
}
|
|
157
157
|
|
|
158
158
|
/**
|
|
159
159
|
* The function checks if a vertex exists in a graph.
|
|
160
|
-
* @param {V |
|
|
161
|
-
* (`
|
|
160
|
+
* @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
|
|
161
|
+
* (`VertexKey`).
|
|
162
162
|
* @returns a boolean value.
|
|
163
163
|
*/
|
|
164
|
-
hasVertex(
|
|
165
|
-
return this._vertices.has(this.
|
|
164
|
+
hasVertex(vertexOrKey: V | VertexKey): boolean {
|
|
165
|
+
return this._vertices.has(this._getVertexKey(vertexOrKey));
|
|
166
166
|
}
|
|
167
167
|
|
|
168
168
|
addVertex(vertex: V): boolean;
|
|
169
169
|
|
|
170
|
-
addVertex(
|
|
170
|
+
addVertex(key: VertexKey, val?: V['val']): boolean;
|
|
171
171
|
|
|
172
|
-
addVertex(
|
|
173
|
-
if (
|
|
174
|
-
return this._addVertexOnly(
|
|
172
|
+
addVertex(keyOrVertex: VertexKey | V, val?: V['val']): boolean {
|
|
173
|
+
if (keyOrVertex instanceof AbstractVertex) {
|
|
174
|
+
return this._addVertexOnly(keyOrVertex);
|
|
175
175
|
} else {
|
|
176
|
-
const newVertex = this.createVertex(
|
|
176
|
+
const newVertex = this.createVertex(keyOrVertex, val);
|
|
177
177
|
return this._addVertexOnly(newVertex);
|
|
178
178
|
}
|
|
179
179
|
}
|
|
180
180
|
|
|
181
181
|
/**
|
|
182
182
|
* The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
183
|
-
* @param {V |
|
|
184
|
-
* (`
|
|
183
|
+
* @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
|
|
184
|
+
* (`VertexKey`).
|
|
185
185
|
* @returns The method is returning a boolean value.
|
|
186
186
|
*/
|
|
187
|
-
removeVertex(
|
|
188
|
-
const
|
|
189
|
-
return this._vertices.delete(
|
|
187
|
+
removeVertex(vertexOrKey: V | VertexKey): boolean {
|
|
188
|
+
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
189
|
+
return this._vertices.delete(vertexKey);
|
|
190
190
|
}
|
|
191
191
|
|
|
192
192
|
/**
|
|
193
193
|
* The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
|
|
194
|
-
* @param {V[] |
|
|
195
|
-
* of vertex IDs (`
|
|
194
|
+
* @param {V[] | VertexKey[]} vertices - The `vertices` parameter can be either an array of vertices (`V[]`) or an array
|
|
195
|
+
* of vertex IDs (`VertexKey[]`).
|
|
196
196
|
* @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
|
|
197
197
|
* were removed.
|
|
198
198
|
*/
|
|
199
|
-
removeAllVertices(vertices: V[] |
|
|
199
|
+
removeAllVertices(vertices: V[] | VertexKey[]): boolean {
|
|
200
200
|
const removed: boolean[] = [];
|
|
201
201
|
for (const v of vertices) {
|
|
202
202
|
removed.push(this.removeVertex(v));
|
|
@@ -206,50 +206,50 @@ export abstract class AbstractGraph<
|
|
|
206
206
|
|
|
207
207
|
/**
|
|
208
208
|
* The function checks if there is an edge between two vertices and returns a boolean value indicating the result.
|
|
209
|
-
* @param {
|
|
209
|
+
* @param {VertexKey | V} v1 - The parameter v1 can be either a VertexKey or a V. A VertexKey represents the unique
|
|
210
210
|
* identifier of a vertex in a graph, while V represents the type of the vertex object itself.
|
|
211
|
-
* @param {
|
|
212
|
-
* `
|
|
211
|
+
* @param {VertexKey | V} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
|
|
212
|
+
* `VertexKey` or a `V` type, which represents the type of the vertex.
|
|
213
213
|
* @returns A boolean value is being returned.
|
|
214
214
|
*/
|
|
215
|
-
hasEdge(v1:
|
|
215
|
+
hasEdge(v1: VertexKey | V, v2: VertexKey | V): boolean {
|
|
216
216
|
const edge = this.getEdge(v1, v2);
|
|
217
217
|
return !!edge;
|
|
218
218
|
}
|
|
219
219
|
|
|
220
220
|
addEdge(edge: E): boolean;
|
|
221
221
|
|
|
222
|
-
addEdge(src: V |
|
|
222
|
+
addEdge(src: V | VertexKey, dest: V | VertexKey, weight?: number, val?: E['val']): boolean;
|
|
223
223
|
|
|
224
|
-
addEdge(srcOrEdge: V |
|
|
224
|
+
addEdge(srcOrEdge: V | VertexKey | E, dest?: V | VertexKey, weight?: number, val?: E['val']): boolean {
|
|
225
225
|
if (srcOrEdge instanceof AbstractEdge) {
|
|
226
226
|
return this._addEdgeOnly(srcOrEdge);
|
|
227
227
|
} else {
|
|
228
228
|
if (dest instanceof AbstractVertex || typeof dest === 'string' || typeof dest === 'number') {
|
|
229
229
|
if (!(this.hasVertex(srcOrEdge) && this.hasVertex(dest))) return false;
|
|
230
|
-
if (srcOrEdge instanceof AbstractVertex) srcOrEdge = srcOrEdge.
|
|
231
|
-
if (dest instanceof AbstractVertex) dest = dest.
|
|
230
|
+
if (srcOrEdge instanceof AbstractVertex) srcOrEdge = srcOrEdge.key;
|
|
231
|
+
if (dest instanceof AbstractVertex) dest = dest.key;
|
|
232
232
|
const newEdge = this.createEdge(srcOrEdge, dest, weight, val);
|
|
233
233
|
return this._addEdgeOnly(newEdge);
|
|
234
234
|
} else {
|
|
235
|
-
throw new Error('dest must be a Vertex or vertex
|
|
235
|
+
throw new Error('dest must be a Vertex or vertex key while srcOrEdge is an Edge');
|
|
236
236
|
}
|
|
237
237
|
}
|
|
238
238
|
}
|
|
239
239
|
|
|
240
240
|
/**
|
|
241
241
|
* The function sets the weight of an edge between two vertices in a graph.
|
|
242
|
-
* @param {
|
|
242
|
+
* @param {VertexKey | V} srcOrKey - The `srcOrKey` parameter can be either a `VertexKey` or a `V` object. It represents
|
|
243
243
|
* the source vertex of the edge.
|
|
244
|
-
* @param {
|
|
245
|
-
* either a `
|
|
246
|
-
* @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (
|
|
247
|
-
* and the destination vertex (
|
|
244
|
+
* @param {VertexKey | V} destOrKey - The `destOrKey` parameter represents the destination vertex of the edge. It can be
|
|
245
|
+
* either a `VertexKey` or a vertex object `V`.
|
|
246
|
+
* @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrKey)
|
|
247
|
+
* and the destination vertex (destOrKey).
|
|
248
248
|
* @returns a boolean value. If the edge exists between the source and destination vertices, the function will update
|
|
249
249
|
* the weight of the edge and return true. If the edge does not exist, the function will return false.
|
|
250
250
|
*/
|
|
251
|
-
setEdgeWeight(
|
|
252
|
-
const edge = this.getEdge(
|
|
251
|
+
setEdgeWeight(srcOrKey: VertexKey | V, destOrKey: VertexKey | V, weight: number): boolean {
|
|
252
|
+
const edge = this.getEdge(srcOrKey, destOrKey);
|
|
253
253
|
if (edge) {
|
|
254
254
|
edge.weight = weight;
|
|
255
255
|
return true;
|
|
@@ -260,12 +260,12 @@ export abstract class AbstractGraph<
|
|
|
260
260
|
|
|
261
261
|
/**
|
|
262
262
|
* The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
|
|
263
|
-
* @param {V |
|
|
263
|
+
* @param {V | VertexKey} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexKey`).
|
|
264
264
|
* It is the starting vertex for finding paths.
|
|
265
|
-
* @param {V |
|
|
265
|
+
* @param {V | VertexKey} v2 - The parameter `v2` represents either a vertex object (`V`) or a vertex ID (`VertexKey`).
|
|
266
266
|
* @returns The function `getAllPathsBetween` returns an array of arrays of vertices (`V[][]`).
|
|
267
267
|
*/
|
|
268
|
-
getAllPathsBetween(v1: V |
|
|
268
|
+
getAllPathsBetween(v1: V | VertexKey, v2: V | VertexKey): V[][] {
|
|
269
269
|
const paths: V[][] = [];
|
|
270
270
|
const vertex1 = this._getVertex(v1);
|
|
271
271
|
const vertex2 = this._getVertex(v2);
|
|
@@ -312,8 +312,8 @@ export abstract class AbstractGraph<
|
|
|
312
312
|
/**
|
|
313
313
|
* The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
|
|
314
314
|
* weights or using a breadth-first search algorithm.
|
|
315
|
-
* @param {V |
|
|
316
|
-
* @param {V |
|
|
315
|
+
* @param {V | VertexKey} v1 - The parameter `v1` represents the starting vertex or its ID.
|
|
316
|
+
* @param {V | VertexKey} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
|
|
317
317
|
* you want to find the minimum cost or weight from the source vertex `v1`.
|
|
318
318
|
* @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edges have weights.
|
|
319
319
|
* If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
|
|
@@ -323,7 +323,7 @@ export abstract class AbstractGraph<
|
|
|
323
323
|
* vertices. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
|
|
324
324
|
* minimum number of
|
|
325
325
|
*/
|
|
326
|
-
getMinCostBetween(v1: V |
|
|
326
|
+
getMinCostBetween(v1: V | VertexKey, v2: V | VertexKey, isWeight?: boolean): number | null {
|
|
327
327
|
if (isWeight === undefined) isWeight = false;
|
|
328
328
|
|
|
329
329
|
if (isWeight) {
|
|
@@ -371,9 +371,9 @@ export abstract class AbstractGraph<
|
|
|
371
371
|
/**
|
|
372
372
|
* The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
|
|
373
373
|
* using a breadth-first search algorithm.
|
|
374
|
-
* @param {V |
|
|
375
|
-
* object (`V`) or a vertex ID (`
|
|
376
|
-
* @param {V |
|
|
374
|
+
* @param {V | VertexKey} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
|
|
375
|
+
* object (`V`) or a vertex ID (`VertexKey`).
|
|
376
|
+
* @param {V | VertexKey} v2 - V | VertexKey - The second vertex or vertex ID between which we want to find the minimum
|
|
377
377
|
* path.
|
|
378
378
|
* @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edges in finding the
|
|
379
379
|
* minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
|
|
@@ -381,7 +381,7 @@ export abstract class AbstractGraph<
|
|
|
381
381
|
* @returns The function `getMinPathBetween` returns an array of vertices (`V[]`) representing the minimum path between
|
|
382
382
|
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `null`.
|
|
383
383
|
*/
|
|
384
|
-
getMinPathBetween(v1: V |
|
|
384
|
+
getMinPathBetween(v1: V | VertexKey, v2: V | VertexKey, isWeight?: boolean): V[] | null {
|
|
385
385
|
if (isWeight === undefined) isWeight = false;
|
|
386
386
|
|
|
387
387
|
if (isWeight) {
|
|
@@ -440,9 +440,9 @@ export abstract class AbstractGraph<
|
|
|
440
440
|
* Dijkstra algorithm time: O(VE) space: O(V + E)
|
|
441
441
|
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
|
|
442
442
|
* a graph without using a heap data structure.
|
|
443
|
-
* @param {V |
|
|
443
|
+
* @param {V | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
444
444
|
* vertex object or a vertex ID.
|
|
445
|
-
* @param {V |
|
|
445
|
+
* @param {V | VertexKey | null} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
446
446
|
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
447
447
|
* identifier. If no destination is provided, the value is set to `null`.
|
|
448
448
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
@@ -454,8 +454,8 @@ export abstract class AbstractGraph<
|
|
|
454
454
|
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<V>`.
|
|
455
455
|
*/
|
|
456
456
|
dijkstraWithoutHeap(
|
|
457
|
-
src: V |
|
|
458
|
-
dest?: V |
|
|
457
|
+
src: V | VertexKey,
|
|
458
|
+
dest?: V | VertexKey | null,
|
|
459
459
|
getMinDist?: boolean,
|
|
460
460
|
genPaths?: boolean
|
|
461
461
|
): DijkstraResult<V> {
|
|
@@ -481,8 +481,8 @@ export abstract class AbstractGraph<
|
|
|
481
481
|
}
|
|
482
482
|
|
|
483
483
|
for (const vertex of vertices) {
|
|
484
|
-
const
|
|
485
|
-
if (
|
|
484
|
+
const vertexOrKey = vertex[1];
|
|
485
|
+
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
486
486
|
}
|
|
487
487
|
distMap.set(srcVertex, 0);
|
|
488
488
|
preMap.set(srcVertex, null);
|
|
@@ -503,11 +503,11 @@ export abstract class AbstractGraph<
|
|
|
503
503
|
|
|
504
504
|
const getPaths = (minV: V | null) => {
|
|
505
505
|
for (const vertex of vertices) {
|
|
506
|
-
const
|
|
506
|
+
const vertexOrKey = vertex[1];
|
|
507
507
|
|
|
508
|
-
if (
|
|
509
|
-
const path: V[] = [
|
|
510
|
-
let parent = preMap.get(
|
|
508
|
+
if (vertexOrKey instanceof AbstractVertex) {
|
|
509
|
+
const path: V[] = [vertexOrKey];
|
|
510
|
+
let parent = preMap.get(vertexOrKey);
|
|
511
511
|
while (parent) {
|
|
512
512
|
path.push(parent);
|
|
513
513
|
parent = preMap.get(parent);
|
|
@@ -580,9 +580,9 @@ export abstract class AbstractGraph<
|
|
|
580
580
|
* Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
|
|
581
581
|
* The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
|
|
582
582
|
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
583
|
-
* @param {V |
|
|
583
|
+
* @param {V | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
584
584
|
* start. It can be either a vertex object or a vertex ID.
|
|
585
|
-
* @param {V |
|
|
585
|
+
* @param {V | VertexKey | null} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
586
586
|
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
587
587
|
* will calculate the shortest paths to all other vertices from the source vertex.
|
|
588
588
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
@@ -593,7 +593,12 @@ export abstract class AbstractGraph<
|
|
|
593
593
|
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
594
594
|
* @returns The function `dijkstra` returns an object of type `DijkstraResult<V>`.
|
|
595
595
|
*/
|
|
596
|
-
dijkstra(
|
|
596
|
+
dijkstra(
|
|
597
|
+
src: V | VertexKey,
|
|
598
|
+
dest?: V | VertexKey | null,
|
|
599
|
+
getMinDist?: boolean,
|
|
600
|
+
genPaths?: boolean
|
|
601
|
+
): DijkstraResult<V> {
|
|
597
602
|
if (getMinDist === undefined) getMinDist = false;
|
|
598
603
|
if (genPaths === undefined) genPaths = false;
|
|
599
604
|
|
|
@@ -613,14 +618,14 @@ export abstract class AbstractGraph<
|
|
|
613
618
|
if (!srcVertex) return null;
|
|
614
619
|
|
|
615
620
|
for (const vertex of vertices) {
|
|
616
|
-
const
|
|
617
|
-
if (
|
|
621
|
+
const vertexOrKey = vertex[1];
|
|
622
|
+
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
618
623
|
}
|
|
619
624
|
|
|
620
|
-
const heap = new PriorityQueue<{
|
|
621
|
-
comparator: (a, b) => a.
|
|
625
|
+
const heap = new PriorityQueue<{key: number; val: V}>({
|
|
626
|
+
comparator: (a, b) => a.key - b.key
|
|
622
627
|
});
|
|
623
|
-
heap.add({
|
|
628
|
+
heap.add({key: 0, val: srcVertex});
|
|
624
629
|
|
|
625
630
|
distMap.set(srcVertex, 0);
|
|
626
631
|
preMap.set(srcVertex, null);
|
|
@@ -632,10 +637,10 @@ export abstract class AbstractGraph<
|
|
|
632
637
|
*/
|
|
633
638
|
const getPaths = (minV: V | null) => {
|
|
634
639
|
for (const vertex of vertices) {
|
|
635
|
-
const
|
|
636
|
-
if (
|
|
637
|
-
const path: V[] = [
|
|
638
|
-
let parent = preMap.get(
|
|
640
|
+
const vertexOrKey = vertex[1];
|
|
641
|
+
if (vertexOrKey instanceof AbstractVertex) {
|
|
642
|
+
const path: V[] = [vertexOrKey];
|
|
643
|
+
let parent = preMap.get(vertexOrKey);
|
|
639
644
|
while (parent) {
|
|
640
645
|
path.push(parent);
|
|
641
646
|
parent = preMap.get(parent);
|
|
@@ -649,7 +654,7 @@ export abstract class AbstractGraph<
|
|
|
649
654
|
|
|
650
655
|
while (heap.size > 0) {
|
|
651
656
|
const curHeapNode = heap.poll();
|
|
652
|
-
const dist = curHeapNode?.
|
|
657
|
+
const dist = curHeapNode?.key;
|
|
653
658
|
const cur = curHeapNode?.val;
|
|
654
659
|
if (dist !== undefined) {
|
|
655
660
|
if (cur) {
|
|
@@ -671,7 +676,7 @@ export abstract class AbstractGraph<
|
|
|
671
676
|
const distSrcToNeighbor = distMap.get(neighbor);
|
|
672
677
|
if (distSrcToNeighbor) {
|
|
673
678
|
if (dist + weight < distSrcToNeighbor) {
|
|
674
|
-
heap.add({
|
|
679
|
+
heap.add({key: dist + weight, val: neighbor});
|
|
675
680
|
preMap.set(neighbor, cur);
|
|
676
681
|
distMap.set(neighbor, dist + weight);
|
|
677
682
|
}
|
|
@@ -712,7 +717,7 @@ export abstract class AbstractGraph<
|
|
|
712
717
|
* The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
713
718
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
714
719
|
* all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.
|
|
715
|
-
* @param {V |
|
|
720
|
+
* @param {V | VertexKey} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
|
|
716
721
|
* start calculating the shortest paths. It can be either a vertex object or a vertex ID.
|
|
717
722
|
* @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
|
|
718
723
|
* @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
|
|
@@ -722,7 +727,7 @@ export abstract class AbstractGraph<
|
|
|
722
727
|
* vertex.
|
|
723
728
|
* @returns The function `bellmanFord` returns an object with the following properties:
|
|
724
729
|
*/
|
|
725
|
-
bellmanFord(src: V |
|
|
730
|
+
bellmanFord(src: V | VertexKey, scanNegativeCycle?: boolean, getMin?: boolean, genPath?: boolean) {
|
|
726
731
|
if (getMin === undefined) getMin = false;
|
|
727
732
|
if (genPath === undefined) genPath = false;
|
|
728
733
|
|
|
@@ -780,10 +785,10 @@ export abstract class AbstractGraph<
|
|
|
780
785
|
|
|
781
786
|
if (genPath) {
|
|
782
787
|
for (const vertex of vertices) {
|
|
783
|
-
const
|
|
784
|
-
if (
|
|
785
|
-
const path: V[] = [
|
|
786
|
-
let parent = preMap.get(
|
|
788
|
+
const vertexOrKey = vertex[1];
|
|
789
|
+
if (vertexOrKey instanceof AbstractVertex) {
|
|
790
|
+
const path: V[] = [vertexOrKey];
|
|
791
|
+
let parent = preMap.get(vertexOrKey);
|
|
787
792
|
while (parent !== undefined) {
|
|
788
793
|
path.push(parent);
|
|
789
794
|
parent = preMap.get(parent);
|
|
@@ -884,7 +889,7 @@ export abstract class AbstractGraph<
|
|
|
884
889
|
}
|
|
885
890
|
|
|
886
891
|
/**
|
|
887
|
-
* Tarjan is an algorithm based on
|
|
892
|
+
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
888
893
|
* Tarjan can find cycles in directed or undirected graph
|
|
889
894
|
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
890
895
|
* Tarjan solve the bi-connected components of undirected graphs;
|
|
@@ -892,7 +897,7 @@ export abstract class AbstractGraph<
|
|
|
892
897
|
* /
|
|
893
898
|
|
|
894
899
|
/**
|
|
895
|
-
* Tarjan is an algorithm based on
|
|
900
|
+
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
896
901
|
* Tarjan can find cycles in directed or undirected graph
|
|
897
902
|
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
898
903
|
* Tarjan solve the bi-connected components of undirected graphs;
|
|
@@ -913,8 +918,8 @@ export abstract class AbstractGraph<
|
|
|
913
918
|
* @returns The function `tarjan` returns an object with the following properties:
|
|
914
919
|
*/
|
|
915
920
|
tarjan(needArticulationPoints?: boolean, needBridges?: boolean, needSCCs?: boolean, needCycles?: boolean) {
|
|
916
|
-
// !! in undirected graph we will not let child visit parent when
|
|
917
|
-
// !! articulation point(in
|
|
921
|
+
// !! in undirected graph we will not let child visit parent when dfs
|
|
922
|
+
// !! articulation point(in dfs search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
|
|
918
923
|
// !! bridge: low(child) > dfn(cur)
|
|
919
924
|
|
|
920
925
|
const defaultConfig = false;
|
|
@@ -942,7 +947,7 @@ export abstract class AbstractGraph<
|
|
|
942
947
|
lowMap.set(cur, dfn);
|
|
943
948
|
|
|
944
949
|
const neighbors = this.getNeighbors(cur);
|
|
945
|
-
let childCount = 0; // child in
|
|
950
|
+
let childCount = 0; // child in dfs tree not child in graph
|
|
946
951
|
for (const neighbor of neighbors) {
|
|
947
952
|
if (neighbor !== parent) {
|
|
948
953
|
if (dfnMap.get(neighbor) === -1) {
|
|
@@ -1019,22 +1024,22 @@ export abstract class AbstractGraph<
|
|
|
1019
1024
|
protected _addVertexOnly(newVertex: V): boolean {
|
|
1020
1025
|
if (this.hasVertex(newVertex)) {
|
|
1021
1026
|
return false;
|
|
1022
|
-
// throw (new Error('Duplicated vertex
|
|
1027
|
+
// throw (new Error('Duplicated vertex key is not allowed'));
|
|
1023
1028
|
}
|
|
1024
|
-
this._vertices.set(newVertex.
|
|
1029
|
+
this._vertices.set(newVertex.key, newVertex);
|
|
1025
1030
|
return true;
|
|
1026
1031
|
}
|
|
1027
1032
|
|
|
1028
|
-
protected _getVertex(
|
|
1029
|
-
const
|
|
1030
|
-
return this._vertices.get(
|
|
1033
|
+
protected _getVertex(vertexOrKey: VertexKey | V): V | null {
|
|
1034
|
+
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
1035
|
+
return this._vertices.get(vertexKey) || null;
|
|
1031
1036
|
}
|
|
1032
1037
|
|
|
1033
|
-
protected
|
|
1034
|
-
return
|
|
1038
|
+
protected _getVertexKey(vertexOrKey: V | VertexKey): VertexKey {
|
|
1039
|
+
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|
|
1035
1040
|
}
|
|
1036
1041
|
|
|
1037
|
-
protected _setVertices(value: Map<
|
|
1042
|
+
protected _setVertices(value: Map<VertexKey, V>) {
|
|
1038
1043
|
this._vertices = value;
|
|
1039
1044
|
}
|
|
1040
1045
|
}
|