data-structure-typed 1.18.7 → 1.19.0
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 +208 -419
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +198 -167
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +413 -398
- package/dist/data-structures/binary-tree/avl-tree.d.ts +24 -16
- package/dist/data-structures/binary-tree/avl-tree.js +23 -17
- package/dist/data-structures/binary-tree/binary-tree.d.ts +12 -26
- package/dist/data-structures/binary-tree/binary-tree.js +12 -27
- package/dist/data-structures/binary-tree/bst.d.ts +66 -90
- package/dist/data-structures/binary-tree/bst.js +85 -163
- package/dist/data-structures/binary-tree/rb-tree.d.ts +6 -17
- package/dist/data-structures/binary-tree/rb-tree.js +5 -17
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +187 -18
- package/dist/data-structures/binary-tree/tree-multiset.js +716 -39
- package/dist/data-structures/graph/abstract-graph.d.ts +112 -52
- package/dist/data-structures/graph/abstract-graph.js +138 -72
- package/dist/data-structures/graph/directed-graph.d.ts +104 -101
- package/dist/data-structures/graph/directed-graph.js +134 -129
- package/dist/data-structures/graph/undirected-graph.d.ts +66 -65
- package/dist/data-structures/graph/undirected-graph.js +107 -106
- package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +12 -18
- package/dist/data-structures/interfaces/abstract-graph.d.ts +2 -4
- package/dist/data-structures/interfaces/avl-tree.d.ts +2 -2
- package/dist/data-structures/interfaces/binary-tree.d.ts +1 -1
- package/dist/data-structures/interfaces/bst.d.ts +4 -5
- package/dist/data-structures/interfaces/rb-tree.d.ts +2 -3
- package/dist/data-structures/interfaces/tree-multiset.d.ts +7 -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 +10 -6
- 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 +5 -5
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/types/index.d.ts +1 -0
- package/dist/utils/types/index.js +1 -0
- package/dist/utils/types/validate-type.d.ts +19 -0
- package/dist/utils/types/validate-type.js +2 -0
- package/dist/utils/utils.d.ts +2 -2
- package/dist/utils/utils.js +0 -62
- package/dist/utils/validate-type.d.ts +45 -0
- package/dist/utils/validate-type.js +58 -0
- package/package.json +5 -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 -2451
- package/docs/classes/AVLTreeNode.html +0 -499
- package/docs/classes/AaTree.html +0 -172
- package/docs/classes/AbstractBinaryTree.html +0 -2118
- package/docs/classes/AbstractBinaryTreeNode.html +0 -524
- package/docs/classes/AbstractEdge.html +0 -295
- package/docs/classes/AbstractGraph.html +0 -1043
- package/docs/classes/AbstractVertex.html +0 -258
- package/docs/classes/ArrayDeque.html +0 -439
- package/docs/classes/BST.html +0 -2297
- package/docs/classes/BSTNode.html +0 -503
- package/docs/classes/BTree.html +0 -172
- package/docs/classes/BinaryIndexedTree.html +0 -341
- package/docs/classes/BinaryTree.html +0 -2133
- package/docs/classes/BinaryTreeNode.html +0 -501
- package/docs/classes/Character.html +0 -220
- package/docs/classes/CoordinateMap.html +0 -483
- package/docs/classes/CoordinateSet.html +0 -444
- package/docs/classes/Deque.html +0 -975
- package/docs/classes/DirectedEdge.html +0 -366
- package/docs/classes/DirectedGraph.html +0 -1443
- package/docs/classes/DirectedVertex.html +0 -254
- package/docs/classes/DoublyLinkedList.html +0 -968
- package/docs/classes/DoublyLinkedListNode.html +0 -297
- package/docs/classes/HashTable.html +0 -172
- package/docs/classes/Heap.html +0 -423
- package/docs/classes/HeapItem.html +0 -255
- package/docs/classes/Matrix2D.html +0 -502
- package/docs/classes/MatrixNTI2D.html +0 -240
- package/docs/classes/MaxHeap.html +0 -436
- package/docs/classes/MaxPriorityQueue.html +0 -836
- package/docs/classes/MinHeap.html +0 -437
- package/docs/classes/MinPriorityQueue.html +0 -838
- package/docs/classes/Navigator.html +0 -313
- package/docs/classes/ObjectDeque.html +0 -455
- package/docs/classes/Pair.html +0 -172
- package/docs/classes/PriorityQueue.html +0 -760
- package/docs/classes/Queue.html +0 -392
- package/docs/classes/RBTree.html +0 -2388
- package/docs/classes/RBTreeNode.html +0 -516
- package/docs/classes/SegmentTree.html +0 -434
- package/docs/classes/SegmentTreeNode.html +0 -357
- package/docs/classes/SinglyLinkedList.html +0 -788
- package/docs/classes/SinglyLinkedListNode.html +0 -270
- package/docs/classes/SkipLinkedList.html +0 -172
- package/docs/classes/SplayTree.html +0 -172
- package/docs/classes/Stack.html +0 -368
- package/docs/classes/TreeMap.html +0 -172
- package/docs/classes/TreeMultiSet.html +0 -2297
- package/docs/classes/TreeMultiSetNode.html +0 -499
- package/docs/classes/TreeNode.html +0 -343
- package/docs/classes/TreeSet.html +0 -172
- package/docs/classes/Trie.html +0 -372
- package/docs/classes/TrieNode.html +0 -280
- package/docs/classes/TwoThreeTree.html +0 -172
- package/docs/classes/UndirectedEdge.html +0 -337
- package/docs/classes/UndirectedGraph.html +0 -1210
- package/docs/classes/UndirectedVertex.html +0 -254
- package/docs/classes/Vector2D.html +0 -805
- package/docs/enums/CP.html +0 -181
- package/docs/enums/FamilyPosition.html +0 -181
- package/docs/enums/LoopType.html +0 -182
- package/docs/enums/RBColor.html +0 -174
- package/docs/enums/TopologicalProperty.html +0 -181
- package/docs/index.html +0 -645
- package/docs/interfaces/IAVLTree.html +0 -1378
- package/docs/interfaces/IAVLTreeNode.html +0 -405
- package/docs/interfaces/IAbstractBinaryTree.html +0 -1124
- package/docs/interfaces/IAbstractBinaryTreeNode.html +0 -384
- package/docs/interfaces/IAbstractGraph.html +0 -433
- package/docs/interfaces/IBST.html +0 -1271
- package/docs/interfaces/IBSTNode.html +0 -408
- package/docs/interfaces/IDirectedGraph.html +0 -572
- package/docs/interfaces/IUNDirectedGraph.html +0 -465
- package/docs/modules.html +0 -262
- package/docs/types/AVLTreeOptions.html +0 -150
- package/docs/types/AbstractBinaryTreeNodeProperties.html +0 -152
- package/docs/types/AbstractBinaryTreeNodeProperty.html +0 -152
- package/docs/types/AbstractBinaryTreeOptions.html +0 -156
- package/docs/types/AbstractRecursiveBinaryTreeNode.html +0 -152
- package/docs/types/BSTComparator.html +0 -162
- package/docs/types/BSTOptions.html +0 -152
- package/docs/types/BinaryTreeDeletedResult.html +0 -159
- package/docs/types/BinaryTreeNodeId.html +0 -147
- package/docs/types/BinaryTreeNodePropertyName.html +0 -147
- package/docs/types/BinaryTreeOptions.html +0 -150
- package/docs/types/DFSOrderPattern.html +0 -147
- package/docs/types/DijkstraResult.html +0 -167
- package/docs/types/Direction.html +0 -147
- package/docs/types/EdgeId.html +0 -147
- package/docs/types/HeapOptions.html +0 -168
- package/docs/types/IdObject.html +0 -157
- package/docs/types/KeyValObject.html +0 -152
- package/docs/types/NavigatorParams.html +0 -181
- package/docs/types/NodeOrPropertyName.html +0 -147
- package/docs/types/PriorityQueueComparator.html +0 -167
- package/docs/types/PriorityQueueDFSOrderPattern.html +0 -147
- package/docs/types/PriorityQueueOptions.html +0 -161
- package/docs/types/RBTreeOptions.html +0 -150
- package/docs/types/RecursiveAVLTreeNode.html +0 -152
- package/docs/types/RecursiveBSTNode.html +0 -152
- package/docs/types/RecursiveBinaryTreeNode.html +0 -152
- package/docs/types/RecursiveTreeMultiSetNode.html +0 -152
- package/docs/types/SegmentTreeNodeVal.html +0 -147
- package/docs/types/TopologicalStatus.html +0 -147
- package/docs/types/TreeMultiSetOptions.html +0 -152
- package/docs/types/Turning.html +0 -147
- package/docs/types/VertexId.html +0 -147
- package/notes/bst.test.ts +0 -181
- package/notes/note.md +0 -34
|
@@ -47,6 +47,13 @@ exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
|
|
|
47
47
|
var utils_1 = require("../../utils");
|
|
48
48
|
var priority_queue_1 = require("../priority-queue");
|
|
49
49
|
var AbstractVertex = /** @class */ (function () {
|
|
50
|
+
/**
|
|
51
|
+
* The function is a protected constructor that takes an id and an optional value as parameters.
|
|
52
|
+
* @param {VertexId} id - The `id` parameter is of type `VertexId` and represents the identifier of the vertex. It is
|
|
53
|
+
* used to uniquely identify the vertex object.
|
|
54
|
+
* @param {T} [val] - The parameter "val" is an optional parameter of type T. It is used to assign a value to the
|
|
55
|
+
* vertex. If no value is provided, it will be set to undefined.
|
|
56
|
+
*/
|
|
50
57
|
function AbstractVertex(id, val) {
|
|
51
58
|
this._id = id;
|
|
52
59
|
this._val = val;
|
|
@@ -75,6 +82,15 @@ var AbstractVertex = /** @class */ (function () {
|
|
|
75
82
|
}());
|
|
76
83
|
exports.AbstractVertex = AbstractVertex;
|
|
77
84
|
var AbstractEdge = /** @class */ (function () {
|
|
85
|
+
/**
|
|
86
|
+
* The above function is a protected constructor that initializes the weight, value, and hash code properties of an
|
|
87
|
+
* object.
|
|
88
|
+
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the object. If
|
|
89
|
+
* a value is provided, it will be assigned to the `_weight` property. If no value is provided, the default value of 1
|
|
90
|
+
* will be assigned.
|
|
91
|
+
* @param {T} [val] - The `val` parameter is of type `T`, which means it can be any type. It is an optional parameter,
|
|
92
|
+
* meaning it can be omitted when creating an instance of the class.
|
|
93
|
+
*/
|
|
78
94
|
function AbstractEdge(weight, val) {
|
|
79
95
|
this._weight = weight !== undefined ? weight : 1;
|
|
80
96
|
this._val = val;
|
|
@@ -107,28 +123,24 @@ var AbstractEdge = /** @class */ (function () {
|
|
|
107
123
|
enumerable: false,
|
|
108
124
|
configurable: true
|
|
109
125
|
});
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
126
|
+
/**
|
|
127
|
+
* 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.
|
|
128
|
+
* 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.
|
|
129
|
+
*/
|
|
130
|
+
/**
|
|
131
|
+
* The function sets the value of the _hashCode property to the provided string.
|
|
132
|
+
* @param {string} v - The parameter "v" is of type string and represents the value that will be assigned to the
|
|
133
|
+
* "_hashCode" property.
|
|
134
|
+
*/
|
|
119
135
|
AbstractEdge.prototype._setHashCode = function (v) {
|
|
120
136
|
this._hashCode = v;
|
|
121
137
|
};
|
|
122
138
|
return AbstractEdge;
|
|
123
139
|
}());
|
|
124
140
|
exports.AbstractEdge = AbstractEdge;
|
|
125
|
-
// Connected Component === Largest Connected Sub-Graph
|
|
126
141
|
var AbstractGraph = /** @class */ (function () {
|
|
127
142
|
function AbstractGraph() {
|
|
128
143
|
this._vertices = new Map();
|
|
129
|
-
// unionFind() {}
|
|
130
|
-
/**--- end find cycles --- */
|
|
131
|
-
// Minimum Spanning Tree
|
|
132
144
|
}
|
|
133
145
|
Object.defineProperty(AbstractGraph.prototype, "vertices", {
|
|
134
146
|
get: function () {
|
|
@@ -137,35 +149,39 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
137
149
|
enumerable: false,
|
|
138
150
|
configurable: true
|
|
139
151
|
});
|
|
152
|
+
/**
|
|
153
|
+
* The function "getVertex" returns the vertex with the specified ID or null if it doesn't exist.
|
|
154
|
+
* @param {VertexId} vertexId - The `vertexId` parameter is the identifier of the vertex that you want to retrieve from
|
|
155
|
+
* the `_vertices` map.
|
|
156
|
+
* @returns The method `getVertex` returns the vertex with the specified `vertexId` if it exists in the `_vertices`
|
|
157
|
+
* map. If the vertex does not exist, it returns `null`.
|
|
158
|
+
*/
|
|
140
159
|
AbstractGraph.prototype.getVertex = function (vertexId) {
|
|
141
160
|
return this._vertices.get(vertexId) || null;
|
|
142
161
|
};
|
|
143
162
|
/**
|
|
144
163
|
* The function checks if a vertex exists in a graph.
|
|
145
|
-
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can
|
|
164
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
146
165
|
* (`VertexId`).
|
|
147
|
-
* @returns
|
|
166
|
+
* @returns a boolean value.
|
|
148
167
|
*/
|
|
149
168
|
AbstractGraph.prototype.hasVertex = function (vertexOrId) {
|
|
150
169
|
return this._vertices.has(this._getVertexId(vertexOrId));
|
|
151
170
|
};
|
|
152
|
-
AbstractGraph.prototype.
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
return
|
|
159
|
-
// throw (new Error('Duplicated vertex id is not allowed'));
|
|
171
|
+
AbstractGraph.prototype.addVertex = function (idOrVertex, val) {
|
|
172
|
+
if (idOrVertex instanceof AbstractVertex) {
|
|
173
|
+
return this._addVertexOnly(idOrVertex);
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
var newVertex = this.createVertex(idOrVertex, val);
|
|
177
|
+
return this._addVertexOnly(newVertex);
|
|
160
178
|
}
|
|
161
|
-
this._vertices.set(newVertex.id, newVertex);
|
|
162
|
-
return true;
|
|
163
179
|
};
|
|
164
180
|
/**
|
|
165
181
|
* The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
166
182
|
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
167
183
|
* (`VertexId`).
|
|
168
|
-
* @returns The method
|
|
184
|
+
* @returns The method is returning a boolean value.
|
|
169
185
|
*/
|
|
170
186
|
AbstractGraph.prototype.removeVertex = function (vertexOrId) {
|
|
171
187
|
var vertexId = this._getVertexId(vertexOrId);
|
|
@@ -197,25 +213,36 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
197
213
|
return removed.length > 0;
|
|
198
214
|
};
|
|
199
215
|
/**
|
|
200
|
-
* The function checks if there is an edge between two vertices
|
|
201
|
-
* @param {VertexId | V} v1 - The parameter v1 can be either a VertexId or a V. A VertexId represents the
|
|
202
|
-
* a vertex in a graph, while V represents the type of the vertex itself.
|
|
203
|
-
* @param {VertexId | V} v2 - The parameter `v2` represents the second vertex in
|
|
204
|
-
* or a `V` type.
|
|
205
|
-
* @returns
|
|
206
|
-
* vertices `v1` and `v2`, and `false` otherwise.
|
|
216
|
+
* The function checks if there is an edge between two vertices and returns a boolean value indicating the result.
|
|
217
|
+
* @param {VertexId | V} v1 - The parameter v1 can be either a VertexId or a V. A VertexId represents the unique
|
|
218
|
+
* identifier of a vertex in a graph, while V represents the type of the vertex object itself.
|
|
219
|
+
* @param {VertexId | V} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
|
|
220
|
+
* `VertexId` or a `V` type, which represents the type of the vertex.
|
|
221
|
+
* @returns A boolean value is being returned.
|
|
207
222
|
*/
|
|
208
223
|
AbstractGraph.prototype.hasEdge = function (v1, v2) {
|
|
209
224
|
var edge = this.getEdge(v1, v2);
|
|
210
225
|
return !!edge;
|
|
211
226
|
};
|
|
212
|
-
AbstractGraph.prototype.
|
|
213
|
-
if (
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
227
|
+
AbstractGraph.prototype.addEdge = function (srcOrEdge, dest, weight, val) {
|
|
228
|
+
if (srcOrEdge instanceof AbstractEdge) {
|
|
229
|
+
return this._addEdgeOnly(srcOrEdge);
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
if (dest instanceof AbstractVertex || typeof dest === 'string' || typeof dest === 'number') {
|
|
233
|
+
if (!(this.hasVertex(srcOrEdge) && this.hasVertex(dest)))
|
|
234
|
+
return false;
|
|
235
|
+
if (srcOrEdge instanceof AbstractVertex)
|
|
236
|
+
srcOrEdge = srcOrEdge.id;
|
|
237
|
+
if (dest instanceof AbstractVertex)
|
|
238
|
+
dest = dest.id;
|
|
239
|
+
var newEdge = this.createEdge(srcOrEdge, dest, weight, val);
|
|
240
|
+
return this._addEdgeOnly(newEdge);
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
throw new Error('dest must be a Vertex or vertex id while srcOrEdge is an Edge');
|
|
244
|
+
}
|
|
245
|
+
}
|
|
219
246
|
};
|
|
220
247
|
/**
|
|
221
248
|
* The function sets the weight of an edge between two vertices in a graph.
|
|
@@ -242,10 +269,8 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
242
269
|
* The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
|
|
243
270
|
* @param {V | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
|
|
244
271
|
* It is the starting vertex for finding paths.
|
|
245
|
-
* @param {V | VertexId} v2 - The parameter `v2` represents
|
|
246
|
-
*
|
|
247
|
-
* @returns an array of arrays of vertices (V[][]). Each inner array represents a path between the given vertices (v1
|
|
248
|
-
* and v2).
|
|
272
|
+
* @param {V | VertexId} v2 - The parameter `v2` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
|
|
273
|
+
* @returns The function `getAllPathsBetween` returns an array of arrays of vertices (`V[][]`).
|
|
249
274
|
*/
|
|
250
275
|
AbstractGraph.prototype.getAllPathsBetween = function (v1, v2) {
|
|
251
276
|
var _this = this;
|
|
@@ -303,16 +328,16 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
303
328
|
/**
|
|
304
329
|
* The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
|
|
305
330
|
* weights or using a breadth-first search algorithm.
|
|
306
|
-
* @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex or
|
|
307
|
-
* @param {V | VertexId} v2 - The parameter `v2` represents the
|
|
308
|
-
*
|
|
331
|
+
* @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex or its ID.
|
|
332
|
+
* @param {V | VertexId} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
|
|
333
|
+
* you want to find the minimum cost or weight from the source vertex `v1`.
|
|
309
334
|
* @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edges have weights.
|
|
310
335
|
* If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
|
|
311
336
|
* the edges. If isWeight is set to false or not provided, the function will calculate the
|
|
312
337
|
* @returns The function `getMinCostBetween` returns a number representing the minimum cost between two vertices (`v1`
|
|
313
|
-
* and `v2`)
|
|
314
|
-
* If `isWeight` is `false` or not provided, it
|
|
315
|
-
*
|
|
338
|
+
* and `v2`). If the `isWeight` parameter is `true`, it calculates the minimum weight among all paths between the
|
|
339
|
+
* vertices. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
|
|
340
|
+
* minimum number of
|
|
316
341
|
*/
|
|
317
342
|
AbstractGraph.prototype.getMinCostBetween = function (v1, v2, isWeight) {
|
|
318
343
|
var e_3, _a, e_4, _b;
|
|
@@ -382,14 +407,15 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
382
407
|
/**
|
|
383
408
|
* The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
|
|
384
409
|
* using a breadth-first search algorithm.
|
|
385
|
-
* @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex
|
|
386
|
-
*
|
|
387
|
-
*
|
|
410
|
+
* @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
|
|
411
|
+
* object (`V`) or a vertex ID (`VertexId`).
|
|
412
|
+
* @param {V | VertexId} v2 - V | VertexId - The second vertex or vertex ID between which we want to find the minimum
|
|
413
|
+
* path.
|
|
388
414
|
* @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edges in finding the
|
|
389
415
|
* minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
|
|
390
|
-
* to false, the function will use breadth-first search (BFS) to find the minimum path.
|
|
416
|
+
* to false, the function will use breadth-first search (BFS) to find the minimum path.
|
|
391
417
|
* @returns The function `getMinPathBetween` returns an array of vertices (`V[]`) representing the minimum path between
|
|
392
|
-
* two vertices (`v1` and `v2`). If no path
|
|
418
|
+
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `null`.
|
|
393
419
|
*/
|
|
394
420
|
AbstractGraph.prototype.getMinPathBetween = function (v1, v2, isWeight) {
|
|
395
421
|
var e_5, _a;
|
|
@@ -464,6 +490,10 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
464
490
|
}
|
|
465
491
|
};
|
|
466
492
|
/**
|
|
493
|
+
* Dijkstra algorithm time: O(VE) space: O(V + E)
|
|
494
|
+
* /
|
|
495
|
+
|
|
496
|
+
/**
|
|
467
497
|
* Dijkstra algorithm time: O(VE) space: O(V + E)
|
|
468
498
|
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
|
|
469
499
|
* a graph without using a heap data structure.
|
|
@@ -626,6 +656,14 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
626
656
|
};
|
|
627
657
|
/**
|
|
628
658
|
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
659
|
+
*
|
|
660
|
+
* 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.
|
|
661
|
+
* 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.
|
|
662
|
+
* 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.
|
|
663
|
+
*
|
|
664
|
+
* /
|
|
665
|
+
|
|
666
|
+
/**
|
|
629
667
|
* 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.
|
|
630
668
|
* The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
|
|
631
669
|
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
@@ -772,7 +810,31 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
772
810
|
}
|
|
773
811
|
return { distMap: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
|
|
774
812
|
};
|
|
813
|
+
/**
|
|
814
|
+
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
815
|
+
* /
|
|
816
|
+
|
|
817
|
+
/**
|
|
818
|
+
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
819
|
+
* 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.
|
|
820
|
+
*/
|
|
821
|
+
/**
|
|
822
|
+
* BellmanFord time:O(VE) space:O(V)
|
|
823
|
+
* one to rest pairs
|
|
824
|
+
* 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.
|
|
825
|
+
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
826
|
+
*/
|
|
827
|
+
/**
|
|
828
|
+
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
829
|
+
* all pairs
|
|
830
|
+
* 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.
|
|
831
|
+
*/
|
|
775
832
|
/**
|
|
833
|
+
* BellmanFord time:O(VE) space:O(V)
|
|
834
|
+
* one to rest pairs
|
|
835
|
+
* /
|
|
836
|
+
|
|
837
|
+
/**
|
|
776
838
|
* BellmanFord time:O(VE) space:O(V)
|
|
777
839
|
* one to rest pairs
|
|
778
840
|
* 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.
|
|
@@ -885,11 +947,11 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
885
947
|
return { hasNegativeCycle: hasNegativeCycle, distMap: distMap, preMap: preMap, paths: paths, min: min, minPath: minPath };
|
|
886
948
|
};
|
|
887
949
|
/**
|
|
888
|
-
*
|
|
889
|
-
*
|
|
890
|
-
*
|
|
891
|
-
|
|
892
|
-
|
|
950
|
+
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
951
|
+
* all pairs
|
|
952
|
+
* /
|
|
953
|
+
|
|
954
|
+
/**
|
|
893
955
|
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
894
956
|
* all pairs
|
|
895
957
|
* 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.
|
|
@@ -932,6 +994,14 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
932
994
|
return { costs: costs, predecessor: predecessor };
|
|
933
995
|
};
|
|
934
996
|
/**
|
|
997
|
+
* Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs.
|
|
998
|
+
* Tarjan can find cycles in directed or undirected graph
|
|
999
|
+
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
1000
|
+
* Tarjan solve the bi-connected components of undirected graphs;
|
|
1001
|
+
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
1002
|
+
* /
|
|
1003
|
+
|
|
1004
|
+
/**
|
|
935
1005
|
* Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs.
|
|
936
1006
|
* Tarjan can find cycles in directed or undirected graph
|
|
937
1007
|
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
@@ -1058,25 +1128,21 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
1058
1128
|
}
|
|
1059
1129
|
return { dfnMap: dfnMap, lowMap: lowMap, bridges: bridges, articulationPoints: articulationPoints, SCCs: SCCs, cycles: cycles };
|
|
1060
1130
|
};
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1131
|
+
AbstractGraph.prototype._addVertexOnly = function (newVertex) {
|
|
1132
|
+
if (this.hasVertex(newVertex)) {
|
|
1133
|
+
return false;
|
|
1134
|
+
// throw (new Error('Duplicated vertex id is not allowed'));
|
|
1135
|
+
}
|
|
1136
|
+
this._vertices.set(newVertex.id, newVertex);
|
|
1137
|
+
return true;
|
|
1138
|
+
};
|
|
1067
1139
|
AbstractGraph.prototype._getVertex = function (vertexOrId) {
|
|
1068
1140
|
var vertexId = this._getVertexId(vertexOrId);
|
|
1069
1141
|
return this._vertices.get(vertexId) || null;
|
|
1070
1142
|
};
|
|
1071
|
-
/**
|
|
1072
|
-
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
1073
|
-
* all pairs
|
|
1074
|
-
* 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.
|
|
1075
|
-
*/
|
|
1076
1143
|
AbstractGraph.prototype._getVertexId = function (vertexOrId) {
|
|
1077
1144
|
return vertexOrId instanceof AbstractVertex ? vertexOrId.id : vertexOrId;
|
|
1078
1145
|
};
|
|
1079
|
-
/**--- start find cycles --- */
|
|
1080
1146
|
AbstractGraph.prototype._setVertices = function (value) {
|
|
1081
1147
|
this._vertices = value;
|
|
1082
1148
|
};
|