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,5 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
1
5
|
import {arrayRemove} from '../../utils';
|
|
2
|
-
import {AbstractEdge, AbstractGraph, AbstractVertex
|
|
6
|
+
import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
|
|
7
|
+
import type {IDirectedGraph, TopologicalStatus, VertexId} from '../types';
|
|
3
8
|
|
|
4
9
|
export class DirectedVertex extends AbstractVertex {
|
|
5
10
|
constructor(id: VertexId) {
|
|
@@ -34,25 +39,8 @@ export class DirectedEdge extends AbstractEdge {
|
|
|
34
39
|
}
|
|
35
40
|
}
|
|
36
41
|
|
|
37
|
-
export interface I_DirectedGraph<V, E> {
|
|
38
|
-
incomingEdgesOf(vertex: V): E[];
|
|
39
|
-
|
|
40
|
-
outgoingEdgesOf(vertex: V): E[];
|
|
41
|
-
|
|
42
|
-
inDegreeOf(vertexOrId: V | VertexId): number;
|
|
43
|
-
|
|
44
|
-
outDegreeOf(vertexOrId: V | VertexId): number;
|
|
45
|
-
|
|
46
|
-
getEdgeSrc(e: E): V | null;
|
|
47
|
-
|
|
48
|
-
getEdgeDest(e: E): V | null;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// 0 means unknown, 1 means visiting, 2 means visited;
|
|
52
|
-
export type TopologicalStatus = 0 | 1 | 2;
|
|
53
|
-
|
|
54
42
|
// Strongly connected, One direction connected, Weakly connected
|
|
55
|
-
export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> extends AbstractGraph<V, E> implements
|
|
43
|
+
export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> extends AbstractGraph<V, E> implements IDirectedGraph<V, E> {
|
|
56
44
|
|
|
57
45
|
protected _outEdgeMap: Map<V, E[]> = new Map<V, E[]>();
|
|
58
46
|
|
|
@@ -62,6 +50,14 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
|
|
|
62
50
|
super();
|
|
63
51
|
}
|
|
64
52
|
|
|
53
|
+
/**
|
|
54
|
+
* The function `getEdge` returns the first edge between two vertices, given their source and destination.
|
|
55
|
+
* @param {V | null | VertexId} srcOrId - The `srcOrId` parameter can be either a vertex object (`V`), a vertex ID
|
|
56
|
+
* (`VertexId`), or `null`. It represents the source vertex of the edge.
|
|
57
|
+
* @param {V | null | VertexId} destOrId - The `destOrId` parameter is either a vertex object (`V`), a vertex ID
|
|
58
|
+
* (`VertexId`), or `null`. It represents the destination vertex of the edge.
|
|
59
|
+
* @returns an edge (E) or null.
|
|
60
|
+
*/
|
|
65
61
|
getEdge(srcOrId: V | null | VertexId, destOrId: V | null | VertexId): E | null {
|
|
66
62
|
let edges: E[] = [];
|
|
67
63
|
|
|
@@ -80,6 +76,13 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
|
|
|
80
76
|
return edges[0] || null;
|
|
81
77
|
}
|
|
82
78
|
|
|
79
|
+
/**
|
|
80
|
+
* The `addEdge` function adds an edge to a graph if the source and destination vertices exist.
|
|
81
|
+
* @param {E} edge - The parameter `edge` is of type `E`, which represents an edge in the graph. It contains
|
|
82
|
+
* information about the source vertex (`src`) and the destination vertex (`dest`) of the edge.
|
|
83
|
+
* @returns The `addEdge` function returns a boolean value. It returns `true` if the edge was successfully added to the
|
|
84
|
+
* graph, and `false` if either the source or destination vertices of the edge are not present in the graph.
|
|
85
|
+
*/
|
|
83
86
|
addEdge(edge: E): boolean {
|
|
84
87
|
if (!(this.containsVertex(edge.src) && this.containsVertex(edge.dest))) {
|
|
85
88
|
return false;
|
|
@@ -109,6 +112,16 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
|
|
|
109
112
|
}
|
|
110
113
|
}
|
|
111
114
|
|
|
115
|
+
/**
|
|
116
|
+
* The function removes an edge between two vertices in a directed graph and returns the removed edge.
|
|
117
|
+
* @param {V | VertexId} srcOrId - The source vertex or its ID.
|
|
118
|
+
* @param {V | VertexId} destOrId - The `destOrId` parameter in the `removeEdgeBetween` function represents the
|
|
119
|
+
* destination vertex of the edge that needs to be removed. It can be either a vertex object (`V`) or a vertex ID
|
|
120
|
+
* (`VertexId`).
|
|
121
|
+
* @returns The function `removeEdgeBetween` returns the removed edge (`E`) if the edge between the source and
|
|
122
|
+
* destination vertices is successfully removed. If either the source or destination vertex is not found, or if the
|
|
123
|
+
* edge does not exist, it returns `null`.
|
|
124
|
+
*/
|
|
112
125
|
removeEdgeBetween(srcOrId: V | VertexId, destOrId: V | VertexId): E | null {
|
|
113
126
|
|
|
114
127
|
const src: V | null = this.getVertex(srcOrId);
|
|
@@ -120,29 +133,44 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
|
|
|
120
133
|
|
|
121
134
|
const srcOutEdges = this._outEdgeMap.get(src);
|
|
122
135
|
if (srcOutEdges) {
|
|
123
|
-
|
|
136
|
+
/**
|
|
137
|
+
* The removeEdge function removes an edge from a graph and returns the removed edge, or null if the edge was not
|
|
138
|
+
* found.
|
|
139
|
+
* @param {E} edge - The `edge` parameter represents the edge that you want to remove from the graph. It should be an
|
|
140
|
+
* object that has `src` and `dest` properties, which represent the source and destination vertices of the edge,
|
|
141
|
+
* respectively.
|
|
142
|
+
* @returns The method `removeEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
|
|
143
|
+
*/
|
|
144
|
+
arrayRemove<E>(srcOutEdges, (edge: DirectedEdge) => edge.dest === dest.id);
|
|
124
145
|
}
|
|
125
146
|
|
|
126
147
|
const destInEdges = this._inEdgeMap.get(dest);
|
|
127
148
|
if (destInEdges) {
|
|
128
|
-
removed = arrayRemove<E>(destInEdges, edge => edge.src === src.id)[0] || null;
|
|
149
|
+
removed = arrayRemove<E>(destInEdges, (edge: DirectedEdge) => edge.src === src.id)[0] || null;
|
|
129
150
|
}
|
|
130
151
|
return removed;
|
|
131
152
|
}
|
|
132
153
|
|
|
133
|
-
|
|
154
|
+
/**
|
|
155
|
+
* The removeEdge function removes an edge from a graph and returns the removed edge, or null if the edge was not
|
|
156
|
+
* found.
|
|
157
|
+
* @param {E} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
|
|
158
|
+
* and `dest`, which represent the source and destination vertices of the edge, respectively.
|
|
159
|
+
* @returns The method `removeEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
|
|
160
|
+
*/
|
|
161
|
+
removeEdge(edge: E ): E | null {
|
|
134
162
|
let removed: E | null = null;
|
|
135
163
|
const src = this.getVertex(edge.src);
|
|
136
164
|
const dest = this.getVertex(edge.dest);
|
|
137
165
|
if (src && dest) {
|
|
138
166
|
const srcOutEdges = this._outEdgeMap.get(src);
|
|
139
167
|
if (srcOutEdges && srcOutEdges.length > 0) {
|
|
140
|
-
arrayRemove(srcOutEdges, edge => edge.src === src.id);
|
|
168
|
+
arrayRemove(srcOutEdges, (edge: DirectedEdge) => edge.src === src.id);
|
|
141
169
|
}
|
|
142
170
|
|
|
143
171
|
const destInEdges = this._inEdgeMap.get(dest);
|
|
144
172
|
if (destInEdges && destInEdges.length > 0) {
|
|
145
|
-
removed = arrayRemove(destInEdges, edge => edge.dest === dest.id)[0];
|
|
173
|
+
removed = arrayRemove(destInEdges, (edge: DirectedEdge) => edge.dest === dest.id)[0];
|
|
146
174
|
}
|
|
147
175
|
|
|
148
176
|
}
|
|
@@ -150,10 +178,24 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
|
|
|
150
178
|
return removed;
|
|
151
179
|
}
|
|
152
180
|
|
|
181
|
+
/**
|
|
182
|
+
* The function removeAllEdges removes all edges between two vertices.
|
|
183
|
+
* @param {VertexId | V} src - The `src` parameter represents the source vertex from which the edges will be removed.
|
|
184
|
+
* It can be either a `VertexId` or a `V` type, which represents the identifier or object of the vertex.
|
|
185
|
+
* @param {VertexId | V} dest - The `dest` parameter represents the destination vertex of an edge. It can be either a
|
|
186
|
+
* `VertexId` or a vertex object `V`.
|
|
187
|
+
* @returns An empty array is being returned.
|
|
188
|
+
*/
|
|
153
189
|
removeAllEdges(src: VertexId | V, dest: VertexId | V): E[] {
|
|
154
190
|
return [];
|
|
155
191
|
}
|
|
156
192
|
|
|
193
|
+
/**
|
|
194
|
+
* The function `incomingEdgesOf` returns an array of incoming edges for a given vertex or vertex ID.
|
|
195
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
196
|
+
* (`VertexId`).
|
|
197
|
+
* @returns The method `incomingEdgesOf` returns an array of edges (`E[]`).
|
|
198
|
+
*/
|
|
157
199
|
incomingEdgesOf(vertexOrId: V | VertexId): E[] {
|
|
158
200
|
const target = this.getVertex(vertexOrId);
|
|
159
201
|
if (target) {
|
|
@@ -162,6 +204,12 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
|
|
|
162
204
|
return [];
|
|
163
205
|
}
|
|
164
206
|
|
|
207
|
+
/**
|
|
208
|
+
* The function `outgoingEdgesOf` returns an array of outgoing edges from a given vertex or vertex ID.
|
|
209
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
|
|
210
|
+
* (`VertexId`).
|
|
211
|
+
* @returns The method `outgoingEdgesOf` returns an array of outgoing edges from a given vertex or vertex ID.
|
|
212
|
+
*/
|
|
165
213
|
outgoingEdgesOf(vertexOrId: V | VertexId): E[] {
|
|
166
214
|
const target = this.getVertex(vertexOrId);
|
|
167
215
|
if (target) {
|
|
@@ -170,31 +218,68 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
|
|
|
170
218
|
return [];
|
|
171
219
|
}
|
|
172
220
|
|
|
221
|
+
/**
|
|
222
|
+
* The function "degreeOf" returns the total degree of a vertex, which is the sum of its out-degree and in-degree.
|
|
223
|
+
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
|
|
224
|
+
* @returns the sum of the out-degree and in-degree of the specified vertex or vertex ID.
|
|
225
|
+
*/
|
|
173
226
|
degreeOf(vertexOrId: VertexId | V): number {
|
|
174
227
|
return this.outDegreeOf(vertexOrId) + this.inDegreeOf(vertexOrId);
|
|
175
228
|
}
|
|
176
229
|
|
|
230
|
+
/**
|
|
231
|
+
* The function "inDegreeOf" returns the number of incoming edges for a given vertex.
|
|
232
|
+
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
|
|
233
|
+
* @returns The number of incoming edges of the specified vertex or vertex ID.
|
|
234
|
+
*/
|
|
177
235
|
inDegreeOf(vertexOrId: VertexId | V): number {
|
|
178
236
|
return this.incomingEdgesOf(vertexOrId).length;
|
|
179
237
|
}
|
|
180
238
|
|
|
239
|
+
/**
|
|
240
|
+
* The function `outDegreeOf` returns the number of outgoing edges from a given vertex.
|
|
241
|
+
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
|
|
242
|
+
* @returns The number of outgoing edges from the specified vertex or vertex ID.
|
|
243
|
+
*/
|
|
181
244
|
outDegreeOf(vertexOrId: VertexId | V): number {
|
|
182
245
|
return this.outgoingEdgesOf(vertexOrId).length;
|
|
183
246
|
}
|
|
184
247
|
|
|
248
|
+
/**
|
|
249
|
+
* The function "edgesOf" returns an array of both outgoing and incoming edges of a given vertex or vertex ID.
|
|
250
|
+
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
|
|
251
|
+
* @returns The function `edgesOf` returns an array of edges.
|
|
252
|
+
*/
|
|
185
253
|
edgesOf(vertexOrId: VertexId | V): E[] {
|
|
186
254
|
return [...this.outgoingEdgesOf(vertexOrId), ...this.incomingEdgesOf(vertexOrId)];
|
|
187
255
|
}
|
|
188
256
|
|
|
257
|
+
/**
|
|
258
|
+
* The function "getEdgeSrc" returns the source vertex of an edge, or null if the edge does not exist.
|
|
259
|
+
* @param {E} e - E - an edge object
|
|
260
|
+
* @returns the source vertex of the given edge, or null if the edge does not exist.
|
|
261
|
+
*/
|
|
189
262
|
getEdgeSrc(e: E): V | null {
|
|
190
263
|
return this.getVertex(e.src);
|
|
191
264
|
}
|
|
192
265
|
|
|
266
|
+
/**
|
|
267
|
+
* The function "getEdgeDest" returns the vertex associated with the destination of an edge.
|
|
268
|
+
* @param {E} e - The parameter `e` is of type `E`, which represents an edge in a graph.
|
|
269
|
+
* @returns either a vertex object (of type V) or null.
|
|
270
|
+
*/
|
|
193
271
|
getEdgeDest(e: E): V | null {
|
|
194
272
|
return this.getVertex(e.dest);
|
|
195
273
|
}
|
|
196
274
|
|
|
197
|
-
|
|
275
|
+
/**
|
|
276
|
+
* The function `getDestinations` returns an array of destination vertices connected to a given vertex.
|
|
277
|
+
* @param {V | VertexId | null} vertex - The `vertex` parameter represents the starting vertex from which we want to
|
|
278
|
+
* find the destinations. It can be either a `V` object, a `VertexId` (which is a unique identifier for a vertex), or
|
|
279
|
+
* `null` if we want to find destinations from all vertices.
|
|
280
|
+
* @returns an array of vertices (V[]).
|
|
281
|
+
*/
|
|
282
|
+
getDestinations(vertex: V | VertexId | null): V[] {
|
|
198
283
|
if (vertex === null) {
|
|
199
284
|
return [];
|
|
200
285
|
}
|
|
@@ -214,6 +299,10 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
|
|
|
214
299
|
/**
|
|
215
300
|
* when stored with adjacency list time: O(V+E)
|
|
216
301
|
* when stored with adjacency matrix time: O(V^2)
|
|
302
|
+
* The `topologicalSort` function performs a topological sort on a directed graph and returns the sorted vertices in
|
|
303
|
+
* reverse order, or null if the graph contains a cycle.
|
|
304
|
+
* @returns The function `topologicalSort()` returns an array of vertices in topological order if there is no cycle in
|
|
305
|
+
* the graph. If there is a cycle in the graph, it returns `null`.
|
|
217
306
|
*/
|
|
218
307
|
topologicalSort(): V[] | null {
|
|
219
308
|
// vector<vector<int>> g;
|
|
@@ -252,7 +341,7 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
|
|
|
252
341
|
statusMap.set(entry[1], 0);
|
|
253
342
|
}
|
|
254
343
|
|
|
255
|
-
const sorted: V[] = [];
|
|
344
|
+
const sorted: (V)[] = [];
|
|
256
345
|
let hasCycle = false;
|
|
257
346
|
const dfs = (cur: V) => {
|
|
258
347
|
statusMap.set(cur, 1);
|
|
@@ -283,6 +372,10 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
|
|
|
283
372
|
|
|
284
373
|
/**--- end find cycles --- */
|
|
285
374
|
|
|
375
|
+
/**
|
|
376
|
+
* The `edgeSet` function returns an array of all the edges in the graph.
|
|
377
|
+
* @returns The `edgeSet()` method returns an array of edges (`E[]`).
|
|
378
|
+
*/
|
|
286
379
|
edgeSet(): E[] {
|
|
287
380
|
let edges: E[] = [];
|
|
288
381
|
this._outEdgeMap.forEach(outEdges => {
|
|
@@ -291,6 +384,12 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
|
|
|
291
384
|
return edges;
|
|
292
385
|
}
|
|
293
386
|
|
|
387
|
+
/**
|
|
388
|
+
* The function `getNeighbors` returns an array of neighboring vertices for a given vertex or vertex ID.
|
|
389
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
390
|
+
* (`VertexId`).
|
|
391
|
+
* @returns an array of vertices (V[]).
|
|
392
|
+
*/
|
|
294
393
|
getNeighbors(vertexOrId: V | VertexId): V[] {
|
|
295
394
|
const neighbors: V[] = [];
|
|
296
395
|
const vertex = this.getVertex(vertexOrId);
|
|
@@ -307,6 +406,13 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
|
|
|
307
406
|
return neighbors;
|
|
308
407
|
}
|
|
309
408
|
|
|
409
|
+
/**
|
|
410
|
+
* The function "getEndsOfEdge" returns the source and destination vertices of an edge if it exists in the graph,
|
|
411
|
+
* otherwise it returns null.
|
|
412
|
+
* @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
|
|
413
|
+
* @returns an array containing two vertices [V, V] if the edge is found in the graph. If the edge is not found, it
|
|
414
|
+
* returns null.
|
|
415
|
+
*/
|
|
310
416
|
getEndsOfEdge(edge: E): [V, V] | null {
|
|
311
417
|
if (!this.containsEdge(edge.src, edge.dest)) {
|
|
312
418
|
return null;
|
|
@@ -1,5 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
1
5
|
import {arrayRemove} from '../../utils';
|
|
2
|
-
import {AbstractEdge, AbstractGraph, AbstractVertex
|
|
6
|
+
import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
|
|
7
|
+
import type {VertexId} from '../types';
|
|
3
8
|
|
|
4
9
|
export class UndirectedVertex extends AbstractVertex {
|
|
5
10
|
constructor(id: VertexId) {
|
|
@@ -8,6 +13,11 @@ export class UndirectedVertex extends AbstractVertex {
|
|
|
8
13
|
}
|
|
9
14
|
|
|
10
15
|
export class UndirectedEdge extends AbstractEdge {
|
|
16
|
+
constructor(v1: VertexId, v2: VertexId, weight?: number) {
|
|
17
|
+
super(weight);
|
|
18
|
+
this._vertices = [v1, v2];
|
|
19
|
+
}
|
|
20
|
+
|
|
11
21
|
private _vertices: [VertexId, VertexId];
|
|
12
22
|
|
|
13
23
|
public get vertices() {
|
|
@@ -17,20 +27,23 @@ export class UndirectedEdge extends AbstractEdge {
|
|
|
17
27
|
public set vertices(v: [VertexId, VertexId]) {
|
|
18
28
|
this._vertices = v;
|
|
19
29
|
}
|
|
20
|
-
|
|
21
|
-
constructor(v1: VertexId, v2: VertexId, weight?: number) {
|
|
22
|
-
super(weight);
|
|
23
|
-
this._vertices = [v1, v2];
|
|
24
|
-
}
|
|
25
30
|
}
|
|
26
31
|
|
|
27
32
|
export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdge> extends AbstractGraph<V, E> {
|
|
33
|
+
protected _edges: Map<V, E[]> = new Map();
|
|
34
|
+
|
|
28
35
|
constructor() {
|
|
29
36
|
super();
|
|
30
37
|
}
|
|
31
38
|
|
|
32
|
-
|
|
33
|
-
|
|
39
|
+
/**
|
|
40
|
+
* The function `getEdge` returns the first edge that connects two vertices, or null if no such edge exists.
|
|
41
|
+
* @param {V | null | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID
|
|
42
|
+
* (`VertexId`). It can also be `null`.
|
|
43
|
+
* @param {V | null | VertexId} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `V` (vertex
|
|
44
|
+
* object), `null`, or `VertexId` (vertex ID).
|
|
45
|
+
* @returns an edge (E) or null.
|
|
46
|
+
*/
|
|
34
47
|
getEdge(v1: V | null | VertexId, v2: V | null | VertexId): E | null {
|
|
35
48
|
let edges: E[] | undefined = [];
|
|
36
49
|
|
|
@@ -46,6 +59,11 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
|
|
|
46
59
|
return edges ? edges[0] || null : null;
|
|
47
60
|
}
|
|
48
61
|
|
|
62
|
+
/**
|
|
63
|
+
* The function adds an edge to a graph by connecting two vertices.
|
|
64
|
+
* @param {E} edge - The `edge` parameter is an object of type `E`, which represents an edge in a graph.
|
|
65
|
+
* @returns a boolean value.
|
|
66
|
+
*/
|
|
49
67
|
addEdge(edge: E): boolean {
|
|
50
68
|
for (const end of edge.vertices) {
|
|
51
69
|
const endVertex = this.getVertex(end);
|
|
@@ -62,6 +80,13 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
|
|
|
62
80
|
return true;
|
|
63
81
|
}
|
|
64
82
|
|
|
83
|
+
/**
|
|
84
|
+
* The function removes an edge between two vertices in a graph and returns the removed edge, or null if either of the
|
|
85
|
+
* vertices does not exist.
|
|
86
|
+
* @param {V | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
|
|
87
|
+
* @param {V | VertexId} v2 - V | VertexId: The second vertex or vertex ID of the edge to be removed.
|
|
88
|
+
* @returns the removed edge (E) if it exists, or null if either of the vertices (v1 or v2) does not exist.
|
|
89
|
+
*/
|
|
65
90
|
removeEdgeBetween(v1: V | VertexId, v2: V | VertexId): E | null {
|
|
66
91
|
|
|
67
92
|
const vertex1: V | null = this.getVertex(v1);
|
|
@@ -74,20 +99,31 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
|
|
|
74
99
|
const v1Edges = this._edges.get(vertex1);
|
|
75
100
|
let removed: E | null = null;
|
|
76
101
|
if (v1Edges) {
|
|
77
|
-
removed = arrayRemove<E>(v1Edges, e => e.vertices.includes(vertex2.id))[0] || null;
|
|
102
|
+
removed = arrayRemove<E>(v1Edges, (e: UndirectedEdge) => e.vertices.includes(vertex2.id))[0] || null;
|
|
78
103
|
}
|
|
79
104
|
const v2Edges = this._edges.get(vertex2);
|
|
80
105
|
if (v2Edges) {
|
|
81
|
-
arrayRemove<E>(v2Edges, e => e.vertices.includes(vertex1.id));
|
|
106
|
+
arrayRemove<E>(v2Edges, (e: UndirectedEdge) => e.vertices.includes(vertex1.id));
|
|
82
107
|
}
|
|
83
108
|
return removed;
|
|
84
109
|
}
|
|
85
110
|
|
|
86
|
-
|
|
111
|
+
/**
|
|
112
|
+
* The removeEdge function removes an edge between two vertices in a graph.
|
|
113
|
+
* @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
|
|
114
|
+
* @returns The method is returning either the removed edge (of type E) or null if the edge was not found.
|
|
115
|
+
*/
|
|
87
116
|
removeEdge(edge: E): E | null {
|
|
88
117
|
return this.removeEdgeBetween(edge.vertices[0], edge.vertices[1]);
|
|
89
118
|
}
|
|
90
119
|
|
|
120
|
+
/**
|
|
121
|
+
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edges connected to that
|
|
122
|
+
* vertex.
|
|
123
|
+
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
|
|
124
|
+
* @returns The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of
|
|
125
|
+
* edges that are incident to that vertex.
|
|
126
|
+
*/
|
|
91
127
|
degreeOf(vertexOrId: VertexId | V): number {
|
|
92
128
|
const vertex = this.getVertex(vertexOrId);
|
|
93
129
|
if (vertex) {
|
|
@@ -97,6 +133,13 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
|
|
|
97
133
|
}
|
|
98
134
|
}
|
|
99
135
|
|
|
136
|
+
/**
|
|
137
|
+
* The function "edgesOf" returns an array of edges connected to a given vertex.
|
|
138
|
+
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
|
|
139
|
+
* @returns an array of edges connected to the specified vertex. If the vertex exists in the graph, the function
|
|
140
|
+
* returns the array of edges connected to that vertex. If the vertex does not exist in the graph, the function returns
|
|
141
|
+
* an empty array.
|
|
142
|
+
*/
|
|
100
143
|
edgesOf(vertexOrId: VertexId | V): E[] {
|
|
101
144
|
const vertex = this.getVertex(vertexOrId);
|
|
102
145
|
if (vertex) {
|
|
@@ -106,6 +149,10 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
|
|
|
106
149
|
}
|
|
107
150
|
}
|
|
108
151
|
|
|
152
|
+
/**
|
|
153
|
+
* The function "edgeSet" returns an array of unique edges from a set of edges.
|
|
154
|
+
* @returns The method `edgeSet()` returns an array of type `E[]`.
|
|
155
|
+
*/
|
|
109
156
|
edgeSet(): E[] {
|
|
110
157
|
const edgeSet: Set<E> = new Set();
|
|
111
158
|
this._edges.forEach(edges => {
|
|
@@ -116,6 +163,12 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
|
|
|
116
163
|
return [...edgeSet];
|
|
117
164
|
}
|
|
118
165
|
|
|
166
|
+
/**
|
|
167
|
+
* The function "getEdgesOf" returns an array of edges connected to a given vertex or vertex ID.
|
|
168
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
|
|
169
|
+
* (`VertexId`).
|
|
170
|
+
* @returns an array of edges (E[]) that are connected to the specified vertex or vertex ID.
|
|
171
|
+
*/
|
|
119
172
|
getEdgesOf(vertexOrId: V | VertexId): E[] {
|
|
120
173
|
const vertex = this.getVertex(vertexOrId);
|
|
121
174
|
if (!vertex) {
|
|
@@ -124,6 +177,12 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
|
|
|
124
177
|
return this._edges.get(vertex) || [];
|
|
125
178
|
}
|
|
126
179
|
|
|
180
|
+
/**
|
|
181
|
+
* The function "getNeighbors" returns an array of neighboring vertices of a given vertex.
|
|
182
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
183
|
+
* (`VertexId`).
|
|
184
|
+
* @returns an array of vertices (V[]).
|
|
185
|
+
*/
|
|
127
186
|
getNeighbors(vertexOrId: V | VertexId): V[] {
|
|
128
187
|
const neighbors: V[] = [];
|
|
129
188
|
const vertex = this.getVertex(vertexOrId);
|
|
@@ -139,6 +198,14 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
|
|
|
139
198
|
return neighbors;
|
|
140
199
|
}
|
|
141
200
|
|
|
201
|
+
/**
|
|
202
|
+
* The function "getEndsOfEdge" returns the vertices at the ends of a given edge, or null if the edge does not exist in
|
|
203
|
+
* the graph.
|
|
204
|
+
* @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
|
|
205
|
+
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[V, V]` if the edge exists in the
|
|
206
|
+
* graph and both vertices are found. If the edge does not exist or one or both vertices are not found, it returns
|
|
207
|
+
* `null`.
|
|
208
|
+
*/
|
|
142
209
|
getEndsOfEdge(edge: E): [V, V] | null {
|
|
143
210
|
if (!this.containsEdge(edge.vertices[0], edge.vertices[1])) {
|
|
144
211
|
return null;
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
5
|
+
export class CoordinateMap<V> extends Map<any, V> {
|
|
2
6
|
private readonly _joint: string = '_';
|
|
3
7
|
|
|
4
8
|
constructor(joint?: string) {
|
|
@@ -6,18 +10,46 @@ export class CoordinateSet<V> extends Map<any, V> {
|
|
|
6
10
|
if (joint !== undefined) this._joint = joint;
|
|
7
11
|
}
|
|
8
12
|
|
|
13
|
+
/**
|
|
14
|
+
* The "has" function overrides the base class's "has" function and checks if a key exists in the map by joining the
|
|
15
|
+
* key array with a specified delimiter.
|
|
16
|
+
* @param {number[]} key - The parameter "key" is an array of numbers.
|
|
17
|
+
* @returns The `has` method is being overridden to return the result of calling the `has` method of the superclass
|
|
18
|
+
* (`super.has`) with the `key` array joined together using the `_joint` property.
|
|
19
|
+
*/
|
|
9
20
|
override has(key: number[]) {
|
|
10
21
|
return super.has(key.join(this._joint));
|
|
11
22
|
}
|
|
12
23
|
|
|
24
|
+
/**
|
|
25
|
+
* The function overrides the set method of a Map object to convert the key from an array to a string using a specified
|
|
26
|
+
* delimiter before calling the original set method.
|
|
27
|
+
* @param {number[]} key - The key parameter is an array of numbers.
|
|
28
|
+
* @param {V} value - The value parameter is the value that you want to associate with the specified key.
|
|
29
|
+
* @returns The `set` method is returning the result of calling the `set` method of the superclass
|
|
30
|
+
* (`super.set(key.join(this._joint), value)`).
|
|
31
|
+
*/
|
|
13
32
|
override set(key: number[], value: V) {
|
|
14
33
|
return super.set(key.join(this._joint), value);
|
|
15
34
|
}
|
|
16
35
|
|
|
36
|
+
/**
|
|
37
|
+
* The function overrides the get method to join the key array with a specified joint and then calls the super get
|
|
38
|
+
* method.
|
|
39
|
+
* @param {number[]} key - An array of numbers
|
|
40
|
+
* @returns The code is returning the value associated with the specified key in the map.
|
|
41
|
+
*/
|
|
17
42
|
override get(key: number[]) {
|
|
18
43
|
return super.get(key.join(this._joint));
|
|
19
44
|
}
|
|
20
45
|
|
|
46
|
+
/**
|
|
47
|
+
* The function overrides the delete method and joins the key array using a specified joint character before calling
|
|
48
|
+
* the super delete method.
|
|
49
|
+
* @param {number[]} key - An array of numbers that represents the key to be deleted.
|
|
50
|
+
* @returns The `delete` method is returning the result of calling the `delete` method on the superclass, with the
|
|
51
|
+
* `key` array joined together using the `_joint` property.
|
|
52
|
+
*/
|
|
21
53
|
override delete(key: number[]) {
|
|
22
54
|
return super.delete(key.join(this._joint));
|
|
23
55
|
}
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
1
5
|
export class CoordinateSet extends Set {
|
|
2
6
|
private readonly _joint: string = '_';
|
|
3
7
|
|
|
@@ -6,14 +10,35 @@ export class CoordinateSet extends Set {
|
|
|
6
10
|
if (joint !== undefined) this._joint = joint;
|
|
7
11
|
}
|
|
8
12
|
|
|
13
|
+
/**
|
|
14
|
+
* The "has" function overrides the "has" method of the superclass and checks if a value exists in an array after
|
|
15
|
+
* joining its elements with a specified separator.
|
|
16
|
+
* @param {number[]} value - The parameter "value" is an array of numbers.
|
|
17
|
+
* @returns The overridden `has` method is returning the result of calling the `has` method of the superclass, passing
|
|
18
|
+
* in the joined value as an argument.
|
|
19
|
+
*/
|
|
9
20
|
override has(value: number[]) {
|
|
10
21
|
return super.has(value.join(this._joint));
|
|
11
22
|
}
|
|
12
23
|
|
|
24
|
+
/**
|
|
25
|
+
* The "add" function overrides the parent class's "add" function by joining the elements of the input array with a
|
|
26
|
+
* specified delimiter before calling the parent class's "add" function.
|
|
27
|
+
* @param {number[]} value - An array of numbers
|
|
28
|
+
* @returns The overridden `add` method is returning the result of calling the `add` method of the superclass
|
|
29
|
+
* (`super.add`) with the joined string representation of the `value` array (`value.join(this._joint)`).
|
|
30
|
+
*/
|
|
13
31
|
override add(value: number[]) {
|
|
14
32
|
return super.add(value.join(this._joint));
|
|
15
33
|
}
|
|
16
34
|
|
|
35
|
+
/**
|
|
36
|
+
* The function overrides the delete method and deletes an element from a Set by joining the elements of the input
|
|
37
|
+
* array with a specified joint and then calling the delete method of the parent class.
|
|
38
|
+
* @param {number[]} value - An array of numbers
|
|
39
|
+
* @returns The `delete` method is returning the result of calling the `delete` method of the superclass, with the
|
|
40
|
+
* `value` array joined together using the `_joint` property.
|
|
41
|
+
*/
|
|
17
42
|
override delete(value: number[]) {
|
|
18
43
|
return super.delete(value.join(this._joint));
|
|
19
44
|
}
|