data-structure-typed 1.21.4 → 1.31.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/.auto-changelog +9 -0
- package/.auto-changelog-template.hbs +36 -0
- package/.eslintrc.js +61 -0
- package/.github/workflows/ci.yml +30 -0
- package/.prettierignore +6 -0
- package/.prettierrc.js +16 -0
- package/CHANGELOG.md +17 -0
- package/README.md +147 -95
- package/dist/data-structures/binary-tree/aa-tree.js +6 -2
- package/dist/data-structures/binary-tree/aa-tree.js.map +1 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +398 -672
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -0
- package/dist/data-structures/binary-tree/avl-tree.js +122 -151
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -0
- package/dist/data-structures/binary-tree/b-tree.js +6 -2
- package/dist/data-structures/binary-tree/b-tree.js.map +1 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +24 -54
- package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -0
- package/dist/data-structures/binary-tree/binary-tree.js +31 -32
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -0
- package/dist/data-structures/binary-tree/bst.js +266 -208
- package/dist/data-structures/binary-tree/bst.js.map +1 -0
- package/dist/data-structures/binary-tree/index.js +1 -0
- package/dist/data-structures/binary-tree/index.js.map +1 -0
- package/dist/data-structures/binary-tree/rb-tree.js +46 -37
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -0
- package/dist/data-structures/binary-tree/segment-tree.js +124 -127
- package/dist/data-structures/binary-tree/segment-tree.js.map +1 -0
- package/dist/data-structures/binary-tree/splay-tree.js +6 -2
- package/dist/data-structures/binary-tree/splay-tree.js.map +1 -0
- package/dist/data-structures/binary-tree/tree-multiset.js +227 -354
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -0
- package/dist/data-structures/binary-tree/two-three-tree.js +6 -2
- package/dist/data-structures/binary-tree/two-three-tree.js.map +1 -0
- package/dist/data-structures/graph/abstract-graph.js +582 -609
- package/dist/data-structures/graph/abstract-graph.js.map +1 -0
- package/dist/data-structures/graph/directed-graph.js +276 -312
- package/dist/data-structures/graph/directed-graph.js.map +1 -0
- package/dist/data-structures/graph/index.js +1 -0
- package/dist/data-structures/graph/index.js.map +1 -0
- package/dist/data-structures/graph/map-graph.js +88 -100
- package/dist/data-structures/graph/map-graph.js.map +1 -0
- package/dist/data-structures/graph/undirected-graph.js +180 -195
- package/dist/data-structures/graph/undirected-graph.js.map +1 -0
- package/dist/data-structures/hash/coordinate-map.js +46 -58
- package/dist/data-structures/hash/coordinate-map.js.map +1 -0
- package/dist/data-structures/hash/coordinate-set.js +43 -48
- package/dist/data-structures/hash/coordinate-set.js.map +1 -0
- package/dist/data-structures/hash/hash-table.js +6 -2
- package/dist/data-structures/hash/hash-table.js.map +1 -0
- package/dist/data-structures/hash/index.js +1 -0
- package/dist/data-structures/hash/index.js.map +1 -0
- package/dist/data-structures/hash/pair.js +6 -2
- package/dist/data-structures/hash/pair.js.map +1 -0
- package/dist/data-structures/hash/tree-map.js +6 -2
- package/dist/data-structures/hash/tree-map.js.map +1 -0
- package/dist/data-structures/hash/tree-set.js +6 -2
- package/dist/data-structures/hash/tree-set.js.map +1 -0
- package/dist/data-structures/heap/heap.js +83 -120
- package/dist/data-structures/heap/heap.js.map +1 -0
- package/dist/data-structures/heap/index.js +1 -0
- package/dist/data-structures/heap/index.js.map +1 -0
- package/dist/data-structures/heap/max-heap.js +27 -24
- package/dist/data-structures/heap/max-heap.js.map +1 -0
- package/dist/data-structures/heap/min-heap.js +27 -25
- package/dist/data-structures/heap/min-heap.js.map +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/index.js.map +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +202 -307
- package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -0
- package/dist/data-structures/linked-list/index.js +1 -0
- package/dist/data-structures/linked-list/index.js.map +1 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +223 -251
- package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +6 -2
- package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -0
- package/dist/data-structures/matrix/index.js +1 -0
- package/dist/data-structures/matrix/index.js.map +1 -0
- package/dist/data-structures/matrix/matrix.js +9 -22
- package/dist/data-structures/matrix/matrix.js.map +1 -0
- package/dist/data-structures/matrix/matrix2d.js +75 -149
- package/dist/data-structures/matrix/matrix2d.js.map +1 -0
- package/dist/data-structures/matrix/navigator.js +38 -46
- package/dist/data-structures/matrix/navigator.js.map +1 -0
- package/dist/data-structures/matrix/vector2d.js +90 -254
- package/dist/data-structures/matrix/vector2d.js.map +1 -0
- package/dist/data-structures/priority-queue/index.js +1 -0
- package/dist/data-structures/priority-queue/index.js.map +1 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +47 -32
- package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +47 -33
- package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -0
- package/dist/data-structures/priority-queue/priority-queue.js +150 -241
- package/dist/data-structures/priority-queue/priority-queue.js.map +1 -0
- package/dist/data-structures/queue/deque.js +130 -162
- package/dist/data-structures/queue/deque.js.map +1 -0
- package/dist/data-structures/queue/index.js +1 -0
- package/dist/data-structures/queue/index.js.map +1 -0
- package/dist/data-structures/queue/queue.js +181 -100
- package/dist/data-structures/queue/queue.js.map +1 -0
- package/dist/data-structures/stack/index.js +1 -0
- package/dist/data-structures/stack/index.js.map +1 -0
- package/dist/data-structures/stack/stack.js +23 -71
- package/dist/data-structures/stack/stack.js.map +1 -0
- package/dist/data-structures/tree/index.js +1 -0
- package/dist/data-structures/tree/index.js.map +1 -0
- package/dist/data-structures/tree/tree.js +46 -33
- package/dist/data-structures/tree/tree.js.map +1 -0
- package/dist/data-structures/trie/index.js +1 -0
- package/dist/data-structures/trie/index.js.map +1 -0
- package/dist/data-structures/trie/trie.js +201 -129
- package/dist/data-structures/trie/trie.js.map +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces/abstract-binary-tree.js +1 -0
- package/dist/interfaces/abstract-binary-tree.js.map +1 -0
- package/dist/interfaces/abstract-graph.js +1 -0
- package/dist/interfaces/abstract-graph.js.map +1 -0
- package/dist/interfaces/avl-tree.js +1 -0
- package/dist/interfaces/avl-tree.js.map +1 -0
- package/dist/interfaces/binary-tree.js +1 -0
- package/dist/interfaces/binary-tree.js.map +1 -0
- package/dist/interfaces/bst.js +1 -0
- package/dist/interfaces/bst.js.map +1 -0
- package/dist/interfaces/directed-graph.js +1 -0
- package/dist/interfaces/directed-graph.js.map +1 -0
- package/dist/interfaces/doubly-linked-list.js +1 -0
- package/dist/interfaces/doubly-linked-list.js.map +1 -0
- package/dist/interfaces/heap.js +1 -0
- package/dist/interfaces/heap.js.map +1 -0
- package/dist/interfaces/index.js +1 -0
- package/dist/interfaces/index.js.map +1 -0
- package/dist/interfaces/navigator.js +1 -0
- package/dist/interfaces/navigator.js.map +1 -0
- package/dist/interfaces/priority-queue.js +1 -0
- package/dist/interfaces/priority-queue.js.map +1 -0
- package/dist/interfaces/rb-tree.js +1 -0
- package/dist/interfaces/rb-tree.js.map +1 -0
- package/dist/interfaces/segment-tree.js +1 -0
- package/dist/interfaces/segment-tree.js.map +1 -0
- package/dist/interfaces/singly-linked-list.js +1 -0
- package/dist/interfaces/singly-linked-list.js.map +1 -0
- package/dist/interfaces/tree-multiset.js +1 -0
- package/dist/interfaces/tree-multiset.js.map +1 -0
- package/dist/interfaces/undirected-graph.js +1 -0
- package/dist/interfaces/undirected-graph.js.map +1 -0
- package/dist/types/data-structures/abstract-binary-tree.js +1 -7
- package/dist/types/data-structures/abstract-binary-tree.js.map +1 -0
- package/dist/types/data-structures/abstract-graph.js +1 -0
- package/dist/types/data-structures/abstract-graph.js.map +1 -0
- package/dist/types/data-structures/avl-tree.js +1 -0
- package/dist/types/data-structures/avl-tree.js.map +1 -0
- package/dist/types/data-structures/binary-tree.js +1 -0
- package/dist/types/data-structures/binary-tree.js.map +1 -0
- package/dist/types/data-structures/bst.js +1 -0
- package/dist/types/data-structures/bst.js.map +1 -0
- package/dist/types/data-structures/directed-graph.js +1 -0
- package/dist/types/data-structures/directed-graph.js.map +1 -0
- package/dist/types/data-structures/doubly-linked-list.js +1 -0
- package/dist/types/data-structures/doubly-linked-list.js.map +1 -0
- package/dist/types/data-structures/heap.js +1 -0
- package/dist/types/data-structures/heap.js.map +1 -0
- package/dist/types/data-structures/index.js +1 -0
- package/dist/types/data-structures/index.js.map +1 -0
- package/dist/types/data-structures/map-graph.js +1 -0
- package/dist/types/data-structures/map-graph.js.map +1 -0
- package/dist/types/data-structures/navigator.js +1 -0
- package/dist/types/data-structures/navigator.js.map +1 -0
- package/dist/types/data-structures/priority-queue.js +1 -0
- package/dist/types/data-structures/priority-queue.js.map +1 -0
- package/dist/types/data-structures/rb-tree.js +1 -0
- package/dist/types/data-structures/rb-tree.js.map +1 -0
- package/dist/types/data-structures/segment-tree.js +1 -0
- package/dist/types/data-structures/segment-tree.js.map +1 -0
- package/dist/types/data-structures/singly-linked-list.js +1 -0
- package/dist/types/data-structures/singly-linked-list.js.map +1 -0
- package/dist/types/data-structures/tree-multiset.js +1 -0
- package/dist/types/data-structures/tree-multiset.js.map +1 -0
- package/dist/types/helpers.js +1 -0
- package/dist/types/helpers.js.map +1 -0
- package/dist/types/index.js +1 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/utils/index.js +1 -0
- package/dist/types/utils/index.js.map +1 -0
- package/dist/types/utils/utils.js +1 -0
- package/dist/types/utils/utils.js.map +1 -0
- package/dist/types/utils/validate-type.js +1 -0
- package/dist/types/utils/validate-type.js.map +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/utils.js +108 -22
- package/dist/utils/utils.js.map +1 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +127 -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 +2259 -0
- package/docs/classes/AVLTreeNode.html +399 -0
- package/docs/classes/AaTree.html +202 -0
- package/docs/classes/AbstractBinaryTree.html +1913 -0
- package/docs/classes/AbstractBinaryTreeNode.html +441 -0
- package/docs/classes/AbstractEdge.html +345 -0
- package/docs/classes/AbstractGraph.html +1105 -0
- package/docs/classes/AbstractVertex.html +299 -0
- package/docs/classes/ArrayDeque.html +469 -0
- package/docs/classes/BST.html +2103 -0
- package/docs/classes/BSTNode.html +400 -0
- package/docs/classes/BTree.html +202 -0
- package/docs/classes/BinaryIndexedTree.html +371 -0
- package/docs/classes/BinaryTree.html +1944 -0
- package/docs/classes/BinaryTreeNode.html +399 -0
- package/docs/classes/Character.html +250 -0
- package/docs/classes/CoordinateMap.html +513 -0
- package/docs/classes/CoordinateSet.html +474 -0
- package/docs/classes/Deque.html +1005 -0
- package/docs/classes/DirectedEdge.html +404 -0
- package/docs/classes/DirectedGraph.html +1530 -0
- package/docs/classes/DirectedVertex.html +286 -0
- package/docs/classes/DoublyLinkedList.html +998 -0
- package/docs/classes/DoublyLinkedListNode.html +327 -0
- package/docs/classes/HashTable.html +202 -0
- package/docs/classes/Heap.html +647 -0
- package/docs/classes/HeapItem.html +296 -0
- package/docs/classes/LinkedListQueue.html +884 -0
- package/docs/classes/MapEdge.html +391 -0
- package/docs/classes/MapGraph.html +1583 -0
- package/docs/classes/MapVertex.html +356 -0
- package/docs/classes/Matrix2D.html +532 -0
- package/docs/classes/MatrixNTI2D.html +270 -0
- package/docs/classes/MaxHeap.html +671 -0
- package/docs/classes/MaxPriorityQueue.html +866 -0
- package/docs/classes/MinHeap.html +672 -0
- package/docs/classes/MinPriorityQueue.html +868 -0
- package/docs/classes/Navigator.html +343 -0
- package/docs/classes/ObjectDeque.html +527 -0
- package/docs/classes/Pair.html +202 -0
- package/docs/classes/PriorityQueue.html +790 -0
- package/docs/classes/Queue.html +521 -0
- package/docs/classes/RBTree.html +2101 -0
- package/docs/classes/RBTreeNode.html +431 -0
- package/docs/classes/SegmentTree.html +464 -0
- package/docs/classes/SegmentTreeNode.html +387 -0
- package/docs/classes/SinglyLinkedList.html +830 -0
- package/docs/classes/SinglyLinkedListNode.html +300 -0
- package/docs/classes/SkipLinkedList.html +202 -0
- package/docs/classes/SplayTree.html +202 -0
- package/docs/classes/Stack.html +398 -0
- package/docs/classes/TreeMap.html +202 -0
- package/docs/classes/TreeMultiset.html +2587 -0
- package/docs/classes/TreeMultisetNode.html +447 -0
- package/docs/classes/TreeNode.html +344 -0
- package/docs/classes/TreeSet.html +202 -0
- package/docs/classes/Trie.html +402 -0
- package/docs/classes/TrieNode.html +310 -0
- package/docs/classes/TwoThreeTree.html +202 -0
- package/docs/classes/UndirectedEdge.html +374 -0
- package/docs/classes/UndirectedGraph.html +1285 -0
- package/docs/classes/UndirectedVertex.html +284 -0
- package/docs/classes/Vector2D.html +835 -0
- package/docs/enums/CP.html +211 -0
- package/docs/enums/FamilyPosition.html +239 -0
- package/docs/enums/LoopType.html +212 -0
- package/docs/enums/RBColor.html +204 -0
- package/docs/enums/TopologicalProperty.html +211 -0
- package/docs/functions/arrayRemove.html +208 -0
- package/docs/functions/isThunk.html +186 -0
- package/docs/functions/toThunk.html +186 -0
- package/docs/functions/trampoline.html +186 -0
- package/docs/functions/trampolineAsync.html +186 -0
- package/docs/functions/uuidV4.html +181 -0
- package/docs/index.html +693 -0
- package/docs/interfaces/IAVLTree.html +1245 -0
- package/docs/interfaces/IAbstractBinaryTree.html +1101 -0
- package/docs/interfaces/IAbstractBinaryTreeNode.html +335 -0
- package/docs/interfaces/IAbstractGraph.html +433 -0
- package/docs/interfaces/IBST.html +1245 -0
- package/docs/interfaces/IDirectedGraph.html +570 -0
- package/docs/interfaces/IRBTree.html +1247 -0
- package/docs/interfaces/IUNDirectedGraph.html +463 -0
- package/docs/modules.html +328 -0
- package/docs/types/AVLTreeNodeNested.html +182 -0
- package/docs/types/AVLTreeOptions.html +180 -0
- package/docs/types/AbstractBinaryTreeNodeNested.html +182 -0
- package/docs/types/AbstractBinaryTreeNodeProperties.html +182 -0
- package/docs/types/AbstractBinaryTreeNodeProperty.html +182 -0
- package/docs/types/AbstractBinaryTreeOptions.html +182 -0
- package/docs/types/BSTComparator.html +192 -0
- package/docs/types/BSTNodeNested.html +182 -0
- package/docs/types/BSTOptions.html +182 -0
- package/docs/types/BinaryTreeDeletedResult.html +189 -0
- package/docs/types/BinaryTreeNodeId.html +177 -0
- package/docs/types/BinaryTreeNodeNested.html +182 -0
- package/docs/types/BinaryTreeNodePropertyName.html +177 -0
- package/docs/types/BinaryTreeOptions.html +180 -0
- package/docs/types/DFSOrderPattern.html +177 -0
- package/docs/types/DijkstraResult.html +199 -0
- package/docs/types/Direction.html +177 -0
- package/docs/types/DummyAny.html +190 -0
- package/docs/types/EdgeId.html +177 -0
- package/docs/types/HeapOptions.html +198 -0
- package/docs/types/IAVLTreeNode.html +184 -0
- package/docs/types/IBSTNode.html +184 -0
- package/docs/types/IBinaryTree.html +182 -0
- package/docs/types/IBinaryTreeNode.html +184 -0
- package/docs/types/IRBTreeNode.html +184 -0
- package/docs/types/ITreeMultiset.html +182 -0
- package/docs/types/ITreeMultisetNode.html +184 -0
- package/docs/types/KeyValueObject.html +182 -0
- package/docs/types/KeyValueObjectWithId.html +184 -0
- package/docs/types/MapGraphCoordinate.html +177 -0
- package/docs/types/NavigatorParams.html +211 -0
- package/docs/types/NodeOrPropertyName.html +177 -0
- package/docs/types/NonNumberNonObjectButDefined.html +177 -0
- package/docs/types/ObjectWithNonNumberId.html +184 -0
- package/docs/types/ObjectWithNumberId.html +184 -0
- package/docs/types/ObjectWithoutId.html +177 -0
- package/docs/types/PriorityQueueComparator.html +197 -0
- package/docs/types/PriorityQueueDFSOrderPattern.html +177 -0
- package/docs/types/PriorityQueueOptions.html +191 -0
- package/docs/types/RBTreeNodeNested.html +182 -0
- package/docs/types/RBTreeOptions.html +180 -0
- package/docs/types/RestrictValById.html +177 -0
- package/docs/types/SegmentTreeNodeVal.html +177 -0
- package/docs/types/SpecifyOptional.html +184 -0
- package/docs/types/Thunk.html +185 -0
- package/docs/types/ToThunkFn.html +185 -0
- package/docs/types/TopologicalStatus.html +177 -0
- package/docs/types/TreeMultisetNodeNested.html +182 -0
- package/docs/types/TreeMultisetOptions.html +180 -0
- package/docs/types/TrlAsyncFn.html +190 -0
- package/docs/types/TrlFn.html +190 -0
- package/docs/types/Turning.html +177 -0
- package/docs/types/VertexId.html +177 -0
- package/docs/variables/THUNK_SYMBOL.html +177 -0
- package/jest.config.js +5 -0
- package/lib/data-structures/binary-tree/aa-tree.js +2 -0
- package/{dist → lib}/data-structures/binary-tree/abstract-binary-tree.d.ts +25 -20
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +1307 -0
- package/{dist → lib}/data-structures/binary-tree/avl-tree.d.ts +12 -12
- package/lib/data-structures/binary-tree/avl-tree.js +311 -0
- package/lib/data-structures/binary-tree/b-tree.js +2 -0
- package/lib/data-structures/binary-tree/binary-indexed-tree.js +69 -0
- package/lib/data-structures/binary-tree/binary-tree.js +35 -0
- package/{dist → lib}/data-structures/binary-tree/bst.d.ts +19 -8
- package/lib/data-structures/binary-tree/bst.js +551 -0
- package/lib/data-structures/binary-tree/index.js +12 -0
- package/{dist → lib}/data-structures/binary-tree/rb-tree.d.ts +0 -6
- package/lib/data-structures/binary-tree/rb-tree.js +22 -0
- package/lib/data-structures/binary-tree/segment-tree.js +210 -0
- package/lib/data-structures/binary-tree/splay-tree.js +2 -0
- package/{dist → lib}/data-structures/binary-tree/tree-multiset.d.ts +20 -23
- package/lib/data-structures/binary-tree/tree-multiset.js +673 -0
- package/lib/data-structures/binary-tree/two-three-tree.js +2 -0
- package/lib/data-structures/graph/abstract-graph.js +918 -0
- package/lib/data-structures/graph/directed-graph.js +416 -0
- package/lib/data-structures/graph/index.js +4 -0
- package/lib/data-structures/graph/map-graph.js +105 -0
- package/lib/data-structures/graph/undirected-graph.js +246 -0
- package/lib/data-structures/hash/coordinate-map.js +61 -0
- package/lib/data-structures/hash/coordinate-set.js +51 -0
- package/lib/data-structures/hash/hash-table.js +2 -0
- package/lib/data-structures/hash/index.js +6 -0
- package/lib/data-structures/hash/pair.js +2 -0
- package/lib/data-structures/hash/tree-map.js +2 -0
- package/lib/data-structures/hash/tree-set.js +2 -0
- package/lib/data-structures/heap/heap.js +152 -0
- package/lib/data-structures/heap/index.js +3 -0
- package/lib/data-structures/heap/max-heap.js +26 -0
- package/lib/data-structures/heap/min-heap.js +27 -0
- package/lib/data-structures/index.js +11 -0
- package/{dist → lib}/data-structures/linked-list/doubly-linked-list.d.ts +5 -5
- package/lib/data-structures/linked-list/doubly-linked-list.js +521 -0
- package/lib/data-structures/linked-list/index.js +3 -0
- package/{dist → lib}/data-structures/linked-list/singly-linked-list.d.ts +6 -5
- package/lib/data-structures/linked-list/singly-linked-list.js +443 -0
- package/lib/data-structures/linked-list/skip-linked-list.js +2 -0
- package/lib/data-structures/matrix/index.js +4 -0
- package/lib/data-structures/matrix/matrix.js +24 -0
- package/lib/data-structures/matrix/matrix2d.js +195 -0
- package/lib/data-structures/matrix/navigator.js +101 -0
- package/lib/data-structures/matrix/vector2d.js +287 -0
- package/lib/data-structures/priority-queue/index.js +3 -0
- package/lib/data-structures/priority-queue/max-priority-queue.js +39 -0
- package/lib/data-structures/priority-queue/min-priority-queue.js +40 -0
- package/lib/data-structures/priority-queue/priority-queue.js +317 -0
- package/{dist → lib}/data-structures/queue/deque.d.ts +45 -0
- package/lib/data-structures/queue/deque.js +270 -0
- package/lib/data-structures/queue/index.js +2 -0
- package/{dist → lib}/data-structures/queue/queue.d.ts +47 -13
- package/lib/data-structures/queue/queue.js +165 -0
- package/lib/data-structures/stack/index.js +1 -0
- package/lib/data-structures/stack/stack.js +87 -0
- package/lib/data-structures/tree/index.js +1 -0
- package/lib/data-structures/tree/tree.js +56 -0
- package/lib/data-structures/trie/index.js +1 -0
- package/lib/data-structures/trie/trie.js +205 -0
- package/lib/index.js +4 -0
- package/{dist → lib}/interfaces/avl-tree.d.ts +1 -9
- package/lib/interfaces/binary-tree.d.ts +4 -0
- package/{dist → lib}/interfaces/bst.d.ts +1 -2
- package/lib/interfaces/heap.js +1 -0
- package/lib/interfaces/index.js +15 -0
- package/lib/interfaces/navigator.d.ts +1 -0
- package/lib/interfaces/navigator.js +1 -0
- package/lib/interfaces/priority-queue.d.ts +1 -0
- package/lib/interfaces/priority-queue.js +1 -0
- package/{dist → lib}/interfaces/rb-tree.d.ts +1 -2
- package/lib/interfaces/rb-tree.js +1 -0
- package/lib/interfaces/segment-tree.d.ts +1 -0
- package/lib/interfaces/segment-tree.js +1 -0
- package/lib/interfaces/singly-linked-list.d.ts +1 -0
- package/lib/interfaces/singly-linked-list.js +1 -0
- package/lib/interfaces/tree-multiset.d.ts +5 -0
- package/lib/interfaces/tree-multiset.js +1 -0
- package/lib/interfaces/undirected-graph.js +1 -0
- package/{dist → lib}/types/data-structures/abstract-binary-tree.d.ts +1 -1
- package/lib/types/data-structures/abstract-binary-tree.js +22 -0
- package/lib/types/data-structures/abstract-graph.js +1 -0
- package/{dist → lib}/types/data-structures/avl-tree.d.ts +1 -1
- package/lib/types/data-structures/avl-tree.js +1 -0
- package/lib/types/data-structures/binary-tree.js +1 -0
- package/lib/types/data-structures/bst.js +6 -0
- package/lib/types/data-structures/directed-graph.js +6 -0
- package/lib/types/data-structures/doubly-linked-list.d.ts +1 -0
- package/lib/types/data-structures/doubly-linked-list.js +1 -0
- package/lib/types/data-structures/heap.js +1 -0
- package/lib/types/data-structures/index.js +15 -0
- package/lib/types/data-structures/map-graph.js +1 -0
- package/lib/types/data-structures/navigator.js +1 -0
- package/lib/types/data-structures/priority-queue.js +1 -0
- package/lib/types/data-structures/rb-tree.js +5 -0
- package/lib/types/data-structures/segment-tree.js +1 -0
- package/lib/types/data-structures/singly-linked-list.d.ts +1 -0
- package/lib/types/data-structures/singly-linked-list.js +1 -0
- package/lib/types/data-structures/tree-multiset.js +1 -0
- package/lib/types/helpers.d.ts +1 -0
- package/lib/types/helpers.js +1 -0
- package/lib/types/index.js +3 -0
- package/lib/types/utils/index.js +2 -0
- package/{dist → lib}/types/utils/utils.d.ts +1 -1
- package/lib/types/utils/utils.js +1 -0
- package/{dist → lib}/types/utils/validate-type.d.ts +1 -1
- package/lib/types/utils/validate-type.js +1 -0
- package/lib/utils/index.js +1 -0
- package/lib/utils/utils.js +57 -0
- package/package.json +147 -56
- package/rename_clear_files.sh +29 -0
- package/test/integration/avl-tree.test.ts +111 -0
- package/test/integration/bst.test.ts +371 -0
- package/test/integration/heap.test.js +19 -0
- package/test/integration/index.html +44 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +111 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +371 -0
- package/test/unit/data-structures/binary-tree/overall.test.ts +57 -0
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +405 -0
- package/test/unit/data-structures/graph/abstract-graph.test.ts +5 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +517 -0
- package/test/unit/data-structures/graph/index.ts +2 -0
- package/test/unit/data-structures/graph/map-graph.test.ts +46 -0
- package/test/unit/data-structures/graph/overall.test.ts +50 -0
- package/test/unit/data-structures/graph/undirected-graph.test.ts +60 -0
- package/test/unit/data-structures/heap/heap.test.ts +56 -0
- package/test/unit/data-structures/heap/max-heap.test.ts +42 -0
- package/test/unit/data-structures/heap/min-heap.test.ts +81 -0
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +365 -0
- package/test/unit/data-structures/linked-list/index.ts +4 -0
- package/test/unit/data-structures/linked-list/linked-list.test.ts +37 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +401 -0
- package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +13 -0
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +106 -0
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +105 -0
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +27 -0
- package/test/unit/data-structures/queue/queue.test.ts +36 -0
- package/test/utils/index.ts +2 -0
- package/test/utils/magnitude.ts +21 -0
- package/test/utils/number.ts +3 -0
- package/tsconfig.build.json +33 -0
- package/tsconfig.json +38 -0
- package/umd/bundle.min.js +3 -0
- package/umd/bundle.min.js.map +1 -0
- package/webpack.config.js +28 -0
- package/dist/bundle.js +0 -2
- package/dist/interfaces/binary-tree.d.ts +0 -6
- package/dist/interfaces/tree-multiset.d.ts +0 -7
- /package/{dist → lib}/data-structures/binary-tree/aa-tree.d.ts +0 -0
- /package/{dist → lib}/data-structures/binary-tree/b-tree.d.ts +0 -0
- /package/{dist → lib}/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -0
- /package/{dist → lib}/data-structures/binary-tree/binary-tree.d.ts +0 -0
- /package/{dist → lib}/data-structures/binary-tree/index.d.ts +0 -0
- /package/{dist → lib}/data-structures/binary-tree/segment-tree.d.ts +0 -0
- /package/{dist → lib}/data-structures/binary-tree/splay-tree.d.ts +0 -0
- /package/{dist → lib}/data-structures/binary-tree/two-three-tree.d.ts +0 -0
- /package/{dist → lib}/data-structures/graph/abstract-graph.d.ts +0 -0
- /package/{dist → lib}/data-structures/graph/directed-graph.d.ts +0 -0
- /package/{dist → lib}/data-structures/graph/index.d.ts +0 -0
- /package/{dist → lib}/data-structures/graph/map-graph.d.ts +0 -0
- /package/{dist → lib}/data-structures/graph/undirected-graph.d.ts +0 -0
- /package/{dist → lib}/data-structures/hash/coordinate-map.d.ts +0 -0
- /package/{dist → lib}/data-structures/hash/coordinate-set.d.ts +0 -0
- /package/{dist → lib}/data-structures/hash/hash-table.d.ts +0 -0
- /package/{dist → lib}/data-structures/hash/index.d.ts +0 -0
- /package/{dist → lib}/data-structures/hash/pair.d.ts +0 -0
- /package/{dist → lib}/data-structures/hash/tree-map.d.ts +0 -0
- /package/{dist → lib}/data-structures/hash/tree-set.d.ts +0 -0
- /package/{dist → lib}/data-structures/heap/heap.d.ts +0 -0
- /package/{dist → lib}/data-structures/heap/index.d.ts +0 -0
- /package/{dist → lib}/data-structures/heap/max-heap.d.ts +0 -0
- /package/{dist → lib}/data-structures/heap/min-heap.d.ts +0 -0
- /package/{dist → lib}/data-structures/index.d.ts +0 -0
- /package/{dist → lib}/data-structures/linked-list/index.d.ts +0 -0
- /package/{dist → lib}/data-structures/linked-list/skip-linked-list.d.ts +0 -0
- /package/{dist → lib}/data-structures/matrix/index.d.ts +0 -0
- /package/{dist → lib}/data-structures/matrix/matrix.d.ts +0 -0
- /package/{dist → lib}/data-structures/matrix/matrix2d.d.ts +0 -0
- /package/{dist → lib}/data-structures/matrix/navigator.d.ts +0 -0
- /package/{dist → lib}/data-structures/matrix/vector2d.d.ts +0 -0
- /package/{dist → lib}/data-structures/priority-queue/index.d.ts +0 -0
- /package/{dist → lib}/data-structures/priority-queue/max-priority-queue.d.ts +0 -0
- /package/{dist → lib}/data-structures/priority-queue/min-priority-queue.d.ts +0 -0
- /package/{dist → lib}/data-structures/priority-queue/priority-queue.d.ts +0 -0
- /package/{dist → lib}/data-structures/queue/index.d.ts +0 -0
- /package/{dist → lib}/data-structures/stack/index.d.ts +0 -0
- /package/{dist → lib}/data-structures/stack/stack.d.ts +0 -0
- /package/{dist → lib}/data-structures/tree/index.d.ts +0 -0
- /package/{dist → lib}/data-structures/tree/tree.d.ts +0 -0
- /package/{dist → lib}/data-structures/trie/index.d.ts +0 -0
- /package/{dist → lib}/data-structures/trie/trie.d.ts +0 -0
- /package/{dist → lib}/index.d.ts +0 -0
- /package/{dist → lib}/interfaces/abstract-binary-tree.d.ts +0 -0
- /package/{dist/interfaces/doubly-linked-list.d.ts → lib/interfaces/abstract-binary-tree.js} +0 -0
- /package/{dist → lib}/interfaces/abstract-graph.d.ts +0 -0
- /package/{dist/interfaces/heap.d.ts → lib/interfaces/abstract-graph.js} +0 -0
- /package/{dist/interfaces/navigator.d.ts → lib/interfaces/avl-tree.js} +0 -0
- /package/{dist/interfaces/priority-queue.d.ts → lib/interfaces/binary-tree.js} +0 -0
- /package/{dist/interfaces/segment-tree.d.ts → lib/interfaces/bst.js} +0 -0
- /package/{dist → lib}/interfaces/directed-graph.d.ts +0 -0
- /package/{dist/interfaces/singly-linked-list.d.ts → lib/interfaces/directed-graph.js} +0 -0
- /package/{dist/types/data-structures → lib/interfaces}/doubly-linked-list.d.ts +0 -0
- /package/{dist/types/data-structures/singly-linked-list.d.ts → lib/interfaces/doubly-linked-list.js} +0 -0
- /package/{dist/types/helpers.d.ts → lib/interfaces/heap.d.ts} +0 -0
- /package/{dist → lib}/interfaces/index.d.ts +0 -0
- /package/{dist → lib}/interfaces/undirected-graph.d.ts +0 -0
- /package/{dist → lib}/types/data-structures/abstract-graph.d.ts +0 -0
- /package/{dist → lib}/types/data-structures/binary-tree.d.ts +0 -0
- /package/{dist → lib}/types/data-structures/bst.d.ts +0 -0
- /package/{dist → lib}/types/data-structures/directed-graph.d.ts +0 -0
- /package/{dist → lib}/types/data-structures/heap.d.ts +0 -0
- /package/{dist → lib}/types/data-structures/index.d.ts +0 -0
- /package/{dist → lib}/types/data-structures/map-graph.d.ts +0 -0
- /package/{dist → lib}/types/data-structures/navigator.d.ts +0 -0
- /package/{dist → lib}/types/data-structures/priority-queue.d.ts +0 -0
- /package/{dist → lib}/types/data-structures/rb-tree.d.ts +0 -0
- /package/{dist → lib}/types/data-structures/segment-tree.d.ts +0 -0
- /package/{dist → lib}/types/data-structures/tree-multiset.d.ts +0 -0
- /package/{dist → lib}/types/index.d.ts +0 -0
- /package/{dist → lib}/types/utils/index.d.ts +0 -0
- /package/{dist → lib}/utils/index.d.ts +0 -0
- /package/{dist → lib}/utils/utils.d.ts +0 -0
- /package/{dist/bundle.js.LICENSE.txt → umd/bundle.min.js.LICENSE.txt} +0 -0
|
@@ -1,157 +1,164 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __values = (this && this.__values) || function(o) {
|
|
3
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
4
|
+
if (m) return m.call(o);
|
|
5
|
+
if (o && typeof o.length === "number") return {
|
|
6
|
+
next: function () {
|
|
7
|
+
if (o && i >= o.length) o = void 0;
|
|
8
|
+
return { value: o && o[i++], done: !o };
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
12
|
+
};
|
|
13
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
14
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
15
|
+
if (!m) return o;
|
|
16
|
+
var i = m.call(o), r, ar = [], e;
|
|
17
|
+
try {
|
|
18
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
19
|
+
}
|
|
20
|
+
catch (error) { e = { error: error }; }
|
|
21
|
+
finally {
|
|
22
|
+
try {
|
|
23
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
24
|
+
}
|
|
25
|
+
finally { if (e) throw e.error; }
|
|
26
|
+
}
|
|
27
|
+
return ar;
|
|
28
|
+
};
|
|
29
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
30
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
31
|
+
if (ar || !(i in from)) {
|
|
32
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
33
|
+
ar[i] = from[i];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
37
|
+
};
|
|
2
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
39
|
exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
-
* @license MIT License
|
|
10
|
-
*/
|
|
11
|
-
const utils_1 = require("../../utils");
|
|
12
|
-
const priority_queue_1 = require("../priority-queue");
|
|
13
|
-
class AbstractVertex {
|
|
14
|
-
/**
|
|
15
|
-
* The function is a protected constructor that takes an id and an optional value as parameters.
|
|
16
|
-
* @param {VertexId} id - The `id` parameter is of type `VertexId` and represents the identifier of the vertex. It is
|
|
17
|
-
* used to uniquely identify the vertex object.
|
|
18
|
-
* @param {T} [val] - The parameter "val" is an optional parameter of type T. It is used to assign a value to the
|
|
19
|
-
* vertex. If no value is provided, it will be set to undefined.
|
|
20
|
-
*/
|
|
21
|
-
constructor(id, val) {
|
|
40
|
+
var utils_1 = require("../../utils");
|
|
41
|
+
var priority_queue_1 = require("../priority-queue");
|
|
42
|
+
var AbstractVertex = (function () {
|
|
43
|
+
function AbstractVertex(id, val) {
|
|
22
44
|
this._id = id;
|
|
23
45
|
this._val = val;
|
|
24
46
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
47
|
+
Object.defineProperty(AbstractVertex.prototype, "id", {
|
|
48
|
+
get: function () {
|
|
49
|
+
return this._id;
|
|
50
|
+
},
|
|
51
|
+
set: function (v) {
|
|
52
|
+
this._id = v;
|
|
53
|
+
},
|
|
54
|
+
enumerable: false,
|
|
55
|
+
configurable: true
|
|
56
|
+
});
|
|
57
|
+
Object.defineProperty(AbstractVertex.prototype, "val", {
|
|
58
|
+
get: function () {
|
|
59
|
+
return this._val;
|
|
60
|
+
},
|
|
61
|
+
set: function (value) {
|
|
62
|
+
this._val = value;
|
|
63
|
+
},
|
|
64
|
+
enumerable: false,
|
|
65
|
+
configurable: true
|
|
66
|
+
});
|
|
67
|
+
return AbstractVertex;
|
|
68
|
+
}());
|
|
38
69
|
exports.AbstractVertex = AbstractVertex;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
* The above function is a protected constructor that initializes the weight, value, and hash code properties of an
|
|
42
|
-
* object.
|
|
43
|
-
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the object. If
|
|
44
|
-
* a value is provided, it will be assigned to the `_weight` property. If no value is provided, the default value of 1
|
|
45
|
-
* will be assigned.
|
|
46
|
-
* @param {T} [val] - The `val` parameter is of type `T`, which means it can be any type. It is an optional parameter,
|
|
47
|
-
* meaning it can be omitted when creating an instance of the class.
|
|
48
|
-
*/
|
|
49
|
-
constructor(weight, val) {
|
|
70
|
+
var AbstractEdge = (function () {
|
|
71
|
+
function AbstractEdge(weight, val) {
|
|
50
72
|
this._weight = weight !== undefined ? weight : 1;
|
|
51
73
|
this._val = val;
|
|
52
74
|
this._hashCode = (0, utils_1.uuidV4)();
|
|
53
75
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
76
|
+
Object.defineProperty(AbstractEdge.prototype, "val", {
|
|
77
|
+
get: function () {
|
|
78
|
+
return this._val;
|
|
79
|
+
},
|
|
80
|
+
set: function (value) {
|
|
81
|
+
this._val = value;
|
|
82
|
+
},
|
|
83
|
+
enumerable: false,
|
|
84
|
+
configurable: true
|
|
85
|
+
});
|
|
86
|
+
Object.defineProperty(AbstractEdge.prototype, "weight", {
|
|
87
|
+
get: function () {
|
|
88
|
+
return this._weight;
|
|
89
|
+
},
|
|
90
|
+
set: function (v) {
|
|
91
|
+
this._weight = v;
|
|
92
|
+
},
|
|
93
|
+
enumerable: false,
|
|
94
|
+
configurable: true
|
|
95
|
+
});
|
|
96
|
+
Object.defineProperty(AbstractEdge.prototype, "hashCode", {
|
|
97
|
+
get: function () {
|
|
98
|
+
return this._hashCode;
|
|
99
|
+
},
|
|
100
|
+
enumerable: false,
|
|
101
|
+
configurable: true
|
|
102
|
+
});
|
|
103
|
+
AbstractEdge.prototype._setHashCode = function (v) {
|
|
79
104
|
this._hashCode = v;
|
|
80
|
-
}
|
|
81
|
-
|
|
105
|
+
};
|
|
106
|
+
return AbstractEdge;
|
|
107
|
+
}());
|
|
82
108
|
exports.AbstractEdge = AbstractEdge;
|
|
83
|
-
|
|
84
|
-
|
|
109
|
+
var AbstractGraph = (function () {
|
|
110
|
+
function AbstractGraph() {
|
|
85
111
|
this._vertices = new Map();
|
|
86
112
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
* map. If the vertex does not exist, it returns `null`.
|
|
96
|
-
*/
|
|
97
|
-
getVertex(vertexId) {
|
|
113
|
+
Object.defineProperty(AbstractGraph.prototype, "vertices", {
|
|
114
|
+
get: function () {
|
|
115
|
+
return this._vertices;
|
|
116
|
+
},
|
|
117
|
+
enumerable: false,
|
|
118
|
+
configurable: true
|
|
119
|
+
});
|
|
120
|
+
AbstractGraph.prototype.getVertex = function (vertexId) {
|
|
98
121
|
return this._vertices.get(vertexId) || null;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
* The function checks if a vertex exists in a graph.
|
|
102
|
-
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
103
|
-
* (`VertexId`).
|
|
104
|
-
* @returns a boolean value.
|
|
105
|
-
*/
|
|
106
|
-
hasVertex(vertexOrId) {
|
|
122
|
+
};
|
|
123
|
+
AbstractGraph.prototype.hasVertex = function (vertexOrId) {
|
|
107
124
|
return this._vertices.has(this._getVertexId(vertexOrId));
|
|
108
|
-
}
|
|
109
|
-
addVertex(idOrVertex, val) {
|
|
125
|
+
};
|
|
126
|
+
AbstractGraph.prototype.addVertex = function (idOrVertex, val) {
|
|
110
127
|
if (idOrVertex instanceof AbstractVertex) {
|
|
111
128
|
return this._addVertexOnly(idOrVertex);
|
|
112
129
|
}
|
|
113
130
|
else {
|
|
114
|
-
|
|
131
|
+
var newVertex = this.createVertex(idOrVertex, val);
|
|
115
132
|
return this._addVertexOnly(newVertex);
|
|
116
133
|
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
121
|
-
* (`VertexId`).
|
|
122
|
-
* @returns The method is returning a boolean value.
|
|
123
|
-
*/
|
|
124
|
-
removeVertex(vertexOrId) {
|
|
125
|
-
const vertexId = this._getVertexId(vertexOrId);
|
|
134
|
+
};
|
|
135
|
+
AbstractGraph.prototype.removeVertex = function (vertexOrId) {
|
|
136
|
+
var vertexId = this._getVertexId(vertexOrId);
|
|
126
137
|
return this._vertices.delete(vertexId);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
138
|
+
};
|
|
139
|
+
AbstractGraph.prototype.removeAllVertices = function (vertices) {
|
|
140
|
+
var e_1, _a;
|
|
141
|
+
var removed = [];
|
|
142
|
+
try {
|
|
143
|
+
for (var vertices_1 = __values(vertices), vertices_1_1 = vertices_1.next(); !vertices_1_1.done; vertices_1_1 = vertices_1.next()) {
|
|
144
|
+
var v = vertices_1_1.value;
|
|
145
|
+
removed.push(this.removeVertex(v));
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
149
|
+
finally {
|
|
150
|
+
try {
|
|
151
|
+
if (vertices_1_1 && !vertices_1_1.done && (_a = vertices_1.return)) _a.call(vertices_1);
|
|
152
|
+
}
|
|
153
|
+
finally { if (e_1) throw e_1.error; }
|
|
139
154
|
}
|
|
140
155
|
return removed.length > 0;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
* @param {VertexId | V} v1 - The parameter v1 can be either a VertexId or a V. A VertexId represents the unique
|
|
145
|
-
* identifier of a vertex in a graph, while V represents the type of the vertex object itself.
|
|
146
|
-
* @param {VertexId | V} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
|
|
147
|
-
* `VertexId` or a `V` type, which represents the type of the vertex.
|
|
148
|
-
* @returns A boolean value is being returned.
|
|
149
|
-
*/
|
|
150
|
-
hasEdge(v1, v2) {
|
|
151
|
-
const edge = this.getEdge(v1, v2);
|
|
156
|
+
};
|
|
157
|
+
AbstractGraph.prototype.hasEdge = function (v1, v2) {
|
|
158
|
+
var edge = this.getEdge(v1, v2);
|
|
152
159
|
return !!edge;
|
|
153
|
-
}
|
|
154
|
-
addEdge(srcOrEdge, dest, weight, val) {
|
|
160
|
+
};
|
|
161
|
+
AbstractGraph.prototype.addEdge = function (srcOrEdge, dest, weight, val) {
|
|
155
162
|
if (srcOrEdge instanceof AbstractEdge) {
|
|
156
163
|
return this._addEdgeOnly(srcOrEdge);
|
|
157
164
|
}
|
|
@@ -163,27 +170,16 @@ class AbstractGraph {
|
|
|
163
170
|
srcOrEdge = srcOrEdge.id;
|
|
164
171
|
if (dest instanceof AbstractVertex)
|
|
165
172
|
dest = dest.id;
|
|
166
|
-
|
|
173
|
+
var newEdge = this.createEdge(srcOrEdge, dest, weight, val);
|
|
167
174
|
return this._addEdgeOnly(newEdge);
|
|
168
175
|
}
|
|
169
176
|
else {
|
|
170
177
|
throw new Error('dest must be a Vertex or vertex id while srcOrEdge is an Edge');
|
|
171
178
|
}
|
|
172
179
|
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
* @param {VertexId | V} srcOrId - The `srcOrId` parameter can be either a `VertexId` or a `V` object. It represents
|
|
177
|
-
* the source vertex of the edge.
|
|
178
|
-
* @param {VertexId | V} destOrId - The `destOrId` parameter represents the destination vertex of the edge. It can be
|
|
179
|
-
* either a `VertexId` or a vertex object `V`.
|
|
180
|
-
* @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrId)
|
|
181
|
-
* and the destination vertex (destOrId).
|
|
182
|
-
* @returns a boolean value. If the edge exists between the source and destination vertices, the function will update
|
|
183
|
-
* the weight of the edge and return true. If the edge does not exist, the function will return false.
|
|
184
|
-
*/
|
|
185
|
-
setEdgeWeight(srcOrId, destOrId, weight) {
|
|
186
|
-
const edge = this.getEdge(srcOrId, destOrId);
|
|
180
|
+
};
|
|
181
|
+
AbstractGraph.prototype.setEdgeWeight = function (srcOrId, destOrId, weight) {
|
|
182
|
+
var edge = this.getEdge(srcOrId, destOrId);
|
|
187
183
|
if (edge) {
|
|
188
184
|
edge.weight = weight;
|
|
189
185
|
return true;
|
|
@@ -191,102 +187,110 @@ class AbstractGraph {
|
|
|
191
187
|
else {
|
|
192
188
|
return false;
|
|
193
189
|
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
* @returns The function `getAllPathsBetween` returns an array of arrays of vertices (`V[][]`).
|
|
201
|
-
*/
|
|
202
|
-
getAllPathsBetween(v1, v2) {
|
|
203
|
-
const paths = [];
|
|
204
|
-
const vertex1 = this._getVertex(v1);
|
|
205
|
-
const vertex2 = this._getVertex(v2);
|
|
190
|
+
};
|
|
191
|
+
AbstractGraph.prototype.getAllPathsBetween = function (v1, v2) {
|
|
192
|
+
var _this = this;
|
|
193
|
+
var paths = [];
|
|
194
|
+
var vertex1 = this._getVertex(v1);
|
|
195
|
+
var vertex2 = this._getVertex(v2);
|
|
206
196
|
if (!(vertex1 && vertex2)) {
|
|
207
197
|
return [];
|
|
208
198
|
}
|
|
209
|
-
|
|
199
|
+
var dfs = function (cur, dest, visiting, path) {
|
|
200
|
+
var e_2, _a;
|
|
210
201
|
visiting.set(cur, true);
|
|
211
202
|
if (cur === dest) {
|
|
212
|
-
paths.push([vertex1,
|
|
203
|
+
paths.push(__spreadArray([vertex1], __read(path), false));
|
|
213
204
|
}
|
|
214
|
-
|
|
215
|
-
|
|
205
|
+
var neighbors = _this.getNeighbors(cur);
|
|
206
|
+
var _loop_1 = function (neighbor) {
|
|
216
207
|
if (!visiting.get(neighbor)) {
|
|
217
208
|
path.push(neighbor);
|
|
218
209
|
dfs(neighbor, dest, visiting, path);
|
|
219
|
-
(0, utils_1.arrayRemove)(path, (vertex)
|
|
210
|
+
(0, utils_1.arrayRemove)(path, function (vertex) { return vertex === neighbor; });
|
|
220
211
|
}
|
|
212
|
+
};
|
|
213
|
+
try {
|
|
214
|
+
for (var neighbors_1 = __values(neighbors), neighbors_1_1 = neighbors_1.next(); !neighbors_1_1.done; neighbors_1_1 = neighbors_1.next()) {
|
|
215
|
+
var neighbor = neighbors_1_1.value;
|
|
216
|
+
_loop_1(neighbor);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
220
|
+
finally {
|
|
221
|
+
try {
|
|
222
|
+
if (neighbors_1_1 && !neighbors_1_1.done && (_a = neighbors_1.return)) _a.call(neighbors_1);
|
|
223
|
+
}
|
|
224
|
+
finally { if (e_2) throw e_2.error; }
|
|
221
225
|
}
|
|
222
226
|
visiting.set(cur, false);
|
|
223
227
|
};
|
|
224
228
|
dfs(vertex1, vertex2, new Map(), []);
|
|
225
229
|
return paths;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
* The function calculates the sum of weights along a given path.
|
|
229
|
-
* @param {V[]} path - An array of vertices (V) representing a path in a graph.
|
|
230
|
-
* @returns The function `getPathSumWeight` returns the sum of the weights of the edges in the given path.
|
|
231
|
-
*/
|
|
232
|
-
getPathSumWeight(path) {
|
|
230
|
+
};
|
|
231
|
+
AbstractGraph.prototype.getPathSumWeight = function (path) {
|
|
233
232
|
var _a;
|
|
234
|
-
|
|
235
|
-
for (
|
|
233
|
+
var sum = 0;
|
|
234
|
+
for (var i = 0; i < path.length; i++) {
|
|
236
235
|
sum += ((_a = this.getEdge(path[i], path[i + 1])) === null || _a === void 0 ? void 0 : _a.weight) || 0;
|
|
237
236
|
}
|
|
238
237
|
return sum;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
* weights or using a breadth-first search algorithm.
|
|
243
|
-
* @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex or its ID.
|
|
244
|
-
* @param {V | VertexId} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
|
|
245
|
-
* you want to find the minimum cost or weight from the source vertex `v1`.
|
|
246
|
-
* @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edges have weights.
|
|
247
|
-
* If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
|
|
248
|
-
* the edges. If isWeight is set to false or not provided, the function will calculate the
|
|
249
|
-
* @returns The function `getMinCostBetween` returns a number representing the minimum cost between two vertices (`v1`
|
|
250
|
-
* and `v2`). If the `isWeight` parameter is `true`, it calculates the minimum weight among all paths between the
|
|
251
|
-
* vertices. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
|
|
252
|
-
* minimum number of
|
|
253
|
-
*/
|
|
254
|
-
getMinCostBetween(v1, v2, isWeight) {
|
|
238
|
+
};
|
|
239
|
+
AbstractGraph.prototype.getMinCostBetween = function (v1, v2, isWeight) {
|
|
240
|
+
var e_3, _a, e_4, _b;
|
|
255
241
|
if (isWeight === undefined)
|
|
256
242
|
isWeight = false;
|
|
257
243
|
if (isWeight) {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
244
|
+
var allPaths = this.getAllPathsBetween(v1, v2);
|
|
245
|
+
var min = Infinity;
|
|
246
|
+
try {
|
|
247
|
+
for (var allPaths_1 = __values(allPaths), allPaths_1_1 = allPaths_1.next(); !allPaths_1_1.done; allPaths_1_1 = allPaths_1.next()) {
|
|
248
|
+
var path = allPaths_1_1.value;
|
|
249
|
+
min = Math.min(this.getPathSumWeight(path), min);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
253
|
+
finally {
|
|
254
|
+
try {
|
|
255
|
+
if (allPaths_1_1 && !allPaths_1_1.done && (_a = allPaths_1.return)) _a.call(allPaths_1);
|
|
256
|
+
}
|
|
257
|
+
finally { if (e_3) throw e_3.error; }
|
|
262
258
|
}
|
|
263
259
|
return min;
|
|
264
260
|
}
|
|
265
261
|
else {
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
const vertex1 = this._getVertex(v1);
|
|
262
|
+
var vertex2 = this._getVertex(v2);
|
|
263
|
+
var vertex1 = this._getVertex(v1);
|
|
269
264
|
if (!(vertex1 && vertex2)) {
|
|
270
265
|
return null;
|
|
271
266
|
}
|
|
272
|
-
|
|
273
|
-
|
|
267
|
+
var visited = new Map();
|
|
268
|
+
var queue = [vertex1];
|
|
274
269
|
visited.set(vertex1, true);
|
|
275
|
-
|
|
270
|
+
var cost = 0;
|
|
276
271
|
while (queue.length > 0) {
|
|
277
|
-
for (
|
|
278
|
-
|
|
272
|
+
for (var i = 0; i < queue.length; i++) {
|
|
273
|
+
var cur = queue.shift();
|
|
279
274
|
if (cur === vertex2) {
|
|
280
275
|
return cost;
|
|
281
276
|
}
|
|
282
|
-
// TODO consider optimizing to AbstractGraph
|
|
283
277
|
if (cur !== undefined) {
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
278
|
+
var neighbors = this.getNeighbors(cur);
|
|
279
|
+
try {
|
|
280
|
+
for (var neighbors_2 = (e_4 = void 0, __values(neighbors)), neighbors_2_1 = neighbors_2.next(); !neighbors_2_1.done; neighbors_2_1 = neighbors_2.next()) {
|
|
281
|
+
var neighbor = neighbors_2_1.value;
|
|
282
|
+
if (!visited.has(neighbor)) {
|
|
283
|
+
visited.set(neighbor, true);
|
|
284
|
+
queue.push(neighbor);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
289
|
+
finally {
|
|
290
|
+
try {
|
|
291
|
+
if (neighbors_2_1 && !neighbors_2_1.done && (_b = neighbors_2.return)) _b.call(neighbors_2);
|
|
289
292
|
}
|
|
293
|
+
finally { if (e_4) throw e_4.error; }
|
|
290
294
|
}
|
|
291
295
|
}
|
|
292
296
|
}
|
|
@@ -294,146 +298,170 @@ class AbstractGraph {
|
|
|
294
298
|
}
|
|
295
299
|
return null;
|
|
296
300
|
}
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
* @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
|
|
302
|
-
* object (`V`) or a vertex ID (`VertexId`).
|
|
303
|
-
* @param {V | VertexId} v2 - V | VertexId - The second vertex or vertex ID between which we want to find the minimum
|
|
304
|
-
* path.
|
|
305
|
-
* @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edges in finding the
|
|
306
|
-
* minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
|
|
307
|
-
* to false, the function will use breadth-first search (BFS) to find the minimum path.
|
|
308
|
-
* @returns The function `getMinPathBetween` returns an array of vertices (`V[]`) representing the minimum path between
|
|
309
|
-
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `null`.
|
|
310
|
-
*/
|
|
311
|
-
getMinPathBetween(v1, v2, isWeight) {
|
|
301
|
+
};
|
|
302
|
+
AbstractGraph.prototype.getMinPathBetween = function (v1, v2, isWeight) {
|
|
303
|
+
var e_5, _a;
|
|
304
|
+
var _this = this;
|
|
312
305
|
if (isWeight === undefined)
|
|
313
306
|
isWeight = false;
|
|
314
307
|
if (isWeight) {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
308
|
+
var allPaths = this.getAllPathsBetween(v1, v2);
|
|
309
|
+
var min = Infinity;
|
|
310
|
+
var minIndex = -1;
|
|
311
|
+
var index = 0;
|
|
312
|
+
try {
|
|
313
|
+
for (var allPaths_2 = __values(allPaths), allPaths_2_1 = allPaths_2.next(); !allPaths_2_1.done; allPaths_2_1 = allPaths_2.next()) {
|
|
314
|
+
var path = allPaths_2_1.value;
|
|
315
|
+
var pathSumWeight = this.getPathSumWeight(path);
|
|
316
|
+
if (pathSumWeight < min) {
|
|
317
|
+
min = pathSumWeight;
|
|
318
|
+
minIndex = index;
|
|
319
|
+
}
|
|
320
|
+
index++;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
324
|
+
finally {
|
|
325
|
+
try {
|
|
326
|
+
if (allPaths_2_1 && !allPaths_2_1.done && (_a = allPaths_2.return)) _a.call(allPaths_2);
|
|
327
|
+
}
|
|
328
|
+
finally { if (e_5) throw e_5.error; }
|
|
326
329
|
}
|
|
327
330
|
return allPaths[minIndex] || null;
|
|
328
331
|
}
|
|
329
332
|
else {
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
if (!(vertex1 && vertex2)) {
|
|
333
|
+
var minPath_1 = [];
|
|
334
|
+
var vertex1_1 = this._getVertex(v1);
|
|
335
|
+
var vertex2 = this._getVertex(v2);
|
|
336
|
+
if (!(vertex1_1 && vertex2)) {
|
|
335
337
|
return [];
|
|
336
338
|
}
|
|
337
|
-
|
|
339
|
+
var dfs_1 = function (cur, dest, visiting, path) {
|
|
340
|
+
var e_6, _a;
|
|
338
341
|
visiting.set(cur, true);
|
|
339
342
|
if (cur === dest) {
|
|
340
|
-
|
|
343
|
+
minPath_1 = __spreadArray([vertex1_1], __read(path), false);
|
|
341
344
|
return;
|
|
342
345
|
}
|
|
343
|
-
|
|
344
|
-
|
|
346
|
+
var neighbors = _this.getNeighbors(cur);
|
|
347
|
+
var _loop_2 = function (neighbor) {
|
|
345
348
|
if (!visiting.get(neighbor)) {
|
|
346
349
|
path.push(neighbor);
|
|
347
|
-
|
|
348
|
-
(0, utils_1.arrayRemove)(path, (vertex)
|
|
350
|
+
dfs_1(neighbor, dest, visiting, path);
|
|
351
|
+
(0, utils_1.arrayRemove)(path, function (vertex) { return vertex === neighbor; });
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
try {
|
|
355
|
+
for (var neighbors_3 = __values(neighbors), neighbors_3_1 = neighbors_3.next(); !neighbors_3_1.done; neighbors_3_1 = neighbors_3.next()) {
|
|
356
|
+
var neighbor = neighbors_3_1.value;
|
|
357
|
+
_loop_2(neighbor);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
361
|
+
finally {
|
|
362
|
+
try {
|
|
363
|
+
if (neighbors_3_1 && !neighbors_3_1.done && (_a = neighbors_3.return)) _a.call(neighbors_3);
|
|
349
364
|
}
|
|
365
|
+
finally { if (e_6) throw e_6.error; }
|
|
350
366
|
}
|
|
351
367
|
visiting.set(cur, false);
|
|
352
368
|
};
|
|
353
|
-
|
|
354
|
-
return
|
|
369
|
+
dfs_1(vertex1_1, vertex2, new Map(), []);
|
|
370
|
+
return minPath_1;
|
|
355
371
|
}
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
* /
|
|
360
|
-
|
|
361
|
-
/**
|
|
362
|
-
* Dijkstra algorithm time: O(VE) space: O(V + E)
|
|
363
|
-
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
|
|
364
|
-
* a graph without using a heap data structure.
|
|
365
|
-
* @param {V | VertexId} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
366
|
-
* vertex object or a vertex ID.
|
|
367
|
-
* @param {V | VertexId | null} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
368
|
-
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
369
|
-
* identifier. If no destination is provided, the value is set to `null`.
|
|
370
|
-
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
371
|
-
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
372
|
-
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
373
|
-
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
374
|
-
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
375
|
-
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
376
|
-
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<V>`.
|
|
377
|
-
*/
|
|
378
|
-
dijkstraWithoutHeap(src, dest, getMinDist, genPaths) {
|
|
372
|
+
};
|
|
373
|
+
AbstractGraph.prototype.dijkstraWithoutHeap = function (src, dest, getMinDist, genPaths) {
|
|
374
|
+
var e_7, _a, e_8, _b;
|
|
379
375
|
if (getMinDist === undefined)
|
|
380
376
|
getMinDist = false;
|
|
381
377
|
if (genPaths === undefined)
|
|
382
378
|
genPaths = false;
|
|
383
379
|
if (dest === undefined)
|
|
384
380
|
dest = null;
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
381
|
+
var minDist = Infinity;
|
|
382
|
+
var minDest = null;
|
|
383
|
+
var minPath = [];
|
|
384
|
+
var paths = [];
|
|
385
|
+
var vertices = this._vertices;
|
|
386
|
+
var distMap = new Map();
|
|
387
|
+
var seen = new Set();
|
|
388
|
+
var preMap = new Map();
|
|
389
|
+
var srcVertex = this._getVertex(src);
|
|
390
|
+
var destVertex = dest ? this._getVertex(dest) : null;
|
|
395
391
|
if (!srcVertex) {
|
|
396
392
|
return null;
|
|
397
393
|
}
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
394
|
+
try {
|
|
395
|
+
for (var vertices_2 = __values(vertices), vertices_2_1 = vertices_2.next(); !vertices_2_1.done; vertices_2_1 = vertices_2.next()) {
|
|
396
|
+
var vertex = vertices_2_1.value;
|
|
397
|
+
var vertexOrId = vertex[1];
|
|
398
|
+
if (vertexOrId instanceof AbstractVertex)
|
|
399
|
+
distMap.set(vertexOrId, Infinity);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
403
|
+
finally {
|
|
404
|
+
try {
|
|
405
|
+
if (vertices_2_1 && !vertices_2_1.done && (_a = vertices_2.return)) _a.call(vertices_2);
|
|
406
|
+
}
|
|
407
|
+
finally { if (e_7) throw e_7.error; }
|
|
402
408
|
}
|
|
403
409
|
distMap.set(srcVertex, 0);
|
|
404
410
|
preMap.set(srcVertex, null);
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
411
|
+
var getMinOfNoSeen = function () {
|
|
412
|
+
var e_9, _a;
|
|
413
|
+
var min = Infinity;
|
|
414
|
+
var minV = null;
|
|
415
|
+
try {
|
|
416
|
+
for (var distMap_1 = __values(distMap), distMap_1_1 = distMap_1.next(); !distMap_1_1.done; distMap_1_1 = distMap_1.next()) {
|
|
417
|
+
var _b = __read(distMap_1_1.value, 2), key = _b[0], val = _b[1];
|
|
418
|
+
if (!seen.has(key)) {
|
|
419
|
+
if (val < min) {
|
|
420
|
+
min = val;
|
|
421
|
+
minV = key;
|
|
422
|
+
}
|
|
413
423
|
}
|
|
414
424
|
}
|
|
415
425
|
}
|
|
426
|
+
catch (e_9_1) { e_9 = { error: e_9_1 }; }
|
|
427
|
+
finally {
|
|
428
|
+
try {
|
|
429
|
+
if (distMap_1_1 && !distMap_1_1.done && (_a = distMap_1.return)) _a.call(distMap_1);
|
|
430
|
+
}
|
|
431
|
+
finally { if (e_9) throw e_9.error; }
|
|
432
|
+
}
|
|
416
433
|
return minV;
|
|
417
434
|
};
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
path
|
|
426
|
-
|
|
435
|
+
var getPaths = function (minV) {
|
|
436
|
+
var e_10, _a;
|
|
437
|
+
try {
|
|
438
|
+
for (var vertices_3 = __values(vertices), vertices_3_1 = vertices_3.next(); !vertices_3_1.done; vertices_3_1 = vertices_3.next()) {
|
|
439
|
+
var vertex = vertices_3_1.value;
|
|
440
|
+
var vertexOrId = vertex[1];
|
|
441
|
+
if (vertexOrId instanceof AbstractVertex) {
|
|
442
|
+
var path = [vertexOrId];
|
|
443
|
+
var parent_1 = preMap.get(vertexOrId);
|
|
444
|
+
while (parent_1) {
|
|
445
|
+
path.push(parent_1);
|
|
446
|
+
parent_1 = preMap.get(parent_1);
|
|
447
|
+
}
|
|
448
|
+
var reversed = path.reverse();
|
|
449
|
+
if (vertex[1] === minV)
|
|
450
|
+
minPath = reversed;
|
|
451
|
+
paths.push(reversed);
|
|
427
452
|
}
|
|
428
|
-
const reversed = path.reverse();
|
|
429
|
-
if (vertex[1] === minV)
|
|
430
|
-
minPath = reversed;
|
|
431
|
-
paths.push(reversed);
|
|
432
453
|
}
|
|
433
454
|
}
|
|
455
|
+
catch (e_10_1) { e_10 = { error: e_10_1 }; }
|
|
456
|
+
finally {
|
|
457
|
+
try {
|
|
458
|
+
if (vertices_3_1 && !vertices_3_1.done && (_a = vertices_3.return)) _a.call(vertices_3);
|
|
459
|
+
}
|
|
460
|
+
finally { if (e_10) throw e_10.error; }
|
|
461
|
+
}
|
|
434
462
|
};
|
|
435
|
-
for (
|
|
436
|
-
|
|
463
|
+
for (var i = 1; i < vertices.size; i++) {
|
|
464
|
+
var cur = getMinOfNoSeen();
|
|
437
465
|
if (cur) {
|
|
438
466
|
seen.add(cur);
|
|
439
467
|
if (destVertex && destVertex === cur) {
|
|
@@ -443,120 +471,123 @@ class AbstractGraph {
|
|
|
443
471
|
if (genPaths) {
|
|
444
472
|
getPaths(destVertex);
|
|
445
473
|
}
|
|
446
|
-
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
if (
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
if (
|
|
458
|
-
|
|
459
|
-
|
|
474
|
+
return { distMap: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
|
|
475
|
+
}
|
|
476
|
+
var neighbors = this.getNeighbors(cur);
|
|
477
|
+
try {
|
|
478
|
+
for (var neighbors_4 = (e_8 = void 0, __values(neighbors)), neighbors_4_1 = neighbors_4.next(); !neighbors_4_1.done; neighbors_4_1 = neighbors_4.next()) {
|
|
479
|
+
var neighbor = neighbors_4_1.value;
|
|
480
|
+
if (!seen.has(neighbor)) {
|
|
481
|
+
var edge = this.getEdge(cur, neighbor);
|
|
482
|
+
if (edge) {
|
|
483
|
+
var curFromMap = distMap.get(cur);
|
|
484
|
+
var neighborFromMap = distMap.get(neighbor);
|
|
485
|
+
if (curFromMap !== undefined && neighborFromMap !== undefined) {
|
|
486
|
+
if (edge.weight + curFromMap < neighborFromMap) {
|
|
487
|
+
distMap.set(neighbor, edge.weight + curFromMap);
|
|
488
|
+
preMap.set(neighbor, cur);
|
|
489
|
+
}
|
|
460
490
|
}
|
|
461
491
|
}
|
|
462
492
|
}
|
|
463
493
|
}
|
|
464
494
|
}
|
|
495
|
+
catch (e_8_1) { e_8 = { error: e_8_1 }; }
|
|
496
|
+
finally {
|
|
497
|
+
try {
|
|
498
|
+
if (neighbors_4_1 && !neighbors_4_1.done && (_b = neighbors_4.return)) _b.call(neighbors_4);
|
|
499
|
+
}
|
|
500
|
+
finally { if (e_8) throw e_8.error; }
|
|
501
|
+
}
|
|
465
502
|
}
|
|
466
503
|
}
|
|
467
|
-
getMinDist &&
|
|
468
|
-
|
|
469
|
-
if (
|
|
470
|
-
minDist
|
|
471
|
-
|
|
472
|
-
|
|
504
|
+
getMinDist &&
|
|
505
|
+
distMap.forEach(function (d, v) {
|
|
506
|
+
if (v !== srcVertex) {
|
|
507
|
+
if (d < minDist) {
|
|
508
|
+
minDist = d;
|
|
509
|
+
if (genPaths)
|
|
510
|
+
minDest = v;
|
|
511
|
+
}
|
|
473
512
|
}
|
|
474
|
-
}
|
|
475
|
-
});
|
|
513
|
+
});
|
|
476
514
|
genPaths && getPaths(minDest);
|
|
477
|
-
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
* 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.
|
|
483
|
-
* 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.
|
|
484
|
-
* 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.
|
|
485
|
-
*
|
|
486
|
-
* /
|
|
487
|
-
|
|
488
|
-
/**
|
|
489
|
-
* 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.
|
|
490
|
-
* The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
|
|
491
|
-
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
492
|
-
* @param {V | VertexId} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
493
|
-
* start. It can be either a vertex object or a vertex ID.
|
|
494
|
-
* @param {V | VertexId | null} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
495
|
-
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
496
|
-
* will calculate the shortest paths to all other vertices from the source vertex.
|
|
497
|
-
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
498
|
-
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
499
|
-
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
500
|
-
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
501
|
-
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
502
|
-
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
503
|
-
* @returns The function `dijkstra` returns an object of type `DijkstraResult<V>`.
|
|
504
|
-
*/
|
|
505
|
-
dijkstra(src, dest, getMinDist, genPaths) {
|
|
506
|
-
var _a;
|
|
515
|
+
return { distMap: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
|
|
516
|
+
};
|
|
517
|
+
AbstractGraph.prototype.dijkstra = function (src, dest, getMinDist, genPaths) {
|
|
518
|
+
var e_11, _a, e_12, _b;
|
|
519
|
+
var _c;
|
|
507
520
|
if (getMinDist === undefined)
|
|
508
521
|
getMinDist = false;
|
|
509
522
|
if (genPaths === undefined)
|
|
510
523
|
genPaths = false;
|
|
511
524
|
if (dest === undefined)
|
|
512
525
|
dest = null;
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
526
|
+
var minDist = Infinity;
|
|
527
|
+
var minDest = null;
|
|
528
|
+
var minPath = [];
|
|
529
|
+
var paths = [];
|
|
530
|
+
var vertices = this._vertices;
|
|
531
|
+
var distMap = new Map();
|
|
532
|
+
var seen = new Set();
|
|
533
|
+
var preMap = new Map();
|
|
534
|
+
var srcVertex = this._getVertex(src);
|
|
535
|
+
var destVertex = dest ? this._getVertex(dest) : null;
|
|
523
536
|
if (!srcVertex)
|
|
524
537
|
return null;
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
538
|
+
try {
|
|
539
|
+
for (var vertices_4 = __values(vertices), vertices_4_1 = vertices_4.next(); !vertices_4_1.done; vertices_4_1 = vertices_4.next()) {
|
|
540
|
+
var vertex = vertices_4_1.value;
|
|
541
|
+
var vertexOrId = vertex[1];
|
|
542
|
+
if (vertexOrId instanceof AbstractVertex)
|
|
543
|
+
distMap.set(vertexOrId, Infinity);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
catch (e_11_1) { e_11 = { error: e_11_1 }; }
|
|
547
|
+
finally {
|
|
548
|
+
try {
|
|
549
|
+
if (vertices_4_1 && !vertices_4_1.done && (_a = vertices_4.return)) _a.call(vertices_4);
|
|
550
|
+
}
|
|
551
|
+
finally { if (e_11) throw e_11.error; }
|
|
529
552
|
}
|
|
530
|
-
|
|
553
|
+
var heap = new priority_queue_1.PriorityQueue({
|
|
554
|
+
comparator: function (a, b) { return a.id - b.id; }
|
|
555
|
+
});
|
|
531
556
|
heap.add({ id: 0, val: srcVertex });
|
|
532
557
|
distMap.set(srcVertex, 0);
|
|
533
558
|
preMap.set(srcVertex, null);
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
559
|
+
var getPaths = function (minV) {
|
|
560
|
+
var e_13, _a;
|
|
561
|
+
try {
|
|
562
|
+
for (var vertices_5 = __values(vertices), vertices_5_1 = vertices_5.next(); !vertices_5_1.done; vertices_5_1 = vertices_5.next()) {
|
|
563
|
+
var vertex = vertices_5_1.value;
|
|
564
|
+
var vertexOrId = vertex[1];
|
|
565
|
+
if (vertexOrId instanceof AbstractVertex) {
|
|
566
|
+
var path = [vertexOrId];
|
|
567
|
+
var parent_2 = preMap.get(vertexOrId);
|
|
568
|
+
while (parent_2) {
|
|
569
|
+
path.push(parent_2);
|
|
570
|
+
parent_2 = preMap.get(parent_2);
|
|
571
|
+
}
|
|
572
|
+
var reversed = path.reverse();
|
|
573
|
+
if (vertex[1] === minV)
|
|
574
|
+
minPath = reversed;
|
|
575
|
+
paths.push(reversed);
|
|
548
576
|
}
|
|
549
|
-
const reversed = path.reverse();
|
|
550
|
-
if (vertex[1] === minV)
|
|
551
|
-
minPath = reversed;
|
|
552
|
-
paths.push(reversed);
|
|
553
577
|
}
|
|
554
578
|
}
|
|
579
|
+
catch (e_13_1) { e_13 = { error: e_13_1 }; }
|
|
580
|
+
finally {
|
|
581
|
+
try {
|
|
582
|
+
if (vertices_5_1 && !vertices_5_1.done && (_a = vertices_5.return)) _a.call(vertices_5);
|
|
583
|
+
}
|
|
584
|
+
finally { if (e_13) throw e_13.error; }
|
|
585
|
+
}
|
|
555
586
|
};
|
|
556
587
|
while (heap.size > 0) {
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
588
|
+
var curHeapNode = heap.poll();
|
|
589
|
+
var dist = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.id;
|
|
590
|
+
var cur = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.val;
|
|
560
591
|
if (dist !== undefined) {
|
|
561
592
|
if (cur) {
|
|
562
593
|
seen.add(cur);
|
|
@@ -567,29 +598,39 @@ class AbstractGraph {
|
|
|
567
598
|
if (genPaths) {
|
|
568
599
|
getPaths(destVertex);
|
|
569
600
|
}
|
|
570
|
-
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
601
|
+
return { distMap: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
|
|
571
602
|
}
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
if (
|
|
577
|
-
|
|
578
|
-
if (
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
603
|
+
var neighbors = this.getNeighbors(cur);
|
|
604
|
+
try {
|
|
605
|
+
for (var neighbors_5 = (e_12 = void 0, __values(neighbors)), neighbors_5_1 = neighbors_5.next(); !neighbors_5_1.done; neighbors_5_1 = neighbors_5.next()) {
|
|
606
|
+
var neighbor = neighbors_5_1.value;
|
|
607
|
+
if (!seen.has(neighbor)) {
|
|
608
|
+
var weight = (_c = this.getEdge(cur, neighbor)) === null || _c === void 0 ? void 0 : _c.weight;
|
|
609
|
+
if (typeof weight === 'number') {
|
|
610
|
+
var distSrcToNeighbor = distMap.get(neighbor);
|
|
611
|
+
if (distSrcToNeighbor) {
|
|
612
|
+
if (dist + weight < distSrcToNeighbor) {
|
|
613
|
+
heap.add({ id: dist + weight, val: neighbor });
|
|
614
|
+
preMap.set(neighbor, cur);
|
|
615
|
+
distMap.set(neighbor, dist + weight);
|
|
616
|
+
}
|
|
583
617
|
}
|
|
584
618
|
}
|
|
585
619
|
}
|
|
586
620
|
}
|
|
587
621
|
}
|
|
622
|
+
catch (e_12_1) { e_12 = { error: e_12_1 }; }
|
|
623
|
+
finally {
|
|
624
|
+
try {
|
|
625
|
+
if (neighbors_5_1 && !neighbors_5_1.done && (_b = neighbors_5.return)) _b.call(neighbors_5);
|
|
626
|
+
}
|
|
627
|
+
finally { if (e_12) throw e_12.error; }
|
|
628
|
+
}
|
|
588
629
|
}
|
|
589
630
|
}
|
|
590
631
|
}
|
|
591
632
|
if (getMinDist) {
|
|
592
|
-
distMap.forEach((d, v)
|
|
633
|
+
distMap.forEach(function (d, v) {
|
|
593
634
|
if (v !== srcVertex) {
|
|
594
635
|
if (d < minDist) {
|
|
595
636
|
minDist = d;
|
|
@@ -602,62 +643,41 @@ class AbstractGraph {
|
|
|
602
643
|
if (genPaths) {
|
|
603
644
|
getPaths(minDest);
|
|
604
645
|
}
|
|
605
|
-
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
* one to rest pairs
|
|
610
|
-
* /
|
|
611
|
-
|
|
612
|
-
/**
|
|
613
|
-
* BellmanFord time:O(VE) space:O(V)
|
|
614
|
-
* one to rest pairs
|
|
615
|
-
* 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.
|
|
616
|
-
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
617
|
-
* all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.
|
|
618
|
-
* @param {V | VertexId} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
|
|
619
|
-
* start calculating the shortest paths. It can be either a vertex object or a vertex ID.
|
|
620
|
-
* @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
|
|
621
|
-
* @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
|
|
622
|
-
* calculate the minimum distance from the source vertex to all other vertices in the graph. If `getMin` is set to
|
|
623
|
-
* `true`, the algorithm will find the minimum distance and update the `min` variable with the minimum
|
|
624
|
-
* @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all vertices from the source
|
|
625
|
-
* vertex.
|
|
626
|
-
* @returns The function `bellmanFord` returns an object with the following properties:
|
|
627
|
-
*/
|
|
628
|
-
bellmanFord(src, scanNegativeCycle, getMin, genPath) {
|
|
646
|
+
return { distMap: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
|
|
647
|
+
};
|
|
648
|
+
AbstractGraph.prototype.bellmanFord = function (src, scanNegativeCycle, getMin, genPath) {
|
|
649
|
+
var e_14, _a;
|
|
629
650
|
if (getMin === undefined)
|
|
630
651
|
getMin = false;
|
|
631
652
|
if (genPath === undefined)
|
|
632
653
|
genPath = false;
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
let hasNegativeCycle;
|
|
654
|
+
var srcVertex = this._getVertex(src);
|
|
655
|
+
var paths = [];
|
|
656
|
+
var distMap = new Map();
|
|
657
|
+
var preMap = new Map();
|
|
658
|
+
var min = Infinity;
|
|
659
|
+
var minPath = [];
|
|
660
|
+
var hasNegativeCycle;
|
|
641
661
|
if (scanNegativeCycle)
|
|
642
662
|
hasNegativeCycle = false;
|
|
643
663
|
if (!srcVertex)
|
|
644
|
-
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
this._vertices.forEach(vertex
|
|
664
|
+
return { hasNegativeCycle: hasNegativeCycle, distMap: distMap, preMap: preMap, paths: paths, min: min, minPath: minPath };
|
|
665
|
+
var vertices = this._vertices;
|
|
666
|
+
var numOfVertices = vertices.size;
|
|
667
|
+
var edges = this.edgeSet();
|
|
668
|
+
var numOfEdges = edges.length;
|
|
669
|
+
this._vertices.forEach(function (vertex) {
|
|
650
670
|
distMap.set(vertex, Infinity);
|
|
651
671
|
});
|
|
652
672
|
distMap.set(srcVertex, 0);
|
|
653
|
-
for (
|
|
654
|
-
for (
|
|
655
|
-
|
|
673
|
+
for (var i = 1; i < numOfVertices; ++i) {
|
|
674
|
+
for (var j = 0; j < numOfEdges; ++j) {
|
|
675
|
+
var ends = this.getEndsOfEdge(edges[j]);
|
|
656
676
|
if (ends) {
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
677
|
+
var _b = __read(ends, 2), s = _b[0], d = _b[1];
|
|
678
|
+
var weight = edges[j].weight;
|
|
679
|
+
var sWeight = distMap.get(s);
|
|
680
|
+
var dWeight = distMap.get(d);
|
|
661
681
|
if (sWeight !== undefined && dWeight !== undefined) {
|
|
662
682
|
if (distMap.get(s) !== Infinity && sWeight + weight < dWeight) {
|
|
663
683
|
distMap.set(d, sWeight + weight);
|
|
@@ -667,9 +687,9 @@ class AbstractGraph {
|
|
|
667
687
|
}
|
|
668
688
|
}
|
|
669
689
|
}
|
|
670
|
-
|
|
690
|
+
var minDest = null;
|
|
671
691
|
if (getMin) {
|
|
672
|
-
distMap.forEach((d, v)
|
|
692
|
+
distMap.forEach(function (d, v) {
|
|
673
693
|
if (v !== srcVertex) {
|
|
674
694
|
if (d < min) {
|
|
675
695
|
min = d;
|
|
@@ -680,93 +700,67 @@ class AbstractGraph {
|
|
|
680
700
|
});
|
|
681
701
|
}
|
|
682
702
|
if (genPath) {
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
703
|
+
try {
|
|
704
|
+
for (var vertices_6 = __values(vertices), vertices_6_1 = vertices_6.next(); !vertices_6_1.done; vertices_6_1 = vertices_6.next()) {
|
|
705
|
+
var vertex = vertices_6_1.value;
|
|
706
|
+
var vertexOrId = vertex[1];
|
|
707
|
+
if (vertexOrId instanceof AbstractVertex) {
|
|
708
|
+
var path = [vertexOrId];
|
|
709
|
+
var parent_3 = preMap.get(vertexOrId);
|
|
710
|
+
while (parent_3 !== undefined) {
|
|
711
|
+
path.push(parent_3);
|
|
712
|
+
parent_3 = preMap.get(parent_3);
|
|
713
|
+
}
|
|
714
|
+
var reversed = path.reverse();
|
|
715
|
+
if (vertex[1] === minDest)
|
|
716
|
+
minPath = reversed;
|
|
717
|
+
paths.push(reversed);
|
|
691
718
|
}
|
|
692
|
-
const reversed = path.reverse();
|
|
693
|
-
if (vertex[1] === minDest)
|
|
694
|
-
minPath = reversed;
|
|
695
|
-
paths.push(reversed);
|
|
696
719
|
}
|
|
697
720
|
}
|
|
721
|
+
catch (e_14_1) { e_14 = { error: e_14_1 }; }
|
|
722
|
+
finally {
|
|
723
|
+
try {
|
|
724
|
+
if (vertices_6_1 && !vertices_6_1.done && (_a = vertices_6.return)) _a.call(vertices_6);
|
|
725
|
+
}
|
|
726
|
+
finally { if (e_14) throw e_14.error; }
|
|
727
|
+
}
|
|
698
728
|
}
|
|
699
|
-
for (
|
|
700
|
-
|
|
729
|
+
for (var j = 0; j < numOfEdges; ++j) {
|
|
730
|
+
var ends = this.getEndsOfEdge(edges[j]);
|
|
701
731
|
if (ends) {
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
732
|
+
var _c = __read(ends, 1), s = _c[0];
|
|
733
|
+
var weight = edges[j].weight;
|
|
734
|
+
var sWeight = distMap.get(s);
|
|
705
735
|
if (sWeight) {
|
|
706
736
|
if (sWeight !== Infinity && sWeight + weight < sWeight)
|
|
707
737
|
hasNegativeCycle = true;
|
|
708
738
|
}
|
|
709
739
|
}
|
|
710
740
|
}
|
|
711
|
-
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
715
|
-
* /
|
|
716
|
-
|
|
717
|
-
/**
|
|
718
|
-
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
719
|
-
* 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.
|
|
720
|
-
*/
|
|
721
|
-
/**
|
|
722
|
-
* BellmanFord time:O(VE) space:O(V)
|
|
723
|
-
* one to rest pairs
|
|
724
|
-
* 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.
|
|
725
|
-
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
726
|
-
*/
|
|
727
|
-
/**
|
|
728
|
-
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
729
|
-
* all pairs
|
|
730
|
-
* 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.
|
|
731
|
-
*/
|
|
732
|
-
/**
|
|
733
|
-
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
734
|
-
* all pairs
|
|
735
|
-
* /
|
|
736
|
-
|
|
737
|
-
/**
|
|
738
|
-
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
739
|
-
* all pairs
|
|
740
|
-
* 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.
|
|
741
|
-
* The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a
|
|
742
|
-
* graph.
|
|
743
|
-
* @returns The function `floyd()` returns an object with two properties: `costs` and `predecessor`. The `costs`
|
|
744
|
-
* property is a 2D array of numbers representing the shortest path costs between vertices in a graph. The
|
|
745
|
-
* `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
|
|
746
|
-
* path between vertices in the
|
|
747
|
-
*/
|
|
748
|
-
floyd() {
|
|
741
|
+
return { hasNegativeCycle: hasNegativeCycle, distMap: distMap, preMap: preMap, paths: paths, min: min, minPath: minPath };
|
|
742
|
+
};
|
|
743
|
+
AbstractGraph.prototype.floyd = function () {
|
|
749
744
|
var _a;
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
for (let i = 0; i < n; i++) {
|
|
745
|
+
var idAndVertices = __spreadArray([], __read(this._vertices), false);
|
|
746
|
+
var n = idAndVertices.length;
|
|
747
|
+
var costs = [];
|
|
748
|
+
var predecessor = [];
|
|
749
|
+
for (var i = 0; i < n; i++) {
|
|
756
750
|
costs[i] = [];
|
|
757
751
|
predecessor[i] = [];
|
|
758
|
-
for (
|
|
752
|
+
for (var j = 0; j < n; j++) {
|
|
759
753
|
predecessor[i][j] = null;
|
|
760
754
|
}
|
|
761
755
|
}
|
|
762
|
-
for (
|
|
763
|
-
for (
|
|
756
|
+
for (var i = 0; i < n; i++) {
|
|
757
|
+
for (var j = 0; j < n; j++) {
|
|
764
758
|
costs[i][j] = ((_a = this.getEdge(idAndVertices[i][1], idAndVertices[j][1])) === null || _a === void 0 ? void 0 : _a.weight) || Infinity;
|
|
765
759
|
}
|
|
766
760
|
}
|
|
767
|
-
for (
|
|
768
|
-
for (
|
|
769
|
-
for (
|
|
761
|
+
for (var k = 0; k < n; k++) {
|
|
762
|
+
for (var i = 0; i < n; i++) {
|
|
763
|
+
for (var j = 0; j < n; j++) {
|
|
770
764
|
if (costs[i][j] > costs[i][k] + costs[k][j]) {
|
|
771
765
|
costs[i][j] = costs[i][k] + costs[k][j];
|
|
772
766
|
predecessor[i][j] = idAndVertices[k][1];
|
|
@@ -774,42 +768,11 @@ class AbstractGraph {
|
|
|
774
768
|
}
|
|
775
769
|
}
|
|
776
770
|
}
|
|
777
|
-
return { costs, predecessor };
|
|
778
|
-
}
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
783
|
-
* Tarjan solve the bi-connected components of undirected graphs;
|
|
784
|
-
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
785
|
-
* /
|
|
786
|
-
|
|
787
|
-
/**
|
|
788
|
-
* Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs.
|
|
789
|
-
* Tarjan can find cycles in directed or undirected graph
|
|
790
|
-
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
|
|
791
|
-
* Tarjan solve the bi-connected components of undirected graphs;
|
|
792
|
-
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
793
|
-
* The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
|
|
794
|
-
* strongly connected components (SCCs), and cycles in a graph.
|
|
795
|
-
* @param {boolean} [needArticulationPoints] - A boolean value indicating whether or not to calculate and return the
|
|
796
|
-
* articulation points in the graph. Articulation points are the vertices in a graph whose removal would increase the
|
|
797
|
-
* number of connected components in the graph.
|
|
798
|
-
* @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
|
|
799
|
-
* (edges whose removal would increase the number of connected components in the graph).
|
|
800
|
-
* @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
|
|
801
|
-
* graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
|
|
802
|
-
* SCCs will not be calculated or returned.
|
|
803
|
-
* @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
|
|
804
|
-
* set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
|
|
805
|
-
* are arrays of vertices that form cycles within the SCCs.
|
|
806
|
-
* @returns The function `tarjan` returns an object with the following properties:
|
|
807
|
-
*/
|
|
808
|
-
tarjan(needArticulationPoints, needBridges, needSCCs, needCycles) {
|
|
809
|
-
// !! in undirected graph we will not let child visit parent when DFS
|
|
810
|
-
// !! articulation point(in DFS search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
|
|
811
|
-
// !! bridge: low(child) > dfn(cur)
|
|
812
|
-
const defaultConfig = false;
|
|
771
|
+
return { costs: costs, predecessor: predecessor };
|
|
772
|
+
};
|
|
773
|
+
AbstractGraph.prototype.tarjan = function (needArticulationPoints, needBridges, needSCCs, needCycles) {
|
|
774
|
+
var _this = this;
|
|
775
|
+
var defaultConfig = false;
|
|
813
776
|
if (needArticulationPoints === undefined)
|
|
814
777
|
needArticulationPoints = defaultConfig;
|
|
815
778
|
if (needBridges === undefined)
|
|
@@ -818,60 +781,69 @@ class AbstractGraph {
|
|
|
818
781
|
needSCCs = defaultConfig;
|
|
819
782
|
if (needCycles === undefined)
|
|
820
783
|
needCycles = defaultConfig;
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
vertices.forEach(v
|
|
784
|
+
var dfnMap = new Map();
|
|
785
|
+
var lowMap = new Map();
|
|
786
|
+
var vertices = this._vertices;
|
|
787
|
+
vertices.forEach(function (v) {
|
|
825
788
|
dfnMap.set(v, -1);
|
|
826
789
|
lowMap.set(v, Infinity);
|
|
827
790
|
});
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
791
|
+
var _a = __read(vertices.values(), 1), root = _a[0];
|
|
792
|
+
var articulationPoints = [];
|
|
793
|
+
var bridges = [];
|
|
794
|
+
var dfn = 0;
|
|
795
|
+
var dfs = function (cur, parent) {
|
|
796
|
+
var e_15, _a;
|
|
833
797
|
dfn++;
|
|
834
798
|
dfnMap.set(cur, dfn);
|
|
835
799
|
lowMap.set(cur, dfn);
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
const curLow = lowMap.get(cur);
|
|
846
|
-
// TODO after no-non-null-assertion not ensure the logic
|
|
847
|
-
if (curLow !== undefined && childLow !== undefined) {
|
|
848
|
-
lowMap.set(cur, Math.min(curLow, childLow));
|
|
849
|
-
}
|
|
850
|
-
const curFromMap = dfnMap.get(cur);
|
|
851
|
-
if (childLow !== undefined && curFromMap !== undefined) {
|
|
852
|
-
if (needArticulationPoints) {
|
|
853
|
-
if ((cur === root && childCount >= 2) || ((cur !== root) && (childLow >= curFromMap))) {
|
|
854
|
-
// todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) {
|
|
855
|
-
articulationPoints.push(cur);
|
|
856
|
-
}
|
|
800
|
+
var neighbors = _this.getNeighbors(cur);
|
|
801
|
+
var childCount = 0;
|
|
802
|
+
try {
|
|
803
|
+
for (var neighbors_6 = __values(neighbors), neighbors_6_1 = neighbors_6.next(); !neighbors_6_1.done; neighbors_6_1 = neighbors_6.next()) {
|
|
804
|
+
var neighbor = neighbors_6_1.value;
|
|
805
|
+
if (neighbor !== parent) {
|
|
806
|
+
if (dfnMap.get(neighbor) === -1) {
|
|
807
|
+
childCount++;
|
|
808
|
+
dfs(neighbor, cur);
|
|
857
809
|
}
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
810
|
+
var childLow = lowMap.get(neighbor);
|
|
811
|
+
var curLow = lowMap.get(cur);
|
|
812
|
+
if (curLow !== undefined && childLow !== undefined) {
|
|
813
|
+
lowMap.set(cur, Math.min(curLow, childLow));
|
|
814
|
+
}
|
|
815
|
+
var curFromMap = dfnMap.get(cur);
|
|
816
|
+
if (childLow !== undefined && curFromMap !== undefined) {
|
|
817
|
+
if (needArticulationPoints) {
|
|
818
|
+
if ((cur === root && childCount >= 2) || (cur !== root && childLow >= curFromMap)) {
|
|
819
|
+
articulationPoints.push(cur);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
if (needBridges) {
|
|
823
|
+
if (childLow > curFromMap) {
|
|
824
|
+
var edgeCurToNeighbor = _this.getEdge(cur, neighbor);
|
|
825
|
+
if (edgeCurToNeighbor) {
|
|
826
|
+
bridges.push(edgeCurToNeighbor);
|
|
827
|
+
}
|
|
863
828
|
}
|
|
864
829
|
}
|
|
865
830
|
}
|
|
866
831
|
}
|
|
867
832
|
}
|
|
868
833
|
}
|
|
834
|
+
catch (e_15_1) { e_15 = { error: e_15_1 }; }
|
|
835
|
+
finally {
|
|
836
|
+
try {
|
|
837
|
+
if (neighbors_6_1 && !neighbors_6_1.done && (_a = neighbors_6.return)) _a.call(neighbors_6);
|
|
838
|
+
}
|
|
839
|
+
finally { if (e_15) throw e_15.error; }
|
|
840
|
+
}
|
|
869
841
|
};
|
|
870
842
|
dfs(root, null);
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
lowMap.forEach((low, vertex)
|
|
843
|
+
var SCCs = new Map();
|
|
844
|
+
var getSCCs = function () {
|
|
845
|
+
var SCCs = new Map();
|
|
846
|
+
lowMap.forEach(function (low, vertex) {
|
|
875
847
|
var _a;
|
|
876
848
|
if (!SCCs.has(low)) {
|
|
877
849
|
SCCs.set(low, [vertex]);
|
|
@@ -885,37 +857,38 @@ class AbstractGraph {
|
|
|
885
857
|
if (needSCCs) {
|
|
886
858
|
SCCs = getSCCs();
|
|
887
859
|
}
|
|
888
|
-
|
|
860
|
+
var cycles = new Map();
|
|
889
861
|
if (needCycles) {
|
|
890
|
-
|
|
891
|
-
if (
|
|
892
|
-
|
|
862
|
+
var SCCs_1 = new Map();
|
|
863
|
+
if (SCCs_1.size < 1) {
|
|
864
|
+
SCCs_1 = getSCCs();
|
|
893
865
|
}
|
|
894
|
-
|
|
866
|
+
SCCs_1.forEach(function (SCC, low) {
|
|
895
867
|
if (SCC.length > 1) {
|
|
896
868
|
cycles.set(low, SCC);
|
|
897
869
|
}
|
|
898
870
|
});
|
|
899
871
|
}
|
|
900
|
-
return { dfnMap, lowMap, bridges, articulationPoints, SCCs, cycles };
|
|
901
|
-
}
|
|
902
|
-
_addVertexOnly(newVertex) {
|
|
872
|
+
return { dfnMap: dfnMap, lowMap: lowMap, bridges: bridges, articulationPoints: articulationPoints, SCCs: SCCs, cycles: cycles };
|
|
873
|
+
};
|
|
874
|
+
AbstractGraph.prototype._addVertexOnly = function (newVertex) {
|
|
903
875
|
if (this.hasVertex(newVertex)) {
|
|
904
876
|
return false;
|
|
905
|
-
// throw (new Error('Duplicated vertex id is not allowed'));
|
|
906
877
|
}
|
|
907
878
|
this._vertices.set(newVertex.id, newVertex);
|
|
908
879
|
return true;
|
|
909
|
-
}
|
|
910
|
-
_getVertex(vertexOrId) {
|
|
911
|
-
|
|
880
|
+
};
|
|
881
|
+
AbstractGraph.prototype._getVertex = function (vertexOrId) {
|
|
882
|
+
var vertexId = this._getVertexId(vertexOrId);
|
|
912
883
|
return this._vertices.get(vertexId) || null;
|
|
913
|
-
}
|
|
914
|
-
_getVertexId(vertexOrId) {
|
|
884
|
+
};
|
|
885
|
+
AbstractGraph.prototype._getVertexId = function (vertexOrId) {
|
|
915
886
|
return vertexOrId instanceof AbstractVertex ? vertexOrId.id : vertexOrId;
|
|
916
|
-
}
|
|
917
|
-
_setVertices(value) {
|
|
887
|
+
};
|
|
888
|
+
AbstractGraph.prototype._setVertices = function (value) {
|
|
918
889
|
this._vertices = value;
|
|
919
|
-
}
|
|
920
|
-
|
|
890
|
+
};
|
|
891
|
+
return AbstractGraph;
|
|
892
|
+
}());
|
|
921
893
|
exports.AbstractGraph = AbstractGraph;
|
|
894
|
+
//# sourceMappingURL=abstract-graph.js.map
|