data-structure-typed 0.8.18 → 1.12.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/.dependency-cruiser.js +449 -0
- package/.idea/data-structure-typed.iml +2 -0
- package/.idea/modules.xml +1 -1
- package/README.md +298 -2
- package/dist/data-structures/binary-tree/aa-tree.js +5 -2
- package/dist/data-structures/binary-tree/avl-tree.d.ts +58 -5
- package/dist/data-structures/binary-tree/avl-tree.js +150 -46
- package/dist/data-structures/binary-tree/b-tree.js +5 -2
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +28 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +41 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +230 -36
- package/dist/data-structures/binary-tree/binary-tree.js +747 -369
- package/dist/data-structures/binary-tree/bst.d.ts +20 -8
- package/dist/data-structures/binary-tree/bst.js +164 -107
- package/dist/data-structures/binary-tree/rb-tree.js +5 -2
- package/dist/data-structures/binary-tree/segment-tree.d.ts +7 -3
- package/dist/data-structures/binary-tree/segment-tree.js +95 -61
- package/dist/data-structures/binary-tree/splay-tree.js +5 -2
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +5 -5
- package/dist/data-structures/binary-tree/tree-multiset.js +35 -11
- package/dist/data-structures/binary-tree/two-three-tree.js +5 -2
- package/dist/data-structures/graph/abstract-graph.d.ts +168 -46
- package/dist/data-structures/graph/abstract-graph.js +712 -323
- package/dist/data-structures/graph/directed-graph.d.ts +114 -12
- package/dist/data-structures/graph/directed-graph.js +372 -128
- package/dist/data-structures/graph/undirected-graph.d.ts +67 -3
- package/dist/data-structures/graph/undirected-graph.js +233 -81
- package/dist/data-structures/hash/coordinate-map.d.ts +33 -1
- package/dist/data-structures/hash/coordinate-map.js +70 -20
- package/dist/data-structures/hash/coordinate-set.d.ts +25 -0
- package/dist/data-structures/hash/coordinate-set.js +58 -15
- package/dist/data-structures/hash/index.d.ts +5 -0
- package/dist/data-structures/hash/index.js +5 -0
- package/dist/data-structures/heap/heap.d.ts +26 -37
- package/dist/data-structures/heap/heap.js +56 -60
- package/dist/data-structures/heap/max-heap.d.ts +8 -2
- package/dist/data-structures/heap/max-heap.js +32 -9
- package/dist/data-structures/heap/min-heap.d.ts +9 -2
- package/dist/data-structures/heap/min-heap.js +33 -9
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -7
- package/dist/data-structures/linked-list/doubly-linked-list.js +101 -61
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +18 -19
- package/dist/data-structures/linked-list/singly-linked-list.js +312 -174
- package/dist/data-structures/matrix/matrix.d.ts +9 -0
- package/dist/data-structures/matrix/matrix.js +19 -7
- package/dist/data-structures/matrix/matrix2d.d.ts +84 -4
- package/dist/data-structures/matrix/matrix2d.js +158 -61
- package/dist/data-structures/matrix/navigator.d.ts +34 -16
- package/dist/data-structures/matrix/navigator.js +65 -18
- package/dist/data-structures/matrix/vector2d.d.ts +153 -29
- package/dist/data-structures/matrix/vector2d.js +249 -102
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +11 -2
- package/dist/data-structures/priority-queue/max-priority-queue.js +33 -8
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -2
- package/dist/data-structures/priority-queue/min-priority-queue.js +33 -8
- package/dist/data-structures/priority-queue/priority-queue.d.ts +145 -21
- package/dist/data-structures/priority-queue/priority-queue.js +285 -116
- package/dist/data-structures/queue/deque.d.ts +69 -0
- package/dist/data-structures/queue/deque.js +151 -56
- package/dist/data-structures/queue/queue.d.ts +34 -37
- package/dist/data-structures/queue/queue.js +59 -61
- package/dist/data-structures/stack/stack.d.ts +29 -35
- package/dist/data-structures/stack/stack.js +51 -56
- package/dist/data-structures/trie/trie.d.ts +36 -6
- package/dist/data-structures/trie/trie.js +256 -83
- package/dist/data-structures/types/abstract-graph.d.ts +29 -0
- package/dist/data-structures/types/abstract-graph.js +2 -0
- package/dist/data-structures/types/avl-tree.d.ts +5 -0
- package/dist/data-structures/types/avl-tree.js +2 -0
- package/dist/data-structures/types/binary-tree.d.ts +16 -0
- package/dist/data-structures/types/binary-tree.js +2 -0
- package/dist/data-structures/types/bst.d.ts +7 -0
- package/dist/data-structures/types/bst.js +2 -0
- package/dist/data-structures/types/directed-graph.d.ts +10 -0
- package/dist/data-structures/types/directed-graph.js +2 -0
- package/dist/data-structures/types/doubly-linked-list.d.ts +1 -0
- package/dist/data-structures/types/doubly-linked-list.js +2 -0
- package/dist/data-structures/types/heap.d.ts +7 -0
- package/dist/data-structures/types/heap.js +2 -0
- package/dist/data-structures/types/index.d.ts +13 -0
- package/dist/data-structures/types/index.js +29 -0
- package/dist/data-structures/types/navigator.d.ts +14 -0
- package/dist/data-structures/types/navigator.js +2 -0
- package/dist/data-structures/types/priority-queue.d.ts +7 -0
- package/dist/data-structures/types/priority-queue.js +2 -0
- package/dist/data-structures/types/segment-tree.d.ts +1 -0
- package/dist/data-structures/types/segment-tree.js +2 -0
- package/dist/data-structures/types/singly-linked-list.js +2 -0
- package/dist/data-structures/types/tree-multiset.d.ts +5 -0
- package/dist/data-structures/types/tree-multiset.js +2 -0
- package/dist/utils/trampoline.d.ts +14 -0
- package/dist/utils/trampoline.js +130 -0
- package/dist/utils/types/index.js +17 -0
- package/dist/{types → utils}/types/utils.d.ts +15 -1
- package/dist/{types → utils/types}/utils.js +21 -19
- package/dist/{utils.d.ts → utils/utils.d.ts} +5 -22
- package/dist/utils/utils.js +651 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +85 -0
- package/docs/assets/main.js +58 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1367 -0
- package/docs/classes/AVLTree.html +2046 -0
- package/docs/classes/AVLTreeNode.html +423 -0
- package/docs/classes/AaTree.html +117 -0
- package/docs/classes/AbstractEdge.html +198 -0
- package/docs/classes/AbstractGraph.html +891 -0
- package/docs/classes/AbstractVertex.html +164 -0
- package/docs/classes/ArrayDeque.html +384 -0
- package/docs/classes/BST.html +1893 -0
- package/docs/classes/BSTNode.html +425 -0
- package/docs/classes/BTree.html +117 -0
- package/docs/classes/BinaryIndexedTree.html +244 -0
- package/docs/classes/BinaryTree.html +1754 -0
- package/docs/classes/BinaryTreeNode.html +396 -0
- package/docs/classes/Character.html +165 -0
- package/docs/classes/CoordinateMap.html +394 -0
- package/docs/classes/CoordinateSet.html +355 -0
- package/docs/classes/Deque.html +617 -0
- package/docs/classes/DirectedEdge.html +247 -0
- package/docs/classes/DirectedGraph.html +1207 -0
- package/docs/classes/DirectedVertex.html +154 -0
- package/docs/classes/DoublyLinkedList.html +619 -0
- package/docs/classes/DoublyLinkedListNode.html +160 -0
- package/docs/classes/Heap.html +315 -0
- package/docs/classes/Matrix2D.html +447 -0
- package/docs/classes/MatrixNTI2D.html +181 -0
- package/docs/classes/MaxHeap.html +325 -0
- package/docs/classes/MaxPriorityQueue.html +668 -0
- package/docs/classes/MinHeap.html +326 -0
- package/docs/classes/MinPriorityQueue.html +668 -0
- package/docs/classes/Navigator.html +285 -0
- package/docs/classes/ObjectDeque.html +289 -0
- package/docs/classes/PriorityQueue.html +643 -0
- package/docs/classes/Queue.html +337 -0
- package/docs/classes/RBTree.html +117 -0
- package/docs/classes/SegmentTree.html +234 -0
- package/docs/classes/SegmentTreeNode.html +302 -0
- package/docs/classes/SinglyLinkedList.html +1035 -0
- package/docs/classes/SinglyLinkedListNode.html +304 -0
- package/docs/classes/SplayTree.html +117 -0
- package/docs/classes/Stack.html +313 -0
- package/docs/classes/TreeMultiSet.html +1897 -0
- package/docs/classes/Trie.html +317 -0
- package/docs/classes/TrieNode.html +221 -0
- package/docs/classes/TwoThreeTree.html +117 -0
- package/docs/classes/UndirectedEdge.html +220 -0
- package/docs/classes/UndirectedGraph.html +1006 -0
- package/docs/classes/UndirectedVertex.html +154 -0
- package/docs/classes/Vector2D.html +746 -0
- package/docs/enums/CP.html +126 -0
- package/docs/enums/FamilyPosition.html +126 -0
- package/docs/enums/LoopType.html +119 -0
- package/docs/index.html +288 -0
- package/docs/modules.html +146 -0
- package/jest.config.js +5 -0
- package/package.json +33 -47
- package/rename_clear_files.sh +29 -0
- package/src/assets/complexities-diff.jpg +0 -0
- package/src/assets/data-structure-complexities.jpg +0 -0
- package/src/data-structures/binary-tree/avl-tree.ts +58 -6
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +31 -4
- package/src/data-structures/binary-tree/binary-tree.ts +460 -145
- package/src/data-structures/binary-tree/bst.ts +31 -25
- package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- package/src/data-structures/binary-tree/segment-tree.ts +25 -12
- package/src/data-structures/binary-tree/tree-multiset.ts +5 -4
- package/src/data-structures/diagrams/README.md +5 -0
- package/src/data-structures/graph/abstract-graph.ts +224 -108
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/src/data-structures/graph/diagrams/mst.jpg +0 -0
- package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
- package/src/data-structures/graph/directed-graph.ts +132 -26
- package/src/data-structures/graph/undirected-graph.ts +78 -11
- package/src/data-structures/hash/coordinate-map.ts +33 -1
- package/src/data-structures/hash/coordinate-set.ts +25 -0
- package/src/data-structures/hash/index.ts +5 -0
- package/src/data-structures/heap/heap.ts +27 -41
- package/src/data-structures/heap/max-heap.ts +8 -2
- package/src/data-structures/heap/min-heap.ts +9 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +50 -17
- package/src/data-structures/linked-list/singly-linked-list.ts +56 -39
- package/src/data-structures/matrix/matrix.ts +11 -0
- package/src/data-structures/matrix/matrix2d.ts +90 -10
- package/src/data-structures/matrix/navigator.ts +34 -14
- package/src/data-structures/matrix/vector2d.ts +187 -63
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -3
- package/src/data-structures/priority-queue/priority-queue.ts +200 -78
- package/src/data-structures/queue/deque.ts +71 -2
- package/src/data-structures/queue/queue.ts +37 -40
- package/src/data-structures/stack/stack.ts +32 -38
- package/src/data-structures/trie/trie.ts +83 -14
- package/src/data-structures/types/abstract-graph.ts +51 -0
- package/src/data-structures/types/avl-tree.ts +6 -0
- package/src/data-structures/types/binary-tree.ts +15 -0
- package/src/data-structures/types/bst.ts +5 -0
- package/src/data-structures/types/directed-graph.ts +18 -0
- package/src/data-structures/types/doubly-linked-list.ts +1 -0
- package/src/data-structures/types/heap.ts +8 -0
- package/src/data-structures/types/index.ts +13 -0
- package/src/data-structures/types/navigator.ts +13 -0
- package/src/data-structures/types/priority-queue.ts +9 -0
- package/src/data-structures/types/segment-tree.ts +1 -0
- package/src/data-structures/types/singly-linked-list.ts +1 -0
- package/src/data-structures/types/tree-multiset.ts +3 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/trampoline.ts +51 -0
- package/src/utils/types/index.ts +1 -0
- package/src/{types → utils/types}/utils.ts +27 -5
- package/src/{utils.ts → utils/utils.ts} +41 -131
- package/tests/unit/data-structures/binary-tree/bst.test.ts +185 -0
- package/tests/unit/data-structures/graph/directed-graph.test.ts +71 -0
- package/{dist/types/data-structures/graph/index.d.ts → tests/unit/data-structures/graph/index.ts} +1 -1
- package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
- package/tsconfig.json +9 -6
- package/dist/data-structures/trampoline.d.ts +0 -25
- package/dist/data-structures/trampoline.js +0 -52
- package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
- package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
- package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
- package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
- package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
- package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
- package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
- package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
- package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
- package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/hash/pair.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-map.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-set.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +0 -72
- package/dist/types/data-structures/heap/index.d.ts +0 -3
- package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
- package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
- package/dist/types/data-structures/index.d.ts +0 -9
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
- package/dist/types/data-structures/linked-list/index.d.ts +0 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +0 -1
- package/dist/types/data-structures/matrix/index.d.ts +0 -3
- package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
- package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
- package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
- package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
- package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
- package/dist/types/data-structures/queue/deque.d.ts +0 -37
- package/dist/types/data-structures/queue/index.d.ts +0 -1
- package/dist/types/data-structures/queue/queue.d.ts +0 -76
- package/dist/types/data-structures/stack/index.d.ts +0 -1
- package/dist/types/data-structures/stack/stack.d.ts +0 -69
- package/dist/types/data-structures/trampoline.d.ts +0 -25
- package/dist/types/data-structures/trie/index.d.ts +0 -1
- package/dist/types/data-structures/trie/trie.d.ts +0 -28
- package/dist/types/utils.d.ts +0 -46
- package/dist/utils.js +0 -569
- package/src/data-structures/trampoline.ts +0 -91
- package/src/types/index.ts +0 -1
- /package/dist/{types/data-structures/hash/hash-table.d.ts → data-structures/types/singly-linked-list.d.ts} +0 -0
- /package/dist/{types → utils}/index.d.ts +0 -0
- /package/dist/{types → utils}/index.js +0 -0
- /package/dist/{types → utils}/types/index.d.ts +0 -0
- /package/{src/types/patches/index.d.ts → tests/unit/data-structures/graph/abstract-graph.ts} +0 -0
|
@@ -1,60 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
1
5
|
import {arrayRemove, uuidV4} from '../../utils';
|
|
2
6
|
import {PriorityQueue} from '../priority-queue';
|
|
7
|
+
import type {DijkstraResult, IGraph, VertexId} from '../types';
|
|
3
8
|
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export interface I_Graph<V, E> {
|
|
10
|
-
|
|
11
|
-
containsVertex(vertexOrId: V | VertexId): boolean;
|
|
12
|
-
|
|
13
|
-
getVertex(vertexOrId: VertexId | V): V | null;
|
|
14
|
-
|
|
15
|
-
getVertexId(vertexOrId: V | VertexId): VertexId;
|
|
16
|
-
|
|
17
|
-
vertexSet(): Map<VertexId, V>;
|
|
18
|
-
|
|
19
|
-
addVertex(v: V): boolean;
|
|
20
|
-
|
|
21
|
-
removeVertex(vertexOrId: V | VertexId): boolean;
|
|
22
|
-
|
|
23
|
-
removeAllVertices(vertices: V[] | VertexId[]): boolean;
|
|
24
|
-
|
|
25
|
-
degreeOf(vertexOrId: V | VertexId): number;
|
|
26
|
-
|
|
27
|
-
edgesOf(vertexOrId: V | VertexId): E[];
|
|
28
|
-
|
|
29
|
-
containsEdge(src: V | VertexId, dest: V | VertexId): boolean;
|
|
30
|
-
|
|
31
|
-
// containsEdge(e: E): boolean;
|
|
32
|
-
|
|
33
|
-
getEdge(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
|
|
34
|
-
|
|
35
|
-
// getAllEdges(src: V, dest: V): E[];
|
|
36
|
-
|
|
37
|
-
edgeSet(): E[];
|
|
38
|
-
|
|
39
|
-
addEdge(edge: E): boolean;
|
|
40
|
-
|
|
41
|
-
removeEdgeBetween(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
|
|
42
|
-
|
|
43
|
-
removeEdge(edge: E): E | null;
|
|
44
|
-
|
|
45
|
-
// removeAllEdges(v1: VertexId | V, v2: VertexId | V): (E | null)[];
|
|
46
|
-
|
|
47
|
-
// removeAllEdges(edges: E[] | [VertexId, VertexId]): boolean;
|
|
48
|
-
|
|
49
|
-
setEdgeWeight(srcOrId: V | VertexId, destOrId: V | VertexId, weight: number): boolean;
|
|
50
|
-
|
|
51
|
-
getMinPathBetween(v1: V | VertexId, v2: V | VertexId, isWeight?: boolean): V[] | null;
|
|
9
|
+
export class AbstractVertex {
|
|
10
|
+
constructor(id: VertexId) {
|
|
11
|
+
this._id = id;
|
|
12
|
+
}
|
|
52
13
|
|
|
53
|
-
|
|
54
|
-
}
|
|
14
|
+
protected _id: VertexId;
|
|
55
15
|
|
|
56
|
-
export class AbstractVertex {
|
|
57
|
-
private _id: VertexId;
|
|
58
16
|
public get id(): VertexId {
|
|
59
17
|
return this._id;
|
|
60
18
|
}
|
|
@@ -62,15 +20,20 @@ export class AbstractVertex {
|
|
|
62
20
|
public set id(v: VertexId) {
|
|
63
21
|
this._id = v;
|
|
64
22
|
}
|
|
65
|
-
|
|
66
|
-
constructor(id: VertexId) {
|
|
67
|
-
this._id = id;
|
|
68
|
-
}
|
|
69
23
|
}
|
|
70
24
|
|
|
71
25
|
export abstract class AbstractEdge {
|
|
72
26
|
|
|
27
|
+
static DEFAULT_EDGE_WEIGHT = 1;
|
|
28
|
+
|
|
29
|
+
protected constructor(weight?: number) {
|
|
30
|
+
if (weight === undefined) weight = AbstractEdge.DEFAULT_EDGE_WEIGHT;
|
|
31
|
+
this._weight = weight;
|
|
32
|
+
this._hashCode = uuidV4();
|
|
33
|
+
}
|
|
34
|
+
|
|
73
35
|
private _weight: number;
|
|
36
|
+
|
|
74
37
|
get weight(): number {
|
|
75
38
|
return this._weight;
|
|
76
39
|
}
|
|
@@ -88,18 +51,10 @@ export abstract class AbstractEdge {
|
|
|
88
51
|
set hashCode(v: string) {
|
|
89
52
|
this._hashCode = v;
|
|
90
53
|
}
|
|
91
|
-
|
|
92
|
-
protected constructor(weight?: number) {
|
|
93
|
-
if (weight === undefined) weight = AbstractEdge.DEFAULT_EDGE_WEIGHT;
|
|
94
|
-
this._weight = weight;
|
|
95
|
-
this._hashCode = uuidV4();
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
static DEFAULT_EDGE_WEIGHT = 1;
|
|
99
54
|
}
|
|
100
55
|
|
|
101
56
|
// Connected Component === Largest Connected Sub-Graph
|
|
102
|
-
export abstract class AbstractGraph<V extends AbstractVertex, E extends AbstractEdge> implements
|
|
57
|
+
export abstract class AbstractGraph<V extends AbstractVertex, E extends AbstractEdge> implements IGraph<V, E> {
|
|
103
58
|
|
|
104
59
|
protected _vertices: Map<VertexId, V> = new Map<VertexId, V>();
|
|
105
60
|
|
|
@@ -107,25 +62,55 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
107
62
|
|
|
108
63
|
abstract removeEdge(edge: E): E | null;
|
|
109
64
|
|
|
65
|
+
/**
|
|
66
|
+
* The function `getVertex` returns the vertex object associated with a given vertex ID or vertex object, or null if it
|
|
67
|
+
* does not exist.
|
|
68
|
+
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
|
|
69
|
+
* @returns The function `getVertex` returns the vertex object (`V`) corresponding to the given `vertexOrId` parameter.
|
|
70
|
+
* If the vertex is found in the `_vertices` map, it is returned. Otherwise, `null` is returned.
|
|
71
|
+
*/
|
|
110
72
|
getVertex(vertexOrId: VertexId | V): V | null {
|
|
111
73
|
const vertexId = this.getVertexId(vertexOrId);
|
|
112
74
|
return this._vertices.get(vertexId) || null;
|
|
113
75
|
}
|
|
114
76
|
|
|
77
|
+
/**
|
|
78
|
+
* The function `getVertexId` returns the id of a vertex, whether it is passed as an instance of `AbstractVertex` or as
|
|
79
|
+
* a `VertexId`.
|
|
80
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
81
|
+
* (`VertexId`).
|
|
82
|
+
* @returns the id of the vertex.
|
|
83
|
+
*/
|
|
115
84
|
getVertexId(vertexOrId: V | VertexId): VertexId {
|
|
116
85
|
return vertexOrId instanceof AbstractVertex ? vertexOrId.id : vertexOrId;
|
|
117
86
|
}
|
|
118
87
|
|
|
88
|
+
/**
|
|
89
|
+
* The function checks if a vertex exists in a graph.
|
|
90
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
|
|
91
|
+
* (`VertexId`).
|
|
92
|
+
* @returns The method `containsVertex` returns a boolean value.
|
|
93
|
+
*/
|
|
119
94
|
containsVertex(vertexOrId: V | VertexId): boolean {
|
|
120
95
|
return this._vertices.has(this.getVertexId(vertexOrId));
|
|
121
96
|
}
|
|
122
97
|
|
|
98
|
+
/**
|
|
99
|
+
* The function `vertexSet()` returns a map of vertices.
|
|
100
|
+
* @returns The method `vertexSet()` returns a map of vertex IDs to vertex objects.
|
|
101
|
+
*/
|
|
123
102
|
vertexSet(): Map<VertexId, V> {
|
|
124
103
|
return this._vertices;
|
|
125
104
|
}
|
|
126
105
|
|
|
127
106
|
abstract getEdge(srcOrId: V | null | VertexId, destOrId: V | null | VertexId): E | null;
|
|
128
107
|
|
|
108
|
+
/**
|
|
109
|
+
* The addVertex function adds a new vertex to a graph if it does not already exist.
|
|
110
|
+
* @param {V} newVertex - The parameter "newVertex" is of type V, which represents a vertex in a graph.
|
|
111
|
+
* @returns The method is returning a boolean value. If the newVertex is already contained in the graph, it will return
|
|
112
|
+
* false. Otherwise, it will add the newVertex to the graph and return true.
|
|
113
|
+
*/
|
|
129
114
|
addVertex(newVertex: V): boolean {
|
|
130
115
|
if (this.containsVertex(newVertex)) {
|
|
131
116
|
return false;
|
|
@@ -134,11 +119,24 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
134
119
|
return true;
|
|
135
120
|
}
|
|
136
121
|
|
|
122
|
+
/**
|
|
123
|
+
* The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
124
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
125
|
+
* (`VertexId`).
|
|
126
|
+
* @returns The method `removeVertex` returns a boolean value.
|
|
127
|
+
*/
|
|
137
128
|
removeVertex(vertexOrId: V | VertexId): boolean {
|
|
138
129
|
const vertexId = this.getVertexId(vertexOrId);
|
|
139
130
|
return this._vertices.delete(vertexId);
|
|
140
131
|
}
|
|
141
132
|
|
|
133
|
+
/**
|
|
134
|
+
* The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
|
|
135
|
+
* @param {V[] | VertexId[]} vertices - The `vertices` parameter can be either an array of vertices (`V[]`) or an array
|
|
136
|
+
* of vertex IDs (`VertexId[]`).
|
|
137
|
+
* @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
|
|
138
|
+
* were removed.
|
|
139
|
+
*/
|
|
142
140
|
removeAllVertices(vertices: V[] | VertexId[]): boolean {
|
|
143
141
|
const removed: boolean[] = [];
|
|
144
142
|
for (const v of vertices) {
|
|
@@ -153,6 +151,15 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
153
151
|
|
|
154
152
|
abstract edgesOf(vertexOrId: V | VertexId): E[];
|
|
155
153
|
|
|
154
|
+
/**
|
|
155
|
+
* The function checks if there is an edge between two vertices in a graph.
|
|
156
|
+
* @param {VertexId | V} v1 - The parameter v1 can be either a VertexId or a V. A VertexId represents the identifier of
|
|
157
|
+
* a vertex in a graph, while V represents the type of the vertex itself.
|
|
158
|
+
* @param {VertexId | V} v2 - The parameter `v2` represents the second vertex in an edge. It can be either a `VertexId`
|
|
159
|
+
* or a `V` type.
|
|
160
|
+
* @returns The function `containsEdge` returns a boolean value. It returns `true` if there is an edge between the
|
|
161
|
+
* vertices `v1` and `v2`, and `false` otherwise.
|
|
162
|
+
*/
|
|
156
163
|
containsEdge(v1: VertexId | V, v2: VertexId | V): boolean {
|
|
157
164
|
const edge = this.getEdge(v1, v2);
|
|
158
165
|
return !!edge;
|
|
@@ -160,6 +167,17 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
160
167
|
|
|
161
168
|
abstract addEdge(edge: E): boolean;
|
|
162
169
|
|
|
170
|
+
/**
|
|
171
|
+
* The function sets the weight of an edge between two vertices in a graph.
|
|
172
|
+
* @param {VertexId | V} srcOrId - The `srcOrId` parameter can be either a `VertexId` or a `V` object. It represents
|
|
173
|
+
* the source vertex of the edge.
|
|
174
|
+
* @param {VertexId | V} destOrId - The `destOrId` parameter represents the destination vertex of the edge. It can be
|
|
175
|
+
* either a `VertexId` or a vertex object `V`.
|
|
176
|
+
* @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrId)
|
|
177
|
+
* and the destination vertex (destOrId).
|
|
178
|
+
* @returns a boolean value. If the edge exists between the source and destination vertices, the function will update
|
|
179
|
+
* the weight of the edge and return true. If the edge does not exist, the function will return false.
|
|
180
|
+
*/
|
|
163
181
|
setEdgeWeight(srcOrId: VertexId | V, destOrId: VertexId | V, weight: number): boolean {
|
|
164
182
|
const edge = this.getEdge(srcOrId, destOrId);
|
|
165
183
|
if (edge) {
|
|
@@ -172,6 +190,15 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
172
190
|
|
|
173
191
|
abstract getNeighbors(vertexOrId: V | VertexId): V[];
|
|
174
192
|
|
|
193
|
+
/**
|
|
194
|
+
* The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
|
|
195
|
+
* @param {V | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
|
|
196
|
+
* It is the starting vertex for finding paths.
|
|
197
|
+
* @param {V | VertexId} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex that we
|
|
198
|
+
* want to find paths to from the starting vertex `v1`.
|
|
199
|
+
* @returns an array of arrays of vertices (V[][]). Each inner array represents a path between the given vertices (v1
|
|
200
|
+
* and v2).
|
|
201
|
+
*/
|
|
175
202
|
getAllPathsBetween(v1: V | VertexId, v2: V | VertexId): V[][] {
|
|
176
203
|
const paths: V[][] = [];
|
|
177
204
|
const vertex1 = this.getVertex(v1);
|
|
@@ -192,7 +219,7 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
192
219
|
if (!visiting.get(neighbor)) {
|
|
193
220
|
path.push(neighbor);
|
|
194
221
|
dfs(neighbor, dest, visiting, path);
|
|
195
|
-
arrayRemove(path, vertex => vertex === neighbor);
|
|
222
|
+
arrayRemove(path, (vertex: AbstractVertex) => vertex === neighbor);
|
|
196
223
|
}
|
|
197
224
|
}
|
|
198
225
|
|
|
@@ -203,7 +230,11 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
203
230
|
return paths;
|
|
204
231
|
}
|
|
205
232
|
|
|
206
|
-
|
|
233
|
+
/**
|
|
234
|
+
* The function calculates the sum of weights along a given path.
|
|
235
|
+
* @param {V[]} path - An array of vertices (V) representing a path in a graph.
|
|
236
|
+
* @returns The function `getPathSumWeight` returns the sum of the weights of the edges in the given path.
|
|
237
|
+
*/
|
|
207
238
|
getPathSumWeight(path: V[]): number {
|
|
208
239
|
let sum = 0;
|
|
209
240
|
for (let i = 0; i < path.length; i++) {
|
|
@@ -212,6 +243,20 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
212
243
|
return sum;
|
|
213
244
|
}
|
|
214
245
|
|
|
246
|
+
/**
|
|
247
|
+
* The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
|
|
248
|
+
* weights or using a breadth-first search algorithm.
|
|
249
|
+
* @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex or vertex ID of the graph.
|
|
250
|
+
* @param {V | VertexId} v2 - The parameter `v2` represents the second vertex in the graph. It can be either a vertex
|
|
251
|
+
* object or a vertex ID.
|
|
252
|
+
* @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edges have weights.
|
|
253
|
+
* If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
|
|
254
|
+
* the edges. If isWeight is set to false or not provided, the function will calculate the
|
|
255
|
+
* @returns The function `getMinCostBetween` returns a number representing the minimum cost between two vertices (`v1`
|
|
256
|
+
* and `v2`) in a graph. If the `isWeight` parameter is `true`, it calculates the minimum weight between the vertices.
|
|
257
|
+
* If `isWeight` is `false` or not provided, it calculates the minimum number of edges between the vertices. If the
|
|
258
|
+
* vertices are not
|
|
259
|
+
*/
|
|
215
260
|
getMinCostBetween(v1: V | VertexId, v2: V | VertexId, isWeight?: boolean): number | null {
|
|
216
261
|
if (isWeight === undefined) isWeight = false;
|
|
217
262
|
|
|
@@ -257,6 +302,18 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
257
302
|
}
|
|
258
303
|
}
|
|
259
304
|
|
|
305
|
+
/**
|
|
306
|
+
* The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
|
|
307
|
+
* using a breadth-first search algorithm.
|
|
308
|
+
* @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex or its ID.
|
|
309
|
+
* @param {V | VertexId} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex that we
|
|
310
|
+
* want to find the minimum path to from the source vertex `v1`.
|
|
311
|
+
* @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edges in finding the
|
|
312
|
+
* minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
|
|
313
|
+
* to false, the function will use breadth-first search (BFS) to find the minimum path. If
|
|
314
|
+
* @returns The function `getMinPathBetween` returns an array of vertices (`V[]`) representing the minimum path between
|
|
315
|
+
* two vertices (`v1` and `v2`). If no path is found, it returns `null`.
|
|
316
|
+
*/
|
|
260
317
|
getMinPathBetween(v1: V | VertexId, v2: V | VertexId, isWeight?: boolean): V[] | null {
|
|
261
318
|
if (isWeight === undefined) isWeight = false;
|
|
262
319
|
|
|
@@ -296,7 +353,7 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
296
353
|
if (!visiting.get(neighbor)) {
|
|
297
354
|
path.push(neighbor);
|
|
298
355
|
dfs(neighbor, dest, visiting, path);
|
|
299
|
-
arrayRemove(path, vertex => vertex === neighbor);
|
|
356
|
+
arrayRemove(path, (vertex: AbstractVertex) => vertex === neighbor);
|
|
300
357
|
}
|
|
301
358
|
}
|
|
302
359
|
|
|
@@ -310,10 +367,20 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
310
367
|
|
|
311
368
|
/**
|
|
312
369
|
* Dijkstra algorithm time: O(VE) space: O(V + E)
|
|
313
|
-
*
|
|
314
|
-
*
|
|
315
|
-
* @param
|
|
316
|
-
*
|
|
370
|
+
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
|
|
371
|
+
* a graph without using a heap data structure.
|
|
372
|
+
* @param {V | VertexId} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
373
|
+
* vertex object or a vertex ID.
|
|
374
|
+
* @param {V | VertexId | null} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
375
|
+
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
376
|
+
* identifier. If no destination is provided, the value is set to `null`.
|
|
377
|
+
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
378
|
+
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
379
|
+
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
380
|
+
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
381
|
+
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
382
|
+
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
383
|
+
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<V>`.
|
|
317
384
|
*/
|
|
318
385
|
dijkstraWithoutHeap(src: V | VertexId, dest?: V | VertexId | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<V> {
|
|
319
386
|
if (getMinDist === undefined) getMinDist = false;
|
|
@@ -338,7 +405,8 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
338
405
|
}
|
|
339
406
|
|
|
340
407
|
for (const vertex of vertices) {
|
|
341
|
-
|
|
408
|
+
const vertexOrId = vertex[1];
|
|
409
|
+
if (vertexOrId instanceof AbstractVertex) distMap.set(vertexOrId, Infinity);
|
|
342
410
|
}
|
|
343
411
|
distMap.set(srcVertex, 0);
|
|
344
412
|
preMap.set(srcVertex, null);
|
|
@@ -359,15 +427,19 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
359
427
|
|
|
360
428
|
const getPaths = (minV: V | null) => {
|
|
361
429
|
for (const vertex of vertices) {
|
|
362
|
-
const
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
path
|
|
366
|
-
parent = preMap.get(
|
|
430
|
+
const vertexOrId = vertex[1];
|
|
431
|
+
|
|
432
|
+
if (vertexOrId instanceof AbstractVertex) {
|
|
433
|
+
const path: V[] = [vertexOrId];
|
|
434
|
+
let parent = preMap.get(vertexOrId);
|
|
435
|
+
while (parent) {
|
|
436
|
+
path.push(parent);
|
|
437
|
+
parent = preMap.get(parent);
|
|
438
|
+
}
|
|
439
|
+
const reversed = path.reverse();
|
|
440
|
+
if (vertex[1] === minV) minPath = reversed;
|
|
441
|
+
paths.push(reversed);
|
|
367
442
|
}
|
|
368
|
-
const reversed = path.reverse();
|
|
369
|
-
if (vertex[1] === minV) minPath = reversed;
|
|
370
|
-
paths.push(reversed);
|
|
371
443
|
}
|
|
372
444
|
};
|
|
373
445
|
|
|
@@ -419,13 +491,22 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
419
491
|
return {distMap, preMap, seen, paths, minDist, minPath};
|
|
420
492
|
}
|
|
421
493
|
|
|
422
|
-
|
|
423
494
|
/**
|
|
424
495
|
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
425
|
-
*
|
|
426
|
-
*
|
|
427
|
-
* @param
|
|
428
|
-
*
|
|
496
|
+
* The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
|
|
497
|
+
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
498
|
+
* @param {V | VertexId} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
499
|
+
* start. It can be either a vertex object or a vertex ID.
|
|
500
|
+
* @param {V | VertexId | null} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
501
|
+
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
502
|
+
* will calculate the shortest paths to all other vertices from the source vertex.
|
|
503
|
+
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
504
|
+
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
505
|
+
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
506
|
+
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
507
|
+
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
508
|
+
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
509
|
+
* @returns The function `dijkstra` returns an object of type `DijkstraResult<V>`.
|
|
429
510
|
*/
|
|
430
511
|
dijkstra(src: V | VertexId, dest?: V | VertexId | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<V> {
|
|
431
512
|
if (getMinDist === undefined) getMinDist = false;
|
|
@@ -449,7 +530,8 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
449
530
|
}
|
|
450
531
|
|
|
451
532
|
for (const vertex of vertices) {
|
|
452
|
-
|
|
533
|
+
const vertexOrId = vertex[1];
|
|
534
|
+
if (vertexOrId instanceof AbstractVertex) distMap.set(vertexOrId, Infinity);
|
|
453
535
|
}
|
|
454
536
|
|
|
455
537
|
const heap = new PriorityQueue<{ id: number, val: V }>({comparator: (a, b) => a.id - b.id});
|
|
@@ -460,15 +542,19 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
460
542
|
|
|
461
543
|
const getPaths = (minV: V | null) => {
|
|
462
544
|
for (const vertex of vertices) {
|
|
463
|
-
const
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
545
|
+
const vertexOrId = vertex[1];
|
|
546
|
+
if (vertexOrId instanceof AbstractVertex) {
|
|
547
|
+
const path: V[] = [vertexOrId];
|
|
548
|
+
let parent = preMap.get(vertexOrId);
|
|
549
|
+
while (parent) {
|
|
550
|
+
path.push(parent);
|
|
551
|
+
parent = preMap.get(parent);
|
|
552
|
+
}
|
|
553
|
+
const reversed = path.reverse();
|
|
554
|
+
if (vertex[1] === minV) minPath = reversed;
|
|
555
|
+
paths.push(reversed);
|
|
468
556
|
}
|
|
469
|
-
|
|
470
|
-
if (vertex[1] === minV) minPath = reversed;
|
|
471
|
-
paths.push(reversed);
|
|
557
|
+
|
|
472
558
|
}
|
|
473
559
|
};
|
|
474
560
|
|
|
@@ -535,10 +621,17 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
535
621
|
/**
|
|
536
622
|
* BellmanFord time:O(VE) space:O(V)
|
|
537
623
|
* one to rest pairs
|
|
538
|
-
*
|
|
539
|
-
*
|
|
540
|
-
* @param
|
|
541
|
-
*
|
|
624
|
+
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
625
|
+
* all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.
|
|
626
|
+
* @param {V | VertexId} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
|
|
627
|
+
* start calculating the shortest paths. It can be either a vertex object or a vertex ID.
|
|
628
|
+
* @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
|
|
629
|
+
* @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
|
|
630
|
+
* calculate the minimum distance from the source vertex to all other vertices in the graph. If `getMin` is set to
|
|
631
|
+
* `true`, the algorithm will find the minimum distance and update the `min` variable with the minimum
|
|
632
|
+
* @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all vertices from the source
|
|
633
|
+
* vertex.
|
|
634
|
+
* @returns The function `bellmanFord` returns an object with the following properties:
|
|
542
635
|
*/
|
|
543
636
|
bellmanFord(src: V | VertexId, scanNegativeCycle?: boolean, getMin?: boolean, genPath?: boolean) {
|
|
544
637
|
if (getMin === undefined) getMin = false;
|
|
@@ -551,7 +644,7 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
551
644
|
let min = Infinity;
|
|
552
645
|
let minPath: V[] = [];
|
|
553
646
|
// TODO
|
|
554
|
-
let hasNegativeCycle: boolean | undefined
|
|
647
|
+
let hasNegativeCycle: boolean | undefined;
|
|
555
648
|
if (scanNegativeCycle) hasNegativeCycle = false;
|
|
556
649
|
if (!srcVertex) return {hasNegativeCycle, distMap, preMap, paths, min, minPath};
|
|
557
650
|
|
|
@@ -598,15 +691,18 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
598
691
|
|
|
599
692
|
if (genPath) {
|
|
600
693
|
for (const vertex of vertices) {
|
|
601
|
-
const
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
parent
|
|
694
|
+
const vertexOrId = vertex[1];
|
|
695
|
+
if (vertexOrId instanceof AbstractVertex) {
|
|
696
|
+
const path: V[] = [vertexOrId];
|
|
697
|
+
let parent = preMap.get(vertexOrId);
|
|
698
|
+
while (parent !== undefined) {
|
|
699
|
+
path.push(parent);
|
|
700
|
+
parent = preMap.get(parent);
|
|
701
|
+
}
|
|
702
|
+
const reversed = path.reverse();
|
|
703
|
+
if (vertex[1] === minDest) minPath = reversed;
|
|
704
|
+
paths.push(reversed);
|
|
606
705
|
}
|
|
607
|
-
const reversed = path.reverse();
|
|
608
|
-
if (vertex[1] === minDest) minPath = reversed;
|
|
609
|
-
paths.push(reversed);
|
|
610
706
|
}
|
|
611
707
|
}
|
|
612
708
|
|
|
@@ -628,6 +724,12 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
628
724
|
/**
|
|
629
725
|
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
630
726
|
* all pairs
|
|
727
|
+
* The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a
|
|
728
|
+
* graph.
|
|
729
|
+
* @returns The function `floyd()` returns an object with two properties: `costs` and `predecessor`. The `costs`
|
|
730
|
+
* property is a 2D array of numbers representing the shortest path costs between vertices in a graph. The
|
|
731
|
+
* `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
|
|
732
|
+
* path between vertices in the
|
|
631
733
|
*/
|
|
632
734
|
floyd(): { costs: number[][], predecessor: (V | null)[][] } {
|
|
633
735
|
const idAndVertices = [...this._vertices];
|
|
@@ -674,6 +776,20 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
674
776
|
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
675
777
|
* Tarjan solve the bi-connected components of undirected graphs;
|
|
676
778
|
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
779
|
+
* The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
|
|
780
|
+
* strongly connected components (SCCs), and cycles in a graph.
|
|
781
|
+
* @param {boolean} [needArticulationPoints] - A boolean value indicating whether or not to calculate and return the
|
|
782
|
+
* articulation points in the graph. Articulation points are the vertices in a graph whose removal would increase the
|
|
783
|
+
* number of connected components in the graph.
|
|
784
|
+
* @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
|
|
785
|
+
* (edges whose removal would increase the number of connected components in the graph).
|
|
786
|
+
* @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
|
|
787
|
+
* graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
|
|
788
|
+
* SCCs will not be calculated or returned.
|
|
789
|
+
* @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
|
|
790
|
+
* set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
|
|
791
|
+
* are arrays of vertices that form cycles within the SCCs.
|
|
792
|
+
* @returns The function `tarjan` returns an object with the following properties:
|
|
677
793
|
*/
|
|
678
794
|
tarjan(needArticulationPoints?: boolean, needBridges?: boolean, needSCCs?: boolean, needCycles?: boolean) {
|
|
679
795
|
// !! in undirected graph we will not let child visit parent when DFS
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|