data-structure-typed 1.41.6 → 1.41.7

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.
Files changed (107) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +11 -11
  3. package/benchmark/report.html +11 -11
  4. package/benchmark/report.json +111 -111
  5. package/dist/cjs/src/data-structures/graph/abstract-graph.js +5 -5
  6. package/dist/cjs/src/data-structures/graph/abstract-graph.js.map +1 -1
  7. package/dist/mjs/src/data-structures/graph/abstract-graph.js +5 -5
  8. package/dist/umd/data-structure-typed.min.js +1 -1
  9. package/dist/umd/data-structure-typed.min.js.map +1 -1
  10. package/package.json +5 -5
  11. package/src/data-structures/graph/abstract-graph.ts +6 -6
  12. package/test/config.ts +1 -0
  13. package/test/integration/avl-tree.test.ts +110 -0
  14. package/test/integration/bst.test.ts +385 -0
  15. package/test/integration/heap.test.js +16 -0
  16. package/test/integration/index.html +51 -0
  17. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +36 -0
  18. package/test/performance/data-structures/binary-tree/binary-index-tree.test.ts +0 -0
  19. package/test/performance/data-structures/binary-tree/binary-tree.test.ts +45 -0
  20. package/test/performance/data-structures/binary-tree/bst.test.ts +36 -0
  21. package/test/performance/data-structures/binary-tree/overall.test.ts +0 -0
  22. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +0 -0
  23. package/test/performance/data-structures/binary-tree/segment-tree.test.ts +0 -0
  24. package/test/performance/data-structures/binary-tree/tree-multiset.test.ts +0 -0
  25. package/test/performance/data-structures/graph/abstract-graph.test.ts +0 -0
  26. package/test/performance/data-structures/graph/directed-graph.test.ts +49 -0
  27. package/test/performance/data-structures/graph/map-graph.test.ts +0 -0
  28. package/test/performance/data-structures/graph/overall.test.ts +0 -0
  29. package/test/performance/data-structures/graph/undirected-graph.test.ts +0 -0
  30. package/test/performance/data-structures/hash/coordinate-map.test.ts +0 -0
  31. package/test/performance/data-structures/hash/coordinate-set.test.ts +0 -0
  32. package/test/performance/data-structures/hash/hash-map.test.ts +0 -0
  33. package/test/performance/data-structures/hash/hash-table.test.ts +0 -0
  34. package/test/performance/data-structures/heap/heap.test.ts +30 -0
  35. package/test/performance/data-structures/heap/max-heap.test.ts +0 -0
  36. package/test/performance/data-structures/heap/min-heap.test.ts +0 -0
  37. package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +40 -0
  38. package/test/performance/data-structures/linked-list/linked-list.test.ts +0 -0
  39. package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +34 -0
  40. package/test/performance/data-structures/linked-list/skip-linked-list.test.ts +0 -0
  41. package/test/performance/data-structures/linked-list/skip-list.test.ts +0 -0
  42. package/test/performance/data-structures/matrix/matrix.test.ts +0 -0
  43. package/test/performance/data-structures/matrix/matrix2d.test.ts +0 -0
  44. package/test/performance/data-structures/matrix/navigator.test.ts +0 -0
  45. package/test/performance/data-structures/matrix/vector2d.test.ts +0 -0
  46. package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +19 -0
  47. package/test/performance/data-structures/priority-queue/min-priority-queue.test.ts +0 -0
  48. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +0 -0
  49. package/test/performance/data-structures/queue/deque.test.ts +21 -0
  50. package/test/performance/data-structures/queue/queue.test.ts +25 -0
  51. package/test/performance/data-structures/stack/stack.test.ts +0 -0
  52. package/test/performance/data-structures/tree/tree.test.ts +0 -0
  53. package/test/performance/data-structures/trie/trie.test.ts +22 -0
  54. package/test/performance/reportor.ts +186 -0
  55. package/test/performance/types/index.ts +1 -0
  56. package/test/performance/types/reportor.ts +3 -0
  57. package/test/types/index.ts +1 -0
  58. package/test/types/utils/big-o.ts +1 -0
  59. package/test/types/utils/index.ts +2 -0
  60. package/test/types/utils/json2html.ts +1 -0
  61. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +269 -0
  62. package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +320 -0
  63. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +486 -0
  64. package/test/unit/data-structures/binary-tree/bst.test.ts +840 -0
  65. package/test/unit/data-structures/binary-tree/overall.test.ts +66 -0
  66. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +435 -0
  67. package/test/unit/data-structures/binary-tree/segment-tree.test.ts +50 -0
  68. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +542 -0
  69. package/test/unit/data-structures/graph/abstract-graph.test.ts +100 -0
  70. package/test/unit/data-structures/graph/directed-graph.test.ts +564 -0
  71. package/test/unit/data-structures/graph/map-graph.test.ts +126 -0
  72. package/test/unit/data-structures/graph/overall.test.ts +49 -0
  73. package/test/unit/data-structures/graph/salty-edges.json +1 -0
  74. package/test/unit/data-structures/graph/salty-vertexes.json +1 -0
  75. package/test/unit/data-structures/graph/undirected-graph.test.ts +168 -0
  76. package/test/unit/data-structures/hash/coordinate-map.test.ts +74 -0
  77. package/test/unit/data-structures/hash/coordinate-set.test.ts +66 -0
  78. package/test/unit/data-structures/hash/hash-map.test.ts +103 -0
  79. package/test/unit/data-structures/hash/hash-table.test.ts +186 -0
  80. package/test/unit/data-structures/heap/heap.test.ts +254 -0
  81. package/test/unit/data-structures/heap/max-heap.test.ts +52 -0
  82. package/test/unit/data-structures/heap/min-heap.test.ts +52 -0
  83. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +400 -0
  84. package/test/unit/data-structures/linked-list/linked-list.test.ts +8 -0
  85. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +474 -0
  86. package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +13 -0
  87. package/test/unit/data-structures/linked-list/skip-list.test.ts +86 -0
  88. package/test/unit/data-structures/matrix/matrix.test.ts +54 -0
  89. package/test/unit/data-structures/matrix/matrix2d.test.ts +345 -0
  90. package/test/unit/data-structures/matrix/navigator.test.ts +244 -0
  91. package/test/unit/data-structures/matrix/vector2d.test.ts +171 -0
  92. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +73 -0
  93. package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +63 -0
  94. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +53 -0
  95. package/test/unit/data-structures/queue/deque.test.ts +410 -0
  96. package/test/unit/data-structures/queue/queue.test.ts +207 -0
  97. package/test/unit/data-structures/stack/stack.test.ts +67 -0
  98. package/test/unit/data-structures/tree/tree.test.ts +39 -0
  99. package/test/unit/data-structures/trie/trie.test.ts +825 -0
  100. package/test/utils/array.ts +5514 -0
  101. package/test/utils/big-o.ts +207 -0
  102. package/test/utils/console.ts +31 -0
  103. package/test/utils/index.ts +7 -0
  104. package/test/utils/is.ts +56 -0
  105. package/test/utils/json2html.ts +322 -0
  106. package/test/utils/number.ts +13 -0
  107. package/test/utils/string.ts +1 -0
