data-structure-typed 1.18.6 → 1.18.8
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/README.md +154 -366
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +72 -51
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +226 -158
- package/dist/data-structures/binary-tree/avl-tree.d.ts +7 -6
- package/dist/data-structures/binary-tree/avl-tree.js +5 -3
- package/dist/data-structures/binary-tree/binary-tree.d.ts +16 -6
- package/dist/data-structures/binary-tree/binary-tree.js +16 -7
- package/dist/data-structures/binary-tree/bst.d.ts +37 -28
- package/dist/data-structures/binary-tree/bst.js +44 -76
- package/dist/data-structures/binary-tree/rb-tree.d.ts +30 -1
- package/dist/data-structures/binary-tree/rb-tree.js +27 -14
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +18 -7
- package/dist/data-structures/binary-tree/tree-multiset.js +20 -15
- package/dist/data-structures/graph/abstract-graph.d.ts +28 -27
- package/dist/data-structures/graph/abstract-graph.js +58 -36
- package/dist/data-structures/graph/directed-graph.d.ts +27 -24
- package/dist/data-structures/graph/directed-graph.js +65 -60
- package/dist/data-structures/graph/undirected-graph.d.ts +12 -18
- package/dist/data-structures/graph/undirected-graph.js +52 -65
- package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +100 -0
- package/dist/data-structures/interfaces/abstract-binary-tree.js +2 -0
- package/dist/data-structures/interfaces/abstract-graph.d.ts +3 -8
- package/dist/data-structures/interfaces/avl-tree.d.ts +16 -1
- package/dist/data-structures/interfaces/binary-tree.d.ts +4 -24
- package/dist/data-structures/interfaces/bst.d.ts +18 -1
- package/dist/data-structures/interfaces/directed-graph.d.ts +4 -1
- package/dist/data-structures/interfaces/index.d.ts +1 -1
- package/dist/data-structures/interfaces/index.js +1 -1
- package/dist/data-structures/interfaces/rb-tree.d.ts +9 -0
- package/dist/data-structures/interfaces/rb-tree.js +2 -0
- package/dist/data-structures/interfaces/tree-multiset.d.ts +7 -1
- package/dist/data-structures/interfaces/undirected-graph.d.ts +4 -1
- package/dist/data-structures/tree/tree.d.ts +1 -4
- package/dist/data-structures/tree/tree.js +1 -12
- package/dist/data-structures/types/abstract-binary-tree.d.ts +11 -7
- package/dist/data-structures/types/abstract-binary-tree.js +9 -5
- package/dist/data-structures/types/avl-tree.d.ts +1 -1
- package/dist/data-structures/types/binary-tree.d.ts +1 -1
- package/dist/data-structures/types/bst.d.ts +1 -1
- package/dist/data-structures/types/bst.js +1 -1
- package/dist/data-structures/types/directed-graph.js +1 -1
- package/dist/data-structures/types/helpers.d.ts +1 -8
- package/dist/data-structures/types/rb-tree.d.ts +2 -0
- package/dist/data-structures/types/rb-tree.js +1 -1
- package/dist/data-structures/types/tree-multiset.d.ts +4 -4
- package/dist/utils/types/utils.d.ts +18 -0
- package/dist/utils/utils.d.ts +6 -1
- package/dist/utils/utils.js +83 -1
- package/package.json +2 -2
- package/backup/recursive-type/src/assets/complexities-diff.jpg +0 -0
- package/backup/recursive-type/src/assets/data-structure-complexities.jpg +0 -0
- package/backup/recursive-type/src/assets/logo.png +0 -0
- package/backup/recursive-type/src/assets/overview-diagram-of-data-structures.png +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/backup/recursive-type/src/data-structures/binary-tree/avl-tree.ts +0 -288
- package/backup/recursive-type/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/backup/recursive-type/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -78
- package/backup/recursive-type/src/data-structures/binary-tree/binary-tree.ts +0 -1502
- package/backup/recursive-type/src/data-structures/binary-tree/bst.ts +0 -503
- package/backup/recursive-type/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/index.ts +0 -11
- package/backup/recursive-type/src/data-structures/binary-tree/rb-tree.ts +0 -110
- package/backup/recursive-type/src/data-structures/binary-tree/segment-tree.ts +0 -243
- package/backup/recursive-type/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/backup/recursive-type/src/data-structures/binary-tree/tree-multiset.ts +0 -55
- package/backup/recursive-type/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/backup/recursive-type/src/data-structures/diagrams/README.md +0 -5
- package/backup/recursive-type/src/data-structures/graph/abstract-graph.ts +0 -985
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/mst.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.webp +0 -0
- package/backup/recursive-type/src/data-structures/graph/directed-graph.ts +0 -478
- package/backup/recursive-type/src/data-structures/graph/index.ts +0 -3
- package/backup/recursive-type/src/data-structures/graph/undirected-graph.ts +0 -293
- package/backup/recursive-type/src/data-structures/hash/coordinate-map.ts +0 -67
- package/backup/recursive-type/src/data-structures/hash/coordinate-set.ts +0 -56
- package/backup/recursive-type/src/data-structures/hash/hash-table.ts +0 -3
- package/backup/recursive-type/src/data-structures/hash/index.ts +0 -6
- package/backup/recursive-type/src/data-structures/hash/pair.ts +0 -3
- package/backup/recursive-type/src/data-structures/hash/tree-map.ts +0 -3
- package/backup/recursive-type/src/data-structures/hash/tree-set.ts +0 -3
- package/backup/recursive-type/src/data-structures/heap/heap.ts +0 -176
- package/backup/recursive-type/src/data-structures/heap/index.ts +0 -3
- package/backup/recursive-type/src/data-structures/heap/max-heap.ts +0 -31
- package/backup/recursive-type/src/data-structures/heap/min-heap.ts +0 -34
- package/backup/recursive-type/src/data-structures/index.ts +0 -15
- package/backup/recursive-type/src/data-structures/interfaces/abstract-graph.ts +0 -42
- package/backup/recursive-type/src/data-structures/interfaces/avl-tree.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/binary-tree.ts +0 -56
- package/backup/recursive-type/src/data-structures/interfaces/bst.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/directed-graph.ts +0 -15
- package/backup/recursive-type/src/data-structures/interfaces/doubly-linked-list.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/heap.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/index.ts +0 -13
- package/backup/recursive-type/src/data-structures/interfaces/navigator.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/priority-queue.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/segment-tree.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/singly-linked-list.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/tree-multiset.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/undirected-graph.ts +0 -3
- package/backup/recursive-type/src/data-structures/linked-list/doubly-linked-list.ts +0 -573
- package/backup/recursive-type/src/data-structures/linked-list/index.ts +0 -3
- package/backup/recursive-type/src/data-structures/linked-list/singly-linked-list.ts +0 -490
- package/backup/recursive-type/src/data-structures/linked-list/skip-linked-list.ts +0 -3
- package/backup/recursive-type/src/data-structures/matrix/index.ts +0 -4
- package/backup/recursive-type/src/data-structures/matrix/matrix.ts +0 -27
- package/backup/recursive-type/src/data-structures/matrix/matrix2d.ts +0 -208
- package/backup/recursive-type/src/data-structures/matrix/navigator.ts +0 -122
- package/backup/recursive-type/src/data-structures/matrix/vector2d.ts +0 -316
- package/backup/recursive-type/src/data-structures/priority-queue/index.ts +0 -3
- package/backup/recursive-type/src/data-structures/priority-queue/max-priority-queue.ts +0 -49
- package/backup/recursive-type/src/data-structures/priority-queue/min-priority-queue.ts +0 -50
- package/backup/recursive-type/src/data-structures/priority-queue/priority-queue.ts +0 -354
- package/backup/recursive-type/src/data-structures/queue/deque.ts +0 -251
- package/backup/recursive-type/src/data-structures/queue/index.ts +0 -2
- package/backup/recursive-type/src/data-structures/queue/queue.ts +0 -120
- package/backup/recursive-type/src/data-structures/stack/index.ts +0 -1
- package/backup/recursive-type/src/data-structures/stack/stack.ts +0 -98
- package/backup/recursive-type/src/data-structures/tree/index.ts +0 -1
- package/backup/recursive-type/src/data-structures/tree/tree.ts +0 -80
- package/backup/recursive-type/src/data-structures/trie/index.ts +0 -1
- package/backup/recursive-type/src/data-structures/trie/trie.ts +0 -227
- package/backup/recursive-type/src/data-structures/types/abstract-graph.ts +0 -5
- package/backup/recursive-type/src/data-structures/types/avl-tree.ts +0 -8
- package/backup/recursive-type/src/data-structures/types/binary-tree.ts +0 -10
- package/backup/recursive-type/src/data-structures/types/bst.ts +0 -6
- package/backup/recursive-type/src/data-structures/types/directed-graph.ts +0 -8
- package/backup/recursive-type/src/data-structures/types/doubly-linked-list.ts +0 -1
- package/backup/recursive-type/src/data-structures/types/heap.ts +0 -5
- package/backup/recursive-type/src/data-structures/types/index.ts +0 -12
- package/backup/recursive-type/src/data-structures/types/navigator.ts +0 -13
- package/backup/recursive-type/src/data-structures/types/priority-queue.ts +0 -9
- package/backup/recursive-type/src/data-structures/types/segment-tree.ts +0 -1
- package/backup/recursive-type/src/data-structures/types/singly-linked-list.ts +0 -1
- package/backup/recursive-type/src/data-structures/types/tree-multiset.ts +0 -1
- package/backup/recursive-type/src/index.ts +0 -1
- package/backup/recursive-type/src/utils/index.ts +0 -2
- package/backup/recursive-type/src/utils/types/index.ts +0 -1
- package/backup/recursive-type/src/utils/types/utils.ts +0 -6
- package/backup/recursive-type/src/utils/utils.ts +0 -78
- package/docs/.nojekyll +0 -1
- package/docs/assets/highlight.css +0 -92
- package/docs/assets/main.js +0 -58
- package/docs/assets/search.js +0 -1
- package/docs/assets/style.css +0 -1367
- package/docs/classes/AVLTree.html +0 -2339
- package/docs/classes/AVLTreeNode.html +0 -450
- package/docs/classes/AaTree.html +0 -166
- package/docs/classes/AbstractBinaryTree.html +0 -2023
- package/docs/classes/AbstractBinaryTreeNode.html +0 -491
- package/docs/classes/AbstractEdge.html +0 -289
- package/docs/classes/AbstractGraph.html +0 -1046
- package/docs/classes/AbstractVertex.html +0 -252
- package/docs/classes/ArrayDeque.html +0 -433
- package/docs/classes/BST.html +0 -2196
- package/docs/classes/BSTNode.html +0 -453
- package/docs/classes/BTree.html +0 -166
- package/docs/classes/BinaryIndexedTree.html +0 -335
- package/docs/classes/BinaryTree.html +0 -2037
- package/docs/classes/BinaryTreeNode.html +0 -452
- package/docs/classes/Character.html +0 -214
- package/docs/classes/CoordinateMap.html +0 -477
- package/docs/classes/CoordinateSet.html +0 -438
- package/docs/classes/Deque.html +0 -969
- package/docs/classes/DirectedEdge.html +0 -360
- package/docs/classes/DirectedGraph.html +0 -1408
- package/docs/classes/DirectedVertex.html +0 -248
- package/docs/classes/DoublyLinkedList.html +0 -962
- package/docs/classes/DoublyLinkedListNode.html +0 -291
- package/docs/classes/HashTable.html +0 -166
- package/docs/classes/Heap.html +0 -417
- package/docs/classes/HeapItem.html +0 -249
- package/docs/classes/Matrix2D.html +0 -496
- package/docs/classes/MatrixNTI2D.html +0 -234
- package/docs/classes/MaxHeap.html +0 -430
- package/docs/classes/MaxPriorityQueue.html +0 -830
- package/docs/classes/MinHeap.html +0 -431
- package/docs/classes/MinPriorityQueue.html +0 -832
- package/docs/classes/Navigator.html +0 -307
- package/docs/classes/ObjectDeque.html +0 -449
- package/docs/classes/Pair.html +0 -166
- package/docs/classes/PriorityQueue.html +0 -754
- package/docs/classes/Queue.html +0 -386
- package/docs/classes/SegmentTree.html +0 -428
- package/docs/classes/SegmentTreeNode.html +0 -351
- package/docs/classes/SinglyLinkedList.html +0 -782
- package/docs/classes/SinglyLinkedListNode.html +0 -264
- package/docs/classes/SkipLinkedList.html +0 -166
- package/docs/classes/SplayTree.html +0 -166
- package/docs/classes/Stack.html +0 -362
- package/docs/classes/TreeMap.html +0 -166
- package/docs/classes/TreeMultiSet.html +0 -2196
- package/docs/classes/TreeMultiSetNode.html +0 -450
- package/docs/classes/TreeNode.html +0 -337
- package/docs/classes/TreeSet.html +0 -166
- package/docs/classes/Trie.html +0 -366
- package/docs/classes/TrieNode.html +0 -274
- package/docs/classes/TwoThreeTree.html +0 -166
- package/docs/classes/UndirectedEdge.html +0 -331
- package/docs/classes/UndirectedGraph.html +0 -1196
- package/docs/classes/UndirectedVertex.html +0 -248
- package/docs/classes/Vector2D.html +0 -799
- package/docs/enums/CP.html +0 -175
- package/docs/enums/FamilyPosition.html +0 -175
- package/docs/enums/LoopType.html +0 -176
- package/docs/enums/RBColor.html +0 -168
- package/docs/enums/TopologicalProperty.html +0 -175
- package/docs/index.html +0 -639
- package/docs/interfaces/IBinaryTree.html +0 -189
- package/docs/interfaces/IBinaryTreeNode.html +0 -396
- package/docs/interfaces/IDirectedGraph.html +0 -259
- package/docs/interfaces/IGraph.html +0 -471
- package/docs/interfaces/IUNDirectedGraph.html +0 -151
- package/docs/modules.html +0 -250
- package/docs/types/AVLTreeOptions.html +0 -144
- package/docs/types/AbstractBinaryTreeOptions.html +0 -150
- package/docs/types/AbstractRecursiveBinaryTreeNode.html +0 -146
- package/docs/types/AbstractResultByProperty.html +0 -146
- package/docs/types/AbstractResultsByProperty.html +0 -146
- package/docs/types/BSTComparator.html +0 -156
- package/docs/types/BSTOptions.html +0 -146
- package/docs/types/BinaryTreeDeletedResult.html +0 -153
- package/docs/types/BinaryTreeNodeId.html +0 -141
- package/docs/types/BinaryTreeNodePropertyName.html +0 -141
- package/docs/types/BinaryTreeOptions.html +0 -144
- package/docs/types/DFSOrderPattern.html +0 -141
- package/docs/types/DijkstraResult.html +0 -161
- package/docs/types/Direction.html +0 -141
- package/docs/types/EdgeId.html +0 -141
- package/docs/types/HeapOptions.html +0 -162
- package/docs/types/IdObject.html +0 -151
- package/docs/types/KeyValObject.html +0 -146
- package/docs/types/NavigatorParams.html +0 -175
- package/docs/types/NodeOrPropertyName.html +0 -141
- package/docs/types/PriorityQueueComparator.html +0 -161
- package/docs/types/PriorityQueueDFSOrderPattern.html +0 -141
- package/docs/types/PriorityQueueOptions.html +0 -155
- package/docs/types/RBTreeOptions.html +0 -144
- package/docs/types/RecursiveAVLTreeNode.html +0 -146
- package/docs/types/RecursiveBSTNode.html +0 -146
- package/docs/types/RecursiveBinaryTreeNode.html +0 -146
- package/docs/types/RecursiveTreeMultiSetNode.html +0 -146
- package/docs/types/SegmentTreeNodeVal.html +0 -141
- package/docs/types/TopologicalStatus.html +0 -141
- package/docs/types/TreeMultiSetOptions.html +0 -146
- package/docs/types/Turning.html +0 -141
- package/docs/types/VertexId.html +0 -141
- package/notes/bst.test.ts +0 -181
- package/notes/note.md +0 -34
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { DijkstraResult, VertexId } from '../types';
|
|
2
|
-
import {
|
|
2
|
+
import { IAbstractGraph } from '../interfaces';
|
|
3
3
|
export declare abstract class AbstractVertex<T = number> {
|
|
4
4
|
protected constructor(id: VertexId, val?: T);
|
|
5
5
|
private _id;
|
|
@@ -21,7 +21,7 @@ export declare abstract class AbstractEdge<T = number> {
|
|
|
21
21
|
get hashCode(): string;
|
|
22
22
|
protected _setHashCode(v: string): void;
|
|
23
23
|
}
|
|
24
|
-
export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E extends AbstractEdge<any>> implements
|
|
24
|
+
export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E extends AbstractEdge<any>> implements IAbstractGraph<V, E> {
|
|
25
25
|
private _vertices;
|
|
26
26
|
get vertices(): Map<VertexId, V>;
|
|
27
27
|
/**
|
|
@@ -30,7 +30,7 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
30
30
|
* @param id
|
|
31
31
|
* @param val
|
|
32
32
|
*/
|
|
33
|
-
abstract
|
|
33
|
+
abstract createVertex(id: VertexId, val?: V): V;
|
|
34
34
|
/**
|
|
35
35
|
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
36
36
|
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
@@ -39,12 +39,9 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
39
39
|
* @param weight
|
|
40
40
|
* @param val
|
|
41
41
|
*/
|
|
42
|
-
abstract
|
|
43
|
-
abstract removeEdgeBetween(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
|
|
42
|
+
abstract createEdge(srcOrV1: VertexId | string, destOrV2: VertexId | string, weight?: number, val?: E): E;
|
|
44
43
|
abstract removeEdge(edge: E): E | null;
|
|
45
|
-
_getVertex(vertexOrId: VertexId | V): V | null;
|
|
46
44
|
getVertex(vertexId: VertexId): V | null;
|
|
47
|
-
_getVertexId(vertexOrId: V | VertexId): VertexId;
|
|
48
45
|
/**
|
|
49
46
|
* The function checks if a vertex exists in a graph.
|
|
50
47
|
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
|
|
@@ -53,8 +50,8 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
53
50
|
*/
|
|
54
51
|
hasVertex(vertexOrId: V | VertexId): boolean;
|
|
55
52
|
abstract getEdge(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
|
|
56
|
-
|
|
57
|
-
addVertex(
|
|
53
|
+
addVertex(vertex: V): boolean;
|
|
54
|
+
addVertex(id: VertexId, val?: V['val']): boolean;
|
|
58
55
|
/**
|
|
59
56
|
* The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
60
57
|
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
@@ -83,8 +80,8 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
83
80
|
* vertices `v1` and `v2`, and `false` otherwise.
|
|
84
81
|
*/
|
|
85
82
|
hasEdge(v1: VertexId | V, v2: VertexId | V): boolean;
|
|
86
|
-
|
|
87
|
-
|
|
83
|
+
addEdge(edge: E): boolean;
|
|
84
|
+
addEdge(src: V | VertexId, dest: V | VertexId, weight?: number, val?: E['val']): boolean;
|
|
88
85
|
/**
|
|
89
86
|
* The function sets the weight of an edge between two vertices in a graph.
|
|
90
87
|
* @param {VertexId | V} srcOrId - The `srcOrId` parameter can be either a `VertexId` or a `V` object. It represents
|
|
@@ -179,11 +176,6 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
179
176
|
* @returns The function `dijkstra` returns an object of type `DijkstraResult<V>`.
|
|
180
177
|
*/
|
|
181
178
|
dijkstra(src: V | VertexId, dest?: V | VertexId | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<V>;
|
|
182
|
-
/**
|
|
183
|
-
* Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
|
|
184
|
-
* Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
|
|
185
|
-
* The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
|
|
186
|
-
*/
|
|
187
179
|
/**
|
|
188
180
|
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
189
181
|
* Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
|
|
@@ -213,12 +205,6 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
213
205
|
min: number;
|
|
214
206
|
minPath: V[];
|
|
215
207
|
};
|
|
216
|
-
/**
|
|
217
|
-
* BellmanFord time:O(VE) space:O(V)
|
|
218
|
-
* one to rest pairs
|
|
219
|
-
* The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
220
|
-
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
221
|
-
*/
|
|
222
208
|
/**
|
|
223
209
|
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
224
210
|
* all pairs
|
|
@@ -234,11 +220,6 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
234
220
|
costs: number[][];
|
|
235
221
|
predecessor: (V | null)[][];
|
|
236
222
|
};
|
|
237
|
-
/**
|
|
238
|
-
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
239
|
-
* all pairs
|
|
240
|
-
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
|
|
241
|
-
*/
|
|
242
223
|
/**
|
|
243
224
|
* Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs.
|
|
244
225
|
* Tarjan can find cycles in directed or undirected graph
|
|
@@ -268,6 +249,26 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
268
249
|
SCCs: Map<number, V[]>;
|
|
269
250
|
cycles: Map<number, V[]>;
|
|
270
251
|
};
|
|
252
|
+
/**
|
|
253
|
+
* Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
|
|
254
|
+
* Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
|
|
255
|
+
* The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
|
|
256
|
+
*/
|
|
257
|
+
protected _addVertexOnly(newVertex: V): boolean;
|
|
258
|
+
protected abstract _addEdgeOnly(edge: E): boolean;
|
|
259
|
+
/**
|
|
260
|
+
* BellmanFord time:O(VE) space:O(V)
|
|
261
|
+
* one to rest pairs
|
|
262
|
+
* The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
263
|
+
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
264
|
+
*/
|
|
265
|
+
protected _getVertex(vertexOrId: VertexId | V): V | null;
|
|
266
|
+
/**
|
|
267
|
+
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
268
|
+
* all pairs
|
|
269
|
+
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
|
|
270
|
+
*/
|
|
271
|
+
protected _getVertexId(vertexOrId: V | VertexId): VertexId;
|
|
271
272
|
/**--- start find cycles --- */
|
|
272
273
|
protected _setVertices(value: Map<VertexId, V>): void;
|
|
273
274
|
}
|
|
@@ -115,7 +115,7 @@ var AbstractEdge = /** @class */ (function () {
|
|
|
115
115
|
// * @param weight
|
|
116
116
|
// * @param val
|
|
117
117
|
// */
|
|
118
|
-
// abstract
|
|
118
|
+
// abstract createEdge(srcOrV1: VertexId | string, destOrV2: VertexId | string, weight?: number, val?: E): E;
|
|
119
119
|
AbstractEdge.prototype._setHashCode = function (v) {
|
|
120
120
|
this._hashCode = v;
|
|
121
121
|
};
|
|
@@ -137,16 +137,9 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
137
137
|
enumerable: false,
|
|
138
138
|
configurable: true
|
|
139
139
|
});
|
|
140
|
-
AbstractGraph.prototype._getVertex = function (vertexOrId) {
|
|
141
|
-
var vertexId = this._getVertexId(vertexOrId);
|
|
142
|
-
return this._vertices.get(vertexId) || null;
|
|
143
|
-
};
|
|
144
140
|
AbstractGraph.prototype.getVertex = function (vertexId) {
|
|
145
141
|
return this._vertices.get(vertexId) || null;
|
|
146
142
|
};
|
|
147
|
-
AbstractGraph.prototype._getVertexId = function (vertexOrId) {
|
|
148
|
-
return vertexOrId instanceof AbstractVertex ? vertexOrId.id : vertexOrId;
|
|
149
|
-
};
|
|
150
143
|
/**
|
|
151
144
|
* The function checks if a vertex exists in a graph.
|
|
152
145
|
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
|
|
@@ -156,17 +149,14 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
156
149
|
AbstractGraph.prototype.hasVertex = function (vertexOrId) {
|
|
157
150
|
return this._vertices.has(this._getVertexId(vertexOrId));
|
|
158
151
|
};
|
|
159
|
-
AbstractGraph.prototype.
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
return
|
|
166
|
-
// throw (new Error('Duplicated vertex id is not allowed'));
|
|
152
|
+
AbstractGraph.prototype.addVertex = function (idOrVertex, val) {
|
|
153
|
+
if (idOrVertex instanceof AbstractVertex) {
|
|
154
|
+
return this._addVertexOnly(idOrVertex);
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
var newVertex = this.createVertex(idOrVertex, val);
|
|
158
|
+
return this._addVertexOnly(newVertex);
|
|
167
159
|
}
|
|
168
|
-
this._vertices.set(newVertex.id, newVertex);
|
|
169
|
-
return true;
|
|
170
160
|
};
|
|
171
161
|
/**
|
|
172
162
|
* The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
@@ -216,13 +206,25 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
216
206
|
var edge = this.getEdge(v1, v2);
|
|
217
207
|
return !!edge;
|
|
218
208
|
};
|
|
219
|
-
AbstractGraph.prototype.
|
|
220
|
-
if (
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
209
|
+
AbstractGraph.prototype.addEdge = function (srcOrEdge, dest, weight, val) {
|
|
210
|
+
if (srcOrEdge instanceof AbstractEdge) {
|
|
211
|
+
return this._addEdgeOnly(srcOrEdge);
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
if (dest instanceof AbstractVertex || typeof dest === 'string' || typeof dest === 'number') {
|
|
215
|
+
if (!(this.hasVertex(srcOrEdge) && this.hasVertex(dest)))
|
|
216
|
+
return false;
|
|
217
|
+
if (srcOrEdge instanceof AbstractVertex)
|
|
218
|
+
srcOrEdge = srcOrEdge.id;
|
|
219
|
+
if (dest instanceof AbstractVertex)
|
|
220
|
+
dest = dest.id;
|
|
221
|
+
var newEdge = this.createEdge(srcOrEdge, dest, weight, val);
|
|
222
|
+
return this._addEdgeOnly(newEdge);
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
throw new Error('dest must be a Vertex or vertex id while srcOrEdge is an Edge');
|
|
226
|
+
}
|
|
227
|
+
}
|
|
226
228
|
};
|
|
227
229
|
/**
|
|
228
230
|
* The function sets the weight of an edge between two vertices in a graph.
|
|
@@ -891,12 +893,6 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
891
893
|
}
|
|
892
894
|
return { hasNegativeCycle: hasNegativeCycle, distMap: distMap, preMap: preMap, paths: paths, min: min, minPath: minPath };
|
|
893
895
|
};
|
|
894
|
-
/**
|
|
895
|
-
* BellmanFord time:O(VE) space:O(V)
|
|
896
|
-
* one to rest pairs
|
|
897
|
-
* The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
898
|
-
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
899
|
-
*/
|
|
900
896
|
/**
|
|
901
897
|
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
902
898
|
* all pairs
|
|
@@ -939,11 +935,6 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
939
935
|
}
|
|
940
936
|
return { costs: costs, predecessor: predecessor };
|
|
941
937
|
};
|
|
942
|
-
/**
|
|
943
|
-
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
944
|
-
* all pairs
|
|
945
|
-
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
|
|
946
|
-
*/
|
|
947
938
|
/**
|
|
948
939
|
* Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs.
|
|
949
940
|
* Tarjan can find cycles in directed or undirected graph
|
|
@@ -1071,6 +1062,37 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
1071
1062
|
}
|
|
1072
1063
|
return { dfnMap: dfnMap, lowMap: lowMap, bridges: bridges, articulationPoints: articulationPoints, SCCs: SCCs, cycles: cycles };
|
|
1073
1064
|
};
|
|
1065
|
+
/**
|
|
1066
|
+
* Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
|
|
1067
|
+
* Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
|
|
1068
|
+
* The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
|
|
1069
|
+
*/
|
|
1070
|
+
AbstractGraph.prototype._addVertexOnly = function (newVertex) {
|
|
1071
|
+
if (this.hasVertex(newVertex)) {
|
|
1072
|
+
return false;
|
|
1073
|
+
// throw (new Error('Duplicated vertex id is not allowed'));
|
|
1074
|
+
}
|
|
1075
|
+
this._vertices.set(newVertex.id, newVertex);
|
|
1076
|
+
return true;
|
|
1077
|
+
};
|
|
1078
|
+
/**
|
|
1079
|
+
* BellmanFord time:O(VE) space:O(V)
|
|
1080
|
+
* one to rest pairs
|
|
1081
|
+
* The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
1082
|
+
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
1083
|
+
*/
|
|
1084
|
+
AbstractGraph.prototype._getVertex = function (vertexOrId) {
|
|
1085
|
+
var vertexId = this._getVertexId(vertexOrId);
|
|
1086
|
+
return this._vertices.get(vertexId) || null;
|
|
1087
|
+
};
|
|
1088
|
+
/**
|
|
1089
|
+
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
1090
|
+
* all pairs
|
|
1091
|
+
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
|
|
1092
|
+
*/
|
|
1093
|
+
AbstractGraph.prototype._getVertexId = function (vertexOrId) {
|
|
1094
|
+
return vertexOrId instanceof AbstractVertex ? vertexOrId.id : vertexOrId;
|
|
1095
|
+
};
|
|
1074
1096
|
/**--- start find cycles --- */
|
|
1075
1097
|
AbstractGraph.prototype._setVertices = function (value) {
|
|
1076
1098
|
this._vertices = value;
|
|
@@ -44,7 +44,7 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
|
|
|
44
44
|
* @param id
|
|
45
45
|
* @param val
|
|
46
46
|
*/
|
|
47
|
-
|
|
47
|
+
createVertex(id: VertexId, val?: V['val']): V;
|
|
48
48
|
/**
|
|
49
49
|
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
50
50
|
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
@@ -53,7 +53,7 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
|
|
|
53
53
|
* @param weight
|
|
54
54
|
* @param val
|
|
55
55
|
*/
|
|
56
|
-
|
|
56
|
+
createEdge(src: VertexId, dest: VertexId, weight?: number, val?: E['val']): E;
|
|
57
57
|
/**
|
|
58
58
|
* The function `getEdge` returns the directed edge between two vertices, given their source and destination.
|
|
59
59
|
* @param {V | null | VertexId} srcOrId - The source vertex or its ID. It can be either a
|
|
@@ -63,14 +63,6 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
|
|
|
63
63
|
* @returns a E object or null.
|
|
64
64
|
*/
|
|
65
65
|
getEdge(srcOrId: V | null | VertexId, destOrId: V | null | VertexId): E | null;
|
|
66
|
-
/**
|
|
67
|
-
* The `addEdge` function adds a directed edge to a graph if the source and destination vertices exist.
|
|
68
|
-
* @param edge - The parameter `edge` is of type `E`, which represents a directed edge in a graph. It
|
|
69
|
-
* contains two properties:
|
|
70
|
-
* @returns The method `addEdge` returns a boolean value. It returns `true` if the edge was successfully added to the
|
|
71
|
-
* graph, and `false` if either the source or destination vertex of the edge is not present in the graph.
|
|
72
|
-
*/
|
|
73
|
-
addEdge(edge: E): boolean;
|
|
74
66
|
/**
|
|
75
67
|
* The `removeEdgeBetween` function removes an edge between two vertices in a directed graph and returns the removed
|
|
76
68
|
* edge, or null if the edge was not found.
|
|
@@ -81,7 +73,7 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
|
|
|
81
73
|
* @returns The function `removeEdgeBetween` returns the removed edge (`E`) if it exists, or `null` if
|
|
82
74
|
* the edge does not exist.
|
|
83
75
|
*/
|
|
84
|
-
|
|
76
|
+
removeEdgeSrcToDest(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
|
|
85
77
|
/**
|
|
86
78
|
* The `removeEdge` function removes a directed edge from a graph and returns the removed edge, or null if the edge was
|
|
87
79
|
* not found.
|
|
@@ -92,13 +84,14 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
|
|
|
92
84
|
*/
|
|
93
85
|
removeEdge(edge: E): E | null;
|
|
94
86
|
/**
|
|
95
|
-
* The function
|
|
96
|
-
* @param {VertexId | V}
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
87
|
+
* The function removes all edges between two vertices and returns the removed edges.
|
|
88
|
+
* @param {VertexId | V} v1 - The parameter `v1` represents either a `VertexId` or a `V` object. It is used to identify
|
|
89
|
+
* the first vertex in the graph.
|
|
90
|
+
* @param {VertexId | V} v2 - The parameter `v2` represents either a `VertexId` or a `V`. It is used to identify the
|
|
91
|
+
* second vertex involved in the edges that need to be removed.
|
|
92
|
+
* @returns The function `removeEdgesBetween` returns an array of removed edges (`E[]`).
|
|
100
93
|
*/
|
|
101
|
-
|
|
94
|
+
removeEdgesBetween(v1: VertexId | V, v2: VertexId | V): E[];
|
|
102
95
|
/**
|
|
103
96
|
* The function returns an array of incoming edges of a given vertex or vertex ID.
|
|
104
97
|
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a `V`
|
|
@@ -163,18 +156,19 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
|
|
|
163
156
|
*/
|
|
164
157
|
getDestinations(vertex: V | VertexId | null): V[];
|
|
165
158
|
/**
|
|
166
|
-
* The `topologicalSort` function performs a topological sort on a
|
|
167
|
-
*
|
|
168
|
-
* @
|
|
169
|
-
*
|
|
159
|
+
* The `topologicalSort` function performs a topological sort on a graph and returns an array of vertices or vertex IDs
|
|
160
|
+
* in the sorted order, or null if the graph contains a cycle.
|
|
161
|
+
* @param {'vertex' | 'id'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
|
|
162
|
+
* property to use for sorting the vertices. It can have two possible values: 'vertex' or 'id'. If 'vertex' is
|
|
163
|
+
* specified, the vertices themselves will be used for sorting. If 'id' is specified, the ids of
|
|
164
|
+
* @returns an array of vertices or vertex IDs in topological order, or null if there is a cycle in the graph.
|
|
170
165
|
*/
|
|
171
|
-
topologicalSort(): Array<V | VertexId> | null;
|
|
166
|
+
topologicalSort(propertyName?: 'vertex' | 'id'): Array<V | VertexId> | null;
|
|
172
167
|
/**
|
|
173
168
|
* The `edgeSet` function returns an array of all directed edges in the graph.
|
|
174
169
|
* @returns The `edgeSet()` method returns an array of `E` objects.
|
|
175
170
|
*/
|
|
176
171
|
edgeSet(): E[];
|
|
177
|
-
/**--- start find cycles --- */
|
|
178
172
|
/**
|
|
179
173
|
* The function `getNeighbors` returns an array of neighboring vertices of a given vertex in a directed graph.
|
|
180
174
|
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a `V`
|
|
@@ -182,7 +176,7 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
|
|
|
182
176
|
* @returns an array of DirectedVertex objects.
|
|
183
177
|
*/
|
|
184
178
|
getNeighbors(vertexOrId: V | VertexId): V[];
|
|
185
|
-
/**---
|
|
179
|
+
/**--- start find cycles --- */
|
|
186
180
|
/**
|
|
187
181
|
* The function "getEndsOfEdge" returns the source and destination vertices of a directed edge if it exists in the
|
|
188
182
|
* graph, otherwise it returns null.
|
|
@@ -191,6 +185,15 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
|
|
|
191
185
|
* not exist in the graph.
|
|
192
186
|
*/
|
|
193
187
|
getEndsOfEdge(edge: E): [V, V] | null;
|
|
188
|
+
/**--- end find cycles --- */
|
|
189
|
+
/**
|
|
190
|
+
* The `_addEdgeOnly` function adds a directed edge to a graph if the source and destination vertices exist.
|
|
191
|
+
* @param edge - The parameter `edge` is of type `E`, which represents a directed edge in a graph. It
|
|
192
|
+
* contains two properties:
|
|
193
|
+
* @returns The method `_addEdgeOnly` returns a boolean value. It returns `true` if the edge was successfully added to the
|
|
194
|
+
* graph, and `false` if either the source or destination vertex of the edge is not present in the graph.
|
|
195
|
+
*/
|
|
196
|
+
protected _addEdgeOnly(edge: E): boolean;
|
|
194
197
|
protected _setOutEdgeMap(value: Map<V, E[]>): void;
|
|
195
198
|
protected _setInEdgeMap(value: Map<V, E[]>): void;
|
|
196
199
|
}
|
|
@@ -148,7 +148,7 @@ var DirectedGraph = /** @class */ (function (_super) {
|
|
|
148
148
|
* @param id
|
|
149
149
|
* @param val
|
|
150
150
|
*/
|
|
151
|
-
DirectedGraph.prototype.
|
|
151
|
+
DirectedGraph.prototype.createVertex = function (id, val) {
|
|
152
152
|
return new DirectedVertex(id, val !== null && val !== void 0 ? val : id);
|
|
153
153
|
};
|
|
154
154
|
/**
|
|
@@ -159,7 +159,7 @@ var DirectedGraph = /** @class */ (function (_super) {
|
|
|
159
159
|
* @param weight
|
|
160
160
|
* @param val
|
|
161
161
|
*/
|
|
162
|
-
DirectedGraph.prototype.
|
|
162
|
+
DirectedGraph.prototype.createEdge = function (src, dest, weight, val) {
|
|
163
163
|
return new DirectedEdge(src, dest, weight !== null && weight !== void 0 ? weight : 1, val);
|
|
164
164
|
};
|
|
165
165
|
/**
|
|
@@ -184,41 +184,6 @@ var DirectedGraph = /** @class */ (function (_super) {
|
|
|
184
184
|
}
|
|
185
185
|
return edges[0] || null;
|
|
186
186
|
};
|
|
187
|
-
/**
|
|
188
|
-
* The `addEdge` function adds a directed edge to a graph if the source and destination vertices exist.
|
|
189
|
-
* @param edge - The parameter `edge` is of type `E`, which represents a directed edge in a graph. It
|
|
190
|
-
* contains two properties:
|
|
191
|
-
* @returns The method `addEdge` returns a boolean value. It returns `true` if the edge was successfully added to the
|
|
192
|
-
* graph, and `false` if either the source or destination vertex of the edge is not present in the graph.
|
|
193
|
-
*/
|
|
194
|
-
DirectedGraph.prototype.addEdge = function (edge) {
|
|
195
|
-
if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
|
|
196
|
-
return false;
|
|
197
|
-
}
|
|
198
|
-
var srcVertex = this._getVertex(edge.src);
|
|
199
|
-
var destVertex = this._getVertex(edge.dest);
|
|
200
|
-
// TODO after no-non-null-assertion not ensure the logic
|
|
201
|
-
if (srcVertex && destVertex) {
|
|
202
|
-
var srcOutEdges = this._outEdgeMap.get(srcVertex);
|
|
203
|
-
if (srcOutEdges) {
|
|
204
|
-
srcOutEdges.push(edge);
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
this._outEdgeMap.set(srcVertex, [edge]);
|
|
208
|
-
}
|
|
209
|
-
var destInEdges = this._inEdgeMap.get(destVertex);
|
|
210
|
-
if (destInEdges) {
|
|
211
|
-
destInEdges.push(edge);
|
|
212
|
-
}
|
|
213
|
-
else {
|
|
214
|
-
this._inEdgeMap.set(destVertex, [edge]);
|
|
215
|
-
}
|
|
216
|
-
return true;
|
|
217
|
-
}
|
|
218
|
-
else {
|
|
219
|
-
return false;
|
|
220
|
-
}
|
|
221
|
-
};
|
|
222
187
|
/**
|
|
223
188
|
* The `removeEdgeBetween` function removes an edge between two vertices in a directed graph and returns the removed
|
|
224
189
|
* edge, or null if the edge was not found.
|
|
@@ -229,7 +194,7 @@ var DirectedGraph = /** @class */ (function (_super) {
|
|
|
229
194
|
* @returns The function `removeEdgeBetween` returns the removed edge (`E`) if it exists, or `null` if
|
|
230
195
|
* the edge does not exist.
|
|
231
196
|
*/
|
|
232
|
-
DirectedGraph.prototype.
|
|
197
|
+
DirectedGraph.prototype.removeEdgeSrcToDest = function (srcOrId, destOrId) {
|
|
233
198
|
var src = this._getVertex(srcOrId);
|
|
234
199
|
var dest = this._getVertex(destOrId);
|
|
235
200
|
var removed = null;
|
|
@@ -238,14 +203,6 @@ var DirectedGraph = /** @class */ (function (_super) {
|
|
|
238
203
|
}
|
|
239
204
|
var srcOutEdges = this._outEdgeMap.get(src);
|
|
240
205
|
if (srcOutEdges) {
|
|
241
|
-
/**
|
|
242
|
-
* The removeEdge function removes an edge from a graph and returns the removed edge, or null if the edge was not
|
|
243
|
-
* found.
|
|
244
|
-
* @param {E} edge - The `edge` parameter represents the edge that you want to remove from the graph. It should be an
|
|
245
|
-
* object that has `src` and `dest` properties, which represent the source and destination vertices of the edge,
|
|
246
|
-
* respectively.
|
|
247
|
-
* @returns The method `removeEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
|
|
248
|
-
*/
|
|
249
206
|
(0, utils_1.arrayRemove)(srcOutEdges, function (edge) { return edge.dest === dest.id; });
|
|
250
207
|
}
|
|
251
208
|
var destInEdges = this._inEdgeMap.get(dest);
|
|
@@ -279,14 +236,22 @@ var DirectedGraph = /** @class */ (function (_super) {
|
|
|
279
236
|
return removed;
|
|
280
237
|
};
|
|
281
238
|
/**
|
|
282
|
-
* The function
|
|
283
|
-
* @param {VertexId | V}
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
*
|
|
239
|
+
* The function removes all edges between two vertices and returns the removed edges.
|
|
240
|
+
* @param {VertexId | V} v1 - The parameter `v1` represents either a `VertexId` or a `V` object. It is used to identify
|
|
241
|
+
* the first vertex in the graph.
|
|
242
|
+
* @param {VertexId | V} v2 - The parameter `v2` represents either a `VertexId` or a `V`. It is used to identify the
|
|
243
|
+
* second vertex involved in the edges that need to be removed.
|
|
244
|
+
* @returns The function `removeEdgesBetween` returns an array of removed edges (`E[]`).
|
|
287
245
|
*/
|
|
288
|
-
DirectedGraph.prototype.
|
|
289
|
-
|
|
246
|
+
DirectedGraph.prototype.removeEdgesBetween = function (v1, v2) {
|
|
247
|
+
var removed = [];
|
|
248
|
+
if (v1 && v2) {
|
|
249
|
+
var v1ToV2 = this.removeEdgeSrcToDest(v1, v2);
|
|
250
|
+
var v2ToV1 = this.removeEdgeSrcToDest(v2, v1);
|
|
251
|
+
v1ToV2 && removed.push(v1ToV2);
|
|
252
|
+
v2ToV1 && removed.push(v2ToV1);
|
|
253
|
+
}
|
|
254
|
+
return removed;
|
|
290
255
|
};
|
|
291
256
|
/**
|
|
292
257
|
* The function returns an array of incoming edges of a given vertex or vertex ID.
|
|
@@ -400,14 +365,17 @@ var DirectedGraph = /** @class */ (function (_super) {
|
|
|
400
365
|
return destinations;
|
|
401
366
|
};
|
|
402
367
|
/**
|
|
403
|
-
* The `topologicalSort` function performs a topological sort on a
|
|
404
|
-
*
|
|
405
|
-
* @
|
|
406
|
-
*
|
|
368
|
+
* The `topologicalSort` function performs a topological sort on a graph and returns an array of vertices or vertex IDs
|
|
369
|
+
* in the sorted order, or null if the graph contains a cycle.
|
|
370
|
+
* @param {'vertex' | 'id'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
|
|
371
|
+
* property to use for sorting the vertices. It can have two possible values: 'vertex' or 'id'. If 'vertex' is
|
|
372
|
+
* specified, the vertices themselves will be used for sorting. If 'id' is specified, the ids of
|
|
373
|
+
* @returns an array of vertices or vertex IDs in topological order, or null if there is a cycle in the graph.
|
|
407
374
|
*/
|
|
408
|
-
DirectedGraph.prototype.topologicalSort = function () {
|
|
375
|
+
DirectedGraph.prototype.topologicalSort = function (propertyName) {
|
|
409
376
|
var e_2, _a, e_3, _b;
|
|
410
377
|
var _this = this;
|
|
378
|
+
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
411
379
|
// When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
|
|
412
380
|
// When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
|
|
413
381
|
var statusMap = new Map();
|
|
@@ -469,6 +437,8 @@ var DirectedGraph = /** @class */ (function (_super) {
|
|
|
469
437
|
}
|
|
470
438
|
if (hasCycle)
|
|
471
439
|
return null;
|
|
440
|
+
if (propertyName === 'id')
|
|
441
|
+
sorted = sorted.map(function (vertex) { return vertex instanceof DirectedVertex ? vertex.id : vertex; });
|
|
472
442
|
return sorted.reverse();
|
|
473
443
|
};
|
|
474
444
|
/**
|
|
@@ -482,7 +452,6 @@ var DirectedGraph = /** @class */ (function (_super) {
|
|
|
482
452
|
});
|
|
483
453
|
return edges;
|
|
484
454
|
};
|
|
485
|
-
/**--- start find cycles --- */
|
|
486
455
|
/**
|
|
487
456
|
* The function `getNeighbors` returns an array of neighboring vertices of a given vertex in a directed graph.
|
|
488
457
|
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a `V`
|
|
@@ -515,7 +484,7 @@ var DirectedGraph = /** @class */ (function (_super) {
|
|
|
515
484
|
}
|
|
516
485
|
return neighbors;
|
|
517
486
|
};
|
|
518
|
-
/**---
|
|
487
|
+
/**--- start find cycles --- */
|
|
519
488
|
/**
|
|
520
489
|
* The function "getEndsOfEdge" returns the source and destination vertices of a directed edge if it exists in the
|
|
521
490
|
* graph, otherwise it returns null.
|
|
@@ -536,6 +505,42 @@ var DirectedGraph = /** @class */ (function (_super) {
|
|
|
536
505
|
return null;
|
|
537
506
|
}
|
|
538
507
|
};
|
|
508
|
+
/**--- end find cycles --- */
|
|
509
|
+
/**
|
|
510
|
+
* The `_addEdgeOnly` function adds a directed edge to a graph if the source and destination vertices exist.
|
|
511
|
+
* @param edge - The parameter `edge` is of type `E`, which represents a directed edge in a graph. It
|
|
512
|
+
* contains two properties:
|
|
513
|
+
* @returns The method `_addEdgeOnly` returns a boolean value. It returns `true` if the edge was successfully added to the
|
|
514
|
+
* graph, and `false` if either the source or destination vertex of the edge is not present in the graph.
|
|
515
|
+
*/
|
|
516
|
+
DirectedGraph.prototype._addEdgeOnly = function (edge) {
|
|
517
|
+
if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
|
|
518
|
+
return false;
|
|
519
|
+
}
|
|
520
|
+
var srcVertex = this._getVertex(edge.src);
|
|
521
|
+
var destVertex = this._getVertex(edge.dest);
|
|
522
|
+
// TODO after no-non-null-assertion not ensure the logic
|
|
523
|
+
if (srcVertex && destVertex) {
|
|
524
|
+
var srcOutEdges = this._outEdgeMap.get(srcVertex);
|
|
525
|
+
if (srcOutEdges) {
|
|
526
|
+
srcOutEdges.push(edge);
|
|
527
|
+
}
|
|
528
|
+
else {
|
|
529
|
+
this._outEdgeMap.set(srcVertex, [edge]);
|
|
530
|
+
}
|
|
531
|
+
var destInEdges = this._inEdgeMap.get(destVertex);
|
|
532
|
+
if (destInEdges) {
|
|
533
|
+
destInEdges.push(edge);
|
|
534
|
+
}
|
|
535
|
+
else {
|
|
536
|
+
this._inEdgeMap.set(destVertex, [edge]);
|
|
537
|
+
}
|
|
538
|
+
return true;
|
|
539
|
+
}
|
|
540
|
+
else {
|
|
541
|
+
return false;
|
|
542
|
+
}
|
|
543
|
+
};
|
|
539
544
|
DirectedGraph.prototype._setOutEdgeMap = function (value) {
|
|
540
545
|
this._outEdgeMap = value;
|
|
541
546
|
};
|