data-structure-typed 1.39.3 → 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/CHANGELOG.md +2 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +4 -2
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +5 -13
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +17 -25
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.js +6 -6
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js +2 -2
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +88 -88
- package/dist/cjs/data-structures/graph/abstract-graph.js +41 -41
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +63 -63
- package/dist/cjs/data-structures/graph/directed-graph.js +36 -36
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +10 -10
- package/dist/cjs/data-structures/graph/map-graph.js +7 -7
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +38 -38
- package/dist/cjs/data-structures/graph/undirected-graph.js +21 -21
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/cjs/data-structures/queue/queue.js +3 -3
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/interfaces/graph.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +4 -2
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +5 -13
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +17 -25
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/bst.js +6 -6
- package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/tree-multiset.js +2 -2
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +88 -88
- package/dist/mjs/data-structures/graph/abstract-graph.js +41 -41
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +63 -63
- package/dist/mjs/data-structures/graph/directed-graph.js +36 -36
- package/dist/mjs/data-structures/graph/map-graph.d.ts +10 -10
- package/dist/mjs/data-structures/graph/map-graph.js +7 -7
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +38 -38
- package/dist/mjs/data-structures/graph/undirected-graph.js +21 -21
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/mjs/data-structures/queue/queue.js +3 -3
- package/dist/mjs/interfaces/graph.d.ts +3 -3
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -5
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-tree.ts +19 -28
- package/src/data-structures/binary-tree/bst.ts +6 -6
- package/src/data-structures/binary-tree/tree-multiset.ts +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
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +0 -1
- package/test/unit/data-structures/graph/directed-graph.test.ts +20 -15
- package/test/unit/data-structures/graph/map-graph.test.ts +23 -23
- package/test/unit/data-structures/graph/undirected-graph.test.ts +2 -2
- package/test/unit/data-structures/queue/queue.test.ts +8 -8
|
@@ -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
|
}
|
|
@@ -92,52 +92,57 @@ describe('DirectedGraph Operation Test', () => {
|
|
|
92
92
|
});
|
|
93
93
|
});
|
|
94
94
|
|
|
95
|
-
class MyVertex<V
|
|
95
|
+
class MyVertex<V = any> extends DirectedVertex<V> {
|
|
96
96
|
constructor(key: VertexKey, val?: V) {
|
|
97
97
|
super(key, val);
|
|
98
98
|
this._data = val;
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
-
private _data:
|
|
101
|
+
private _data: V | undefined;
|
|
102
102
|
|
|
103
|
-
get data():
|
|
103
|
+
get data(): V | undefined {
|
|
104
104
|
return this._data;
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
set data(value:
|
|
107
|
+
set data(value: V | undefined) {
|
|
108
108
|
this._data = value;
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
class MyEdge<E
|
|
112
|
+
class MyEdge<E = any> extends DirectedEdge<E> {
|
|
113
113
|
constructor(v1: VertexKey, v2: VertexKey, weight?: number, val?: E) {
|
|
114
114
|
super(v1, v2, weight, val);
|
|
115
115
|
this._data = val;
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
-
private _data:
|
|
118
|
+
private _data: E | undefined;
|
|
119
119
|
|
|
120
|
-
get data():
|
|
120
|
+
get data(): E | undefined {
|
|
121
121
|
return this._data;
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
-
set data(value:
|
|
124
|
+
set data(value: E | undefined) {
|
|
125
125
|
this._data = value;
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
-
class MyDirectedGraph<
|
|
130
|
-
|
|
131
|
-
|
|
129
|
+
class MyDirectedGraph<
|
|
130
|
+
V = any,
|
|
131
|
+
E = any,
|
|
132
|
+
VO extends MyVertex<V> = MyVertex<V>,
|
|
133
|
+
EO extends MyEdge<E> = MyEdge<E>
|
|
134
|
+
> extends DirectedGraph<V, E, VO, EO> {
|
|
135
|
+
createVertex(key: VertexKey, val: V): VO {
|
|
136
|
+
return new MyVertex(key, val) as VO;
|
|
132
137
|
}
|
|
133
138
|
|
|
134
|
-
createEdge(src: VertexKey, dest: VertexKey, weight?: number, val?: E
|
|
135
|
-
return new MyEdge(src, dest, weight ?? 1, val) as
|
|
139
|
+
createEdge(src: VertexKey, dest: VertexKey, weight?: number, val?: E): EO {
|
|
140
|
+
return new MyEdge(src, dest, weight ?? 1, val) as EO;
|
|
136
141
|
}
|
|
137
142
|
}
|
|
138
143
|
|
|
139
144
|
describe('Inherit from DirectedGraph and perform operations', () => {
|
|
140
|
-
let myGraph = new MyDirectedGraph<
|
|
145
|
+
let myGraph = new MyDirectedGraph<string, string>();
|
|
141
146
|
beforeEach(() => {
|
|
142
147
|
myGraph = new MyDirectedGraph();
|
|
143
148
|
});
|
|
@@ -234,7 +239,7 @@ describe('Inherit from DirectedGraph and perform operations', () => {
|
|
|
234
239
|
});
|
|
235
240
|
|
|
236
241
|
describe('Inherit from DirectedGraph and perform operations test2.', () => {
|
|
237
|
-
const myGraph = new MyDirectedGraph<
|
|
242
|
+
const myGraph = new MyDirectedGraph<string, string>();
|
|
238
243
|
|
|
239
244
|
it('should test graph operations', () => {
|
|
240
245
|
const vertex1 = new MyVertex(1, 'data1');
|
|
@@ -4,17 +4,17 @@ describe('MapGraph Operation Test', () => {
|
|
|
4
4
|
it('dijkstra shortest path', () => {
|
|
5
5
|
const mapGraph = new MapGraph([5.500338, 100.173665]);
|
|
6
6
|
|
|
7
|
-
mapGraph.addVertex(new MapVertex('Surin', 5.466724, 100.274805));
|
|
8
|
-
mapGraph.addVertex(new MapVertex('Batu Feringgi Beach', 5.475141, 100.27667));
|
|
9
|
-
mapGraph.addVertex(new MapVertex('Lotus', 5.459044, 100.308767));
|
|
10
|
-
mapGraph.addVertex(new MapVertex('The Breeza', 5.454197, 100.307859));
|
|
11
|
-
mapGraph.addVertex(new MapVertex('Hard Rock Hotel', 5.46785, 100.241876));
|
|
12
|
-
mapGraph.addVertex(new MapVertex('Mira', 5.456749, 100.28665));
|
|
13
|
-
mapGraph.addVertex(new MapVertex('Penang Bible Church', 5.428683, 100.314825));
|
|
14
|
-
mapGraph.addVertex(new MapVertex('Queensbay', 5.33276, 100.306651));
|
|
15
|
-
mapGraph.addVertex(new MapVertex('Saanen Goat Farm', 5.405738, 100.207699));
|
|
16
|
-
mapGraph.addVertex(new MapVertex('Trinity Auto', 5.401126, 100.303739));
|
|
17
|
-
mapGraph.addVertex(new MapVertex('Penang Airport', 5.293185, 100.265772));
|
|
7
|
+
mapGraph.addVertex(new MapVertex('Surin', '', 5.466724, 100.274805));
|
|
8
|
+
mapGraph.addVertex(new MapVertex('Batu Feringgi Beach', '', 5.475141, 100.27667));
|
|
9
|
+
mapGraph.addVertex(new MapVertex('Lotus', '', 5.459044, 100.308767));
|
|
10
|
+
mapGraph.addVertex(new MapVertex('The Breeza', '', 5.454197, 100.307859));
|
|
11
|
+
mapGraph.addVertex(new MapVertex('Hard Rock Hotel', '', 5.46785, 100.241876));
|
|
12
|
+
mapGraph.addVertex(new MapVertex('Mira', '', 5.456749, 100.28665));
|
|
13
|
+
mapGraph.addVertex(new MapVertex('Penang Bible Church', '', 5.428683, 100.314825));
|
|
14
|
+
mapGraph.addVertex(new MapVertex('Queensbay', '', 5.33276, 100.306651));
|
|
15
|
+
mapGraph.addVertex(new MapVertex('Saanen Goat Farm', '', 5.405738, 100.207699));
|
|
16
|
+
mapGraph.addVertex(new MapVertex('Trinity Auto', '', 5.401126, 100.303739));
|
|
17
|
+
mapGraph.addVertex(new MapVertex('Penang Airport', '', 5.293185, 100.265772));
|
|
18
18
|
mapGraph.addEdge('Surin', 'Lotus', 4.7);
|
|
19
19
|
mapGraph.addEdge('Lotus', 'The Breeza', 1);
|
|
20
20
|
mapGraph.addEdge('Batu Feringgi Beach', 'Hard Rock Hotel', 5.2);
|
|
@@ -45,17 +45,17 @@ describe('MapGraph Operation Test', () => {
|
|
|
45
45
|
});
|
|
46
46
|
|
|
47
47
|
describe('MapGraph', () => {
|
|
48
|
-
let mapGraph: MapGraph
|
|
48
|
+
let mapGraph: MapGraph<string, string>;
|
|
49
49
|
|
|
50
50
|
beforeEach(() => {
|
|
51
51
|
// Create a new MapGraph instance before each test
|
|
52
|
-
mapGraph = new MapGraph([0, 0], [100, 100]);
|
|
52
|
+
mapGraph = new MapGraph<string, string>([0, 0], [100, 100]);
|
|
53
53
|
});
|
|
54
54
|
|
|
55
55
|
// Test adding vertices to the graph
|
|
56
56
|
it('should add vertices to the graph', () => {
|
|
57
|
-
const locationA = new MapVertex('A',
|
|
58
|
-
const locationB = new MapVertex('B',
|
|
57
|
+
const locationA = new MapVertex('A', 'Location A', 10, 20);
|
|
58
|
+
const locationB = new MapVertex('B', 'Location B', 30, 40);
|
|
59
59
|
|
|
60
60
|
mapGraph.addVertex(locationA);
|
|
61
61
|
mapGraph.addVertex(locationB);
|
|
@@ -66,8 +66,8 @@ describe('MapGraph', () => {
|
|
|
66
66
|
|
|
67
67
|
// Test adding edges to the graph
|
|
68
68
|
it('should add edges to the graph', () => {
|
|
69
|
-
const locationA = new MapVertex('A',
|
|
70
|
-
const locationB = new MapVertex('B',
|
|
69
|
+
const locationA = new MapVertex('A', 'Location A', 10, 20);
|
|
70
|
+
const locationB = new MapVertex('B', 'Location B', 30, 40);
|
|
71
71
|
const edgeAB = new MapEdge('A', 'B', 50, 'Edge from A to B');
|
|
72
72
|
|
|
73
73
|
mapGraph.addVertex(locationA);
|
|
@@ -79,12 +79,12 @@ describe('MapGraph', () => {
|
|
|
79
79
|
|
|
80
80
|
// Test getting neighbors of a vertex
|
|
81
81
|
it('should return the neighbors of a vertex', () => {
|
|
82
|
-
const locationA = new MapVertex('A',
|
|
82
|
+
const locationA = new MapVertex('A', 'Location A', 10, 20);
|
|
83
83
|
locationA.lat = locationA.lat;
|
|
84
84
|
locationA.long = locationA.long;
|
|
85
|
-
const locationB = mapGraph.createVertex('B',
|
|
85
|
+
const locationB = mapGraph.createVertex('B', 'Location B', 30, 40);
|
|
86
86
|
|
|
87
|
-
const locationC = new MapVertex('C',
|
|
87
|
+
const locationC = new MapVertex('C', 'Location C', 50, 60);
|
|
88
88
|
const edgeAB = new MapEdge('A', 'B', 50, 'Edge from A to B');
|
|
89
89
|
const edgeBC = new MapEdge('B', 'C', 60, 'Edge from B to C');
|
|
90
90
|
|
|
@@ -106,9 +106,9 @@ describe('MapGraph', () => {
|
|
|
106
106
|
|
|
107
107
|
// Test finding the shortest path between locations
|
|
108
108
|
it('should find the shortest path between two locations', () => {
|
|
109
|
-
const locationA = new MapVertex('A',
|
|
110
|
-
const locationB = new MapVertex('B',
|
|
111
|
-
const locationC = new MapVertex('C',
|
|
109
|
+
const locationA = new MapVertex('A', 'Location A', 10, 20);
|
|
110
|
+
const locationB = new MapVertex('B', 'Location B', 30, 40);
|
|
111
|
+
const locationC = new MapVertex('C', 'Location C', 50, 60);
|
|
112
112
|
const edgeAB = new MapEdge('A', 'B', 50, 'Edge from A to B');
|
|
113
113
|
const edgeBC = new MapEdge('B', 'C', 60, 'Edge from B to C');
|
|
114
114
|
|
|
@@ -59,11 +59,11 @@ describe('UndirectedGraph Operation Test', () => {
|
|
|
59
59
|
});
|
|
60
60
|
|
|
61
61
|
describe('UndirectedGraph', () => {
|
|
62
|
-
let undirectedGraph: UndirectedGraph<
|
|
62
|
+
let undirectedGraph: UndirectedGraph<string, string>;
|
|
63
63
|
|
|
64
64
|
beforeEach(() => {
|
|
65
65
|
// Create a new UndirectedGraph instance before each test
|
|
66
|
-
undirectedGraph = new UndirectedGraph<
|
|
66
|
+
undirectedGraph = new UndirectedGraph<string, string>();
|
|
67
67
|
});
|
|
68
68
|
|
|
69
69
|
// Test adding vertices to the graph
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {SkipQueue, Queue} from '../../../../src';
|
|
2
2
|
import {bigO, magnitude} from '../../../utils';
|
|
3
3
|
import {isDebugTest} from '../../../config';
|
|
4
4
|
|
|
@@ -168,11 +168,11 @@ describe('Queue', () => {
|
|
|
168
168
|
expect(values).toEqual([1, 2, 3]);
|
|
169
169
|
});
|
|
170
170
|
});
|
|
171
|
-
describe('
|
|
172
|
-
let queue:
|
|
171
|
+
describe('SkipQueue', () => {
|
|
172
|
+
let queue: SkipQueue<string>;
|
|
173
173
|
|
|
174
174
|
beforeEach(() => {
|
|
175
|
-
queue = new
|
|
175
|
+
queue = new SkipQueue<string>();
|
|
176
176
|
});
|
|
177
177
|
|
|
178
178
|
it('should enqueue elements to the end of the queue', () => {
|
|
@@ -197,7 +197,7 @@ describe('LinkedListQueue', () => {
|
|
|
197
197
|
expect(queue.peek()).toBe('A');
|
|
198
198
|
});
|
|
199
199
|
|
|
200
|
-
// Add more test cases for other methods of
|
|
200
|
+
// Add more test cases for other methods of SkipQueue.
|
|
201
201
|
});
|
|
202
202
|
|
|
203
203
|
describe('Queue Performance Test', () => {
|
|
@@ -228,16 +228,16 @@ describe('Queue Performance Test', () => {
|
|
|
228
228
|
expect(performance.now() - startTime2).toBeLessThan(bigO.CUBED * 100);
|
|
229
229
|
});
|
|
230
230
|
|
|
231
|
-
it('should numeric
|
|
231
|
+
it('should numeric SkipQueue be efficient', function () {
|
|
232
232
|
const startTime = performance.now();
|
|
233
|
-
const queue = new
|
|
233
|
+
const queue = new SkipQueue<number>();
|
|
234
234
|
for (let i = 0; i < dataSize; i++) {
|
|
235
235
|
queue.enqueue(i);
|
|
236
236
|
}
|
|
237
237
|
for (let i = 0; i < dataSize; i++) {
|
|
238
238
|
queue.dequeue();
|
|
239
239
|
}
|
|
240
|
-
console.log(`
|
|
240
|
+
console.log(`SkipQueue Performance Test: ${performance.now() - startTime} ms`);
|
|
241
241
|
expect(performance.now() - startTime).toBeLessThan(bigO.LINEAR * 100);
|
|
242
242
|
});
|
|
243
243
|
});
|