min-heap-typed 1.39.4 → 1.39.5
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/graph/abstract-graph.d.ts +88 -88
- package/dist/data-structures/graph/abstract-graph.js +41 -41
- package/dist/data-structures/graph/directed-graph.d.ts +63 -63
- package/dist/data-structures/graph/directed-graph.js +36 -36
- package/dist/data-structures/graph/map-graph.d.ts +10 -10
- package/dist/data-structures/graph/map-graph.js +7 -7
- package/dist/data-structures/graph/undirected-graph.d.ts +38 -38
- package/dist/data-structures/graph/undirected-graph.js +21 -21
- package/dist/data-structures/queue/queue.d.ts +1 -1
- package/dist/data-structures/queue/queue.js +3 -3
- package/dist/interfaces/graph.d.ts +3 -3
- package/package.json +2 -2
- package/src/data-structures/graph/abstract-graph.ts +135 -133
- package/src/data-structures/graph/directed-graph.ts +92 -87
- package/src/data-structures/graph/map-graph.ts +17 -20
- package/src/data-structures/graph/undirected-graph.ts +56 -54
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/graph.ts +3 -3
|
@@ -23,7 +23,7 @@ export class UndirectedVertex<V = any> extends AbstractVertex<V> {
|
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
export class UndirectedEdge<
|
|
26
|
+
export class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
27
27
|
/**
|
|
28
28
|
* The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
|
|
29
29
|
* value.
|
|
@@ -31,10 +31,10 @@ export class UndirectedEdge<V = number> extends AbstractEdge<V> {
|
|
|
31
31
|
* @param {VertexKey} v2 - The parameter `v2` is a `VertexKey`, which represents the identifier of the second vertex in a
|
|
32
32
|
* graph edge.
|
|
33
33
|
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
|
34
|
-
* @param {
|
|
34
|
+
* @param {E} [val] - The "val" parameter is an optional parameter of type E. It is used to store a value associated
|
|
35
35
|
* with the edge.
|
|
36
36
|
*/
|
|
37
|
-
constructor(v1: VertexKey, v2: VertexKey, weight?: number, val?:
|
|
37
|
+
constructor(v1: VertexKey, v2: VertexKey, weight?: number, val?: E) {
|
|
38
38
|
super(weight, val);
|
|
39
39
|
this._vertices = [v1, v2];
|
|
40
40
|
}
|
|
@@ -51,23 +51,25 @@ export class UndirectedEdge<V = number> extends AbstractEdge<V> {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
export class UndirectedGraph<
|
|
54
|
-
V
|
|
55
|
-
E
|
|
54
|
+
V = any,
|
|
55
|
+
E = any,
|
|
56
|
+
VO extends UndirectedVertex<V> = UndirectedVertex<V>,
|
|
57
|
+
EO extends UndirectedEdge<E> = UndirectedEdge<E>
|
|
56
58
|
>
|
|
57
|
-
extends AbstractGraph<V, E>
|
|
58
|
-
implements IGraph<V, E>
|
|
59
|
+
extends AbstractGraph<V, E, VO, EO>
|
|
60
|
+
implements IGraph<V, E, VO, EO>
|
|
59
61
|
{
|
|
60
62
|
/**
|
|
61
63
|
* The constructor initializes a new Map object to store edges.
|
|
62
64
|
*/
|
|
63
65
|
constructor() {
|
|
64
66
|
super();
|
|
65
|
-
this._edges = new Map<
|
|
67
|
+
this._edges = new Map<VO, EO[]>();
|
|
66
68
|
}
|
|
67
69
|
|
|
68
|
-
protected _edges: Map<
|
|
70
|
+
protected _edges: Map<VO, EO[]>;
|
|
69
71
|
|
|
70
|
-
get edges(): Map<
|
|
72
|
+
get edges(): Map<VO, EO[]> {
|
|
71
73
|
return this._edges;
|
|
72
74
|
}
|
|
73
75
|
|
|
@@ -78,10 +80,10 @@ export class UndirectedGraph<
|
|
|
78
80
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the vertex. If a value is provided,
|
|
79
81
|
* it will be used as the value of the vertex. If no value is provided, the `key` parameter will be used as the value of
|
|
80
82
|
* the vertex.
|
|
81
|
-
* @returns The method is returning a new instance of the `UndirectedVertex` class, casted as type `
|
|
83
|
+
* @returns The method is returning a new instance of the `UndirectedVertex` class, casted as type `VO`.
|
|
82
84
|
*/
|
|
83
|
-
override createVertex(key: VertexKey, val?:
|
|
84
|
-
return new UndirectedVertex(key, val ?? key) as
|
|
85
|
+
override createVertex(key: VertexKey, val?: VO['val']): VO {
|
|
86
|
+
return new UndirectedVertex(key, val ?? key) as VO;
|
|
85
87
|
}
|
|
86
88
|
|
|
87
89
|
/**
|
|
@@ -92,26 +94,26 @@ export class UndirectedGraph<
|
|
|
92
94
|
* no weight is provided, it defaults to 1.
|
|
93
95
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the edge. It can be of any type and
|
|
94
96
|
* is used to store additional information or data associated with the edge.
|
|
95
|
-
* @returns a new instance of the `UndirectedEdge` class, which is casted as type `
|
|
97
|
+
* @returns a new instance of the `UndirectedEdge` class, which is casted as type `EO`.
|
|
96
98
|
*/
|
|
97
|
-
override createEdge(v1: VertexKey, v2: VertexKey, weight?: number, val?:
|
|
98
|
-
return new UndirectedEdge(v1, v2, weight ?? 1, val) as
|
|
99
|
+
override createEdge(v1: VertexKey, v2: VertexKey, weight?: number, val?: EO['val']): EO {
|
|
100
|
+
return new UndirectedEdge(v1, v2, weight ?? 1, val) as EO;
|
|
99
101
|
}
|
|
100
102
|
|
|
101
103
|
/**
|
|
102
104
|
* The function `getEdge` returns the first edge that connects two vertices, or null if no such edge exists.
|
|
103
|
-
* @param {
|
|
105
|
+
* @param {VO | null | VertexKey} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
104
106
|
* object), `null`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
105
|
-
* @param {
|
|
107
|
+
* @param {VO | null | VertexKey} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
106
108
|
* object), `null`, or `VertexKey` (vertex ID).
|
|
107
|
-
* @returns an edge (
|
|
109
|
+
* @returns an edge (EO) or null.
|
|
108
110
|
*/
|
|
109
|
-
getEdge(v1:
|
|
110
|
-
let edges:
|
|
111
|
+
getEdge(v1: VO | null | VertexKey, v2: VO | null | VertexKey): EO | null {
|
|
112
|
+
let edges: EO[] | undefined = [];
|
|
111
113
|
|
|
112
114
|
if (v1 !== null && v2 !== null) {
|
|
113
|
-
const vertex1:
|
|
114
|
-
const vertex2:
|
|
115
|
+
const vertex1: VO | null = this._getVertex(v1);
|
|
116
|
+
const vertex2: VO | null = this._getVertex(v2);
|
|
115
117
|
|
|
116
118
|
if (vertex1 && vertex2) {
|
|
117
119
|
edges = this._edges.get(vertex1)?.filter(e => e.vertices.includes(vertex2.key));
|
|
@@ -123,48 +125,48 @@ export class UndirectedGraph<
|
|
|
123
125
|
|
|
124
126
|
/**
|
|
125
127
|
* The function removes an edge between two vertices in a graph and returns the removed edge.
|
|
126
|
-
* @param {
|
|
127
|
-
* @param {
|
|
128
|
+
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
129
|
+
* @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
|
|
128
130
|
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
129
|
-
* @returns the removed edge (
|
|
131
|
+
* @returns the removed edge (EO) if it exists, or null if either of the vertices (VO) does not exist.
|
|
130
132
|
*/
|
|
131
|
-
deleteEdgeBetween(v1:
|
|
132
|
-
const vertex1:
|
|
133
|
-
const vertex2:
|
|
133
|
+
deleteEdgeBetween(v1: VO | VertexKey, v2: VO | VertexKey): EO | null {
|
|
134
|
+
const vertex1: VO | null = this._getVertex(v1);
|
|
135
|
+
const vertex2: VO | null = this._getVertex(v2);
|
|
134
136
|
|
|
135
137
|
if (!vertex1 || !vertex2) {
|
|
136
138
|
return null;
|
|
137
139
|
}
|
|
138
140
|
|
|
139
141
|
const v1Edges = this._edges.get(vertex1);
|
|
140
|
-
let removed:
|
|
142
|
+
let removed: EO | null = null;
|
|
141
143
|
if (v1Edges) {
|
|
142
|
-
removed = arrayRemove<
|
|
144
|
+
removed = arrayRemove<EO>(v1Edges, (e: EO) => e.vertices.includes(vertex2.key))[0] || null;
|
|
143
145
|
}
|
|
144
146
|
const v2Edges = this._edges.get(vertex2);
|
|
145
147
|
if (v2Edges) {
|
|
146
|
-
arrayRemove<
|
|
148
|
+
arrayRemove<EO>(v2Edges, (e: EO) => e.vertices.includes(vertex1.key));
|
|
147
149
|
}
|
|
148
150
|
return removed;
|
|
149
151
|
}
|
|
150
152
|
|
|
151
153
|
/**
|
|
152
154
|
* The deleteEdge function removes an edge between two vertices in a graph.
|
|
153
|
-
* @param {
|
|
154
|
-
* @returns The method is returning either the removed edge (of type
|
|
155
|
+
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
156
|
+
* @returns The method is returning either the removed edge (of type EO) or null if the edge was not found.
|
|
155
157
|
*/
|
|
156
|
-
deleteEdge(edge:
|
|
158
|
+
deleteEdge(edge: EO): EO | null {
|
|
157
159
|
return this.deleteEdgeBetween(edge.vertices[0], edge.vertices[1]);
|
|
158
160
|
}
|
|
159
161
|
|
|
160
162
|
/**
|
|
161
163
|
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edges connected to that
|
|
162
164
|
* vertex.
|
|
163
|
-
* @param {VertexKey |
|
|
165
|
+
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
164
166
|
* @returns The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of
|
|
165
167
|
* edges connected to that vertex.
|
|
166
168
|
*/
|
|
167
|
-
degreeOf(vertexOrKey: VertexKey |
|
|
169
|
+
degreeOf(vertexOrKey: VertexKey | VO): number {
|
|
168
170
|
const vertex = this._getVertex(vertexOrKey);
|
|
169
171
|
if (vertex) {
|
|
170
172
|
return this._edges.get(vertex)?.length || 0;
|
|
@@ -175,11 +177,11 @@ export class UndirectedGraph<
|
|
|
175
177
|
|
|
176
178
|
/**
|
|
177
179
|
* The function returns the edges of a given vertex or vertex ID.
|
|
178
|
-
* @param {VertexKey |
|
|
179
|
-
* unique identifier for a vertex in a graph, while `
|
|
180
|
+
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`. A `VertexKey` is a
|
|
181
|
+
* unique identifier for a vertex in a graph, while `VO` represents the type of the vertex.
|
|
180
182
|
* @returns an array of edges.
|
|
181
183
|
*/
|
|
182
|
-
edgesOf(vertexOrKey: VertexKey |
|
|
184
|
+
edgesOf(vertexOrKey: VertexKey | VO): EO[] {
|
|
183
185
|
const vertex = this._getVertex(vertexOrKey);
|
|
184
186
|
if (vertex) {
|
|
185
187
|
return this._edges.get(vertex) || [];
|
|
@@ -190,10 +192,10 @@ export class UndirectedGraph<
|
|
|
190
192
|
|
|
191
193
|
/**
|
|
192
194
|
* The function "edgeSet" returns an array of unique edges from a set of edges.
|
|
193
|
-
* @returns The method `edgeSet()` returns an array of type `
|
|
195
|
+
* @returns The method `edgeSet()` returns an array of type `EO[]`.
|
|
194
196
|
*/
|
|
195
|
-
edgeSet():
|
|
196
|
-
const edgeSet: Set<
|
|
197
|
+
edgeSet(): EO[] {
|
|
198
|
+
const edgeSet: Set<EO> = new Set();
|
|
197
199
|
this._edges.forEach(edges => {
|
|
198
200
|
edges.forEach(edge => {
|
|
199
201
|
edgeSet.add(edge);
|
|
@@ -204,12 +206,12 @@ export class UndirectedGraph<
|
|
|
204
206
|
|
|
205
207
|
/**
|
|
206
208
|
* The function "getNeighbors" returns an array of neighboring vertices for a given vertex or vertex ID.
|
|
207
|
-
* @param {
|
|
209
|
+
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
208
210
|
* (`VertexKey`).
|
|
209
|
-
* @returns an array of vertices (
|
|
211
|
+
* @returns an array of vertices (VO[]).
|
|
210
212
|
*/
|
|
211
|
-
getNeighbors(vertexOrKey:
|
|
212
|
-
const neighbors:
|
|
213
|
+
getNeighbors(vertexOrKey: VO | VertexKey): VO[] {
|
|
214
|
+
const neighbors: VO[] = [];
|
|
213
215
|
const vertex = this._getVertex(vertexOrKey);
|
|
214
216
|
if (vertex) {
|
|
215
217
|
const neighborEdges = this.edgesOf(vertex);
|
|
@@ -226,11 +228,11 @@ export class UndirectedGraph<
|
|
|
226
228
|
/**
|
|
227
229
|
* The function "getEndsOfEdge" returns the vertices at the ends of an edge if the edge exists in the graph, otherwise
|
|
228
230
|
* it returns null.
|
|
229
|
-
* @param {
|
|
230
|
-
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[
|
|
231
|
+
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
232
|
+
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[VO, VO]` if the edge exists in the
|
|
231
233
|
* graph. If the edge does not exist, it returns `null`.
|
|
232
234
|
*/
|
|
233
|
-
getEndsOfEdge(edge:
|
|
235
|
+
getEndsOfEdge(edge: EO): [VO, VO] | null {
|
|
234
236
|
if (!this.hasEdge(edge.vertices[0], edge.vertices[1])) {
|
|
235
237
|
return null;
|
|
236
238
|
}
|
|
@@ -245,10 +247,10 @@ export class UndirectedGraph<
|
|
|
245
247
|
|
|
246
248
|
/**
|
|
247
249
|
* The function adds an edge to the graph by updating the adjacency list with the vertices of the edge.
|
|
248
|
-
* @param {
|
|
250
|
+
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
249
251
|
* @returns a boolean value.
|
|
250
252
|
*/
|
|
251
|
-
protected _addEdgeOnly(edge:
|
|
253
|
+
protected _addEdgeOnly(edge: EO): boolean {
|
|
252
254
|
for (const end of edge.vertices) {
|
|
253
255
|
const endVertex = this._getVertex(end);
|
|
254
256
|
if (endVertex === null) return false;
|
|
@@ -266,9 +268,9 @@ export class UndirectedGraph<
|
|
|
266
268
|
|
|
267
269
|
/**
|
|
268
270
|
* The function sets the edges of a graph.
|
|
269
|
-
* @param v - A map where the keys are of type
|
|
271
|
+
* @param v - A map where the keys are of type VO and the values are arrays of type EO.
|
|
270
272
|
*/
|
|
271
|
-
protected _setEdges(v: Map<
|
|
273
|
+
protected _setEdges(v: Map<VO, EO[]>) {
|
|
272
274
|
this._edges = v;
|
|
273
275
|
}
|
|
274
276
|
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import {SinglyLinkedList} from '../linked-list';
|
|
7
7
|
|
|
8
|
-
export class
|
|
8
|
+
export class SkipQueue<E = any> extends SinglyLinkedList<E> {
|
|
9
9
|
/**
|
|
10
10
|
* The enqueue function adds a value to the end of an array.
|
|
11
11
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
package/src/interfaces/graph.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {VertexKey} from '../types';
|
|
2
2
|
|
|
3
|
-
export interface IGraph<V, E> {
|
|
4
|
-
createVertex(key: VertexKey, val?: V):
|
|
3
|
+
export interface IGraph<V, E, VO, EO> {
|
|
4
|
+
createVertex(key: VertexKey, val?: V): VO;
|
|
5
5
|
|
|
6
|
-
createEdge(srcOrV1: VertexKey | string, destOrV2: VertexKey | string, weight?: number, val?: E):
|
|
6
|
+
createEdge(srcOrV1: VertexKey | string, destOrV2: VertexKey | string, weight?: number, val?: E): EO;
|
|
7
7
|
}
|