data-structure-typed 1.47.7 → 1.47.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/CHANGELOG.md +1 -1
- package/README.md +98 -17
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +22 -17
- package/dist/cjs/data-structures/graph/abstract-graph.js +71 -30
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/cjs/data-structures/graph/directed-graph.js +29 -29
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/cjs/data-structures/graph/undirected-graph.js +18 -18
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +33 -33
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +21 -21
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/cjs/data-structures/queue/queue.js +13 -13
- package/dist/cjs/data-structures/stack/stack.d.ts +6 -6
- package/dist/cjs/data-structures/stack/stack.js +7 -7
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +22 -17
- package/dist/mjs/data-structures/graph/abstract-graph.js +71 -30
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/mjs/data-structures/graph/directed-graph.js +29 -29
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/mjs/data-structures/graph/undirected-graph.js +18 -18
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +33 -33
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +21 -21
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/mjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/mjs/data-structures/queue/queue.js +13 -13
- package/dist/mjs/data-structures/stack/stack.d.ts +6 -6
- package/dist/mjs/data-structures/stack/stack.js +7 -7
- package/dist/mjs/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/umd/data-structure-typed.js +203 -162
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/segment-tree.ts +10 -10
- package/src/data-structures/graph/abstract-graph.ts +92 -46
- package/src/data-structures/graph/directed-graph.ts +41 -41
- package/src/data-structures/graph/undirected-graph.ts +26 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +45 -45
- package/src/data-structures/linked-list/singly-linked-list.ts +38 -38
- package/src/data-structures/linked-list/skip-linked-list.ts +4 -4
- package/src/data-structures/queue/queue.ts +13 -13
- package/src/data-structures/stack/stack.ts +9 -9
- package/src/types/data-structures/graph/abstract-graph.ts +2 -2
- package/test/integration/index.html +102 -33
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +1 -1
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +1 -1
- package/test/unit/data-structures/graph/abstract-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/directed-graph.test.ts +51 -10
- package/test/unit/data-structures/graph/undirected-graph.test.ts +3 -3
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +14 -14
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +3 -3
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +1 -1
- package/test/unit/data-structures/stack/stack.test.ts +2 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.47.
|
|
3
|
+
"version": "1.47.9",
|
|
4
4
|
"description": "Data Structures of Javascript & TypeScript. Heap, Binary Tree, RedBlack Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack.",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/mjs/index.js",
|
|
@@ -11,16 +11,16 @@ import type { SegmentTreeNodeVal } from '../../types';
|
|
|
11
11
|
export class SegmentTreeNode {
|
|
12
12
|
start = 0;
|
|
13
13
|
end = 0;
|
|
14
|
-
value: SegmentTreeNodeVal |
|
|
14
|
+
value: SegmentTreeNodeVal | undefined = undefined;
|
|
15
15
|
sum = 0;
|
|
16
|
-
left: SegmentTreeNode |
|
|
17
|
-
right: SegmentTreeNode |
|
|
16
|
+
left: SegmentTreeNode | undefined = undefined;
|
|
17
|
+
right: SegmentTreeNode | undefined = undefined;
|
|
18
18
|
|
|
19
|
-
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal |
|
|
19
|
+
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | undefined) {
|
|
20
20
|
this.start = start;
|
|
21
21
|
this.end = end;
|
|
22
22
|
this.sum = sum;
|
|
23
|
-
this.value = value ||
|
|
23
|
+
this.value = value || undefined;
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
|
|
@@ -44,7 +44,7 @@ export class SegmentTree {
|
|
|
44
44
|
if (values.length > 0) {
|
|
45
45
|
this._root = this.build(start, end);
|
|
46
46
|
} else {
|
|
47
|
-
this._root =
|
|
47
|
+
this._root = undefined;
|
|
48
48
|
this._values = [];
|
|
49
49
|
}
|
|
50
50
|
}
|
|
@@ -67,9 +67,9 @@ export class SegmentTree {
|
|
|
67
67
|
return this._end;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
protected _root: SegmentTreeNode |
|
|
70
|
+
protected _root: SegmentTreeNode | undefined;
|
|
71
71
|
|
|
72
|
-
get root(): SegmentTreeNode |
|
|
72
|
+
get root(): SegmentTreeNode | undefined {
|
|
73
73
|
return this._root;
|
|
74
74
|
}
|
|
75
75
|
|
|
@@ -109,7 +109,7 @@ export class SegmentTree {
|
|
|
109
109
|
* @returns The function does not return anything.
|
|
110
110
|
*/
|
|
111
111
|
updateNode(index: number, sum: number, value?: SegmentTreeNodeVal) {
|
|
112
|
-
const root = this.root ||
|
|
112
|
+
const root = this.root || undefined;
|
|
113
113
|
if (!root) {
|
|
114
114
|
return;
|
|
115
115
|
}
|
|
@@ -145,7 +145,7 @@ export class SegmentTree {
|
|
|
145
145
|
* @returns The function `querySumByRange` returns a number.
|
|
146
146
|
*/
|
|
147
147
|
querySumByRange(indexA: number, indexB: number): number {
|
|
148
|
-
const root = this.root ||
|
|
148
|
+
const root = this.root || undefined;
|
|
149
149
|
if (!root) {
|
|
150
150
|
return 0;
|
|
151
151
|
}
|
|
@@ -89,9 +89,9 @@ export abstract class AbstractGraph<
|
|
|
89
89
|
*/
|
|
90
90
|
abstract createEdge(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, value?: E): EO;
|
|
91
91
|
|
|
92
|
-
abstract deleteEdge(edge: EO): EO |
|
|
92
|
+
abstract deleteEdge(edge: EO): EO | undefined;
|
|
93
93
|
|
|
94
|
-
abstract getEdge(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO |
|
|
94
|
+
abstract getEdge(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO | undefined;
|
|
95
95
|
|
|
96
96
|
abstract degreeOf(vertexOrKey: VO | VertexKey): number;
|
|
97
97
|
|
|
@@ -101,7 +101,7 @@ export abstract class AbstractGraph<
|
|
|
101
101
|
|
|
102
102
|
abstract getNeighbors(vertexOrKey: VO | VertexKey): VO[];
|
|
103
103
|
|
|
104
|
-
abstract getEndsOfEdge(edge: EO): [VO, VO] |
|
|
104
|
+
abstract getEndsOfEdge(edge: EO): [VO, VO] | undefined;
|
|
105
105
|
|
|
106
106
|
/**
|
|
107
107
|
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
@@ -112,14 +112,14 @@ export abstract class AbstractGraph<
|
|
|
112
112
|
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
113
113
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
114
114
|
*
|
|
115
|
-
* The function "getVertex" returns the vertex with the specified ID or
|
|
115
|
+
* The function "getVertex" returns the vertex with the specified ID or undefined if it doesn't exist.
|
|
116
116
|
* @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
|
|
117
117
|
* the `_vertices` map.
|
|
118
118
|
* @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertices`
|
|
119
|
-
* map. If the vertex does not exist, it returns `
|
|
119
|
+
* map. If the vertex does not exist, it returns `undefined`.
|
|
120
120
|
*/
|
|
121
|
-
getVertex(vertexKey: VertexKey): VO |
|
|
122
|
-
return this._vertices.get(vertexKey) ||
|
|
121
|
+
getVertex(vertexKey: VertexKey): VO | undefined {
|
|
122
|
+
return this._vertices.get(vertexKey) || undefined;
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
/**
|
|
@@ -365,7 +365,7 @@ export abstract class AbstractGraph<
|
|
|
365
365
|
* vertices. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
|
|
366
366
|
* minimum number of
|
|
367
367
|
*/
|
|
368
|
-
getMinCostBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean): number |
|
|
368
|
+
getMinCostBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean): number | undefined {
|
|
369
369
|
if (isWeight === undefined) isWeight = false;
|
|
370
370
|
|
|
371
371
|
if (isWeight) {
|
|
@@ -380,7 +380,7 @@ export abstract class AbstractGraph<
|
|
|
380
380
|
const vertex2 = this._getVertex(v2);
|
|
381
381
|
const vertex1 = this._getVertex(v1);
|
|
382
382
|
if (!(vertex1 && vertex2)) {
|
|
383
|
-
return
|
|
383
|
+
return undefined;
|
|
384
384
|
}
|
|
385
385
|
|
|
386
386
|
const visited: Map<VO, boolean> = new Map();
|
|
@@ -406,7 +406,7 @@ export abstract class AbstractGraph<
|
|
|
406
406
|
}
|
|
407
407
|
cost++;
|
|
408
408
|
}
|
|
409
|
-
return
|
|
409
|
+
return undefined;
|
|
410
410
|
}
|
|
411
411
|
}
|
|
412
412
|
|
|
@@ -432,9 +432,9 @@ export abstract class AbstractGraph<
|
|
|
432
432
|
* followed by iterative computation of the shortest path. This approach may result in exponential time complexity,
|
|
433
433
|
* so the default method is to use the Dijkstra algorithm to obtain the shortest weighted path.
|
|
434
434
|
* @returns The function `getMinPathBetween` returns an array of vertices (`VO[]`) representing the minimum path between
|
|
435
|
-
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `
|
|
435
|
+
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `undefined`.
|
|
436
436
|
*/
|
|
437
|
-
getMinPathBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean, isDFS = false): VO[] |
|
|
437
|
+
getMinPathBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean, isDFS = false): VO[] | undefined {
|
|
438
438
|
if (isWeight === undefined) isWeight = false;
|
|
439
439
|
|
|
440
440
|
if (isWeight) {
|
|
@@ -451,7 +451,7 @@ export abstract class AbstractGraph<
|
|
|
451
451
|
}
|
|
452
452
|
index++;
|
|
453
453
|
}
|
|
454
|
-
return allPaths[minIndex] ||
|
|
454
|
+
return allPaths[minIndex] || undefined;
|
|
455
455
|
} else {
|
|
456
456
|
return this.dijkstra(v1, v2, true, true)?.minPath ?? [];
|
|
457
457
|
}
|
|
@@ -503,9 +503,9 @@ export abstract class AbstractGraph<
|
|
|
503
503
|
* a graph without using a heap data structure.
|
|
504
504
|
* @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
505
505
|
* vertex object or a vertex ID.
|
|
506
|
-
* @param {VO | VertexKey |
|
|
506
|
+
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
507
507
|
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
508
|
-
* identifier. If no destination is provided, the value is set to `
|
|
508
|
+
* identifier. If no destination is provided, the value is set to `undefined`.
|
|
509
509
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
510
510
|
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
511
511
|
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
@@ -516,29 +516,29 @@ export abstract class AbstractGraph<
|
|
|
516
516
|
*/
|
|
517
517
|
dijkstraWithoutHeap(
|
|
518
518
|
src: VO | VertexKey,
|
|
519
|
-
dest?: VO | VertexKey |
|
|
519
|
+
dest?: VO | VertexKey | undefined,
|
|
520
520
|
getMinDist?: boolean,
|
|
521
521
|
genPaths?: boolean
|
|
522
522
|
): DijkstraResult<VO> {
|
|
523
523
|
if (getMinDist === undefined) getMinDist = false;
|
|
524
524
|
if (genPaths === undefined) genPaths = false;
|
|
525
525
|
|
|
526
|
-
if (dest === undefined) dest =
|
|
526
|
+
if (dest === undefined) dest = undefined;
|
|
527
527
|
let minDist = Infinity;
|
|
528
|
-
let minDest: VO |
|
|
528
|
+
let minDest: VO | undefined = undefined;
|
|
529
529
|
let minPath: VO[] = [];
|
|
530
530
|
const paths: VO[][] = [];
|
|
531
531
|
|
|
532
532
|
const vertices = this._vertices;
|
|
533
533
|
const distMap: Map<VO, number> = new Map();
|
|
534
534
|
const seen: Set<VO> = new Set();
|
|
535
|
-
const preMap: Map<VO, VO |
|
|
535
|
+
const preMap: Map<VO, VO | undefined> = new Map(); // predecessor
|
|
536
536
|
const srcVertex = this._getVertex(src);
|
|
537
537
|
|
|
538
|
-
const destVertex = dest ? this._getVertex(dest) :
|
|
538
|
+
const destVertex = dest ? this._getVertex(dest) : undefined;
|
|
539
539
|
|
|
540
540
|
if (!srcVertex) {
|
|
541
|
-
return
|
|
541
|
+
return undefined;
|
|
542
542
|
}
|
|
543
543
|
|
|
544
544
|
for (const vertex of vertices) {
|
|
@@ -546,11 +546,11 @@ export abstract class AbstractGraph<
|
|
|
546
546
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
547
547
|
}
|
|
548
548
|
distMap.set(srcVertex, 0);
|
|
549
|
-
preMap.set(srcVertex,
|
|
549
|
+
preMap.set(srcVertex, undefined);
|
|
550
550
|
|
|
551
551
|
const getMinOfNoSeen = () => {
|
|
552
552
|
let min = Infinity;
|
|
553
|
-
let minV: VO |
|
|
553
|
+
let minV: VO | undefined = undefined;
|
|
554
554
|
for (const [key, value] of distMap) {
|
|
555
555
|
if (!seen.has(key)) {
|
|
556
556
|
if (value < min) {
|
|
@@ -562,7 +562,7 @@ export abstract class AbstractGraph<
|
|
|
562
562
|
return minV;
|
|
563
563
|
};
|
|
564
564
|
|
|
565
|
-
const getPaths = (minV: VO |
|
|
565
|
+
const getPaths = (minV: VO | undefined) => {
|
|
566
566
|
for (const vertex of vertices) {
|
|
567
567
|
const vertexOrKey = vertex[1];
|
|
568
568
|
|
|
@@ -600,7 +600,7 @@ export abstract class AbstractGraph<
|
|
|
600
600
|
if (edge) {
|
|
601
601
|
const curFromMap = distMap.get(cur);
|
|
602
602
|
const neighborFromMap = distMap.get(neighbor);
|
|
603
|
-
// TODO after no-non-
|
|
603
|
+
// TODO after no-non-undefined-assertion not ensure the logic
|
|
604
604
|
if (curFromMap !== undefined && neighborFromMap !== undefined) {
|
|
605
605
|
if (edge.weight + curFromMap < neighborFromMap) {
|
|
606
606
|
distMap.set(neighbor, edge.weight + curFromMap);
|
|
@@ -651,7 +651,7 @@ export abstract class AbstractGraph<
|
|
|
651
651
|
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
652
652
|
* @param {VO | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
653
653
|
* start. It can be either a vertex object or a vertex ID.
|
|
654
|
-
* @param {VO | VertexKey |
|
|
654
|
+
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
655
655
|
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
656
656
|
* will calculate the shortest paths to all other vertices from the source vertex.
|
|
657
657
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
@@ -664,27 +664,27 @@ export abstract class AbstractGraph<
|
|
|
664
664
|
*/
|
|
665
665
|
dijkstra(
|
|
666
666
|
src: VO | VertexKey,
|
|
667
|
-
dest?: VO | VertexKey |
|
|
667
|
+
dest?: VO | VertexKey | undefined,
|
|
668
668
|
getMinDist?: boolean,
|
|
669
669
|
genPaths?: boolean
|
|
670
670
|
): DijkstraResult<VO> {
|
|
671
671
|
if (getMinDist === undefined) getMinDist = false;
|
|
672
672
|
if (genPaths === undefined) genPaths = false;
|
|
673
673
|
|
|
674
|
-
if (dest === undefined) dest =
|
|
674
|
+
if (dest === undefined) dest = undefined;
|
|
675
675
|
let minDist = Infinity;
|
|
676
|
-
let minDest: VO |
|
|
676
|
+
let minDest: VO | undefined = undefined;
|
|
677
677
|
let minPath: VO[] = [];
|
|
678
678
|
const paths: VO[][] = [];
|
|
679
679
|
const vertices = this._vertices;
|
|
680
680
|
const distMap: Map<VO, number> = new Map();
|
|
681
681
|
const seen: Set<VO> = new Set();
|
|
682
|
-
const preMap: Map<VO, VO |
|
|
682
|
+
const preMap: Map<VO, VO | undefined> = new Map(); // predecessor
|
|
683
683
|
|
|
684
684
|
const srcVertex = this._getVertex(src);
|
|
685
|
-
const destVertex = dest ? this._getVertex(dest) :
|
|
685
|
+
const destVertex = dest ? this._getVertex(dest) : undefined;
|
|
686
686
|
|
|
687
|
-
if (!srcVertex) return
|
|
687
|
+
if (!srcVertex) return undefined;
|
|
688
688
|
|
|
689
689
|
for (const vertex of vertices) {
|
|
690
690
|
const vertexOrKey = vertex[1];
|
|
@@ -695,14 +695,14 @@ export abstract class AbstractGraph<
|
|
|
695
695
|
heap.add({ key: 0, value: srcVertex });
|
|
696
696
|
|
|
697
697
|
distMap.set(srcVertex, 0);
|
|
698
|
-
preMap.set(srcVertex,
|
|
698
|
+
preMap.set(srcVertex, undefined);
|
|
699
699
|
|
|
700
700
|
/**
|
|
701
701
|
* The function `getPaths` retrieves all paths from vertices to a specified minimum vertex.
|
|
702
|
-
* @param {VO |
|
|
703
|
-
*
|
|
702
|
+
* @param {VO | undefined} minV - The parameter `minV` is of type `VO | undefined`. It represents the minimum vertex value or
|
|
703
|
+
* undefined.
|
|
704
704
|
*/
|
|
705
|
-
const getPaths = (minV: VO |
|
|
705
|
+
const getPaths = (minV: VO | undefined) => {
|
|
706
706
|
for (const vertex of vertices) {
|
|
707
707
|
const vertexOrKey = vertex[1];
|
|
708
708
|
if (vertexOrKey instanceof AbstractVertex) {
|
|
@@ -841,7 +841,7 @@ export abstract class AbstractGraph<
|
|
|
841
841
|
}
|
|
842
842
|
}
|
|
843
843
|
|
|
844
|
-
let minDest: VO |
|
|
844
|
+
let minDest: VO | undefined = undefined;
|
|
845
845
|
if (getMin) {
|
|
846
846
|
distMap.forEach((d, v) => {
|
|
847
847
|
if (v !== srcVertex) {
|
|
@@ -920,22 +920,22 @@ export abstract class AbstractGraph<
|
|
|
920
920
|
* graph.
|
|
921
921
|
* @returns The function `floydWarshall()` returns an object with two properties: `costs` and `predecessor`. The `costs`
|
|
922
922
|
* property is a 2D array of numbers representing the shortest path costs between vertices in a graph. The
|
|
923
|
-
* `predecessor` property is a 2D array of vertices (or `
|
|
923
|
+
* `predecessor` property is a 2D array of vertices (or `undefined`) representing the predecessor vertices in the shortest
|
|
924
924
|
* path between vertices in the
|
|
925
925
|
*/
|
|
926
|
-
floydWarshall(): { costs: number[][]; predecessor: (VO |
|
|
926
|
+
floydWarshall(): { costs: number[][]; predecessor: (VO | undefined)[][] } {
|
|
927
927
|
const idAndVertices = [...this._vertices];
|
|
928
928
|
const n = idAndVertices.length;
|
|
929
929
|
|
|
930
930
|
const costs: number[][] = [];
|
|
931
|
-
const predecessor: (VO |
|
|
931
|
+
const predecessor: (VO | undefined)[][] = [];
|
|
932
932
|
// successors
|
|
933
933
|
|
|
934
934
|
for (let i = 0; i < n; i++) {
|
|
935
935
|
costs[i] = [];
|
|
936
936
|
predecessor[i] = [];
|
|
937
937
|
for (let j = 0; j < n; j++) {
|
|
938
|
-
predecessor[i][j] =
|
|
938
|
+
predecessor[i][j] = undefined;
|
|
939
939
|
}
|
|
940
940
|
}
|
|
941
941
|
|
|
@@ -1021,7 +1021,7 @@ export abstract class AbstractGraph<
|
|
|
1021
1021
|
const cutVertexes: VO[] = [];
|
|
1022
1022
|
const bridges: EO[] = [];
|
|
1023
1023
|
let dfn = 0;
|
|
1024
|
-
const dfs = (cur: VO, parent: VO |
|
|
1024
|
+
const dfs = (cur: VO, parent: VO | undefined) => {
|
|
1025
1025
|
dfn++;
|
|
1026
1026
|
dfnMap.set(cur, dfn);
|
|
1027
1027
|
lowMap.set(cur, dfn);
|
|
@@ -1036,7 +1036,7 @@ export abstract class AbstractGraph<
|
|
|
1036
1036
|
}
|
|
1037
1037
|
const childLow = lowMap.get(neighbor);
|
|
1038
1038
|
const curLow = lowMap.get(cur);
|
|
1039
|
-
// TODO after no-non-
|
|
1039
|
+
// TODO after no-non-undefined-assertion not ensure the logic
|
|
1040
1040
|
if (curLow !== undefined && childLow !== undefined) {
|
|
1041
1041
|
lowMap.set(cur, Math.min(curLow, childLow));
|
|
1042
1042
|
}
|
|
@@ -1062,7 +1062,7 @@ export abstract class AbstractGraph<
|
|
|
1062
1062
|
}
|
|
1063
1063
|
};
|
|
1064
1064
|
|
|
1065
|
-
dfs(root,
|
|
1065
|
+
dfs(root, undefined);
|
|
1066
1066
|
|
|
1067
1067
|
let SCCs: Map<number, VO[]> = new Map();
|
|
1068
1068
|
|
|
@@ -1159,6 +1159,52 @@ export abstract class AbstractGraph<
|
|
|
1159
1159
|
return this.tarjan(false, true, false, false).bridges;
|
|
1160
1160
|
}
|
|
1161
1161
|
|
|
1162
|
+
* [Symbol.iterator](): Iterator<[VertexKey, V | undefined]> {
|
|
1163
|
+
for (const vertex of this._vertices.values()) {
|
|
1164
|
+
yield [vertex.key, vertex.value];
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
forEach(callback: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => void): void {
|
|
1169
|
+
let index = 0;
|
|
1170
|
+
for (const vertex of this) {
|
|
1171
|
+
callback(vertex, index, this._vertices);
|
|
1172
|
+
index++;
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
filter(predicate: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => boolean): [VertexKey, V | undefined][] {
|
|
1177
|
+
const filtered: [VertexKey, V | undefined][] = [];
|
|
1178
|
+
let index = 0;
|
|
1179
|
+
for (const entry of this) {
|
|
1180
|
+
if (predicate(entry, index, this._vertices)) {
|
|
1181
|
+
filtered.push(entry);
|
|
1182
|
+
}
|
|
1183
|
+
index++;
|
|
1184
|
+
}
|
|
1185
|
+
return filtered;
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
map<T>(callback: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => T): T[] {
|
|
1189
|
+
const mapped: T[] = [];
|
|
1190
|
+
let index = 0;
|
|
1191
|
+
for (const entry of this) {
|
|
1192
|
+
mapped.push(callback(entry, index, this._vertices));
|
|
1193
|
+
index++;
|
|
1194
|
+
}
|
|
1195
|
+
return mapped;
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
reduce<T>(callback: (accumulator: T, entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => T, initialValue: T): T {
|
|
1199
|
+
let accumulator: T = initialValue;
|
|
1200
|
+
let index = 0;
|
|
1201
|
+
for (const entry of this) {
|
|
1202
|
+
accumulator = callback(accumulator, entry, index, this._vertices);
|
|
1203
|
+
index++;
|
|
1204
|
+
}
|
|
1205
|
+
return accumulator;
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1162
1208
|
protected abstract _addEdgeOnly(edge: EO): boolean;
|
|
1163
1209
|
|
|
1164
1210
|
protected _addVertexOnly(newVertex: VO): boolean {
|
|
@@ -1170,9 +1216,9 @@ export abstract class AbstractGraph<
|
|
|
1170
1216
|
return true;
|
|
1171
1217
|
}
|
|
1172
1218
|
|
|
1173
|
-
protected _getVertex(vertexOrKey: VertexKey | VO): VO |
|
|
1219
|
+
protected _getVertex(vertexOrKey: VertexKey | VO): VO | undefined {
|
|
1174
1220
|
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
1175
|
-
return this._vertices.get(vertexKey) ||
|
|
1221
|
+
return this._vertices.get(vertexKey) || undefined;
|
|
1176
1222
|
}
|
|
1177
1223
|
|
|
1178
1224
|
protected _getVertexKey(vertexOrKey: VO | VertexKey): VertexKey {
|
|
@@ -87,7 +87,7 @@ export class DirectedGraph<
|
|
|
87
87
|
* @returns a new instance of a DirectedVertex object, casted as type VO.
|
|
88
88
|
*/
|
|
89
89
|
createVertex(key: VertexKey, value?: V): VO {
|
|
90
|
-
return new DirectedVertex(key, value
|
|
90
|
+
return new DirectedVertex(key, value) as VO;
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
/**
|
|
@@ -119,18 +119,18 @@ export class DirectedGraph<
|
|
|
119
119
|
* Space Complexity: O(1)
|
|
120
120
|
*
|
|
121
121
|
* The `getEdge` function retrieves an edge between two vertices based on their source and destination IDs.
|
|
122
|
-
* @param {VO | VertexKey |
|
|
123
|
-
* @param {VO | VertexKey |
|
|
124
|
-
* destination vertex of the edge. It can be either a vertex object (`VO`), a vertex ID (`VertexKey`), or `
|
|
122
|
+
* @param {VO | VertexKey | undefined} srcOrKey - The source vertex or its ID. It can be either a vertex object or a vertex ID.
|
|
123
|
+
* @param {VO | VertexKey | undefined} destOrKey - The `destOrKey` parameter in the `getEdge` function represents the
|
|
124
|
+
* destination vertex of the edge. It can be either a vertex object (`VO`), a vertex ID (`VertexKey`), or `undefined` if the
|
|
125
125
|
* destination is not specified.
|
|
126
|
-
* @returns the first edge found between the source and destination vertices, or
|
|
126
|
+
* @returns the first edge found between the source and destination vertices, or undefined if no such edge is found.
|
|
127
127
|
*/
|
|
128
|
-
getEdge(srcOrKey: VO | VertexKey |
|
|
128
|
+
getEdge(srcOrKey: VO | VertexKey | undefined, destOrKey: VO | VertexKey | undefined): EO | undefined {
|
|
129
129
|
let edges: EO[] = [];
|
|
130
130
|
|
|
131
|
-
if (srcOrKey !==
|
|
132
|
-
const src: VO |
|
|
133
|
-
const dest: VO |
|
|
131
|
+
if (srcOrKey !== undefined && destOrKey !== undefined) {
|
|
132
|
+
const src: VO | undefined = this._getVertex(srcOrKey);
|
|
133
|
+
const dest: VO | undefined = this._getVertex(destOrKey);
|
|
134
134
|
|
|
135
135
|
if (src && dest) {
|
|
136
136
|
const srcOutEdges = this._outEdgeMap.get(src);
|
|
@@ -140,7 +140,7 @@ export class DirectedGraph<
|
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
-
return edges[0] ||
|
|
143
|
+
return edges[0] || undefined;
|
|
144
144
|
}
|
|
145
145
|
|
|
146
146
|
/**
|
|
@@ -155,14 +155,14 @@ export class DirectedGraph<
|
|
|
155
155
|
* The function removes an edge between two vertices in a graph and returns the removed edge.
|
|
156
156
|
* @param {VO | VertexKey} srcOrKey - The source vertex or its ID.
|
|
157
157
|
* @param {VO | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
|
|
158
|
-
* @returns the removed edge (EO) if it exists, or
|
|
158
|
+
* @returns the removed edge (EO) if it exists, or undefined if either the source or destination vertex does not exist.
|
|
159
159
|
*/
|
|
160
|
-
deleteEdgeSrcToDest(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO |
|
|
161
|
-
const src: VO |
|
|
162
|
-
const dest: VO |
|
|
163
|
-
let removed: EO |
|
|
160
|
+
deleteEdgeSrcToDest(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO | undefined {
|
|
161
|
+
const src: VO | undefined = this._getVertex(srcOrKey);
|
|
162
|
+
const dest: VO | undefined = this._getVertex(destOrKey);
|
|
163
|
+
let removed: EO | undefined = undefined;
|
|
164
164
|
if (!src || !dest) {
|
|
165
|
-
return
|
|
165
|
+
return undefined;
|
|
166
166
|
}
|
|
167
167
|
|
|
168
168
|
const srcOutEdges = this._outEdgeMap.get(src);
|
|
@@ -172,7 +172,7 @@ export class DirectedGraph<
|
|
|
172
172
|
|
|
173
173
|
const destInEdges = this._inEdgeMap.get(dest);
|
|
174
174
|
if (destInEdges) {
|
|
175
|
-
removed = arrayRemove<EO>(destInEdges, (edge: EO) => edge.src === src.key)[0] ||
|
|
175
|
+
removed = arrayRemove<EO>(destInEdges, (edge: EO) => edge.src === src.key)[0] || undefined;
|
|
176
176
|
}
|
|
177
177
|
return removed;
|
|
178
178
|
}
|
|
@@ -186,13 +186,13 @@ export class DirectedGraph<
|
|
|
186
186
|
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
187
187
|
* Space Complexity: O(1)
|
|
188
188
|
*
|
|
189
|
-
* The function removes an edge from a graph and returns the removed edge, or
|
|
189
|
+
* The function removes an edge from a graph and returns the removed edge, or undefined if the edge was not found.
|
|
190
190
|
* @param {EO} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
|
|
191
191
|
* and `dest`, which represent the source and destination vertices of the edge, respectively.
|
|
192
|
-
* @returns The method `deleteEdge` returns the removed edge (`EO`) if it exists, or `
|
|
192
|
+
* @returns The method `deleteEdge` returns the removed edge (`EO`) if it exists, or `undefined` if the edge does not exist.
|
|
193
193
|
*/
|
|
194
|
-
deleteEdge(edge: EO): EO |
|
|
195
|
-
let removed: EO |
|
|
194
|
+
deleteEdge(edge: EO): EO | undefined {
|
|
195
|
+
let removed: EO | undefined = undefined;
|
|
196
196
|
const src = this._getVertex(edge.src);
|
|
197
197
|
const dest = this._getVertex(edge.dest);
|
|
198
198
|
if (src && dest) {
|
|
@@ -361,11 +361,11 @@ export class DirectedGraph<
|
|
|
361
361
|
* Time Complexity: O(1)
|
|
362
362
|
* Space Complexity: O(1)
|
|
363
363
|
*
|
|
364
|
-
* The function "getEdgeSrc" returns the source vertex of an edge, or
|
|
364
|
+
* The function "getEdgeSrc" returns the source vertex of an edge, or undefined if the edge does not exist.
|
|
365
365
|
* @param {EO} e - The parameter "e" is of type EO, which represents an edge in a graph.
|
|
366
|
-
* @returns either a vertex object (VO) or
|
|
366
|
+
* @returns either a vertex object (VO) or undefined.
|
|
367
367
|
*/
|
|
368
|
-
getEdgeSrc(e: EO): VO |
|
|
368
|
+
getEdgeSrc(e: EO): VO | undefined {
|
|
369
369
|
return this._getVertex(e.src);
|
|
370
370
|
}
|
|
371
371
|
|
|
@@ -380,9 +380,9 @@ export class DirectedGraph<
|
|
|
380
380
|
*
|
|
381
381
|
* The function "getEdgeDest" returns the destination vertex of an edge.
|
|
382
382
|
* @param {EO} e - The parameter "e" is of type "EO", which represents an edge in a graph.
|
|
383
|
-
* @returns either a vertex object of type VO or
|
|
383
|
+
* @returns either a vertex object of type VO or undefined.
|
|
384
384
|
*/
|
|
385
|
-
getEdgeDest(e: EO): VO |
|
|
385
|
+
getEdgeDest(e: EO): VO | undefined {
|
|
386
386
|
return this._getVertex(e.dest);
|
|
387
387
|
}
|
|
388
388
|
|
|
@@ -396,12 +396,12 @@ export class DirectedGraph<
|
|
|
396
396
|
* Space Complexity: O(1)
|
|
397
397
|
*
|
|
398
398
|
* The function `getDestinations` returns an array of destination vertices connected to a given vertex.
|
|
399
|
-
* @param {VO | VertexKey |
|
|
400
|
-
* find the destinations. It can be either a `VO` object, a `VertexKey` value, or `
|
|
399
|
+
* @param {VO | VertexKey | undefined} vertex - The `vertex` parameter represents the starting vertex from which we want to
|
|
400
|
+
* find the destinations. It can be either a `VO` object, a `VertexKey` value, or `undefined`.
|
|
401
401
|
* @returns an array of vertices (VO[]).
|
|
402
402
|
*/
|
|
403
|
-
getDestinations(vertex: VO | VertexKey |
|
|
404
|
-
if (vertex ===
|
|
403
|
+
getDestinations(vertex: VO | VertexKey | undefined): VO[] {
|
|
404
|
+
if (vertex === undefined) {
|
|
405
405
|
return [];
|
|
406
406
|
}
|
|
407
407
|
const destinations: VO[] = [];
|
|
@@ -425,13 +425,13 @@ export class DirectedGraph<
|
|
|
425
425
|
* Space Complexity: O(|V|)
|
|
426
426
|
*
|
|
427
427
|
* The `topologicalSort` function performs a topological sort on a graph and returns an array of vertices or vertex IDs
|
|
428
|
-
* in the sorted order, or
|
|
428
|
+
* in the sorted order, or undefined if the graph contains a cycle.
|
|
429
429
|
* @param {'vertex' | 'key'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
|
|
430
430
|
* property to use for sorting the vertices. It can have two possible values: 'vertex' or 'key'. If 'vertex' is
|
|
431
431
|
* specified, the vertices themselves will be used for sorting. If 'key' is specified, the ids of
|
|
432
|
-
* @returns an array of vertices or vertex IDs in topological order. If there is a cycle in the graph, it returns
|
|
432
|
+
* @returns an array of vertices or vertex IDs in topological order. If there is a cycle in the graph, it returns undefined.
|
|
433
433
|
*/
|
|
434
|
-
topologicalSort(propertyName?: 'vertex' | 'key'): Array<VO | VertexKey> |
|
|
434
|
+
topologicalSort(propertyName?: 'vertex' | 'key'): Array<VO | VertexKey> | undefined {
|
|
435
435
|
propertyName = propertyName ?? 'key';
|
|
436
436
|
// When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
|
|
437
437
|
// When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
|
|
@@ -463,7 +463,7 @@ export class DirectedGraph<
|
|
|
463
463
|
}
|
|
464
464
|
}
|
|
465
465
|
|
|
466
|
-
if (hasCycle) return
|
|
466
|
+
if (hasCycle) return undefined;
|
|
467
467
|
|
|
468
468
|
if (propertyName === 'key') sorted = sorted.map(vertex => (vertex instanceof DirectedVertex ? vertex.key : vertex));
|
|
469
469
|
return sorted.reverse();
|
|
@@ -510,7 +510,7 @@ export class DirectedGraph<
|
|
|
510
510
|
const outEdges = this.outgoingEdgesOf(vertex);
|
|
511
511
|
for (const outEdge of outEdges) {
|
|
512
512
|
const neighbor = this._getVertex(outEdge.dest);
|
|
513
|
-
// TODO after no-non-
|
|
513
|
+
// TODO after no-non-undefined-assertion not ensure the logic
|
|
514
514
|
if (neighbor) {
|
|
515
515
|
neighbors.push(neighbor);
|
|
516
516
|
}
|
|
@@ -529,21 +529,21 @@ export class DirectedGraph<
|
|
|
529
529
|
* Space Complexity: O(1)
|
|
530
530
|
*
|
|
531
531
|
* The function "getEndsOfEdge" returns the source and destination vertices of an edge if it exists in the graph,
|
|
532
|
-
* otherwise it returns
|
|
532
|
+
* otherwise it returns undefined.
|
|
533
533
|
* @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph.
|
|
534
534
|
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[VO, VO]` if the edge exists in the
|
|
535
|
-
* graph. If the edge does not exist, it returns `
|
|
535
|
+
* graph. If the edge does not exist, it returns `undefined`.
|
|
536
536
|
*/
|
|
537
|
-
getEndsOfEdge(edge: EO): [VO, VO] |
|
|
537
|
+
getEndsOfEdge(edge: EO): [VO, VO] | undefined {
|
|
538
538
|
if (!this.hasEdge(edge.src, edge.dest)) {
|
|
539
|
-
return
|
|
539
|
+
return undefined;
|
|
540
540
|
}
|
|
541
541
|
const v1 = this._getVertex(edge.src);
|
|
542
542
|
const v2 = this._getVertex(edge.dest);
|
|
543
543
|
if (v1 && v2) {
|
|
544
544
|
return [v1, v2];
|
|
545
545
|
} else {
|
|
546
|
-
return
|
|
546
|
+
return undefined;
|
|
547
547
|
}
|
|
548
548
|
}
|
|
549
549
|
|
|
@@ -570,7 +570,7 @@ export class DirectedGraph<
|
|
|
570
570
|
const srcVertex = this._getVertex(edge.src);
|
|
571
571
|
const destVertex = this._getVertex(edge.dest);
|
|
572
572
|
|
|
573
|
-
// TODO after no-non-
|
|
573
|
+
// TODO after no-non-undefined-assertion not ensure the logic
|
|
574
574
|
if (srcVertex && destVertex) {
|
|
575
575
|
const srcOutEdges = this._outEdgeMap.get(srcVertex);
|
|
576
576
|
if (srcOutEdges) {
|