@@ -0,0 +1,564 @@
1
+ import {DirectedEdge, DirectedGraph, DirectedVertex, VertexKey} from '../../../../src';
2
+
3
+ describe('DirectedGraph Operation Test', () => {
4
+ let graph: DirectedGraph;
5
+
6
+ beforeEach(() => {
7
+ graph = new DirectedGraph();
8
+ });
9
+
10
+ it('should add vertices', () => {
11
+ const vertex1 = new DirectedVertex('A');
12
+ const vertex2 = new DirectedVertex('B');
13
+
14
+ graph.addVertex(vertex1);
15
+ graph.addVertex(vertex2);
16
+
17
+ expect(graph.hasVertex(vertex1)).toBe(true);
18
+ expect(graph.hasVertex(vertex2)).toBe(true);
19
+ });
20
+
21
+ it('should add edges', () => {
22
+ const vertex1 = new DirectedVertex('A');
23
+ const vertex2 = new DirectedVertex('B');
24
+ const edge = new DirectedEdge('A', 'B');
25
+ edge.src = edge.src;
26
+ edge.dest = edge.dest;
27
+
28
+ graph.addVertex(vertex1);
29
+ graph.addVertex(vertex2);
30
+ graph.addEdge(edge);
31
+
32
+ expect(graph.outEdgeMap.size).toBe(1);
33
+ expect(graph.inEdgeMap.size).toBe(1);
34
+ expect(graph.hasEdge('A', 'B')).toBe(true);
35
+ expect(graph.hasEdge('B', 'A')).toBe(false);
36
+ });
37
+
38
+ it('should delete edges', () => {
39
+ const vertex1 = new DirectedVertex('A');
40
+ // const vertex2 = new DirectedVertex('B');
41
+ graph.createVertex('B');
42
+ const edge = new DirectedEdge('A', 'B');
43
+
44
+ graph.addVertex(vertex1);
45
+ graph.addVertex('B');
46
+ graph.addEdge(edge);
47
+
48
+ expect(graph.deleteEdge(edge)).toBe(edge);
49
+ expect(graph.hasEdge('A', 'B')).toBe(false);
50
+ });
51
+
52
+ it('should perform topological sort', () => {
53
+ const vertexA = new DirectedVertex('A');
54
+ const vertexB = new DirectedVertex('B');
55
+ const vertexC = new DirectedVertex('C');
56
+ const edgeAB = new DirectedEdge('A', 'B');
57
+ graph.createEdge('B', 'C');
58
+
59
+ graph.addVertex(vertexA);
60
+ graph.addVertex(vertexB);
61
+ graph.addVertex(vertexC);
62
+ graph.addEdge(edgeAB);
63
+ graph.addEdge('B', 'C');
64
+
65
+ expect(graph.getEdgeSrc(edgeAB)).toBe(vertexA);
66
+
67
+ const topologicalOrder = graph.topologicalSort();
68
+ if (topologicalOrder) expect(topologicalOrder).toEqual(['A', 'B', 'C']);
69
+
70
+ graph.deleteEdgesBetween('A', 'B');
71
+
72
+ const topologicalOrder1 = graph.topologicalSort();
73
+ if (topologicalOrder1) expect(topologicalOrder1).toEqual(['B', 'C', 'A']);
74
+
75
+ expect(graph.incomingEdgesOf(vertexC)?.length).toBe(1);
76
+ expect(graph.degreeOf(vertexA)).toBe(0);
77
+ expect(graph.inDegreeOf(vertexC)).toBe(1);
78
+ expect(graph.outDegreeOf(vertexC)).toBe(0);
79
+ expect(graph.edgesOf(vertexC)?.length).toBe(1);
80
+
81
+ expect(graph.tarjan(true, true, true, true)?.dfnMap.size).toBe(3);
82
+ expect(graph.bellmanFord(vertexC, true, true, true)?.paths.length).toBe(3);
83
+ expect(graph.getMinPathBetween('B', 'C', true)?.length).toBe(2);
84
+ expect(graph.setEdgeWeight('B', 'C', 100)).toBe(true);
85
+ expect(graph.getMinCostBetween('B', 'C', true)).toBe(100);
86
+ expect(graph.getMinCostBetween('B', 'C')).toBe(1);
87
+ expect(graph.getAllPathsBetween('B', 'C')?.length).toBe(1);
88
+ expect(graph.deleteVertex(vertexB)).toBe(true);
89
+ expect(graph.getAllPathsBetween('B', 'C')?.length).toBe(0);
90
+
91
+ expect(graph.removeManyVertices([vertexB, vertexC])).toBe(true);
92
+ });
93
+ });
94
+
95
+ class MyVertex<V = any> extends DirectedVertex<V> {
96
+ constructor(key: VertexKey, value?: V) {
97
+ super(key, value);
98
+ this._data = value;
99
+ }
100
+
101
+ protected _data: V | undefined;
102
+
103
+ get data(): V | undefined {
104
+ return this._data;
105
+ }
106
+
107
+ set data(value: V | undefined) {
108
+ this._data = value;
109
+ }
110
+ }
111
+
112
+ class MyEdge<E = any> extends DirectedEdge<E> {
113
+ constructor(v1: VertexKey, v2: VertexKey, weight?: number, value?: E) {
114
+ super(v1, v2, weight, value);
115
+ this._data = value;
116
+ }
117
+
118
+ protected _data: E | undefined;
119
+
120
+ get data(): E | undefined {
121
+ return this._data;
122
+ }
123
+
124
+ set data(value: E | undefined) {
125
+ this._data = value;
126
+ }
127
+ }
128
+
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, value: V): VO {
136
+ return new MyVertex(key, value) as VO;
137
+ }
138
+
139
+ createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO {
140
+ return new MyEdge(src, dest, weight ?? 1, value) as EO;
141
+ }
142
+
143
+ setInEdgeMap(value: Map<VO, EO[]>) {
144
+ this._inEdgeMap = value;
145
+ }
146
+
147
+ setOutEdgeMap(value: Map<VO, EO[]>) {
148
+ this._outEdgeMap = value;
149
+ }
150
+ }
151
+
152
+ describe('Inherit from DirectedGraph and perform operations', () => {
153
+ let myGraph = new MyDirectedGraph<string, string>();
154
+ beforeEach(() => {
155
+ myGraph = new MyDirectedGraph();
156
+ });
157
+
158
+ it('Add vertices', () => {
159
+ myGraph.addVertex(1, 'data1');
160
+ myGraph.addVertex(2, 'data2');
161
+ myGraph.addVertex(3, 'data3');
162
+ myGraph.addVertex(4, 'data4');
163
+ myGraph.addVertex(5, 'data5');
164
+ myGraph.addVertex(new MyVertex(6, 'data6'));
165
+ myGraph.addVertex(new MyVertex(7, 'data7'));
166
+ myGraph.addVertex(new MyVertex(8, 'data8'));
167
+ myGraph.addVertex(new MyVertex(9, 'data9'));
168
+ });
169
+
170
+ it('Add edges', () => {
171
+ myGraph.addVertex(1, 'data1');
172
+ myGraph.addVertex(2, 'data2');
173
+ myGraph.addEdge(1, 2, 10, 'edge-data1-2');
174
+ myGraph.addEdge(new MyEdge(2, 1, 20, 'edge-data2-1'));
175
+ myGraph.setInEdgeMap(myGraph.inEdgeMap);
176
+ myGraph.setOutEdgeMap(myGraph.outEdgeMap);
177
+
178
+ expect(myGraph.edgeSet().length).toBe(2);
179
+ // TODO
180
+ expect(myGraph.getEdge(1, 2)).toBeInstanceOf(MyEdge);
181
+ expect(myGraph.getEdge(2, 1)).toBeInstanceOf(MyEdge);
182
+ });
183
+
184
+ it('Get edge', () => {
185
+ myGraph.addVertex(1, 'val1');
186
+ myGraph.addVertex(2, 'val1');
187
+ myGraph.addEdge(1, 2, 1, 'val1');
188
+ const edge1 = myGraph.getEdge(1, 2);
189
+ const edge2 = myGraph.getEdge(myGraph.getVertex(1), myGraph.getVertex(2));
190
+ const edge3 = myGraph.getEdge(1, '100');
191
+ // edge1.data has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class.
192
+ expect(edge1).toBeInstanceOf(MyEdge);
193
+ if (edge1) {
194
+ expect(edge1.data).toBe('val1');
195
+ expect(edge1?.value).toBe('val1');
196
+ expect(edge1).toBeInstanceOf(MyEdge);
197
+ expect(edge1.src).toBe(1);
198
+ expect(edge1).toEqual(edge2);
199
+ expect(edge3).toBeNull();
200
+ }
201
+ });
202
+
203
+ it('Edge set and vertex set', () => {
204
+ expect(true).toBeTruthy();
205
+ });
206
+
207
+ it('Remove edge between vertices', () => {
208
+ myGraph.addVertex(1, 'data1');
209
+ myGraph.addVertex(2, 'data2');
210
+ myGraph.addEdge(1, 2, 10, 'edge-data1-2');
211
+
212
+ const removedEdge = myGraph.deleteEdgeSrcToDest(1, 2);
213
+ const edgeAfterRemoval = myGraph.getEdge(1, 2);
214
+
215
+ expect(removedEdge).toBeInstanceOf(MyEdge);
216
+ if (removedEdge) {
217
+ removedEdge && expect(removedEdge.value).toBe('edge-data1-2');
218
+ removedEdge && expect(removedEdge.src).toBe(1);
219
+ }
220
+ expect(edgeAfterRemoval).toBeNull();
221
+ });
222
+
223
+ it('Topological sort', () => {
224
+ const sorted = myGraph.topologicalSort();
225
+
226
+ expect(sorted).toBeInstanceOf(Array);
227
+ if (sorted && sorted.length > 0) {
228
+ expect(sorted.length).toBe(9);
229
+ if (sorted[0] instanceof MyVertex) expect(sorted[0].data).toBe('data9');
230
+ sorted[3] instanceof MyVertex && expect(sorted[3].data).toBe('data6');
231
+ sorted[8] instanceof MyVertex && expect(sorted[8].key).toBe(1);
232
+ }
233
+ });
234
+
235
+ it('Minimum path between vertices', () => {
236
+ myGraph.addVertex(new MyVertex(1, 'data1'));
237
+ myGraph.addVertex(new MyVertex(2, 'data2'));
238
+ myGraph.addEdge(new MyEdge(1, 2, 10, 'edge-data1-2'));
239
+ });
240
+
241
+ it('All paths between vertices', () => {
242
+ // Add vertices and edges as needed for this test
243
+ myGraph.addVertex(new MyVertex(1, 'data1'));
244
+ myGraph.addVertex(new MyVertex(2, 'data2'));
245
+ myGraph.addEdge(new MyEdge(1, 2, 10, 'edge-data1-2'));
246
+
247
+ // Add expect statements here to verify the allPaths
248
+ });
249
+ });
250
+
251
+ describe('Inherit from DirectedGraph and perform operations test2.', () => {
252
+ const myGraph = new MyDirectedGraph<string, string>();
253
+
254
+ it('should test graph operations', () => {
255
+ const vertex1 = new MyVertex(1, 'data1');
256
+ const vertex2 = new MyVertex(2, 'data2');
257
+ const vertex3 = new MyVertex(3, 'data3');
258
+ const vertex4 = new MyVertex(4, 'data4');
259
+ const vertex5 = new MyVertex(5, 'data5');
260
+ const vertex6 = new MyVertex(6, 'data6');
261
+ const vertex7 = new MyVertex(7, 'data7');
262
+ const vertex8 = new MyVertex(8, 'data8');
263
+ const vertex9 = new MyVertex(9, 'data9');
264
+ myGraph.addVertex(vertex1);
265
+ myGraph.addVertex(vertex2);
266
+ myGraph.addVertex(vertex3);
267
+ myGraph.addVertex(vertex4);
268
+ myGraph.addVertex(vertex5);
269
+ myGraph.addVertex(vertex6);
270
+ myGraph.addVertex(vertex7);
271
+ myGraph.addVertex(vertex8);
272
+ myGraph.addVertex(vertex9);
273
+
274
+ myGraph.addEdge(new MyEdge(1, 2, 10, 'edge-data1-2'));
275
+ myGraph.addEdge(new MyEdge(2, 1, 20, 'edge-data2-1'));
276
+
277
+ expect(myGraph.getEdge(1, 2)).toBeTruthy();
278
+ expect(myGraph.getEdge(2, 1)).toBeTruthy();
279
+ expect(myGraph.getEdge(1, '100')).toBeFalsy();
280
+
281
+ myGraph.deleteEdgeSrcToDest(1, 2);
282
+ expect(myGraph.getEdge(1, 2)).toBeFalsy();
283
+
284
+ myGraph.addEdge(3, 1, 3, 'edge-data-3-1');
285
+
286
+ myGraph.addEdge(1, 9, 19, 'edge-data1-9');
287
+
288
+ myGraph.addEdge(9, 7, 97, 'edge-data9-7');
289
+
290
+ myGraph.addEdge(7, 9, 79, 'edge-data7-9');
291
+
292
+ myGraph.addEdge(1, 4, 14, 'edge-data1-4');
293
+
294
+ myGraph.addEdge(4, 7, 47, 'edge-data4-7');
295
+
296
+ myGraph.addEdge(1, 2, 12, 'edge-data1-2');
297
+
298
+ myGraph.addEdge(2, 3, 23, 'edge-data2-3');
299
+
300
+ myGraph.addEdge(3, 5, 35, 'edge-data3-5');
301
+
302
+ myGraph.addEdge(5, 7, 57, 'edge-data5-7');
303
+
304
+ myGraph.addEdge(new MyEdge(7, 3, 73, 'edge-data7-3'));
305
+ const topologicalSorted = myGraph.topologicalSort();
306
+ expect(topologicalSorted).toBeNull();
307
+
308
+ const minPath1to7 = myGraph.getMinPathBetween(1, 7);
309
+
310
+ expect(minPath1to7).toBeInstanceOf(Array);
311
+ if (minPath1to7 && minPath1to7.length > 0) {
312
+ expect(minPath1to7).toHaveLength(3);
313
+ expect(minPath1to7[0]).toBeInstanceOf(MyVertex);
314
+ expect(minPath1to7[0].key).toBe(1);
315
+ expect(minPath1to7[1].key).toBe(9);
316
+ expect(minPath1to7[2].key).toBe(7);
317
+ }
318
+
319
+ const fordResult1 = myGraph.bellmanFord(1);
320
+ expect(fordResult1).toBeTruthy();
321
+ expect(fordResult1.hasNegativeCycle).toBeUndefined();
322
+ const {distMap, preMap, paths, min, minPath} = fordResult1;
323
+ expect(distMap).toBeInstanceOf(Map);
324
+ expect(distMap.size).toBe(9);
325
+ expect(distMap.get(vertex1)).toBe(0);
326
+ expect(distMap.get(vertex2)).toBe(12);
327
+ expect(distMap.get(vertex3)).toBe(35);
328
+ expect(distMap.get(vertex4)).toBe(14);
329
+ expect(distMap.get(vertex5)).toBe(70);
330
+ expect(distMap.get(vertex6)).toBe(Infinity);
331
+ expect(distMap.get(vertex7)).toBe(61);
332
+ expect(distMap.get(vertex8)).toBe(Infinity);
333
+ expect(distMap.get(vertex9)).toBe(19);
334
+
335
+ expect(preMap).toBeInstanceOf(Map);
336
+ expect(preMap.size).toBe(0);
337
+
338
+ expect(paths).toBeInstanceOf(Array);
339
+ expect(paths.length).toBe(0);
340
+ expect(min).toBe(Infinity);
341
+ expect(minPath).toBeInstanceOf(Array);
342
+
343
+ const floydResult = myGraph.floyd();
344
+ expect(floydResult).toBeTruthy();
345
+ if (floydResult) {
346
+ const {costs, predecessor} = floydResult;
347
+ expect(costs).toBeInstanceOf(Array);
348
+ expect(costs.length).toBe(9);
349
+ expect(costs[0]).toEqual([32, 12, 35, 14, 70, Infinity, 61, Infinity, 19]);
350
+ expect(costs[1]).toEqual([20, 32, 23, 34, 58, Infinity, 81, Infinity, 39]);
351
+ expect(costs[2]).toEqual([3, 15, 38, 17, 35, Infinity, 64, Infinity, 22]);
352
+ expect(costs[3]).toEqual([123, 135, 120, 137, 155, Infinity, 47, Infinity, 126]);
353
+ expect(costs[4]).toEqual([133, 145, 130, 147, 165, Infinity, 57, Infinity, 136]);
354
+ expect(costs[5]).toEqual([
355
+ Infinity,
356
+ Infinity,
357
+ Infinity,
358
+ Infinity,
359
+ Infinity,
360
+ Infinity,
361
+ Infinity,
362
+ Infinity,
363
+ Infinity
364
+ ]);
365
+ expect(costs[6]).toEqual([76, 88, 73, 90, 108, Infinity, 137, Infinity, 79]);
366
+ expect(costs[7]).toEqual([
367
+ Infinity,
368
+ Infinity,
369
+ Infinity,
370
+ Infinity,
371
+ Infinity,
372
+ Infinity,
373
+ Infinity,
374
+ Infinity,
375
+ Infinity
376
+ ]);
377
+ expect(costs[8]).toEqual([173, 185, 170, 187, 205, Infinity, 97, Infinity, 176]);
378
+
379
+ expect(predecessor).toBeInstanceOf(Array);
380
+ expect(predecessor.length).toBe(9);
381
+ expect(predecessor[0]).toEqual([vertex2, null, vertex2, null, vertex3, null, vertex4, null, null]);
382
+ expect(predecessor[1]).toEqual([null, vertex1, null, vertex1, vertex3, null, vertex4, null, vertex1]);
383
+ expect(predecessor[5]).toEqual([null, null, null, null, null, null, null, null, null]);
384
+ expect(predecessor[7]).toEqual([null, null, null, null, null, null, null, null, null]);
385
+ expect(predecessor[8]).toEqual([vertex7, vertex7, vertex7, vertex7, vertex7, null, null, null, vertex7]);
386
+ }
387
+
388
+ const dijkstraRes12tt = myGraph.dijkstra(1, 2, true, true);
389
+
390
+ expect(dijkstraRes12tt).toBeTruthy();
391
+ if (dijkstraRes12tt) {
392
+ const {distMap, minDist, minPath, paths} = dijkstraRes12tt;
393
+ expect(distMap).toBeInstanceOf(Map);
394
+ expect(distMap.size).toBe(9);
395
+ expect(distMap.get(vertex1)).toBe(0);
396
+ expect(distMap.get(vertex2)).toBe(12);
397
+ expect(distMap.get(vertex3)).toBe(Infinity);
398
+ expect(distMap.get(vertex4)).toBe(14);
399
+ expect(distMap.get(vertex5)).toBe(Infinity);
400
+ expect(distMap.get(vertex6)).toBe(Infinity);
401
+ expect(distMap.get(vertex7)).toBe(Infinity);
402
+ expect(distMap.get(vertex8)).toBe(Infinity);
403
+ expect(distMap.get(vertex9)).toBe(19);
404
+
405
+ expect(minDist).toBe(12);
406
+ expect(minPath).toBeInstanceOf(Array);
407
+ expect(minPath.length).toBe(2);
408
+ expect(minPath[0]).toBe(vertex1);
409
+ expect(minPath[1]).toBe(vertex2);
410
+
411
+ expect(paths).toBeInstanceOf(Array);
412
+ expect(paths.length).toBe(9);
413
+ expect(paths[0]).toBeInstanceOf(Array);
414
+ expect(paths[0][0]).toBe(vertex1);
415
+
416
+ expect(paths[1]).toBeInstanceOf(Array);
417
+ expect(paths[1][0]).toBe(vertex1);
418
+ expect(paths[1][1]).toBe(vertex2);
419
+
420
+ expect(paths[2]).toBeInstanceOf(Array);
421
+ expect(paths[2][0]).toBe(vertex3);
422
+ expect(paths[3]).toBeInstanceOf(Array);
423
+ expect(paths[3][0]).toBe(vertex1);
424
+ expect(paths[3][1]).toBe(vertex4);
425
+ expect(paths[4]).toBeInstanceOf(Array);
426
+ expect(paths[4][0]).toBe(vertex5);
427
+
428
+ expect(paths[5]).toBeInstanceOf(Array);
429
+ expect(paths[5][0]).toBe(vertex6);
430
+ expect(paths[6]).toBeInstanceOf(Array);
431
+ expect(paths[6][0]).toBe(vertex7);
432
+ expect(paths[7]).toBeInstanceOf(Array);
433
+ expect(paths[7][0]).toBe(vertex8);
434
+ expect(paths[8]).toBeInstanceOf(Array);
435
+ expect(paths[8][0]).toBe(vertex1);
436
+ expect(paths[8][1]).toBe(vertex9);
437
+ }
438
+
439
+ const dijkstraRes1ntt = myGraph.dijkstra(1, null, true, true);
440
+
441
+ expect(dijkstraRes1ntt).toBeTruthy();
442
+ if (dijkstraRes1ntt) {
443
+ const {distMap, minDist, minPath, paths} = dijkstraRes1ntt;
444
+ expect(distMap).toBeInstanceOf(Map);
445
+ expect(distMap.size).toBe(9);
446
+ expect(distMap.get(vertex1)).toBe(0);
447
+ expect(distMap.get(vertex2)).toBe(12);
448
+ expect(distMap.get(vertex3)).toBe(35);
449
+ expect(distMap.get(vertex4)).toBe(14);
450
+ expect(distMap.get(vertex5)).toBe(70);
451
+ expect(distMap.get(vertex6)).toBe(Infinity);
452
+ expect(distMap.get(vertex7)).toBe(61);
453
+ expect(distMap.get(vertex8)).toBe(Infinity);
454
+ expect(distMap.get(vertex9)).toBe(19);
455
+
456
+ expect(minDist).toBe(12);
457
+ expect(minPath).toBeInstanceOf(Array);
458
+ expect(minPath.length).toBe(2);
459
+ expect(minPath[0]).toBe(vertex1);
460
+ expect(minPath[1]).toBe(vertex2);
461
+
462
+ expect(paths).toBeInstanceOf(Array);
463
+ expect(paths.length).toBe(9);
464
+ expect(paths[0]).toBeInstanceOf(Array);
465
+ expect(paths[0][0]).toBe(vertex1);
466
+
467
+ expect(paths[1]).toBeInstanceOf(Array);
468
+ expect(paths[1][0]).toBe(vertex1);
469
+ expect(paths[1][1]).toBe(vertex2);
470
+
471
+ expect(paths[2]).toBeInstanceOf(Array);
472
+ expect(paths[2][0]).toBe(vertex1);
473
+ expect(paths[2][1]).toBe(vertex2);
474
+ expect(paths[2][2]).toBe(vertex3);
475
+
476
+ expect(paths[3]).toBeInstanceOf(Array);
477
+ expect(paths[3][0]).toBe(vertex1);
478
+ expect(paths[3][1]).toBe(vertex4);
479
+
480
+ expect(paths[4]).toBeInstanceOf(Array);
481
+ expect(paths[4][0]).toBe(vertex1);
482
+ expect(paths[4][1]).toBe(vertex2);
483
+ expect(paths[4][2]).toBe(vertex3);
484
+ expect(paths[4][3]).toBe(vertex5);
485
+
486
+ expect(paths[5]).toBeInstanceOf(Array);
487
+ expect(paths[5][0]).toBe(vertex6);
488
+
489
+ expect(paths[6]).toBeInstanceOf(Array);
490
+ expect(paths[6][0]).toBe(vertex1);
491
+ expect(paths[6][1]).toBe(vertex4);
492
+ expect(paths[6][2]).toBe(vertex7);
493
+
494
+ expect(paths[7]).toBeInstanceOf(Array);
495
+ expect(paths[7][0]).toBe(vertex8);
496
+
497
+ expect(paths[8]).toBeInstanceOf(Array);
498
+ expect(paths[8][0]).toBe(vertex1);
499
+ expect(paths[8][1]).toBe(vertex9);
500
+ }
501
+
502
+ const dijkstraWithoutHeapRes1ntt = myGraph.dijkstraWithoutHeap(1, null, true, true);
503
+ expect(dijkstraWithoutHeapRes1ntt).toBeTruthy();
504
+ if (dijkstraWithoutHeapRes1ntt) {
505
+ const {distMap, minDist, minPath, paths} = dijkstraWithoutHeapRes1ntt;
506
+ expect(distMap).toBeInstanceOf(Map);
507
+ expect(distMap.size).toBe(9);
508
+ expect(distMap.get(vertex1)).toBe(0);
509
+ expect(distMap.get(vertex2)).toBe(12);
510
+ expect(distMap.get(vertex3)).toBe(35);
511
+ expect(distMap.get(vertex4)).toBe(14);
512
+ expect(distMap.get(vertex5)).toBe(70);
513
+ expect(distMap.get(vertex6)).toBe(Infinity);
514
+ expect(distMap.get(vertex7)).toBe(61);
515
+ expect(distMap.get(vertex8)).toBe(Infinity);
516
+ expect(distMap.get(vertex9)).toBe(19);
517
+
518
+ expect(minDist).toBe(12);
519
+ expect(minPath).toBeInstanceOf(Array);
520
+ expect(minPath.length).toBe(2);
521
+ expect(minPath[0]).toBe(vertex1);
522
+ expect(minPath[1]).toBe(vertex2);
523
+
524
+ expect(paths).toBeInstanceOf(Array);
525
+ expect(paths.length).toBe(9);
526
+ expect(paths[0]).toBeInstanceOf(Array);
527
+ expect(paths[0][0]).toBe(vertex1);
528
+
529
+ expect(paths[1]).toBeInstanceOf(Array);
530
+ expect(paths[1][0]).toBe(vertex1);
531
+ expect(paths[1][1]).toBe(vertex2);
532
+
533
+ expect(paths[2]).toBeInstanceOf(Array);
534
+ expect(paths[2][0]).toBe(vertex1);
535
+ expect(paths[2][1]).toBe(vertex2);
536
+ expect(paths[2][2]).toBe(vertex3);
537
+
538
+ expect(paths[3]).toBeInstanceOf(Array);
539
+ expect(paths[3][0]).toBe(vertex1);
540
+ expect(paths[3][1]).toBe(vertex4);
541
+
542
+ expect(paths[4]).toBeInstanceOf(Array);
543
+ expect(paths[4][0]).toBe(vertex1);
544
+ expect(paths[4][1]).toBe(vertex2);
545
+ expect(paths[4][2]).toBe(vertex3);
546
+ expect(paths[4][3]).toBe(vertex5);
547
+
548
+ expect(paths[5]).toBeInstanceOf(Array);
549
+ expect(paths[5][0]).toBe(vertex6);
550
+
551
+ expect(paths[6]).toBeInstanceOf(Array);
552
+ expect(paths[6][0]).toBe(vertex1);
553
+ expect(paths[6][1]).toBe(vertex4);
554
+ expect(paths[6][2]).toBe(vertex7);
555
+
556
+ expect(paths[7]).toBeInstanceOf(Array);
557
+ expect(paths[7][0]).toBe(vertex8);
558
+
559
+ expect(paths[8]).toBeInstanceOf(Array);
560
+ expect(paths[8][0]).toBe(vertex1);
561
+ expect(paths[8][1]).toBe(vertex9);
562
+ }
563
+ });
564
+ });
@@ -0,0 +1,126 @@
1
+ import {MapEdge, MapGraph, MapVertex} from '../../../../src';
2
+
3
+ describe('MapGraph Operation Test', () => {
4
+ it('dijkstra shortest path', () => {
5
+ const mapGraph = new MapGraph([5.500338, 100.173665]);
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));
18
+ mapGraph.addEdge('Surin', 'Lotus', 4.7);
19
+ mapGraph.addEdge('Lotus', 'The Breeza', 1);
20
+ mapGraph.addEdge('Batu Feringgi Beach', 'Hard Rock Hotel', 5.2);
21
+ mapGraph.addEdge('Surin', 'Mira', 2.8);
22
+ mapGraph.addEdge('Mira', 'Penang Bible Church', 7.0);
23
+ mapGraph.addEdge('Lotus', 'Penang Bible Church', 5.7);
24
+ mapGraph.addEdge('Penang Bible Church', 'Queensbay', 13.9);
25
+ mapGraph.addEdge('Hard Rock Hotel', 'Saanen Goat Farm', 18.5);
26
+ mapGraph.addEdge('The Breeza', 'Trinity Auto', 9.1);
27
+ mapGraph.addEdge('Trinity Auto', 'Saanen Goat Farm', 26.3);
28
+ mapGraph.addEdge('The Breeza', 'Penang Airport', 24.8);
29
+ mapGraph.addEdge('Penang Airport', 'Saanen Goat Farm', 21.2);
30
+ const expected1 = ['Surin', 'Lotus', 'The Breeza', 'Trinity Auto', 'Saanen Goat Farm'];
31
+
32
+ const minPathBetween = mapGraph.getMinPathBetween('Surin', 'Saanen Goat Farm');
33
+ expect(minPathBetween?.map(v => v.key)).toEqual(expected1);
34
+ const surinToSaanenGoatFarmDij = mapGraph.dijkstra('Surin', 'Saanen Goat Farm', true, true);
35
+ expect(surinToSaanenGoatFarmDij?.minPath.map(v => v.key)).toEqual(expected1);
36
+ expect(surinToSaanenGoatFarmDij?.minDist).toBe(41.1);
37
+ mapGraph.addEdge('Surin', 'Batu Feringgi Beach', 1.5);
38
+ const expected2 = ['Surin', 'Batu Feringgi Beach', 'Hard Rock Hotel', 'Saanen Goat Farm'];
39
+ const minPathBetweenViaBFB = mapGraph.getMinPathBetween('Surin', 'Saanen Goat Farm', true);
40
+ expect(minPathBetweenViaBFB?.map(v => v.key)).toEqual(expected2);
41
+ const surinToSaanenGoatFarmViaDij = mapGraph.dijkstra('Surin', 'Saanen Goat Farm', true, true);
42
+ expect(surinToSaanenGoatFarmViaDij?.minPath.map(v => v.key)).toEqual(expected2);
43
+ expect(surinToSaanenGoatFarmViaDij?.minDist).toBe(25.2);
44
+ });
45
+ });
46
+
47
+ describe('MapGraph', () => {
48
+ let mapGraph: MapGraph<string, string>;
49
+
50
+ beforeEach(() => {
51
+ // Create a new MapGraph instance before each test
52
+ mapGraph = new MapGraph<string, string>([0, 0], [100, 100]);
53
+ });
54
+
55
+ // Test adding vertices to the graph
56
+ it('should add vertices to the graph', () => {
57
+ const locationA = new MapVertex('A', 'Location A', 10, 20);
58
+ const locationB = new MapVertex('B', 'Location B', 30, 40);
59
+
60
+ mapGraph.addVertex(locationA);
61
+ mapGraph.addVertex(locationB);
62
+
63
+ expect(mapGraph.hasVertex('A')).toBe(true);
64
+ expect(mapGraph.hasVertex('B')).toBe(true);
65
+ });
66
+
67
+ // Test adding edges to the graph
68
+ it('should add edges to the graph', () => {
69
+ const locationA = new MapVertex('A', 'Location A', 10, 20);
70
+ const locationB = new MapVertex('B', 'Location B', 30, 40);
71
+ const edgeAB = new MapEdge('A', 'B', 50, 'Edge from A to B');
72
+
73
+ mapGraph.addVertex(locationA);
74
+ mapGraph.addVertex(locationB);
75
+ mapGraph.addEdge(edgeAB);
76
+
77
+ expect(mapGraph.hasEdge('A', 'B')).toBe(true);
78
+ });
79
+
80
+ // Test getting neighbors of a vertex
81
+ it('should return the neighbors of a vertex', () => {
82
+ const locationA = new MapVertex('A', 'Location A', 10, 20);
83
+ locationA.lat = locationA.lat;
84
+ locationA.long = locationA.long;
85
+ const locationB = mapGraph.createVertex('B', 'Location B', 30, 40);
86
+
87
+ const locationC = new MapVertex('C', 'Location C', 50, 60);
88
+ const edgeAB = new MapEdge('A', 'B', 50, 'Edge from A to B');
89
+ const edgeBC = new MapEdge('B', 'C', 60, 'Edge from B to C');
90
+
91
+ expect(mapGraph.origin).toEqual([0, 0]);
92
+ expect(mapGraph.bottomRight).toEqual([100, 100]);
93
+
94
+ mapGraph.addVertex(locationA);
95
+ mapGraph.addVertex(locationB);
96
+ mapGraph.addVertex(locationC);
97
+ mapGraph.addEdge(edgeAB);
98
+ mapGraph.addEdge(edgeBC);
99
+
100
+ const neighborsOfA = mapGraph.getNeighbors('A');
101
+ const neighborsOfB = mapGraph.getNeighbors('B');
102
+
103
+ expect(neighborsOfA).toEqual([locationB]);
104
+ expect(neighborsOfB).toEqual([locationC]);
105
+ });
106
+
107
+ // Test finding the shortest path between locations
108
+ it('should find the shortest path between two locations', () => {
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
+ const edgeAB = new MapEdge('A', 'B', 50, 'Edge from A to B');
113
+ const edgeBC = new MapEdge('B', 'C', 60, 'Edge from B to C');
114
+
115
+ mapGraph.addVertex(locationA);
116
+ mapGraph.addVertex(locationB);
117
+ mapGraph.addVertex(locationC);
118
+ mapGraph.addEdge(edgeAB);
119
+ mapGraph.addEdge(edgeBC);
120
+
121
+ const shortestPath = mapGraph.dijkstra('A', 'C');
122
+
123
+ expect(shortestPath?.minPath.length).toEqual(0);
124
+ expect(shortestPath?.distMap.size).toBe(3);
125
+ });
126
+ });