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
|
@@ -9,21 +9,21 @@ import { arrayRemove, uuidV4 } from '../../utils';
|
|
|
9
9
|
import { PriorityQueue } from '../priority-queue';
|
|
10
10
|
export class AbstractVertex {
|
|
11
11
|
/**
|
|
12
|
-
* The function is a protected constructor that takes an
|
|
13
|
-
* @param {
|
|
12
|
+
* The function is a protected constructor that takes an key and an optional value as parameters.
|
|
13
|
+
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
|
14
14
|
* used to uniquely identify the vertex object.
|
|
15
15
|
* @param {V} [val] - The parameter "val" is an optional parameter of type V. It is used to assign a value to the
|
|
16
16
|
* vertex. If no value is provided, it will be set to undefined.
|
|
17
17
|
*/
|
|
18
|
-
constructor(
|
|
19
|
-
this.
|
|
18
|
+
constructor(key, val) {
|
|
19
|
+
this._key = key;
|
|
20
20
|
this._val = val;
|
|
21
21
|
}
|
|
22
|
-
get
|
|
23
|
-
return this.
|
|
22
|
+
get key() {
|
|
23
|
+
return this._key;
|
|
24
24
|
}
|
|
25
|
-
set
|
|
26
|
-
this.
|
|
25
|
+
set key(v) {
|
|
26
|
+
this._key = v;
|
|
27
27
|
}
|
|
28
28
|
get val() {
|
|
29
29
|
return this._val;
|
|
@@ -84,46 +84,46 @@ export class AbstractGraph {
|
|
|
84
84
|
}
|
|
85
85
|
/**
|
|
86
86
|
* The function "getVertex" returns the vertex with the specified ID or null if it doesn't exist.
|
|
87
|
-
* @param {
|
|
87
|
+
* @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
|
|
88
88
|
* the `_vertices` map.
|
|
89
|
-
* @returns The method `getVertex` returns the vertex with the specified `
|
|
89
|
+
* @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertices`
|
|
90
90
|
* map. If the vertex does not exist, it returns `null`.
|
|
91
91
|
*/
|
|
92
|
-
getVertex(
|
|
93
|
-
return this._vertices.get(
|
|
92
|
+
getVertex(vertexKey) {
|
|
93
|
+
return this._vertices.get(vertexKey) || null;
|
|
94
94
|
}
|
|
95
95
|
/**
|
|
96
96
|
* The function checks if a vertex exists in a graph.
|
|
97
|
-
* @param {V |
|
|
98
|
-
* (`
|
|
97
|
+
* @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
|
|
98
|
+
* (`VertexKey`).
|
|
99
99
|
* @returns a boolean value.
|
|
100
100
|
*/
|
|
101
|
-
hasVertex(
|
|
102
|
-
return this._vertices.has(this.
|
|
101
|
+
hasVertex(vertexOrKey) {
|
|
102
|
+
return this._vertices.has(this._getVertexKey(vertexOrKey));
|
|
103
103
|
}
|
|
104
|
-
addVertex(
|
|
105
|
-
if (
|
|
106
|
-
return this._addVertexOnly(
|
|
104
|
+
addVertex(keyOrVertex, val) {
|
|
105
|
+
if (keyOrVertex instanceof AbstractVertex) {
|
|
106
|
+
return this._addVertexOnly(keyOrVertex);
|
|
107
107
|
}
|
|
108
108
|
else {
|
|
109
|
-
const newVertex = this.createVertex(
|
|
109
|
+
const newVertex = this.createVertex(keyOrVertex, val);
|
|
110
110
|
return this._addVertexOnly(newVertex);
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
113
|
/**
|
|
114
114
|
* The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
115
|
-
* @param {V |
|
|
116
|
-
* (`
|
|
115
|
+
* @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
|
|
116
|
+
* (`VertexKey`).
|
|
117
117
|
* @returns The method is returning a boolean value.
|
|
118
118
|
*/
|
|
119
|
-
removeVertex(
|
|
120
|
-
const
|
|
121
|
-
return this._vertices.delete(
|
|
119
|
+
removeVertex(vertexOrKey) {
|
|
120
|
+
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
121
|
+
return this._vertices.delete(vertexKey);
|
|
122
122
|
}
|
|
123
123
|
/**
|
|
124
124
|
* The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
|
|
125
|
-
* @param {V[] |
|
|
126
|
-
* of vertex IDs (`
|
|
125
|
+
* @param {V[] | VertexKey[]} vertices - The `vertices` parameter can be either an array of vertices (`V[]`) or an array
|
|
126
|
+
* of vertex IDs (`VertexKey[]`).
|
|
127
127
|
* @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
|
|
128
128
|
* were removed.
|
|
129
129
|
*/
|
|
@@ -136,10 +136,10 @@ export class AbstractGraph {
|
|
|
136
136
|
}
|
|
137
137
|
/**
|
|
138
138
|
* The function checks if there is an edge between two vertices and returns a boolean value indicating the result.
|
|
139
|
-
* @param {
|
|
139
|
+
* @param {VertexKey | V} v1 - The parameter v1 can be either a VertexKey or a V. A VertexKey represents the unique
|
|
140
140
|
* identifier of a vertex in a graph, while V represents the type of the vertex object itself.
|
|
141
|
-
* @param {
|
|
142
|
-
* `
|
|
141
|
+
* @param {VertexKey | V} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
|
|
142
|
+
* `VertexKey` or a `V` type, which represents the type of the vertex.
|
|
143
143
|
* @returns A boolean value is being returned.
|
|
144
144
|
*/
|
|
145
145
|
hasEdge(v1, v2) {
|
|
@@ -155,30 +155,30 @@ export class AbstractGraph {
|
|
|
155
155
|
if (!(this.hasVertex(srcOrEdge) && this.hasVertex(dest)))
|
|
156
156
|
return false;
|
|
157
157
|
if (srcOrEdge instanceof AbstractVertex)
|
|
158
|
-
srcOrEdge = srcOrEdge.
|
|
158
|
+
srcOrEdge = srcOrEdge.key;
|
|
159
159
|
if (dest instanceof AbstractVertex)
|
|
160
|
-
dest = dest.
|
|
160
|
+
dest = dest.key;
|
|
161
161
|
const newEdge = this.createEdge(srcOrEdge, dest, weight, val);
|
|
162
162
|
return this._addEdgeOnly(newEdge);
|
|
163
163
|
}
|
|
164
164
|
else {
|
|
165
|
-
throw new Error('dest must be a Vertex or vertex
|
|
165
|
+
throw new Error('dest must be a Vertex or vertex key while srcOrEdge is an Edge');
|
|
166
166
|
}
|
|
167
167
|
}
|
|
168
168
|
}
|
|
169
169
|
/**
|
|
170
170
|
* The function sets the weight of an edge between two vertices in a graph.
|
|
171
|
-
* @param {
|
|
171
|
+
* @param {VertexKey | V} srcOrKey - The `srcOrKey` parameter can be either a `VertexKey` or a `V` object. It represents
|
|
172
172
|
* the source vertex of the edge.
|
|
173
|
-
* @param {
|
|
174
|
-
* either a `
|
|
175
|
-
* @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (
|
|
176
|
-
* and the destination vertex (
|
|
173
|
+
* @param {VertexKey | V} destOrKey - The `destOrKey` parameter represents the destination vertex of the edge. It can be
|
|
174
|
+
* either a `VertexKey` or a vertex object `V`.
|
|
175
|
+
* @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrKey)
|
|
176
|
+
* and the destination vertex (destOrKey).
|
|
177
177
|
* @returns a boolean value. If the edge exists between the source and destination vertices, the function will update
|
|
178
178
|
* the weight of the edge and return true. If the edge does not exist, the function will return false.
|
|
179
179
|
*/
|
|
180
|
-
setEdgeWeight(
|
|
181
|
-
const edge = this.getEdge(
|
|
180
|
+
setEdgeWeight(srcOrKey, destOrKey, weight) {
|
|
181
|
+
const edge = this.getEdge(srcOrKey, destOrKey);
|
|
182
182
|
if (edge) {
|
|
183
183
|
edge.weight = weight;
|
|
184
184
|
return true;
|
|
@@ -189,9 +189,9 @@ export class AbstractGraph {
|
|
|
189
189
|
}
|
|
190
190
|
/**
|
|
191
191
|
* The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
|
|
192
|
-
* @param {V |
|
|
192
|
+
* @param {V | VertexKey} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexKey`).
|
|
193
193
|
* It is the starting vertex for finding paths.
|
|
194
|
-
* @param {V |
|
|
194
|
+
* @param {V | VertexKey} v2 - The parameter `v2` represents either a vertex object (`V`) or a vertex ID (`VertexKey`).
|
|
195
195
|
* @returns The function `getAllPathsBetween` returns an array of arrays of vertices (`V[][]`).
|
|
196
196
|
*/
|
|
197
197
|
getAllPathsBetween(v1, v2) {
|
|
@@ -235,8 +235,8 @@ export class AbstractGraph {
|
|
|
235
235
|
/**
|
|
236
236
|
* The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
|
|
237
237
|
* weights or using a breadth-first search algorithm.
|
|
238
|
-
* @param {V |
|
|
239
|
-
* @param {V |
|
|
238
|
+
* @param {V | VertexKey} v1 - The parameter `v1` represents the starting vertex or its ID.
|
|
239
|
+
* @param {V | VertexKey} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
|
|
240
240
|
* you want to find the minimum cost or weight from the source vertex `v1`.
|
|
241
241
|
* @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edges have weights.
|
|
242
242
|
* If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
|
|
@@ -293,9 +293,9 @@ export class AbstractGraph {
|
|
|
293
293
|
/**
|
|
294
294
|
* The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
|
|
295
295
|
* using a breadth-first search algorithm.
|
|
296
|
-
* @param {V |
|
|
297
|
-
* object (`V`) or a vertex ID (`
|
|
298
|
-
* @param {V |
|
|
296
|
+
* @param {V | VertexKey} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
|
|
297
|
+
* object (`V`) or a vertex ID (`VertexKey`).
|
|
298
|
+
* @param {V | VertexKey} v2 - V | VertexKey - The second vertex or vertex ID between which we want to find the minimum
|
|
299
299
|
* path.
|
|
300
300
|
* @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edges in finding the
|
|
301
301
|
* minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
|
|
@@ -357,9 +357,9 @@ export class AbstractGraph {
|
|
|
357
357
|
* Dijkstra algorithm time: O(VE) space: O(V + E)
|
|
358
358
|
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
|
|
359
359
|
* a graph without using a heap data structure.
|
|
360
|
-
* @param {V |
|
|
360
|
+
* @param {V | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
361
361
|
* vertex object or a vertex ID.
|
|
362
|
-
* @param {V |
|
|
362
|
+
* @param {V | VertexKey | null} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
363
363
|
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
364
364
|
* identifier. If no destination is provided, the value is set to `null`.
|
|
365
365
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
@@ -391,9 +391,9 @@ export class AbstractGraph {
|
|
|
391
391
|
return null;
|
|
392
392
|
}
|
|
393
393
|
for (const vertex of vertices) {
|
|
394
|
-
const
|
|
395
|
-
if (
|
|
396
|
-
distMap.set(
|
|
394
|
+
const vertexOrKey = vertex[1];
|
|
395
|
+
if (vertexOrKey instanceof AbstractVertex)
|
|
396
|
+
distMap.set(vertexOrKey, Infinity);
|
|
397
397
|
}
|
|
398
398
|
distMap.set(srcVertex, 0);
|
|
399
399
|
preMap.set(srcVertex, null);
|
|
@@ -412,10 +412,10 @@ export class AbstractGraph {
|
|
|
412
412
|
};
|
|
413
413
|
const getPaths = (minV) => {
|
|
414
414
|
for (const vertex of vertices) {
|
|
415
|
-
const
|
|
416
|
-
if (
|
|
417
|
-
const path = [
|
|
418
|
-
let parent = preMap.get(
|
|
415
|
+
const vertexOrKey = vertex[1];
|
|
416
|
+
if (vertexOrKey instanceof AbstractVertex) {
|
|
417
|
+
const path = [vertexOrKey];
|
|
418
|
+
let parent = preMap.get(vertexOrKey);
|
|
419
419
|
while (parent) {
|
|
420
420
|
path.push(parent);
|
|
421
421
|
parent = preMap.get(parent);
|
|
@@ -485,9 +485,9 @@ export class AbstractGraph {
|
|
|
485
485
|
* 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.
|
|
486
486
|
* The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
|
|
487
487
|
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
488
|
-
* @param {V |
|
|
488
|
+
* @param {V | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
489
489
|
* start. It can be either a vertex object or a vertex ID.
|
|
490
|
-
* @param {V |
|
|
490
|
+
* @param {V | VertexKey | null} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
491
491
|
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
492
492
|
* will calculate the shortest paths to all other vertices from the source vertex.
|
|
493
493
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
@@ -519,14 +519,14 @@ export class AbstractGraph {
|
|
|
519
519
|
if (!srcVertex)
|
|
520
520
|
return null;
|
|
521
521
|
for (const vertex of vertices) {
|
|
522
|
-
const
|
|
523
|
-
if (
|
|
524
|
-
distMap.set(
|
|
522
|
+
const vertexOrKey = vertex[1];
|
|
523
|
+
if (vertexOrKey instanceof AbstractVertex)
|
|
524
|
+
distMap.set(vertexOrKey, Infinity);
|
|
525
525
|
}
|
|
526
526
|
const heap = new PriorityQueue({
|
|
527
|
-
comparator: (a, b) => a.
|
|
527
|
+
comparator: (a, b) => a.key - b.key
|
|
528
528
|
});
|
|
529
|
-
heap.add({
|
|
529
|
+
heap.add({ key: 0, val: srcVertex });
|
|
530
530
|
distMap.set(srcVertex, 0);
|
|
531
531
|
preMap.set(srcVertex, null);
|
|
532
532
|
/**
|
|
@@ -536,10 +536,10 @@ export class AbstractGraph {
|
|
|
536
536
|
*/
|
|
537
537
|
const getPaths = (minV) => {
|
|
538
538
|
for (const vertex of vertices) {
|
|
539
|
-
const
|
|
540
|
-
if (
|
|
541
|
-
const path = [
|
|
542
|
-
let parent = preMap.get(
|
|
539
|
+
const vertexOrKey = vertex[1];
|
|
540
|
+
if (vertexOrKey instanceof AbstractVertex) {
|
|
541
|
+
const path = [vertexOrKey];
|
|
542
|
+
let parent = preMap.get(vertexOrKey);
|
|
543
543
|
while (parent) {
|
|
544
544
|
path.push(parent);
|
|
545
545
|
parent = preMap.get(parent);
|
|
@@ -553,7 +553,7 @@ export class AbstractGraph {
|
|
|
553
553
|
};
|
|
554
554
|
while (heap.size > 0) {
|
|
555
555
|
const curHeapNode = heap.poll();
|
|
556
|
-
const dist = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.
|
|
556
|
+
const dist = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.key;
|
|
557
557
|
const cur = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.val;
|
|
558
558
|
if (dist !== undefined) {
|
|
559
559
|
if (cur) {
|
|
@@ -575,7 +575,7 @@ export class AbstractGraph {
|
|
|
575
575
|
const distSrcToNeighbor = distMap.get(neighbor);
|
|
576
576
|
if (distSrcToNeighbor) {
|
|
577
577
|
if (dist + weight < distSrcToNeighbor) {
|
|
578
|
-
heap.add({
|
|
578
|
+
heap.add({ key: dist + weight, val: neighbor });
|
|
579
579
|
preMap.set(neighbor, cur);
|
|
580
580
|
distMap.set(neighbor, dist + weight);
|
|
581
581
|
}
|
|
@@ -613,7 +613,7 @@ export class AbstractGraph {
|
|
|
613
613
|
* 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.
|
|
614
614
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
615
615
|
* all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.
|
|
616
|
-
* @param {V |
|
|
616
|
+
* @param {V | VertexKey} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
|
|
617
617
|
* start calculating the shortest paths. It can be either a vertex object or a vertex ID.
|
|
618
618
|
* @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
|
|
619
619
|
* @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
|
|
@@ -679,10 +679,10 @@ export class AbstractGraph {
|
|
|
679
679
|
}
|
|
680
680
|
if (genPath) {
|
|
681
681
|
for (const vertex of vertices) {
|
|
682
|
-
const
|
|
683
|
-
if (
|
|
684
|
-
const path = [
|
|
685
|
-
let parent = preMap.get(
|
|
682
|
+
const vertexOrKey = vertex[1];
|
|
683
|
+
if (vertexOrKey instanceof AbstractVertex) {
|
|
684
|
+
const path = [vertexOrKey];
|
|
685
|
+
let parent = preMap.get(vertexOrKey);
|
|
686
686
|
while (parent !== undefined) {
|
|
687
687
|
path.push(parent);
|
|
688
688
|
parent = preMap.get(parent);
|
|
@@ -775,7 +775,7 @@ export class AbstractGraph {
|
|
|
775
775
|
return { costs, predecessor };
|
|
776
776
|
}
|
|
777
777
|
/**
|
|
778
|
-
* Tarjan is an algorithm based on
|
|
778
|
+
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
779
779
|
* Tarjan can find cycles in directed or undirected graph
|
|
780
780
|
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
781
781
|
* Tarjan solve the bi-connected components of undirected graphs;
|
|
@@ -783,7 +783,7 @@ export class AbstractGraph {
|
|
|
783
783
|
* /
|
|
784
784
|
|
|
785
785
|
/**
|
|
786
|
-
* Tarjan is an algorithm based on
|
|
786
|
+
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
787
787
|
* Tarjan can find cycles in directed or undirected graph
|
|
788
788
|
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
789
789
|
* Tarjan solve the bi-connected components of undirected graphs;
|
|
@@ -804,8 +804,8 @@ export class AbstractGraph {
|
|
|
804
804
|
* @returns The function `tarjan` returns an object with the following properties:
|
|
805
805
|
*/
|
|
806
806
|
tarjan(needArticulationPoints, needBridges, needSCCs, needCycles) {
|
|
807
|
-
// !! in undirected graph we will not let child visit parent when
|
|
808
|
-
// !! articulation point(in
|
|
807
|
+
// !! in undirected graph we will not let child visit parent when dfs
|
|
808
|
+
// !! articulation point(in dfs search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
|
|
809
809
|
// !! bridge: low(child) > dfn(cur)
|
|
810
810
|
const defaultConfig = false;
|
|
811
811
|
if (needArticulationPoints === undefined)
|
|
@@ -832,7 +832,7 @@ export class AbstractGraph {
|
|
|
832
832
|
dfnMap.set(cur, dfn);
|
|
833
833
|
lowMap.set(cur, dfn);
|
|
834
834
|
const neighbors = this.getNeighbors(cur);
|
|
835
|
-
let childCount = 0; // child in
|
|
835
|
+
let childCount = 0; // child in dfs tree not child in graph
|
|
836
836
|
for (const neighbor of neighbors) {
|
|
837
837
|
if (neighbor !== parent) {
|
|
838
838
|
if (dfnMap.get(neighbor) === -1) {
|
|
@@ -900,17 +900,17 @@ export class AbstractGraph {
|
|
|
900
900
|
_addVertexOnly(newVertex) {
|
|
901
901
|
if (this.hasVertex(newVertex)) {
|
|
902
902
|
return false;
|
|
903
|
-
// throw (new Error('Duplicated vertex
|
|
903
|
+
// throw (new Error('Duplicated vertex key is not allowed'));
|
|
904
904
|
}
|
|
905
|
-
this._vertices.set(newVertex.
|
|
905
|
+
this._vertices.set(newVertex.key, newVertex);
|
|
906
906
|
return true;
|
|
907
907
|
}
|
|
908
|
-
_getVertex(
|
|
909
|
-
const
|
|
910
|
-
return this._vertices.get(
|
|
908
|
+
_getVertex(vertexOrKey) {
|
|
909
|
+
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
910
|
+
return this._vertices.get(vertexKey) || null;
|
|
911
911
|
}
|
|
912
|
-
|
|
913
|
-
return
|
|
912
|
+
_getVertexKey(vertexOrKey) {
|
|
913
|
+
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|
|
914
914
|
}
|
|
915
915
|
_setVertices(value) {
|
|
916
916
|
this._vertices = value;
|
|
@@ -1,35 +1,35 @@
|
|
|
1
1
|
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
|
2
|
-
import type {
|
|
2
|
+
import type { VertexKey } from '../../types';
|
|
3
3
|
import { IDirectedGraph } from '../../interfaces';
|
|
4
4
|
export declare class DirectedVertex<V = any> extends AbstractVertex<V> {
|
|
5
5
|
/**
|
|
6
6
|
* The constructor function initializes a vertex with an optional value.
|
|
7
|
-
* @param {
|
|
7
|
+
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
|
8
8
|
* used to uniquely identify the vertex within a graph or data structure.
|
|
9
9
|
* @param {V} [val] - The "val" parameter is an optional parameter of type V. It is used to initialize the value of the
|
|
10
10
|
* vertex. If no value is provided, the vertex will be initialized with a default value.
|
|
11
11
|
*/
|
|
12
|
-
constructor(
|
|
12
|
+
constructor(key: VertexKey, val?: V);
|
|
13
13
|
}
|
|
14
14
|
export declare class DirectedEdge<V = any> extends AbstractEdge<V> {
|
|
15
15
|
/**
|
|
16
16
|
* The constructor function initializes the source and destination vertices of an edge, along with an optional weight
|
|
17
17
|
* and value.
|
|
18
|
-
* @param {
|
|
18
|
+
* @param {VertexKey} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
|
|
19
19
|
* a graph.
|
|
20
|
-
* @param {
|
|
21
|
-
* `
|
|
20
|
+
* @param {VertexKey} dest - The `dest` parameter represents the destination vertex of an edge. It is of type
|
|
21
|
+
* `VertexKey`, which is likely a unique identifier for a vertex in a graph.
|
|
22
22
|
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
|
23
23
|
* @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value associated with
|
|
24
24
|
* the edge.
|
|
25
25
|
*/
|
|
26
|
-
constructor(src:
|
|
26
|
+
constructor(src: VertexKey, dest: VertexKey, weight?: number, val?: V);
|
|
27
27
|
private _src;
|
|
28
|
-
get src():
|
|
29
|
-
set src(v:
|
|
28
|
+
get src(): VertexKey;
|
|
29
|
+
set src(v: VertexKey);
|
|
30
30
|
private _dest;
|
|
31
|
-
get dest():
|
|
32
|
-
set dest(v:
|
|
31
|
+
get dest(): VertexKey;
|
|
32
|
+
set dest(v: VertexKey);
|
|
33
33
|
}
|
|
34
34
|
export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E extends DirectedEdge<any> = DirectedEdge> extends AbstractGraph<V, E> implements IDirectedGraph<V, E> {
|
|
35
35
|
/**
|
|
@@ -46,45 +46,45 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
|
|
|
46
46
|
*/
|
|
47
47
|
/**
|
|
48
48
|
* The function creates a new vertex with an optional value and returns it.
|
|
49
|
-
* @param {
|
|
49
|
+
* @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is of type `VertexKey`, which
|
|
50
50
|
* could be a number or a string depending on how you want to identify your vertices.
|
|
51
51
|
* @param [val] - The 'val' parameter is an optional value that can be assigned to the vertex. If a value is provided,
|
|
52
52
|
* it will be assigned to the 'val' property of the vertex. If no value is provided, the 'val' property will be
|
|
53
|
-
* assigned the same value as the '
|
|
53
|
+
* assigned the same value as the 'key' parameter
|
|
54
54
|
* @returns a new instance of a DirectedVertex object, casted as type V.
|
|
55
55
|
*/
|
|
56
|
-
createVertex(
|
|
56
|
+
createVertex(key: VertexKey, val?: V['val']): V;
|
|
57
57
|
/**
|
|
58
58
|
* 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.
|
|
59
59
|
* 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.
|
|
60
60
|
*/
|
|
61
61
|
/**
|
|
62
62
|
* The function creates a directed edge between two vertices with an optional weight and value.
|
|
63
|
-
* @param {
|
|
64
|
-
* @param {
|
|
63
|
+
* @param {VertexKey} src - The source vertex ID of the edge. It represents the starting point of the edge.
|
|
64
|
+
* @param {VertexKey} dest - The `dest` parameter is the identifier of the destination vertex for the edge.
|
|
65
65
|
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge. If no
|
|
66
66
|
* weight is provided, it defaults to 1.
|
|
67
67
|
* @param [val] - The 'val' parameter is an optional value that can be assigned to the edge. It can be of any type and
|
|
68
68
|
* is used to store additional information or data associated with the edge.
|
|
69
69
|
* @returns a new instance of a DirectedEdge object, casted as type E.
|
|
70
70
|
*/
|
|
71
|
-
createEdge(src:
|
|
71
|
+
createEdge(src: VertexKey, dest: VertexKey, weight?: number, val?: E['val']): E;
|
|
72
72
|
/**
|
|
73
73
|
* The `getEdge` function retrieves an edge between two vertices based on their source and destination IDs.
|
|
74
|
-
* @param {V | null |
|
|
75
|
-
* @param {V | null |
|
|
76
|
-
* destination vertex of the edge. It can be either a vertex object (`V`), a vertex ID (`
|
|
74
|
+
* @param {V | null | VertexKey} srcOrKey - The source vertex or its ID. It can be either a vertex object or a vertex ID.
|
|
75
|
+
* @param {V | null | VertexKey} destOrKey - The `destOrKey` parameter in the `getEdge` function represents the
|
|
76
|
+
* destination vertex of the edge. It can be either a vertex object (`V`), a vertex ID (`VertexKey`), or `null` if the
|
|
77
77
|
* destination is not specified.
|
|
78
78
|
* @returns the first edge found between the source and destination vertices, or null if no such edge is found.
|
|
79
79
|
*/
|
|
80
|
-
getEdge(
|
|
80
|
+
getEdge(srcOrKey: V | null | VertexKey, destOrKey: V | null | VertexKey): E | null;
|
|
81
81
|
/**
|
|
82
82
|
* The function removes an edge between two vertices in a graph and returns the removed edge.
|
|
83
|
-
* @param {V |
|
|
84
|
-
* @param {V |
|
|
83
|
+
* @param {V | VertexKey} srcOrKey - The source vertex or its ID.
|
|
84
|
+
* @param {V | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
|
|
85
85
|
* @returns the removed edge (E) if it exists, or null if either the source or destination vertex does not exist.
|
|
86
86
|
*/
|
|
87
|
-
removeEdgeSrcToDest(
|
|
87
|
+
removeEdgeSrcToDest(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null;
|
|
88
88
|
/**
|
|
89
89
|
* The function removes an edge from a graph and returns the removed edge, or null if the edge was not found.
|
|
90
90
|
* @param {E} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
|
|
@@ -94,51 +94,51 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
|
|
|
94
94
|
removeEdge(edge: E): E | null;
|
|
95
95
|
/**
|
|
96
96
|
* The function removes edges between two vertices and returns the removed edges.
|
|
97
|
-
* @param {
|
|
97
|
+
* @param {VertexKey | V} v1 - The parameter `v1` can be either a `VertexKey` or a `V`. A `VertexKey` represents the
|
|
98
98
|
* unique identifier of a vertex in a graph, while `V` represents the actual vertex object.
|
|
99
|
-
* @param {
|
|
99
|
+
* @param {VertexKey | V} v2 - The parameter `v2` represents either a `VertexKey` or a `V` object. It is used to specify
|
|
100
100
|
* the second vertex in the edge that needs to be removed.
|
|
101
101
|
* @returns an array of removed edges (E[]).
|
|
102
102
|
*/
|
|
103
|
-
removeEdgesBetween(v1:
|
|
103
|
+
removeEdgesBetween(v1: VertexKey | V, v2: VertexKey | V): E[];
|
|
104
104
|
/**
|
|
105
105
|
* The function `incomingEdgesOf` returns an array of incoming edges for a given vertex or vertex ID.
|
|
106
|
-
* @param {V |
|
|
107
|
-
* (`
|
|
106
|
+
* @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
|
|
107
|
+
* (`VertexKey`).
|
|
108
108
|
* @returns The method `incomingEdgesOf` returns an array of edges (`E[]`).
|
|
109
109
|
*/
|
|
110
|
-
incomingEdgesOf(
|
|
110
|
+
incomingEdgesOf(vertexOrKey: V | VertexKey): E[];
|
|
111
111
|
/**
|
|
112
112
|
* The function `outgoingEdgesOf` returns an array of outgoing edges from a given vertex or vertex ID.
|
|
113
|
-
* @param {V |
|
|
114
|
-
* (`
|
|
113
|
+
* @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can accept either a vertex object (`V`) or a vertex ID
|
|
114
|
+
* (`VertexKey`).
|
|
115
115
|
* @returns The method `outgoingEdgesOf` returns an array of edges (`E[]`).
|
|
116
116
|
*/
|
|
117
|
-
outgoingEdgesOf(
|
|
117
|
+
outgoingEdgesOf(vertexOrKey: V | VertexKey): E[];
|
|
118
118
|
/**
|
|
119
119
|
* The function "degreeOf" returns the total degree of a vertex, which is the sum of its out-degree and in-degree.
|
|
120
|
-
* @param {
|
|
120
|
+
* @param {VertexKey | V} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `V`.
|
|
121
121
|
* @returns The sum of the out-degree and in-degree of the specified vertex or vertex ID.
|
|
122
122
|
*/
|
|
123
|
-
degreeOf(
|
|
123
|
+
degreeOf(vertexOrKey: VertexKey | V): number;
|
|
124
124
|
/**
|
|
125
125
|
* The function "inDegreeOf" returns the number of incoming edges for a given vertex.
|
|
126
|
-
* @param {
|
|
126
|
+
* @param {VertexKey | V} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `V`.
|
|
127
127
|
* @returns The number of incoming edges of the specified vertex or vertex ID.
|
|
128
128
|
*/
|
|
129
|
-
inDegreeOf(
|
|
129
|
+
inDegreeOf(vertexOrKey: VertexKey | V): number;
|
|
130
130
|
/**
|
|
131
131
|
* The function `outDegreeOf` returns the number of outgoing edges from a given vertex.
|
|
132
|
-
* @param {
|
|
132
|
+
* @param {VertexKey | V} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `V`.
|
|
133
133
|
* @returns The number of outgoing edges from the specified vertex or vertex ID.
|
|
134
134
|
*/
|
|
135
|
-
outDegreeOf(
|
|
135
|
+
outDegreeOf(vertexOrKey: VertexKey | V): number;
|
|
136
136
|
/**
|
|
137
137
|
* The function "edgesOf" returns an array of both outgoing and incoming edges of a given vertex or vertex ID.
|
|
138
|
-
* @param {
|
|
138
|
+
* @param {VertexKey | V} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `V`.
|
|
139
139
|
* @returns The function `edgesOf` returns an array of edges.
|
|
140
140
|
*/
|
|
141
|
-
edgesOf(
|
|
141
|
+
edgesOf(vertexOrKey: VertexKey | V): E[];
|
|
142
142
|
/**
|
|
143
143
|
* The function "getEdgeSrc" returns the source vertex of an edge, or null if the edge does not exist.
|
|
144
144
|
* @param {E} e - The parameter "e" is of type E, which represents an edge in a graph.
|
|
@@ -153,20 +153,20 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
|
|
|
153
153
|
getEdgeDest(e: E): V | null;
|
|
154
154
|
/**
|
|
155
155
|
* The function `getDestinations` returns an array of destination vertices connected to a given vertex.
|
|
156
|
-
* @param {V |
|
|
157
|
-
* find the destinations. It can be either a `V` object, a `
|
|
156
|
+
* @param {V | VertexKey | null} vertex - The `vertex` parameter represents the starting vertex from which we want to
|
|
157
|
+
* find the destinations. It can be either a `V` object, a `VertexKey` value, or `null`.
|
|
158
158
|
* @returns an array of vertices (V[]).
|
|
159
159
|
*/
|
|
160
|
-
getDestinations(vertex: V |
|
|
160
|
+
getDestinations(vertex: V | VertexKey | null): V[];
|
|
161
161
|
/**
|
|
162
162
|
* The `topologicalSort` function performs a topological sort on a graph and returns an array of vertices or vertex IDs
|
|
163
163
|
* in the sorted order, or null if the graph contains a cycle.
|
|
164
|
-
* @param {'vertex' | '
|
|
165
|
-
* property to use for sorting the vertices. It can have two possible values: 'vertex' or '
|
|
166
|
-
* specified, the vertices themselves will be used for sorting. If '
|
|
164
|
+
* @param {'vertex' | 'key'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
|
|
165
|
+
* property to use for sorting the vertices. It can have two possible values: 'vertex' or 'key'. If 'vertex' is
|
|
166
|
+
* specified, the vertices themselves will be used for sorting. If 'key' is specified, the ids of
|
|
167
167
|
* @returns an array of vertices or vertex IDs in topological order. If there is a cycle in the graph, it returns null.
|
|
168
168
|
*/
|
|
169
|
-
topologicalSort(propertyName?: 'vertex' | '
|
|
169
|
+
topologicalSort(propertyName?: 'vertex' | 'key'): Array<V | VertexKey> | null;
|
|
170
170
|
/**
|
|
171
171
|
* The `edgeSet` function returns an array of all the edges in the graph.
|
|
172
172
|
* @returns The `edgeSet()` method returns an array of edges (`E[]`).
|
|
@@ -174,11 +174,11 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
|
|
|
174
174
|
edgeSet(): E[];
|
|
175
175
|
/**
|
|
176
176
|
* The function `getNeighbors` returns an array of neighboring vertices of a given vertex or vertex ID in a graph.
|
|
177
|
-
* @param {V |
|
|
178
|
-
* (`
|
|
177
|
+
* @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
|
|
178
|
+
* (`VertexKey`).
|
|
179
179
|
* @returns an array of vertices (V[]).
|
|
180
180
|
*/
|
|
181
|
-
getNeighbors(
|
|
181
|
+
getNeighbors(vertexOrKey: V | VertexKey): V[];
|
|
182
182
|
/**
|
|
183
183
|
* The function "getEndsOfEdge" returns the source and destination vertices of an edge if it exists in the graph,
|
|
184
184
|
* otherwise it returns null.
|