data-structure-typed 2.6.0 → 2.6.2
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/.github/workflows/ci.yml +7 -2
- package/.github/workflows/release-package.yml +9 -2
- package/CHANGELOG.md +9 -1
- package/dist/cjs/binary-tree.cjs +2927 -23688
- package/dist/cjs/graph.cjs +845 -2634
- package/dist/cjs/hash.cjs +185 -616
- package/dist/cjs/heap.cjs +266 -818
- package/dist/cjs/index.cjs +5116 -31358
- package/dist/cjs/linked-list.cjs +644 -2615
- package/dist/cjs/matrix.cjs +220 -535
- package/dist/cjs/priority-queue.cjs +266 -818
- package/dist/cjs/queue.cjs +637 -2343
- package/dist/cjs/stack.cjs +124 -530
- package/dist/cjs/trie.cjs +145 -592
- package/dist/cjs-legacy/binary-tree.cjs +4352 -25113
- package/dist/cjs-legacy/graph.cjs +847 -2636
- package/dist/cjs-legacy/hash.cjs +181 -612
- package/dist/cjs-legacy/heap.cjs +266 -818
- package/dist/cjs-legacy/index.cjs +6657 -32899
- package/dist/cjs-legacy/linked-list.cjs +640 -2611
- package/dist/cjs-legacy/matrix.cjs +220 -535
- package/dist/cjs-legacy/priority-queue.cjs +266 -818
- package/dist/cjs-legacy/queue.cjs +634 -2340
- package/dist/cjs-legacy/stack.cjs +124 -530
- package/dist/cjs-legacy/trie.cjs +145 -592
- package/dist/esm/binary-tree.mjs +2927 -23688
- package/dist/esm/graph.mjs +845 -2634
- package/dist/esm/hash.mjs +185 -616
- package/dist/esm/heap.mjs +266 -818
- package/dist/esm/index.mjs +5116 -31358
- package/dist/esm/linked-list.mjs +644 -2615
- package/dist/esm/matrix.mjs +220 -535
- package/dist/esm/priority-queue.mjs +266 -818
- package/dist/esm/queue.mjs +637 -2343
- package/dist/esm/stack.mjs +124 -530
- package/dist/esm/trie.mjs +145 -592
- package/dist/esm-legacy/binary-tree.mjs +4352 -25113
- package/dist/esm-legacy/graph.mjs +847 -2636
- package/dist/esm-legacy/hash.mjs +181 -612
- package/dist/esm-legacy/heap.mjs +266 -818
- package/dist/esm-legacy/index.mjs +6657 -32899
- package/dist/esm-legacy/linked-list.mjs +640 -2611
- package/dist/esm-legacy/matrix.mjs +220 -535
- package/dist/esm-legacy/priority-queue.mjs +266 -818
- package/dist/esm-legacy/queue.mjs +634 -2340
- package/dist/esm-legacy/stack.mjs +124 -530
- package/dist/esm-legacy/trie.mjs +145 -592
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +29 -500
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +38 -563
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +230 -1212
- package/dist/types/data-structures/binary-tree/bst.d.ts +124 -1063
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +99 -854
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +62 -314
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +366 -5057
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +277 -4885
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +253 -3929
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +261 -4782
- package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -44
- package/dist/types/data-structures/graph/directed-graph.d.ts +130 -527
- package/dist/types/data-structures/graph/undirected-graph.d.ts +125 -482
- package/dist/types/data-structures/hash/hash-map.d.ts +132 -562
- package/dist/types/data-structures/heap/heap.d.ts +194 -746
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +120 -809
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -733
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +193 -863
- package/dist/types/data-structures/matrix/matrix.d.ts +159 -474
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -6
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +6 -11
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/types/data-structures/queue/deque.d.ts +196 -804
- package/dist/types/data-structures/queue/queue.d.ts +99 -585
- package/dist/types/data-structures/stack/stack.d.ts +75 -481
- package/dist/types/data-structures/trie/trie.d.ts +98 -584
- package/dist/umd/data-structure-typed.js +6580 -32822
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +162 -215
- package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +9 -9
- package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +47 -47
- package/docs-site-docusaurus/docs/api/classes/BST.md +155 -208
- package/docs-site-docusaurus/docs/api/classes/BSTNode.md +7 -7
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -29
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +104 -158
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/Deque.md +253 -101
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +60 -80
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +244 -114
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +6 -6
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +52 -48
- package/docs-site-docusaurus/docs/api/classes/Heap.md +119 -64
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +84 -14
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +138 -34
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +149 -41
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +172 -92
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +60 -80
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +57 -52
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +117 -62
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +120 -70
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +117 -62
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +125 -75
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +119 -64
- package/docs-site-docusaurus/docs/api/classes/Queue.md +158 -74
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +171 -225
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +10 -22
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +172 -94
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +4 -4
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +98 -75
- package/docs-site-docusaurus/docs/api/classes/Stack.md +112 -50
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +104 -129
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +90 -121
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +298 -107
- package/docs-site-docusaurus/docs/api/classes/Trie.md +116 -58
- package/docs-site-docusaurus/docs/api/classes/TrieNode.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +59 -77
- package/package.json +45 -46
- package/src/common/error.ts +15 -32
- package/src/common/index.ts +0 -3
- package/src/data-structures/base/iterable-element-base.ts +0 -3
- package/src/data-structures/base/linear-base.ts +2 -36
- package/src/data-structures/binary-tree/avl-tree.ts +31 -529
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -572
- package/src/data-structures/binary-tree/binary-tree.ts +326 -1311
- package/src/data-structures/binary-tree/bst.ts +158 -1082
- package/src/data-structures/binary-tree/red-black-tree.ts +451 -1290
- package/src/data-structures/binary-tree/segment-tree.ts +73 -351
- package/src/data-structures/binary-tree/tree-map.ts +462 -5124
- package/src/data-structures/binary-tree/tree-multi-map.ts +302 -4914
- package/src/data-structures/binary-tree/tree-multi-set.ts +299 -3983
- package/src/data-structures/binary-tree/tree-set.ts +338 -4836
- package/src/data-structures/graph/abstract-graph.ts +98 -167
- package/src/data-structures/graph/directed-graph.ts +137 -562
- package/src/data-structures/graph/map-graph.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +132 -511
- package/src/data-structures/hash/hash-map.ts +154 -582
- package/src/data-structures/heap/heap.ts +200 -795
- package/src/data-structures/linked-list/doubly-linked-list.ts +121 -865
- package/src/data-structures/linked-list/singly-linked-list.ts +122 -794
- package/src/data-structures/linked-list/skip-linked-list.ts +211 -918
- package/src/data-structures/matrix/matrix.ts +179 -518
- package/src/data-structures/matrix/navigator.ts +0 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -6
- package/src/data-structures/priority-queue/min-priority-queue.ts +6 -11
- package/src/data-structures/priority-queue/priority-queue.ts +1 -2
- package/src/data-structures/queue/deque.ts +214 -882
- package/src/data-structures/queue/queue.ts +102 -625
- package/src/data-structures/stack/stack.ts +76 -505
- package/src/data-structures/trie/trie.ts +98 -628
- package/src/types/common.ts +0 -10
- package/src/types/data-structures/binary-tree/bst.ts +0 -7
- package/src/types/data-structures/binary-tree/red-black-tree.ts +0 -1
- package/src/types/data-structures/graph/abstract-graph.ts +0 -2
- package/src/types/data-structures/hash/hash-map.ts +0 -3
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/matrix/navigator.ts +0 -2
- package/src/types/utils/utils.ts +0 -7
- package/src/types/utils/validate-type.ts +0 -7
- package/src/utils/number.ts +0 -2
- package/src/utils/utils.ts +0 -5
|
@@ -726,61 +726,19 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
726
726
|
return this._elements;
|
|
727
727
|
}
|
|
728
728
|
/**
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
* @example
|
|
775
|
-
* // Track heap capacity
|
|
776
|
-
* const heap = new Heap<number>();
|
|
777
|
-
* console.log(heap.size); // 0;
|
|
778
|
-
* heap.add(10);
|
|
779
|
-
* heap.add(20);
|
|
780
|
-
* console.log(heap.size); // 2;
|
|
781
|
-
* heap.poll();
|
|
782
|
-
* console.log(heap.size); // 1;
|
|
783
|
-
*/
|
|
729
|
+
* Get the number of elements.
|
|
730
|
+
* @remarks Time O(1), Space O(1)
|
|
731
|
+
* @returns Heap size.
|
|
732
|
+
* @example
|
|
733
|
+
* // Track heap capacity
|
|
734
|
+
* const heap = new Heap<number>();
|
|
735
|
+
* console.log(heap.size); // 0;
|
|
736
|
+
* heap.add(10);
|
|
737
|
+
* heap.add(20);
|
|
738
|
+
* console.log(heap.size); // 2;
|
|
739
|
+
* heap.poll();
|
|
740
|
+
* console.log(heap.size); // 1;
|
|
741
|
+
*/
|
|
784
742
|
get size() {
|
|
785
743
|
return this.elements.length;
|
|
786
744
|
}
|
|
@@ -793,6 +751,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
793
751
|
var _a;
|
|
794
752
|
return (_a = this.elements[this.size - 1]) != null ? _a : void 0;
|
|
795
753
|
}
|
|
754
|
+
/**
|
|
755
|
+
* Get the comparator used to order elements.
|
|
756
|
+
* @remarks Time O(1), Space O(1)
|
|
757
|
+
* @returns Comparator function.
|
|
758
|
+
*/
|
|
759
|
+
get comparator() {
|
|
760
|
+
return this._comparator;
|
|
761
|
+
}
|
|
796
762
|
/**
|
|
797
763
|
* Create a heap of the same class from an iterable.
|
|
798
764
|
* @remarks Time O(N), Space O(N)
|
|
@@ -819,123 +785,42 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
819
785
|
return new _Heap(elements, options);
|
|
820
786
|
}
|
|
821
787
|
/**
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
* @example
|
|
869
|
-
* // basic Heap creation and add operation
|
|
870
|
-
* // Create a min heap (default)
|
|
871
|
-
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
872
|
-
*
|
|
873
|
-
* // Verify size
|
|
874
|
-
* console.log(minHeap.size); // 6;
|
|
875
|
-
*
|
|
876
|
-
* // Add new element
|
|
877
|
-
* minHeap.add(4);
|
|
878
|
-
* console.log(minHeap.size); // 7;
|
|
879
|
-
*
|
|
880
|
-
* // Min heap property: smallest element at root
|
|
881
|
-
* const min = minHeap.peek();
|
|
882
|
-
* console.log(min); // 1;
|
|
883
|
-
*/
|
|
788
|
+
* Insert an element.
|
|
789
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
790
|
+
* @param element - Element to insert.
|
|
791
|
+
* @returns True.
|
|
792
|
+
* @example
|
|
793
|
+
* // basic Heap creation and add operation
|
|
794
|
+
* // Create a min heap (default)
|
|
795
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
796
|
+
*
|
|
797
|
+
* // Verify size
|
|
798
|
+
* console.log(minHeap.size); // 6;
|
|
799
|
+
*
|
|
800
|
+
* // Add new element
|
|
801
|
+
* minHeap.add(4);
|
|
802
|
+
* console.log(minHeap.size); // 7;
|
|
803
|
+
*
|
|
804
|
+
* // Min heap property: smallest element at root
|
|
805
|
+
* const min = minHeap.peek();
|
|
806
|
+
* console.log(min); // 1;
|
|
807
|
+
*/
|
|
884
808
|
add(element) {
|
|
885
809
|
this._elements.push(element);
|
|
886
810
|
return this._bubbleUp(this.elements.length - 1);
|
|
887
811
|
}
|
|
888
812
|
/**
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
* @example
|
|
933
|
-
* // Add multiple elements
|
|
934
|
-
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
935
|
-
* heap.addMany([5, 3, 7, 1]);
|
|
936
|
-
* console.log(heap.peek()); // 1;
|
|
937
|
-
* console.log(heap.size); // 4;
|
|
938
|
-
*/
|
|
813
|
+
* Insert many elements from an iterable.
|
|
814
|
+
* @remarks Time O(N log N), Space O(1)
|
|
815
|
+
* @param elements - Iterable of elements or raw values.
|
|
816
|
+
* @returns Array of per-element success flags.
|
|
817
|
+
* @example
|
|
818
|
+
* // Add multiple elements
|
|
819
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
820
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
821
|
+
* console.log(heap.peek()); // 1;
|
|
822
|
+
* console.log(heap.size); // 4;
|
|
823
|
+
*/
|
|
939
824
|
addMany(elements) {
|
|
940
825
|
const flags = [];
|
|
941
826
|
for (const el of elements) {
|
|
@@ -950,104 +835,63 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
950
835
|
return flags;
|
|
951
836
|
}
|
|
952
837
|
/**
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
1011
|
-
* });
|
|
1012
|
-
*
|
|
1013
|
-
* console.log(maxHeap.size); // 3;
|
|
1014
|
-
*
|
|
1015
|
-
* // Peek returns highest priority task
|
|
1016
|
-
* const topTask = maxHeap.peek();
|
|
1017
|
-
* console.log(topTask?.priority); // 8;
|
|
1018
|
-
* console.log(topTask?.name); // 'Alert';
|
|
1019
|
-
*/
|
|
1020
|
-
/**
|
|
1021
|
-
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
* @example
|
|
1026
|
-
* // Heap with custom comparator (MaxHeap behavior)
|
|
1027
|
-
* interface Task {
|
|
1028
|
-
* id: number;
|
|
1029
|
-
* priority: number;
|
|
1030
|
-
* name: string;
|
|
1031
|
-
* }
|
|
1032
|
-
*
|
|
1033
|
-
* // Custom comparator for max heap behavior (higher priority first)
|
|
1034
|
-
* const tasks: Task[] = [
|
|
1035
|
-
* { id: 1, priority: 5, name: 'Email' },
|
|
1036
|
-
* { id: 2, priority: 3, name: 'Chat' },
|
|
1037
|
-
* { id: 3, priority: 8, name: 'Alert' }
|
|
1038
|
-
* ];
|
|
1039
|
-
*
|
|
1040
|
-
* const maxHeap = new Heap(tasks, {
|
|
1041
|
-
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
1042
|
-
* });
|
|
1043
|
-
*
|
|
1044
|
-
* console.log(maxHeap.size); // 3;
|
|
1045
|
-
*
|
|
1046
|
-
* // Peek returns highest priority task
|
|
1047
|
-
* const topTask = maxHeap.peek();
|
|
1048
|
-
* console.log(topTask?.priority); // 8;
|
|
1049
|
-
* console.log(topTask?.name); // 'Alert';
|
|
1050
|
-
*/
|
|
838
|
+
* Remove and return the top element.
|
|
839
|
+
* @remarks Time O(log N), Space O(1)
|
|
840
|
+
* @returns Top element or undefined.
|
|
841
|
+
* @example
|
|
842
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
843
|
+
* interface Task {
|
|
844
|
+
* id: number;
|
|
845
|
+
* priority: number;
|
|
846
|
+
* name: string;
|
|
847
|
+
* }
|
|
848
|
+
*
|
|
849
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
850
|
+
* const tasks: Task[] = [
|
|
851
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
852
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
853
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
854
|
+
* ];
|
|
855
|
+
*
|
|
856
|
+
* const maxHeap = new Heap(tasks, {
|
|
857
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
858
|
+
* });
|
|
859
|
+
*
|
|
860
|
+
* console.log(maxHeap.size); // 3;
|
|
861
|
+
*
|
|
862
|
+
* // Peek returns highest priority task
|
|
863
|
+
* const topTask = maxHeap.peek();
|
|
864
|
+
* console.log(topTask?.priority); // 8;
|
|
865
|
+
* console.log(topTask?.name); // 'Alert';
|
|
866
|
+
*/
|
|
867
|
+
/**
|
|
868
|
+
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
869
|
+
* @example
|
|
870
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
871
|
+
* interface Task {
|
|
872
|
+
* id: number;
|
|
873
|
+
* priority: number;
|
|
874
|
+
* name: string;
|
|
875
|
+
* }
|
|
876
|
+
*
|
|
877
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
878
|
+
* const tasks: Task[] = [
|
|
879
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
880
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
881
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
882
|
+
* ];
|
|
883
|
+
*
|
|
884
|
+
* const maxHeap = new Heap(tasks, {
|
|
885
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
886
|
+
* });
|
|
887
|
+
*
|
|
888
|
+
* console.log(maxHeap.size); // 3;
|
|
889
|
+
*
|
|
890
|
+
* // Peek returns highest priority task
|
|
891
|
+
* const topTask = maxHeap.peek();
|
|
892
|
+
* console.log(topTask?.priority); // 8;
|
|
893
|
+
* console.log(topTask?.name); // 'Alert';
|
|
894
|
+
*/
|
|
1051
895
|
poll() {
|
|
1052
896
|
return this.pop();
|
|
1053
897
|
}
|
|
@@ -1067,319 +911,125 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1067
911
|
return value;
|
|
1068
912
|
}
|
|
1069
913
|
/**
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
* });
|
|
1134
|
-
*
|
|
1135
|
-
* // Add events in random order
|
|
1136
|
-
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
1137
|
-
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
1138
|
-
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
1139
|
-
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
1140
|
-
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
1141
|
-
*
|
|
1142
|
-
* console.log(eventHeap.size); // 5;
|
|
1143
|
-
*
|
|
1144
|
-
* // Process events by priority (critical first)
|
|
1145
|
-
* const processedOrder: Event[] = [];
|
|
1146
|
-
* while (eventHeap.size > 0) {
|
|
1147
|
-
* const event = eventHeap.poll();
|
|
1148
|
-
* if (event) {
|
|
1149
|
-
* processedOrder.push(event);
|
|
1150
|
-
* }
|
|
1151
|
-
* }
|
|
1152
|
-
*
|
|
1153
|
-
* // Verify critical events came first
|
|
1154
|
-
* console.log(processedOrder[0].type); // 'critical';
|
|
1155
|
-
* console.log(processedOrder[1].type); // 'critical';
|
|
1156
|
-
* console.log(processedOrder[2].type); // 'warning';
|
|
1157
|
-
* console.log(processedOrder[3].type); // 'info';
|
|
1158
|
-
* console.log(processedOrder[4].type); // 'info';
|
|
1159
|
-
*
|
|
1160
|
-
* // Verify O(log n) operations
|
|
1161
|
-
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
1162
|
-
*
|
|
1163
|
-
* // Add - O(log n)
|
|
1164
|
-
* newHeap.add(2);
|
|
1165
|
-
* console.log(newHeap.size); // 5;
|
|
1166
|
-
*
|
|
1167
|
-
* // Poll - O(log n)
|
|
1168
|
-
* const removed = newHeap.poll();
|
|
1169
|
-
* console.log(removed); // 1;
|
|
1170
|
-
*
|
|
1171
|
-
* // Peek - O(1)
|
|
1172
|
-
* const top = newHeap.peek();
|
|
1173
|
-
* console.log(top); // 2;
|
|
1174
|
-
*/
|
|
914
|
+
* Get the current top element without removing it.
|
|
915
|
+
* @remarks Time O(1), Space O(1)
|
|
916
|
+
* @returns Top element or undefined.
|
|
917
|
+
* @example
|
|
918
|
+
* // Heap for event processing with priority
|
|
919
|
+
* interface Event {
|
|
920
|
+
* id: number;
|
|
921
|
+
* type: 'critical' | 'warning' | 'info';
|
|
922
|
+
* timestamp: number;
|
|
923
|
+
* message: string;
|
|
924
|
+
* }
|
|
925
|
+
*
|
|
926
|
+
* // Custom priority: critical > warning > info
|
|
927
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
928
|
+
*
|
|
929
|
+
* const eventHeap = new Heap<Event>([], {
|
|
930
|
+
* comparator: (a: Event, b: Event) => {
|
|
931
|
+
* const priorityA = priorityMap[a.type];
|
|
932
|
+
* const priorityB = priorityMap[b.type];
|
|
933
|
+
* return priorityB - priorityA; // Higher priority first
|
|
934
|
+
* }
|
|
935
|
+
* });
|
|
936
|
+
*
|
|
937
|
+
* // Add events in random order
|
|
938
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
939
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
940
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
941
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
942
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
943
|
+
*
|
|
944
|
+
* console.log(eventHeap.size); // 5;
|
|
945
|
+
*
|
|
946
|
+
* // Process events by priority (critical first)
|
|
947
|
+
* const processedOrder: Event[] = [];
|
|
948
|
+
* while (eventHeap.size > 0) {
|
|
949
|
+
* const event = eventHeap.poll();
|
|
950
|
+
* if (event) {
|
|
951
|
+
* processedOrder.push(event);
|
|
952
|
+
* }
|
|
953
|
+
* }
|
|
954
|
+
*
|
|
955
|
+
* // Verify critical events came first
|
|
956
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
957
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
958
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
959
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
960
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
961
|
+
*
|
|
962
|
+
* // Verify O(log n) operations
|
|
963
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
964
|
+
*
|
|
965
|
+
* // Add - O(log n)
|
|
966
|
+
* newHeap.add(2);
|
|
967
|
+
* console.log(newHeap.size); // 5;
|
|
968
|
+
*
|
|
969
|
+
* // Poll - O(log n)
|
|
970
|
+
* const removed = newHeap.poll();
|
|
971
|
+
* console.log(removed); // 1;
|
|
972
|
+
*
|
|
973
|
+
* // Peek - O(1)
|
|
974
|
+
* const top = newHeap.peek();
|
|
975
|
+
* console.log(top); // 2;
|
|
976
|
+
*/
|
|
1175
977
|
peek() {
|
|
1176
978
|
return this.elements[0];
|
|
1177
979
|
}
|
|
1178
980
|
/**
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
* @example
|
|
1223
|
-
* // Check if heap is empty
|
|
1224
|
-
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
1225
|
-
* console.log(heap.isEmpty()); // true;
|
|
1226
|
-
* heap.add(1);
|
|
1227
|
-
* console.log(heap.isEmpty()); // false;
|
|
1228
|
-
*/
|
|
981
|
+
* Check whether the heap is empty.
|
|
982
|
+
* @remarks Time O(1), Space O(1)
|
|
983
|
+
* @returns True if size is 0.
|
|
984
|
+
* @example
|
|
985
|
+
* // Check if heap is empty
|
|
986
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
987
|
+
* console.log(heap.isEmpty()); // true;
|
|
988
|
+
* heap.add(1);
|
|
989
|
+
* console.log(heap.isEmpty()); // false;
|
|
990
|
+
*/
|
|
1229
991
|
isEmpty() {
|
|
1230
992
|
return this.size === 0;
|
|
1231
993
|
}
|
|
1232
994
|
/**
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
* @example
|
|
1277
|
-
* // Remove all elements
|
|
1278
|
-
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1279
|
-
* heap.clear();
|
|
1280
|
-
* console.log(heap.isEmpty()); // true;
|
|
1281
|
-
*/
|
|
995
|
+
* Remove all elements.
|
|
996
|
+
* @remarks Time O(1), Space O(1)
|
|
997
|
+
* @returns void
|
|
998
|
+
* @example
|
|
999
|
+
* // Remove all elements
|
|
1000
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1001
|
+
* heap.clear();
|
|
1002
|
+
* console.log(heap.isEmpty()); // true;
|
|
1003
|
+
*/
|
|
1282
1004
|
clear() {
|
|
1283
1005
|
this._elements = [];
|
|
1284
1006
|
}
|
|
1285
1007
|
/**
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
* @example
|
|
1324
|
-
* // Check element existence
|
|
1325
|
-
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
1326
|
-
* console.log(heap.has(1)); // true;
|
|
1327
|
-
* console.log(heap.has(99)); // false;
|
|
1328
|
-
*/
|
|
1008
|
+
* Check if an equal element exists in the heap.
|
|
1009
|
+
* @remarks Time O(N), Space O(1)
|
|
1010
|
+
* @param element - Element to search for.
|
|
1011
|
+
* @returns True if found.
|
|
1012
|
+
* @example
|
|
1013
|
+
* // Check element existence
|
|
1014
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
1015
|
+
* console.log(heap.has(1)); // true;
|
|
1016
|
+
* console.log(heap.has(99)); // false;
|
|
1017
|
+
*/
|
|
1329
1018
|
has(element) {
|
|
1330
1019
|
for (const el of this.elements) if (this._equals(el, element)) return true;
|
|
1331
1020
|
return false;
|
|
1332
1021
|
}
|
|
1333
1022
|
/**
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
* @example
|
|
1378
|
-
* // Remove specific element
|
|
1379
|
-
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
1380
|
-
* heap.delete(4);
|
|
1381
|
-
* console.log(heap.toArray().includes(4)); // false;
|
|
1382
|
-
*/
|
|
1023
|
+
* Delete one occurrence of an element.
|
|
1024
|
+
* @remarks Time O(N), Space O(1)
|
|
1025
|
+
* @param element - Element to delete.
|
|
1026
|
+
* @returns True if an element was removed.
|
|
1027
|
+
* @example
|
|
1028
|
+
* // Remove specific element
|
|
1029
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
1030
|
+
* heap.delete(4);
|
|
1031
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
1032
|
+
*/
|
|
1383
1033
|
delete(element) {
|
|
1384
1034
|
let index = -1;
|
|
1385
1035
|
for (let i = 0; i < this.elements.length; i++) {
|
|
@@ -1443,49 +1093,16 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1443
1093
|
return this;
|
|
1444
1094
|
}
|
|
1445
1095
|
/**
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
* @example
|
|
1484
|
-
* // Depth-first traversal
|
|
1485
|
-
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
1486
|
-
* const result = heap.dfs('IN');
|
|
1487
|
-
* console.log(result.length); // 3;
|
|
1488
|
-
*/
|
|
1096
|
+
* Traverse the binary heap as a complete binary tree and collect elements.
|
|
1097
|
+
* @remarks Time O(N), Space O(H)
|
|
1098
|
+
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
1099
|
+
* @returns Array of visited elements.
|
|
1100
|
+
* @example
|
|
1101
|
+
* // Depth-first traversal
|
|
1102
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
1103
|
+
* const result = heap.dfs('IN');
|
|
1104
|
+
* console.log(result.length); // 3;
|
|
1105
|
+
*/
|
|
1489
1106
|
dfs(order = "PRE") {
|
|
1490
1107
|
const result = [];
|
|
1491
1108
|
const _dfs = /* @__PURE__ */ __name((index) => {
|
|
@@ -1522,57 +1139,15 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1522
1139
|
return results;
|
|
1523
1140
|
}
|
|
1524
1141
|
/**
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
* @example
|
|
1571
|
-
* // Sort elements using heap
|
|
1572
|
-
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
1573
|
-
* const sorted = heap.sort();
|
|
1574
|
-
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
1575
|
-
*/
|
|
1142
|
+
* Return all elements in ascending order by repeatedly polling.
|
|
1143
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1144
|
+
* @returns Sorted array of elements.
|
|
1145
|
+
* @example
|
|
1146
|
+
* // Sort elements using heap
|
|
1147
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
1148
|
+
* const sorted = heap.sort();
|
|
1149
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
1150
|
+
*/
|
|
1576
1151
|
sort() {
|
|
1577
1152
|
const visited = [];
|
|
1578
1153
|
const cloned = this._createInstance();
|
|
@@ -1584,114 +1159,34 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1584
1159
|
return visited;
|
|
1585
1160
|
}
|
|
1586
1161
|
/**
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
* @example
|
|
1631
|
-
* // Create independent copy
|
|
1632
|
-
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
1633
|
-
* const copy = heap.clone();
|
|
1634
|
-
* copy.poll();
|
|
1635
|
-
* console.log(heap.size); // 3;
|
|
1636
|
-
* console.log(copy.size); // 2;
|
|
1637
|
-
*/
|
|
1162
|
+
* Deep clone this heap.
|
|
1163
|
+
* @remarks Time O(N), Space O(N)
|
|
1164
|
+
* @returns A new heap with the same elements.
|
|
1165
|
+
* @example
|
|
1166
|
+
* // Create independent copy
|
|
1167
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
1168
|
+
* const copy = heap.clone();
|
|
1169
|
+
* copy.poll();
|
|
1170
|
+
* console.log(heap.size); // 3;
|
|
1171
|
+
* console.log(copy.size); // 2;
|
|
1172
|
+
*/
|
|
1638
1173
|
clone() {
|
|
1639
1174
|
const next = this._createInstance();
|
|
1640
1175
|
for (const x of this.elements) next.add(x);
|
|
1641
1176
|
return next;
|
|
1642
1177
|
}
|
|
1643
1178
|
/**
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
* @example
|
|
1690
|
-
* // Filter elements
|
|
1691
|
-
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
1692
|
-
* const evens = heap.filter(x => x % 2 === 0);
|
|
1693
|
-
* console.log(evens.size); // 2;
|
|
1694
|
-
*/
|
|
1179
|
+
* Filter elements into a new heap of the same class.
|
|
1180
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1181
|
+
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
1182
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1183
|
+
* @returns A new heap with the kept elements.
|
|
1184
|
+
* @example
|
|
1185
|
+
* // Filter elements
|
|
1186
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
1187
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
1188
|
+
* console.log(evens.size); // 2;
|
|
1189
|
+
*/
|
|
1695
1190
|
filter(callback, thisArg) {
|
|
1696
1191
|
const out = this._createInstance();
|
|
1697
1192
|
let i = 0;
|
|
@@ -1705,59 +1200,20 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1705
1200
|
return out;
|
|
1706
1201
|
}
|
|
1707
1202
|
/**
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
* @example
|
|
1756
|
-
* // Transform elements
|
|
1757
|
-
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1758
|
-
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
1759
|
-
* console.log(doubled.peek()); // 2;
|
|
1760
|
-
*/
|
|
1203
|
+
* Map elements into a new heap of possibly different element type.
|
|
1204
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1205
|
+
* @template EM
|
|
1206
|
+
* @template RM
|
|
1207
|
+
* @param callback - Mapping function (element, index, heap) → newElement.
|
|
1208
|
+
* @param options - Options for the output heap, including comparator for EM.
|
|
1209
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1210
|
+
* @returns A new heap with mapped elements.
|
|
1211
|
+
* @example
|
|
1212
|
+
* // Transform elements
|
|
1213
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1214
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
1215
|
+
* console.log(doubled.peek()); // 2;
|
|
1216
|
+
*/
|
|
1761
1217
|
map(callback, options, thisArg) {
|
|
1762
1218
|
const { comparator, toElementFn, ...rest } = options != null ? options : {};
|
|
1763
1219
|
if (!comparator) raise(TypeError, ERR.comparatorRequired("Heap.map"));
|
|
@@ -1785,14 +1241,6 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1785
1241
|
}
|
|
1786
1242
|
return out;
|
|
1787
1243
|
}
|
|
1788
|
-
/**
|
|
1789
|
-
* Get the comparator used to order elements.
|
|
1790
|
-
* @remarks Time O(1), Space O(1)
|
|
1791
|
-
* @returns Comparator function.
|
|
1792
|
-
*/
|
|
1793
|
-
get comparator() {
|
|
1794
|
-
return this._comparator;
|
|
1795
|
-
}
|
|
1796
1244
|
*_getIterator() {
|
|
1797
1245
|
for (const element of this.elements) yield element;
|
|
1798
1246
|
}
|
|
@@ -1917,124 +1365,32 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1917
1365
|
this._autoCompactRatio = value;
|
|
1918
1366
|
}
|
|
1919
1367
|
/**
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
* @example
|
|
1966
|
-
* // Track queue length
|
|
1967
|
-
* const q = new Queue<number>();
|
|
1968
|
-
* console.log(q.length); // 0;
|
|
1969
|
-
* q.push(1);
|
|
1970
|
-
* q.push(2);
|
|
1971
|
-
* console.log(q.length); // 2;
|
|
1972
|
-
*/
|
|
1368
|
+
* Get the number of elements currently in the queue.
|
|
1369
|
+
* @remarks Time O(1), Space O(1)
|
|
1370
|
+
* @returns Current length.
|
|
1371
|
+
* @example
|
|
1372
|
+
* // Track queue length
|
|
1373
|
+
* const q = new Queue<number>();
|
|
1374
|
+
* console.log(q.length); // 0;
|
|
1375
|
+
* q.push(1);
|
|
1376
|
+
* q.push(2);
|
|
1377
|
+
* console.log(q.length); // 2;
|
|
1378
|
+
*/
|
|
1973
1379
|
get length() {
|
|
1974
1380
|
return this.elements.length - this._offset;
|
|
1975
1381
|
}
|
|
1976
1382
|
/**
|
|
1977
|
-
|
|
1978
|
-
* @remarks Time O(1), Space O(1)
|
|
1979
|
-
* @returns Front element or undefined.
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
* @example
|
|
2023
|
-
* // View the front element
|
|
2024
|
-
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
2025
|
-
* console.log(q.first); // 'first';
|
|
2026
|
-
* console.log(q.length); // 3;
|
|
2027
|
-
*/
|
|
2028
|
-
get first() {
|
|
2029
|
-
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
2030
|
-
}
|
|
2031
|
-
/**
|
|
2032
|
-
* Peek at the front element without removing it (alias for `first`).
|
|
1383
|
+
* Get the first element (front) without removing it.
|
|
2033
1384
|
* @remarks Time O(1), Space O(1)
|
|
2034
1385
|
* @returns Front element or undefined.
|
|
1386
|
+
* @example
|
|
1387
|
+
* // View the front element
|
|
1388
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
1389
|
+
* console.log(q.first); // 'first';
|
|
1390
|
+
* console.log(q.length); // 3;
|
|
2035
1391
|
*/
|
|
2036
|
-
|
|
2037
|
-
return this.
|
|
1392
|
+
get first() {
|
|
1393
|
+
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
2038
1394
|
}
|
|
2039
1395
|
/**
|
|
2040
1396
|
* Get the last element (back) without removing it.
|
|
@@ -2055,131 +1411,55 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2055
1411
|
return new _Queue(elements);
|
|
2056
1412
|
}
|
|
2057
1413
|
/**
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
* @example
|
|
2104
|
-
* // Queue for...of iteration and isEmpty check
|
|
2105
|
-
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
2106
|
-
*
|
|
2107
|
-
* const elements: string[] = [];
|
|
2108
|
-
* for (const item of queue) {
|
|
2109
|
-
* elements.push(item);
|
|
2110
|
-
* }
|
|
2111
|
-
*
|
|
2112
|
-
* // Verify all elements are iterated in order
|
|
2113
|
-
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
2114
|
-
*
|
|
2115
|
-
* // Process all elements
|
|
2116
|
-
* while (queue.length > 0) {
|
|
2117
|
-
* queue.shift();
|
|
2118
|
-
* }
|
|
2119
|
-
*
|
|
2120
|
-
* console.log(queue.length); // 0;
|
|
2121
|
-
*/
|
|
1414
|
+
* Peek at the front element without removing it (alias for `first`).
|
|
1415
|
+
* @remarks Time O(1), Space O(1)
|
|
1416
|
+
* @returns Front element or undefined.
|
|
1417
|
+
*/
|
|
1418
|
+
peek() {
|
|
1419
|
+
return this.first;
|
|
1420
|
+
}
|
|
1421
|
+
/**
|
|
1422
|
+
* Check whether the queue is empty.
|
|
1423
|
+
* @remarks Time O(1), Space O(1)
|
|
1424
|
+
* @returns True if length is 0.
|
|
1425
|
+
* @example
|
|
1426
|
+
* // Queue for...of iteration and isEmpty check
|
|
1427
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
1428
|
+
*
|
|
1429
|
+
* const elements: string[] = [];
|
|
1430
|
+
* for (const item of queue) {
|
|
1431
|
+
* elements.push(item);
|
|
1432
|
+
* }
|
|
1433
|
+
*
|
|
1434
|
+
* // Verify all elements are iterated in order
|
|
1435
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
1436
|
+
*
|
|
1437
|
+
* // Process all elements
|
|
1438
|
+
* while (queue.length > 0) {
|
|
1439
|
+
* queue.shift();
|
|
1440
|
+
* }
|
|
1441
|
+
*
|
|
1442
|
+
* console.log(queue.length); // 0;
|
|
1443
|
+
*/
|
|
2122
1444
|
isEmpty() {
|
|
2123
1445
|
return this.length === 0;
|
|
2124
1446
|
}
|
|
2125
1447
|
/**
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
* @example
|
|
2173
|
-
* // basic Queue creation and push operation
|
|
2174
|
-
* // Create a simple Queue with initial values
|
|
2175
|
-
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
2176
|
-
*
|
|
2177
|
-
* // Verify the queue maintains insertion order
|
|
2178
|
-
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
2179
|
-
*
|
|
2180
|
-
* // Check length
|
|
2181
|
-
* console.log(queue.length); // 5;
|
|
2182
|
-
*/
|
|
1448
|
+
* Enqueue one element at the back.
|
|
1449
|
+
* @remarks Time O(1), Space O(1)
|
|
1450
|
+
* @param element - Element to enqueue.
|
|
1451
|
+
* @returns True on success.
|
|
1452
|
+
* @example
|
|
1453
|
+
* // basic Queue creation and push operation
|
|
1454
|
+
* // Create a simple Queue with initial values
|
|
1455
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
1456
|
+
*
|
|
1457
|
+
* // Verify the queue maintains insertion order
|
|
1458
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
1459
|
+
*
|
|
1460
|
+
* // Check length
|
|
1461
|
+
* console.log(queue.length); // 5;
|
|
1462
|
+
*/
|
|
2183
1463
|
push(element) {
|
|
2184
1464
|
this.elements.push(element);
|
|
2185
1465
|
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
@@ -2200,66 +1480,24 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2200
1480
|
return ans;
|
|
2201
1481
|
}
|
|
2202
1482
|
/**
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
* @example
|
|
2249
|
-
* // Queue shift and peek operations
|
|
2250
|
-
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
2251
|
-
*
|
|
2252
|
-
* // Peek at the front element without removing it
|
|
2253
|
-
* console.log(queue.first); // 10;
|
|
2254
|
-
*
|
|
2255
|
-
* // Remove and get the first element (FIFO)
|
|
2256
|
-
* const first = queue.shift();
|
|
2257
|
-
* console.log(first); // 10;
|
|
2258
|
-
*
|
|
2259
|
-
* // Verify remaining elements and length decreased
|
|
2260
|
-
* console.log([...queue]); // [20, 30, 40];
|
|
2261
|
-
* console.log(queue.length); // 3;
|
|
2262
|
-
*/
|
|
1483
|
+
* Dequeue one element from the front (amortized via offset).
|
|
1484
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
1485
|
+
* @returns Removed element or undefined.
|
|
1486
|
+
* @example
|
|
1487
|
+
* // Queue shift and peek operations
|
|
1488
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
1489
|
+
*
|
|
1490
|
+
* // Peek at the front element without removing it
|
|
1491
|
+
* console.log(queue.first); // 10;
|
|
1492
|
+
*
|
|
1493
|
+
* // Remove and get the first element (FIFO)
|
|
1494
|
+
* const first = queue.shift();
|
|
1495
|
+
* console.log(first); // 10;
|
|
1496
|
+
*
|
|
1497
|
+
* // Verify remaining elements and length decreased
|
|
1498
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
1499
|
+
* console.log(queue.length); // 3;
|
|
1500
|
+
*/
|
|
2263
1501
|
shift() {
|
|
2264
1502
|
if (this.length === 0) return void 0;
|
|
2265
1503
|
const first = this.first;
|
|
@@ -2268,55 +1506,16 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2268
1506
|
return first;
|
|
2269
1507
|
}
|
|
2270
1508
|
/**
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
* @example
|
|
2315
|
-
* // Remove specific element
|
|
2316
|
-
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
2317
|
-
* q.delete(2);
|
|
2318
|
-
* console.log(q.length); // 3;
|
|
2319
|
-
*/
|
|
1509
|
+
* Delete the first occurrence of a specific element.
|
|
1510
|
+
* @remarks Time O(N), Space O(1)
|
|
1511
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
1512
|
+
* @returns True if an element was removed.
|
|
1513
|
+
* @example
|
|
1514
|
+
* // Remove specific element
|
|
1515
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
1516
|
+
* q.delete(2);
|
|
1517
|
+
* console.log(q.length); // 3;
|
|
1518
|
+
*/
|
|
2320
1519
|
delete(element) {
|
|
2321
1520
|
for (let i = this._offset; i < this.elements.length; i++) {
|
|
2322
1521
|
if (Object.is(this.elements[i], element)) {
|
|
@@ -2327,55 +1526,16 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2327
1526
|
return false;
|
|
2328
1527
|
}
|
|
2329
1528
|
/**
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
* @example
|
|
2374
|
-
* // Access element by index
|
|
2375
|
-
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
2376
|
-
* console.log(q.at(0)); // 'a';
|
|
2377
|
-
* console.log(q.at(2)); // 'c';
|
|
2378
|
-
*/
|
|
1529
|
+
* Get the element at a given logical index.
|
|
1530
|
+
* @remarks Time O(1), Space O(1)
|
|
1531
|
+
* @param index - Zero-based index from the front.
|
|
1532
|
+
* @returns Element or undefined.
|
|
1533
|
+
* @example
|
|
1534
|
+
* // Access element by index
|
|
1535
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
1536
|
+
* console.log(q.at(0)); // 'a';
|
|
1537
|
+
* console.log(q.at(2)); // 'c';
|
|
1538
|
+
*/
|
|
2379
1539
|
at(index) {
|
|
2380
1540
|
if (index < 0 || index >= this.length) return void 0;
|
|
2381
1541
|
return this._elements[this._offset + index];
|
|
@@ -2442,110 +1602,31 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2442
1602
|
return this;
|
|
2443
1603
|
}
|
|
2444
1604
|
/**
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
* @example
|
|
2489
|
-
* // Remove all elements
|
|
2490
|
-
* const q = new Queue<number>([1, 2, 3]);
|
|
2491
|
-
* q.clear();
|
|
2492
|
-
* console.log(q.length); // 0;
|
|
2493
|
-
*/
|
|
1605
|
+
* Remove all elements and reset offset.
|
|
1606
|
+
* @remarks Time O(1), Space O(1)
|
|
1607
|
+
* @returns void
|
|
1608
|
+
* @example
|
|
1609
|
+
* // Remove all elements
|
|
1610
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1611
|
+
* q.clear();
|
|
1612
|
+
* console.log(q.length); // 0;
|
|
1613
|
+
*/
|
|
2494
1614
|
clear() {
|
|
2495
1615
|
this._elements = [];
|
|
2496
1616
|
this._offset = 0;
|
|
2497
1617
|
}
|
|
2498
1618
|
/**
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
* @example
|
|
2542
|
-
* // Reclaim unused memory
|
|
2543
|
-
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2544
|
-
* q.shift();
|
|
2545
|
-
* q.shift();
|
|
2546
|
-
* q.compact();
|
|
2547
|
-
* console.log(q.length); // 3;
|
|
2548
|
-
*/
|
|
1619
|
+
* Compact storage by discarding consumed head elements.
|
|
1620
|
+
* @remarks Time O(N), Space O(N)
|
|
1621
|
+
* @returns True when compaction performed.
|
|
1622
|
+
* @example
|
|
1623
|
+
* // Reclaim unused memory
|
|
1624
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1625
|
+
* q.shift();
|
|
1626
|
+
* q.shift();
|
|
1627
|
+
* q.compact();
|
|
1628
|
+
* console.log(q.length); // 3;
|
|
1629
|
+
*/
|
|
2549
1630
|
compact() {
|
|
2550
1631
|
this._elements = this.elements.slice(this._offset);
|
|
2551
1632
|
this._offset = 0;
|
|
@@ -2571,57 +1652,17 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2571
1652
|
return removed;
|
|
2572
1653
|
}
|
|
2573
1654
|
/**
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
* @example
|
|
2618
|
-
* // Create independent copy
|
|
2619
|
-
* const q = new Queue<number>([1, 2, 3]);
|
|
2620
|
-
* const copy = q.clone();
|
|
2621
|
-
* copy.shift();
|
|
2622
|
-
* console.log(q.length); // 3;
|
|
2623
|
-
* console.log(copy.length); // 2;
|
|
2624
|
-
*/
|
|
1655
|
+
* Deep clone this queue and its parameters.
|
|
1656
|
+
* @remarks Time O(N), Space O(N)
|
|
1657
|
+
* @returns A new queue with the same content and options.
|
|
1658
|
+
* @example
|
|
1659
|
+
* // Create independent copy
|
|
1660
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1661
|
+
* const copy = q.clone();
|
|
1662
|
+
* copy.shift();
|
|
1663
|
+
* console.log(q.length); // 3;
|
|
1664
|
+
* console.log(copy.length); // 2;
|
|
1665
|
+
*/
|
|
2625
1666
|
clone() {
|
|
2626
1667
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2627
1668
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -2629,57 +1670,17 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2629
1670
|
return out;
|
|
2630
1671
|
}
|
|
2631
1672
|
/**
|
|
2632
|
-
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
|
|
2638
|
-
|
|
2639
|
-
|
|
2640
|
-
|
|
2641
|
-
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
* @example
|
|
2678
|
-
* // Filter elements
|
|
2679
|
-
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2680
|
-
* const evens = q.filter(x => x % 2 === 0);
|
|
2681
|
-
* console.log(evens.length); // 2;
|
|
2682
|
-
*/
|
|
1673
|
+
* Filter elements into a new queue of the same class.
|
|
1674
|
+
* @remarks Time O(N), Space O(N)
|
|
1675
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
1676
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
1677
|
+
* @returns A new queue with kept elements.
|
|
1678
|
+
* @example
|
|
1679
|
+
* // Filter elements
|
|
1680
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1681
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
1682
|
+
* console.log(evens.length); // 2;
|
|
1683
|
+
*/
|
|
2683
1684
|
filter(predicate, thisArg) {
|
|
2684
1685
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2685
1686
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -2691,59 +1692,20 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2691
1692
|
return out;
|
|
2692
1693
|
}
|
|
2693
1694
|
/**
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
* @example
|
|
2742
|
-
* // Transform elements
|
|
2743
|
-
* const q = new Queue<number>([1, 2, 3]);
|
|
2744
|
-
* const doubled = q.map(x => x * 2);
|
|
2745
|
-
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
2746
|
-
*/
|
|
1695
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
1696
|
+
* @remarks Time O(N), Space O(N)
|
|
1697
|
+
* @template EM
|
|
1698
|
+
* @template RM
|
|
1699
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
1700
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
1701
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1702
|
+
* @returns A new Queue with mapped elements.
|
|
1703
|
+
* @example
|
|
1704
|
+
* // Transform elements
|
|
1705
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1706
|
+
* const doubled = q.map(x => x * 2);
|
|
1707
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
1708
|
+
*/
|
|
2747
1709
|
map(callback, options, thisArg) {
|
|
2748
1710
|
var _a, _b;
|
|
2749
1711
|
const out = new this.constructor([], {
|
|
@@ -2885,6 +1847,13 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
2885
1847
|
get size() {
|
|
2886
1848
|
return this._vertexMap.size;
|
|
2887
1849
|
}
|
|
1850
|
+
/**
|
|
1851
|
+
* The edge connector string used in visual output.
|
|
1852
|
+
* Override in subclasses (e.g., '--' for undirected, '->' for directed).
|
|
1853
|
+
*/
|
|
1854
|
+
get _edgeConnector() {
|
|
1855
|
+
return "--";
|
|
1856
|
+
}
|
|
2888
1857
|
/**
|
|
2889
1858
|
* Get vertex instance by key.
|
|
2890
1859
|
* @param vertexKey - Vertex key.
|
|
@@ -2972,7 +1941,10 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
2972
1941
|
const newEdge = this.createEdge(srcOrEdge, dest, weight, value);
|
|
2973
1942
|
return this._addEdge(newEdge);
|
|
2974
1943
|
} else {
|
|
2975
|
-
raise(
|
|
1944
|
+
raise(
|
|
1945
|
+
TypeError,
|
|
1946
|
+
ERR.invalidArgument("dest must be a Vertex or vertex key when srcOrEdge is an Edge.", "Graph")
|
|
1947
|
+
);
|
|
2976
1948
|
}
|
|
2977
1949
|
}
|
|
2978
1950
|
}
|
|
@@ -3532,6 +2504,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
3532
2504
|
}
|
|
3533
2505
|
return mapped;
|
|
3534
2506
|
}
|
|
2507
|
+
// ===== Same-species factory & cloning helpers =====
|
|
3535
2508
|
/**
|
|
3536
2509
|
* Create a deep clone of the graph with the same species.
|
|
3537
2510
|
* @remarks Time O(V + E), Space O(V + E)
|
|
@@ -3544,7 +2517,87 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
3544
2517
|
clone() {
|
|
3545
2518
|
return this._createLike(void 0, this._snapshotOptions());
|
|
3546
2519
|
}
|
|
3547
|
-
|
|
2520
|
+
/**
|
|
2521
|
+
* Generate a text-based visual representation of the graph.
|
|
2522
|
+
*
|
|
2523
|
+
* **Adjacency list format:**
|
|
2524
|
+
* ```
|
|
2525
|
+
* Graph (5 vertices, 6 edges):
|
|
2526
|
+
* A -> B (1), C (2)
|
|
2527
|
+
* B -> D (3)
|
|
2528
|
+
* C -> (no outgoing edges)
|
|
2529
|
+
* D -> A (1)
|
|
2530
|
+
* E (isolated)
|
|
2531
|
+
* ```
|
|
2532
|
+
*
|
|
2533
|
+
* @param options - Optional display settings.
|
|
2534
|
+
* @param options.showWeight - Whether to show edge weights (default: true).
|
|
2535
|
+
* @returns The visual string.
|
|
2536
|
+
*/
|
|
2537
|
+
toVisual(options) {
|
|
2538
|
+
var _a;
|
|
2539
|
+
const showWeight = (_a = options == null ? void 0 : options.showWeight) != null ? _a : true;
|
|
2540
|
+
const vertices = [...this._vertexMap.values()];
|
|
2541
|
+
const vertexCount = vertices.length;
|
|
2542
|
+
const edgeCount = this.edgeSet().length;
|
|
2543
|
+
const lines = [`Graph (${vertexCount} vertices, ${edgeCount} edges):`];
|
|
2544
|
+
for (const vertex of vertices) {
|
|
2545
|
+
const neighbors = this.getNeighbors(vertex);
|
|
2546
|
+
if (neighbors.length === 0) {
|
|
2547
|
+
lines.push(` ${vertex.key} (isolated)`);
|
|
2548
|
+
} else {
|
|
2549
|
+
const edgeStrs = neighbors.map((neighbor) => {
|
|
2550
|
+
const edge = this.getEdge(vertex, neighbor);
|
|
2551
|
+
if (edge && showWeight && edge.weight !== void 0 && edge.weight !== 1) {
|
|
2552
|
+
return `${neighbor.key} (${edge.weight})`;
|
|
2553
|
+
}
|
|
2554
|
+
return `${neighbor.key}`;
|
|
2555
|
+
});
|
|
2556
|
+
lines.push(` ${vertex.key} ${this._edgeConnector} ${edgeStrs.join(", ")}`);
|
|
2557
|
+
}
|
|
2558
|
+
}
|
|
2559
|
+
return lines.join("\n");
|
|
2560
|
+
}
|
|
2561
|
+
/**
|
|
2562
|
+
* Generate DOT language representation for Graphviz.
|
|
2563
|
+
*
|
|
2564
|
+
* @param options - Optional display settings.
|
|
2565
|
+
* @param options.name - Graph name (default: 'G').
|
|
2566
|
+
* @param options.showWeight - Whether to label edges with weight (default: true).
|
|
2567
|
+
* @returns DOT format string.
|
|
2568
|
+
*/
|
|
2569
|
+
toDot(options) {
|
|
2570
|
+
var _a, _b;
|
|
2571
|
+
const name = (_a = options == null ? void 0 : options.name) != null ? _a : "G";
|
|
2572
|
+
const showWeight = (_b = options == null ? void 0 : options.showWeight) != null ? _b : true;
|
|
2573
|
+
const isDirected = this._edgeConnector === "->";
|
|
2574
|
+
const graphType = isDirected ? "digraph" : "graph";
|
|
2575
|
+
const edgeOp = isDirected ? "->" : "--";
|
|
2576
|
+
const lines = [`${graphType} ${name} {`];
|
|
2577
|
+
for (const vertex of this._vertexMap.values()) {
|
|
2578
|
+
lines.push(` "${vertex.key}";`);
|
|
2579
|
+
}
|
|
2580
|
+
const visited = /* @__PURE__ */ new Set();
|
|
2581
|
+
for (const vertex of this._vertexMap.values()) {
|
|
2582
|
+
for (const neighbor of this.getNeighbors(vertex)) {
|
|
2583
|
+
const edgeId = isDirected ? `${vertex.key}->${neighbor.key}` : [vertex.key, neighbor.key].sort().join("--");
|
|
2584
|
+
if (visited.has(edgeId)) continue;
|
|
2585
|
+
visited.add(edgeId);
|
|
2586
|
+
const edge = this.getEdge(vertex, neighbor);
|
|
2587
|
+
const label = edge && showWeight && edge.weight !== void 0 && edge.weight !== 1 ? ` [label="${edge.weight}"]` : "";
|
|
2588
|
+
lines.push(` "${vertex.key}" ${edgeOp} "${neighbor.key}"${label};`);
|
|
2589
|
+
}
|
|
2590
|
+
}
|
|
2591
|
+
lines.push("}");
|
|
2592
|
+
return lines.join("\n");
|
|
2593
|
+
}
|
|
2594
|
+
/**
|
|
2595
|
+
* Print the graph to console.
|
|
2596
|
+
* @param options - Display settings passed to `toVisual`.
|
|
2597
|
+
*/
|
|
2598
|
+
print(options) {
|
|
2599
|
+
console.log(this.toVisual(options));
|
|
2600
|
+
}
|
|
3548
2601
|
/**
|
|
3549
2602
|
* Internal iterator over `[key, value]` entries in insertion order.
|
|
3550
2603
|
* @returns Iterator of `[VertexKey, V | undefined]`.
|
|
@@ -3656,94 +2709,6 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
3656
2709
|
_getVertexKey(vertexOrKey) {
|
|
3657
2710
|
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|
|
3658
2711
|
}
|
|
3659
|
-
/**
|
|
3660
|
-
* The edge connector string used in visual output.
|
|
3661
|
-
* Override in subclasses (e.g., '--' for undirected, '->' for directed).
|
|
3662
|
-
*/
|
|
3663
|
-
get _edgeConnector() {
|
|
3664
|
-
return "--";
|
|
3665
|
-
}
|
|
3666
|
-
/**
|
|
3667
|
-
* Generate a text-based visual representation of the graph.
|
|
3668
|
-
*
|
|
3669
|
-
* **Adjacency list format:**
|
|
3670
|
-
* ```
|
|
3671
|
-
* Graph (5 vertices, 6 edges):
|
|
3672
|
-
* A -> B (1), C (2)
|
|
3673
|
-
* B -> D (3)
|
|
3674
|
-
* C -> (no outgoing edges)
|
|
3675
|
-
* D -> A (1)
|
|
3676
|
-
* E (isolated)
|
|
3677
|
-
* ```
|
|
3678
|
-
*
|
|
3679
|
-
* @param options - Optional display settings.
|
|
3680
|
-
* @param options.showWeight - Whether to show edge weights (default: true).
|
|
3681
|
-
* @returns The visual string.
|
|
3682
|
-
*/
|
|
3683
|
-
toVisual(options) {
|
|
3684
|
-
var _a;
|
|
3685
|
-
const showWeight = (_a = options == null ? void 0 : options.showWeight) != null ? _a : true;
|
|
3686
|
-
const vertices = [...this._vertexMap.values()];
|
|
3687
|
-
const vertexCount = vertices.length;
|
|
3688
|
-
const edgeCount = this.edgeSet().length;
|
|
3689
|
-
const lines = [`Graph (${vertexCount} vertices, ${edgeCount} edges):`];
|
|
3690
|
-
for (const vertex of vertices) {
|
|
3691
|
-
const neighbors = this.getNeighbors(vertex);
|
|
3692
|
-
if (neighbors.length === 0) {
|
|
3693
|
-
lines.push(` ${vertex.key} (isolated)`);
|
|
3694
|
-
} else {
|
|
3695
|
-
const edgeStrs = neighbors.map((neighbor) => {
|
|
3696
|
-
const edge = this.getEdge(vertex, neighbor);
|
|
3697
|
-
if (edge && showWeight && edge.weight !== void 0 && edge.weight !== 1) {
|
|
3698
|
-
return `${neighbor.key} (${edge.weight})`;
|
|
3699
|
-
}
|
|
3700
|
-
return `${neighbor.key}`;
|
|
3701
|
-
});
|
|
3702
|
-
lines.push(` ${vertex.key} ${this._edgeConnector} ${edgeStrs.join(", ")}`);
|
|
3703
|
-
}
|
|
3704
|
-
}
|
|
3705
|
-
return lines.join("\n");
|
|
3706
|
-
}
|
|
3707
|
-
/**
|
|
3708
|
-
* Generate DOT language representation for Graphviz.
|
|
3709
|
-
*
|
|
3710
|
-
* @param options - Optional display settings.
|
|
3711
|
-
* @param options.name - Graph name (default: 'G').
|
|
3712
|
-
* @param options.showWeight - Whether to label edges with weight (default: true).
|
|
3713
|
-
* @returns DOT format string.
|
|
3714
|
-
*/
|
|
3715
|
-
toDot(options) {
|
|
3716
|
-
var _a, _b;
|
|
3717
|
-
const name = (_a = options == null ? void 0 : options.name) != null ? _a : "G";
|
|
3718
|
-
const showWeight = (_b = options == null ? void 0 : options.showWeight) != null ? _b : true;
|
|
3719
|
-
const isDirected = this._edgeConnector === "->";
|
|
3720
|
-
const graphType = isDirected ? "digraph" : "graph";
|
|
3721
|
-
const edgeOp = isDirected ? "->" : "--";
|
|
3722
|
-
const lines = [`${graphType} ${name} {`];
|
|
3723
|
-
for (const vertex of this._vertexMap.values()) {
|
|
3724
|
-
lines.push(` "${vertex.key}";`);
|
|
3725
|
-
}
|
|
3726
|
-
const visited = /* @__PURE__ */ new Set();
|
|
3727
|
-
for (const vertex of this._vertexMap.values()) {
|
|
3728
|
-
for (const neighbor of this.getNeighbors(vertex)) {
|
|
3729
|
-
const edgeId = isDirected ? `${vertex.key}->${neighbor.key}` : [vertex.key, neighbor.key].sort().join("--");
|
|
3730
|
-
if (visited.has(edgeId)) continue;
|
|
3731
|
-
visited.add(edgeId);
|
|
3732
|
-
const edge = this.getEdge(vertex, neighbor);
|
|
3733
|
-
const label = edge && showWeight && edge.weight !== void 0 && edge.weight !== 1 ? ` [label="${edge.weight}"]` : "";
|
|
3734
|
-
lines.push(` "${vertex.key}" ${edgeOp} "${neighbor.key}"${label};`);
|
|
3735
|
-
}
|
|
3736
|
-
}
|
|
3737
|
-
lines.push("}");
|
|
3738
|
-
return lines.join("\n");
|
|
3739
|
-
}
|
|
3740
|
-
/**
|
|
3741
|
-
* Print the graph to console.
|
|
3742
|
-
* @param options - Display settings passed to `toVisual`.
|
|
3743
|
-
*/
|
|
3744
|
-
print(options) {
|
|
3745
|
-
console.log(this.toVisual(options));
|
|
3746
|
-
}
|
|
3747
2712
|
};
|
|
3748
2713
|
__name(_AbstractGraph, "AbstractGraph");
|
|
3749
2714
|
var AbstractGraph = _AbstractGraph;
|
|
@@ -3778,9 +2743,6 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
3778
2743
|
__publicField(this, "_outEdgeMap", /* @__PURE__ */ new Map());
|
|
3779
2744
|
__publicField(this, "_inEdgeMap", /* @__PURE__ */ new Map());
|
|
3780
2745
|
}
|
|
3781
|
-
get _edgeConnector() {
|
|
3782
|
-
return "->";
|
|
3783
|
-
}
|
|
3784
2746
|
get outEdgeMap() {
|
|
3785
2747
|
return this._outEdgeMap;
|
|
3786
2748
|
}
|
|
@@ -3793,6 +2755,9 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
3793
2755
|
set inEdgeMap(v) {
|
|
3794
2756
|
this._inEdgeMap = v;
|
|
3795
2757
|
}
|
|
2758
|
+
get _edgeConnector() {
|
|
2759
|
+
return "->";
|
|
2760
|
+
}
|
|
3796
2761
|
/**
|
|
3797
2762
|
* Construct a directed graph from keys with value initializer `v => v`.
|
|
3798
2763
|
* @template K - Vertex key type.
|
|
@@ -3843,59 +2808,20 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
3843
2808
|
return new DirectedEdge(src, dest, (_a = weight != null ? weight : this.options.defaultEdgeWeight) != null ? _a : 1, value);
|
|
3844
2809
|
}
|
|
3845
2810
|
/**
|
|
3846
|
-
|
|
3847
|
-
|
|
3848
|
-
|
|
3849
|
-
|
|
3850
|
-
|
|
3851
|
-
|
|
3852
|
-
|
|
3853
|
-
|
|
3854
|
-
|
|
3855
|
-
|
|
3856
|
-
|
|
3857
|
-
|
|
3858
|
-
|
|
3859
|
-
|
|
3860
|
-
|
|
3861
|
-
|
|
3862
|
-
|
|
3863
|
-
|
|
3864
|
-
|
|
3865
|
-
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
|
|
3869
|
-
|
|
3870
|
-
|
|
3871
|
-
|
|
3872
|
-
|
|
3873
|
-
|
|
3874
|
-
|
|
3875
|
-
|
|
3876
|
-
|
|
3877
|
-
|
|
3878
|
-
|
|
3879
|
-
|
|
3880
|
-
|
|
3881
|
-
|
|
3882
|
-
|
|
3883
|
-
|
|
3884
|
-
|
|
3885
|
-
|
|
3886
|
-
|
|
3887
|
-
|
|
3888
|
-
|
|
3889
|
-
|
|
3890
|
-
* @example
|
|
3891
|
-
* // Get edge between vertices
|
|
3892
|
-
* const g = new DirectedGraph();
|
|
3893
|
-
* g.addVertex('A');
|
|
3894
|
-
* g.addVertex('B');
|
|
3895
|
-
* g.addEdge('A', 'B', 5);
|
|
3896
|
-
* const edge = g.getEdge('A', 'B');
|
|
3897
|
-
* console.log(edge?.weight); // 5;
|
|
3898
|
-
*/
|
|
2811
|
+
* Get the unique edge from `src` to `dest`, if present.
|
|
2812
|
+
* @param srcOrKey - Source vertex or key.
|
|
2813
|
+
* @param destOrKey - Destination vertex or key.
|
|
2814
|
+
* @returns Edge instance or `undefined`.
|
|
2815
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
2816
|
+
* @example
|
|
2817
|
+
* // Get edge between vertices
|
|
2818
|
+
* const g = new DirectedGraph();
|
|
2819
|
+
* g.addVertex('A');
|
|
2820
|
+
* g.addVertex('B');
|
|
2821
|
+
* g.addEdge('A', 'B', 5);
|
|
2822
|
+
* const edge = g.getEdge('A', 'B');
|
|
2823
|
+
* console.log(edge?.weight); // 5;
|
|
2824
|
+
*/
|
|
3899
2825
|
getEdge(srcOrKey, destOrKey) {
|
|
3900
2826
|
let edgeMap = [];
|
|
3901
2827
|
if (srcOrKey !== void 0 && destOrKey !== void 0) {
|
|
@@ -3935,79 +2861,37 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
3935
2861
|
return removed;
|
|
3936
2862
|
}
|
|
3937
2863
|
/**
|
|
3938
|
-
|
|
3939
|
-
|
|
3940
|
-
|
|
3941
|
-
|
|
3942
|
-
|
|
3943
|
-
|
|
3944
|
-
|
|
3945
|
-
|
|
3946
|
-
|
|
3947
|
-
|
|
3948
|
-
|
|
3949
|
-
|
|
3950
|
-
|
|
3951
|
-
|
|
3952
|
-
|
|
3953
|
-
|
|
3954
|
-
|
|
3955
|
-
|
|
3956
|
-
|
|
3957
|
-
|
|
3958
|
-
|
|
3959
|
-
|
|
3960
|
-
|
|
3961
|
-
|
|
3962
|
-
|
|
3963
|
-
|
|
3964
|
-
|
|
3965
|
-
|
|
3966
|
-
|
|
3967
|
-
|
|
3968
|
-
|
|
3969
|
-
|
|
3970
|
-
|
|
3971
|
-
|
|
3972
|
-
|
|
3973
|
-
|
|
3974
|
-
|
|
3975
|
-
|
|
3976
|
-
|
|
3977
|
-
|
|
3978
|
-
|
|
3979
|
-
|
|
3980
|
-
|
|
3981
|
-
|
|
3982
|
-
|
|
3983
|
-
|
|
3984
|
-
|
|
3985
|
-
* @example
|
|
3986
|
-
* // DirectedGraph deleteEdge and vertex operations
|
|
3987
|
-
* const graph = new DirectedGraph<string>();
|
|
3988
|
-
*
|
|
3989
|
-
* // Build a small graph
|
|
3990
|
-
* graph.addVertex('X');
|
|
3991
|
-
* graph.addVertex('Y');
|
|
3992
|
-
* graph.addVertex('Z');
|
|
3993
|
-
* graph.addEdge('X', 'Y', 1);
|
|
3994
|
-
* graph.addEdge('Y', 'Z', 2);
|
|
3995
|
-
*
|
|
3996
|
-
* // Delete an edge
|
|
3997
|
-
* graph.deleteEdgeSrcToDest('X', 'Y');
|
|
3998
|
-
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
3999
|
-
*
|
|
4000
|
-
* // Edge in other direction should not exist
|
|
4001
|
-
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
4002
|
-
*
|
|
4003
|
-
* // Other edges should remain
|
|
4004
|
-
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
4005
|
-
*
|
|
4006
|
-
* // Delete a vertex
|
|
4007
|
-
* graph.deleteVertex('Y');
|
|
4008
|
-
* console.log(graph.hasVertex('Y')); // false;
|
|
4009
|
-
* console.log(graph.size); // 2;
|
|
4010
|
-
*/
|
|
2864
|
+
* Delete an edge by instance or by `(srcKey, destKey)`.
|
|
2865
|
+
* @param edgeOrSrcVertexKey - Edge instance or source vertex/key.
|
|
2866
|
+
* @param destVertexKey - Optional destination vertex/key when deleting by pair.
|
|
2867
|
+
* @returns Removed edge or `undefined`.
|
|
2868
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
2869
|
+
* @example
|
|
2870
|
+
* // DirectedGraph deleteEdge and vertex operations
|
|
2871
|
+
* const graph = new DirectedGraph<string>();
|
|
2872
|
+
*
|
|
2873
|
+
* // Build a small graph
|
|
2874
|
+
* graph.addVertex('X');
|
|
2875
|
+
* graph.addVertex('Y');
|
|
2876
|
+
* graph.addVertex('Z');
|
|
2877
|
+
* graph.addEdge('X', 'Y', 1);
|
|
2878
|
+
* graph.addEdge('Y', 'Z', 2);
|
|
2879
|
+
*
|
|
2880
|
+
* // Delete an edge
|
|
2881
|
+
* graph.deleteEdgeSrcToDest('X', 'Y');
|
|
2882
|
+
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
2883
|
+
*
|
|
2884
|
+
* // Edge in other direction should not exist
|
|
2885
|
+
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
2886
|
+
*
|
|
2887
|
+
* // Other edges should remain
|
|
2888
|
+
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
2889
|
+
*
|
|
2890
|
+
* // Delete a vertex
|
|
2891
|
+
* graph.deleteVertex('Y');
|
|
2892
|
+
* console.log(graph.hasVertex('Y')); // false;
|
|
2893
|
+
* console.log(graph.size); // 2;
|
|
2894
|
+
*/
|
|
4011
2895
|
deleteEdge(edgeOrSrcVertexKey, destVertexKey) {
|
|
4012
2896
|
let removed = void 0;
|
|
4013
2897
|
let src, dest;
|
|
@@ -4035,56 +2919,17 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4035
2919
|
return removed;
|
|
4036
2920
|
}
|
|
4037
2921
|
/**
|
|
4038
|
-
|
|
4039
|
-
|
|
4040
|
-
|
|
4041
|
-
|
|
4042
|
-
|
|
4043
|
-
|
|
4044
|
-
|
|
4045
|
-
|
|
4046
|
-
|
|
4047
|
-
|
|
4048
|
-
|
|
4049
|
-
|
|
4050
|
-
|
|
4051
|
-
|
|
4052
|
-
|
|
4053
|
-
|
|
4054
|
-
|
|
4055
|
-
|
|
4056
|
-
|
|
4057
|
-
|
|
4058
|
-
|
|
4059
|
-
|
|
4060
|
-
|
|
4061
|
-
|
|
4062
|
-
|
|
4063
|
-
|
|
4064
|
-
|
|
4065
|
-
|
|
4066
|
-
|
|
4067
|
-
|
|
4068
|
-
|
|
4069
|
-
|
|
4070
|
-
|
|
4071
|
-
|
|
4072
|
-
|
|
4073
|
-
|
|
4074
|
-
|
|
4075
|
-
|
|
4076
|
-
|
|
4077
|
-
|
|
4078
|
-
* @example
|
|
4079
|
-
* // Remove a vertex
|
|
4080
|
-
* const g = new DirectedGraph();
|
|
4081
|
-
* g.addVertex('A');
|
|
4082
|
-
* g.addVertex('B');
|
|
4083
|
-
* g.addEdge('A', 'B');
|
|
4084
|
-
* g.deleteVertex('A');
|
|
4085
|
-
* console.log(g.hasVertex('A')); // false;
|
|
4086
|
-
* console.log(g.hasEdge('A', 'B')); // false;
|
|
4087
|
-
*/
|
|
2922
|
+
* Remove a vertex
|
|
2923
|
+
* @example
|
|
2924
|
+
* // Remove a vertex
|
|
2925
|
+
* const g = new DirectedGraph();
|
|
2926
|
+
* g.addVertex('A');
|
|
2927
|
+
* g.addVertex('B');
|
|
2928
|
+
* g.addEdge('A', 'B');
|
|
2929
|
+
* g.deleteVertex('A');
|
|
2930
|
+
* console.log(g.hasVertex('A')); // false;
|
|
2931
|
+
* console.log(g.hasEdge('A', 'B')); // false;
|
|
2932
|
+
*/
|
|
4088
2933
|
deleteVertex(vertexOrKey) {
|
|
4089
2934
|
let vertexKey;
|
|
4090
2935
|
let vertex;
|
|
@@ -4116,59 +2961,20 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4116
2961
|
return removed;
|
|
4117
2962
|
}
|
|
4118
2963
|
/**
|
|
4119
|
-
|
|
4120
|
-
|
|
4121
|
-
|
|
4122
|
-
|
|
4123
|
-
|
|
4124
|
-
|
|
4125
|
-
|
|
4126
|
-
|
|
4127
|
-
|
|
4128
|
-
|
|
4129
|
-
|
|
4130
|
-
|
|
4131
|
-
|
|
4132
|
-
|
|
4133
|
-
|
|
4134
|
-
|
|
4135
|
-
|
|
4136
|
-
|
|
4137
|
-
|
|
4138
|
-
|
|
4139
|
-
|
|
4140
|
-
|
|
4141
|
-
|
|
4142
|
-
|
|
4143
|
-
|
|
4144
|
-
|
|
4145
|
-
|
|
4146
|
-
|
|
4147
|
-
|
|
4148
|
-
|
|
4149
|
-
|
|
4150
|
-
|
|
4151
|
-
|
|
4152
|
-
|
|
4153
|
-
|
|
4154
|
-
|
|
4155
|
-
|
|
4156
|
-
|
|
4157
|
-
|
|
4158
|
-
|
|
4159
|
-
|
|
4160
|
-
|
|
4161
|
-
|
|
4162
|
-
* @example
|
|
4163
|
-
* // Get incoming edges
|
|
4164
|
-
* const g = new DirectedGraph();
|
|
4165
|
-
* g.addVertex('A');
|
|
4166
|
-
* g.addVertex('B');
|
|
4167
|
-
* g.addVertex('C');
|
|
4168
|
-
* g.addEdge('A', 'C');
|
|
4169
|
-
* g.addEdge('B', 'C');
|
|
4170
|
-
* console.log(g.incomingEdgesOf('C').length); // 2;
|
|
4171
|
-
*/
|
|
2964
|
+
* Incoming edges of a vertex.
|
|
2965
|
+
* @param vertexOrKey - Vertex or key.
|
|
2966
|
+
* @returns Array of incoming edges.
|
|
2967
|
+
* @remarks Time O(deg_in), Space O(deg_in)
|
|
2968
|
+
* @example
|
|
2969
|
+
* // Get incoming edges
|
|
2970
|
+
* const g = new DirectedGraph();
|
|
2971
|
+
* g.addVertex('A');
|
|
2972
|
+
* g.addVertex('B');
|
|
2973
|
+
* g.addVertex('C');
|
|
2974
|
+
* g.addEdge('A', 'C');
|
|
2975
|
+
* g.addEdge('B', 'C');
|
|
2976
|
+
* console.log(g.incomingEdgesOf('C').length); // 2;
|
|
2977
|
+
*/
|
|
4172
2978
|
incomingEdgesOf(vertexOrKey) {
|
|
4173
2979
|
const target = this._getVertex(vertexOrKey);
|
|
4174
2980
|
if (target) {
|
|
@@ -4177,59 +2983,20 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4177
2983
|
return [];
|
|
4178
2984
|
}
|
|
4179
2985
|
/**
|
|
4180
|
-
|
|
4181
|
-
|
|
4182
|
-
|
|
4183
|
-
|
|
4184
|
-
|
|
4185
|
-
|
|
4186
|
-
|
|
4187
|
-
|
|
4188
|
-
|
|
4189
|
-
|
|
4190
|
-
|
|
4191
|
-
|
|
4192
|
-
|
|
4193
|
-
|
|
4194
|
-
|
|
4195
|
-
|
|
4196
|
-
|
|
4197
|
-
|
|
4198
|
-
|
|
4199
|
-
|
|
4200
|
-
|
|
4201
|
-
|
|
4202
|
-
|
|
4203
|
-
|
|
4204
|
-
|
|
4205
|
-
|
|
4206
|
-
|
|
4207
|
-
|
|
4208
|
-
|
|
4209
|
-
|
|
4210
|
-
|
|
4211
|
-
|
|
4212
|
-
|
|
4213
|
-
|
|
4214
|
-
|
|
4215
|
-
|
|
4216
|
-
|
|
4217
|
-
|
|
4218
|
-
|
|
4219
|
-
|
|
4220
|
-
|
|
4221
|
-
|
|
4222
|
-
|
|
4223
|
-
* @example
|
|
4224
|
-
* // Get outgoing edges
|
|
4225
|
-
* const g = new DirectedGraph();
|
|
4226
|
-
* g.addVertex('A');
|
|
4227
|
-
* g.addVertex('B');
|
|
4228
|
-
* g.addVertex('C');
|
|
4229
|
-
* g.addEdge('A', 'B');
|
|
4230
|
-
* g.addEdge('A', 'C');
|
|
4231
|
-
* console.log(g.outgoingEdgesOf('A').length); // 2;
|
|
4232
|
-
*/
|
|
2986
|
+
* Outgoing edges of a vertex.
|
|
2987
|
+
* @param vertexOrKey - Vertex or key.
|
|
2988
|
+
* @returns Array of outgoing edges.
|
|
2989
|
+
* @remarks Time O(deg_out), Space O(deg_out)
|
|
2990
|
+
* @example
|
|
2991
|
+
* // Get outgoing edges
|
|
2992
|
+
* const g = new DirectedGraph();
|
|
2993
|
+
* g.addVertex('A');
|
|
2994
|
+
* g.addVertex('B');
|
|
2995
|
+
* g.addVertex('C');
|
|
2996
|
+
* g.addEdge('A', 'B');
|
|
2997
|
+
* g.addEdge('A', 'C');
|
|
2998
|
+
* console.log(g.outgoingEdgesOf('A').length); // 2;
|
|
2999
|
+
*/
|
|
4233
3000
|
outgoingEdgesOf(vertexOrKey) {
|
|
4234
3001
|
const target = this._getVertex(vertexOrKey);
|
|
4235
3002
|
if (target) {
|
|
@@ -4288,75 +3055,33 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4288
3055
|
return destinations;
|
|
4289
3056
|
}
|
|
4290
3057
|
/**
|
|
4291
|
-
|
|
4292
|
-
|
|
4293
|
-
|
|
4294
|
-
|
|
4295
|
-
|
|
4296
|
-
|
|
4297
|
-
|
|
4298
|
-
|
|
4299
|
-
|
|
4300
|
-
|
|
4301
|
-
|
|
4302
|
-
|
|
4303
|
-
|
|
4304
|
-
|
|
4305
|
-
|
|
4306
|
-
|
|
4307
|
-
|
|
4308
|
-
|
|
4309
|
-
|
|
4310
|
-
|
|
4311
|
-
|
|
4312
|
-
|
|
4313
|
-
|
|
4314
|
-
|
|
4315
|
-
|
|
4316
|
-
|
|
4317
|
-
|
|
4318
|
-
|
|
4319
|
-
|
|
4320
|
-
|
|
4321
|
-
|
|
4322
|
-
|
|
4323
|
-
|
|
4324
|
-
|
|
4325
|
-
|
|
4326
|
-
|
|
4327
|
-
|
|
4328
|
-
|
|
4329
|
-
|
|
4330
|
-
|
|
4331
|
-
|
|
4332
|
-
|
|
4333
|
-
|
|
4334
|
-
|
|
4335
|
-
|
|
4336
|
-
|
|
4337
|
-
* @example
|
|
4338
|
-
* // DirectedGraph topologicalSort for task scheduling
|
|
4339
|
-
* const graph = new DirectedGraph<string>();
|
|
4340
|
-
*
|
|
4341
|
-
* // Build a DAG (Directed Acyclic Graph) for task dependencies
|
|
4342
|
-
* graph.addVertex('Design');
|
|
4343
|
-
* graph.addVertex('Implement');
|
|
4344
|
-
* graph.addVertex('Test');
|
|
4345
|
-
* graph.addVertex('Deploy');
|
|
4346
|
-
*
|
|
4347
|
-
* // Add dependency edges
|
|
4348
|
-
* graph.addEdge('Design', 'Implement', 1); // Design must come before Implement
|
|
4349
|
-
* graph.addEdge('Implement', 'Test', 1); // Implement must come before Test
|
|
4350
|
-
* graph.addEdge('Test', 'Deploy', 1); // Test must come before Deploy
|
|
4351
|
-
*
|
|
4352
|
-
* // Topological sort gives valid execution order
|
|
4353
|
-
* const executionOrder = graph.topologicalSort();
|
|
4354
|
-
* console.log(executionOrder); // defined;
|
|
4355
|
-
* console.log(executionOrder); // ['Design', 'Implement', 'Test', 'Deploy'];
|
|
4356
|
-
*
|
|
4357
|
-
* // All vertices should be included
|
|
4358
|
-
* console.log(executionOrder?.length); // 4;
|
|
4359
|
-
*/
|
|
3058
|
+
* Topological sort if DAG; returns `undefined` if a cycle exists.
|
|
3059
|
+
* @param propertyName - `'key'` to map to keys; `'vertex'` to keep instances.
|
|
3060
|
+
* @returns Array of keys/vertices, or `undefined` when cycle is found.
|
|
3061
|
+
* @remarks Time O(V + E), Space O(V)
|
|
3062
|
+
* @example
|
|
3063
|
+
* // DirectedGraph topologicalSort for task scheduling
|
|
3064
|
+
* const graph = new DirectedGraph<string>();
|
|
3065
|
+
*
|
|
3066
|
+
* // Build a DAG (Directed Acyclic Graph) for task dependencies
|
|
3067
|
+
* graph.addVertex('Design');
|
|
3068
|
+
* graph.addVertex('Implement');
|
|
3069
|
+
* graph.addVertex('Test');
|
|
3070
|
+
* graph.addVertex('Deploy');
|
|
3071
|
+
*
|
|
3072
|
+
* // Add dependency edges
|
|
3073
|
+
* graph.addEdge('Design', 'Implement', 1); // Design must come before Implement
|
|
3074
|
+
* graph.addEdge('Implement', 'Test', 1); // Implement must come before Test
|
|
3075
|
+
* graph.addEdge('Test', 'Deploy', 1); // Test must come before Deploy
|
|
3076
|
+
*
|
|
3077
|
+
* // Topological sort gives valid execution order
|
|
3078
|
+
* const executionOrder = graph.topologicalSort();
|
|
3079
|
+
* console.log(executionOrder); // defined;
|
|
3080
|
+
* console.log(executionOrder); // ['Design', 'Implement', 'Test', 'Deploy'];
|
|
3081
|
+
*
|
|
3082
|
+
* // All vertices should be included
|
|
3083
|
+
* console.log(executionOrder?.length); // 4;
|
|
3084
|
+
*/
|
|
4360
3085
|
topologicalSort(propertyName) {
|
|
4361
3086
|
propertyName = propertyName != null ? propertyName : "key";
|
|
4362
3087
|
const statusMap = /* @__PURE__ */ new Map();
|
|
@@ -4389,54 +3114,15 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4389
3114
|
return sorted.reverse();
|
|
4390
3115
|
}
|
|
4391
3116
|
/**
|
|
4392
|
-
|
|
4393
|
-
|
|
4394
|
-
|
|
4395
|
-
|
|
4396
|
-
|
|
4397
|
-
|
|
4398
|
-
|
|
4399
|
-
|
|
4400
|
-
|
|
4401
|
-
|
|
4402
|
-
|
|
4403
|
-
|
|
4404
|
-
|
|
4405
|
-
|
|
4406
|
-
|
|
4407
|
-
|
|
4408
|
-
|
|
4409
|
-
|
|
4410
|
-
|
|
4411
|
-
|
|
4412
|
-
|
|
4413
|
-
|
|
4414
|
-
|
|
4415
|
-
|
|
4416
|
-
|
|
4417
|
-
|
|
4418
|
-
|
|
4419
|
-
|
|
4420
|
-
|
|
4421
|
-
|
|
4422
|
-
|
|
4423
|
-
|
|
4424
|
-
|
|
4425
|
-
|
|
4426
|
-
|
|
4427
|
-
|
|
4428
|
-
|
|
4429
|
-
|
|
4430
|
-
|
|
4431
|
-
|
|
4432
|
-
* @example
|
|
4433
|
-
* // Get all edges
|
|
4434
|
-
* const g = new DirectedGraph();
|
|
4435
|
-
* g.addVertex('A');
|
|
4436
|
-
* g.addVertex('B');
|
|
4437
|
-
* g.addEdge('A', 'B', 3);
|
|
4438
|
-
* console.log(g.edgeSet().length); // 1;
|
|
4439
|
-
*/
|
|
3117
|
+
* Get all edges
|
|
3118
|
+
* @example
|
|
3119
|
+
* // Get all edges
|
|
3120
|
+
* const g = new DirectedGraph();
|
|
3121
|
+
* g.addVertex('A');
|
|
3122
|
+
* g.addVertex('B');
|
|
3123
|
+
* g.addEdge('A', 'B', 3);
|
|
3124
|
+
* console.log(g.edgeSet().length); // 1;
|
|
3125
|
+
*/
|
|
4440
3126
|
edgeSet() {
|
|
4441
3127
|
let edgeMap = [];
|
|
4442
3128
|
this._outEdgeMap.forEach((outEdges) => {
|
|
@@ -4445,58 +3131,18 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4445
3131
|
return edgeMap;
|
|
4446
3132
|
}
|
|
4447
3133
|
/**
|
|
4448
|
-
|
|
4449
|
-
|
|
4450
|
-
|
|
4451
|
-
|
|
4452
|
-
|
|
4453
|
-
|
|
4454
|
-
|
|
4455
|
-
|
|
4456
|
-
|
|
4457
|
-
|
|
4458
|
-
|
|
4459
|
-
|
|
4460
|
-
|
|
4461
|
-
|
|
4462
|
-
|
|
4463
|
-
|
|
4464
|
-
|
|
4465
|
-
|
|
4466
|
-
|
|
4467
|
-
|
|
4468
|
-
|
|
4469
|
-
|
|
4470
|
-
|
|
4471
|
-
|
|
4472
|
-
|
|
4473
|
-
|
|
4474
|
-
|
|
4475
|
-
|
|
4476
|
-
|
|
4477
|
-
|
|
4478
|
-
|
|
4479
|
-
|
|
4480
|
-
|
|
4481
|
-
|
|
4482
|
-
|
|
4483
|
-
|
|
4484
|
-
|
|
4485
|
-
|
|
4486
|
-
|
|
4487
|
-
|
|
4488
|
-
|
|
4489
|
-
* @example
|
|
4490
|
-
* // Get outgoing neighbors
|
|
4491
|
-
* const g = new DirectedGraph();
|
|
4492
|
-
* g.addVertex('A');
|
|
4493
|
-
* g.addVertex('B');
|
|
4494
|
-
* g.addVertex('C');
|
|
4495
|
-
* g.addEdge('A', 'B');
|
|
4496
|
-
* g.addEdge('A', 'C');
|
|
4497
|
-
* const neighbors = g.getNeighbors('A');
|
|
4498
|
-
* console.log(neighbors.map(v => v.key).sort()); // ['B', 'C'];
|
|
4499
|
-
*/
|
|
3134
|
+
* Get outgoing neighbors
|
|
3135
|
+
* @example
|
|
3136
|
+
* // Get outgoing neighbors
|
|
3137
|
+
* const g = new DirectedGraph();
|
|
3138
|
+
* g.addVertex('A');
|
|
3139
|
+
* g.addVertex('B');
|
|
3140
|
+
* g.addVertex('C');
|
|
3141
|
+
* g.addEdge('A', 'B');
|
|
3142
|
+
* g.addEdge('A', 'C');
|
|
3143
|
+
* const neighbors = g.getNeighbors('A');
|
|
3144
|
+
* console.log(neighbors.map(v => v.key).sort()); // ['B', 'C'];
|
|
3145
|
+
*/
|
|
4500
3146
|
getNeighbors(vertexOrKey) {
|
|
4501
3147
|
const neighbors = [];
|
|
4502
3148
|
const vertex = this._getVertex(vertexOrKey);
|
|
@@ -4554,62 +3200,23 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4554
3200
|
return super.clone();
|
|
4555
3201
|
}
|
|
4556
3202
|
/**
|
|
4557
|
-
|
|
4558
|
-
|
|
4559
|
-
|
|
4560
|
-
|
|
4561
|
-
|
|
4562
|
-
|
|
4563
|
-
|
|
4564
|
-
|
|
4565
|
-
|
|
4566
|
-
|
|
4567
|
-
|
|
4568
|
-
|
|
4569
|
-
|
|
4570
|
-
|
|
4571
|
-
|
|
4572
|
-
|
|
4573
|
-
|
|
4574
|
-
|
|
4575
|
-
|
|
4576
|
-
|
|
4577
|
-
|
|
4578
|
-
|
|
4579
|
-
|
|
4580
|
-
|
|
4581
|
-
|
|
4582
|
-
|
|
4583
|
-
|
|
4584
|
-
|
|
4585
|
-
|
|
4586
|
-
|
|
4587
|
-
|
|
4588
|
-
|
|
4589
|
-
|
|
4590
|
-
|
|
4591
|
-
|
|
4592
|
-
|
|
4593
|
-
|
|
4594
|
-
|
|
4595
|
-
|
|
4596
|
-
|
|
4597
|
-
|
|
4598
|
-
|
|
4599
|
-
* @example
|
|
4600
|
-
* // Find strongly connected components
|
|
4601
|
-
* const g = new DirectedGraph();
|
|
4602
|
-
* g.addVertex('A');
|
|
4603
|
-
* g.addVertex('B');
|
|
4604
|
-
* g.addVertex('C');
|
|
4605
|
-
* g.addEdge('A', 'B');
|
|
4606
|
-
* g.addEdge('B', 'C');
|
|
4607
|
-
* g.addEdge('C', 'A');
|
|
4608
|
-
* const { SCCs } = g.tarjan();
|
|
4609
|
-
* // A→B→C→A forms one SCC with 3 members
|
|
4610
|
-
* const sccArrays = [...SCCs.values()];
|
|
4611
|
-
* console.log(sccArrays.some(scc => scc.length === 3)); // true;
|
|
4612
|
-
*/
|
|
3203
|
+
* Tarjan's algorithm for strongly connected components.
|
|
3204
|
+
* @returns `{ dfnMap, lowMap, SCCs }`.
|
|
3205
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
3206
|
+
* @example
|
|
3207
|
+
* // Find strongly connected components
|
|
3208
|
+
* const g = new DirectedGraph();
|
|
3209
|
+
* g.addVertex('A');
|
|
3210
|
+
* g.addVertex('B');
|
|
3211
|
+
* g.addVertex('C');
|
|
3212
|
+
* g.addEdge('A', 'B');
|
|
3213
|
+
* g.addEdge('B', 'C');
|
|
3214
|
+
* g.addEdge('C', 'A');
|
|
3215
|
+
* const { SCCs } = g.tarjan();
|
|
3216
|
+
* // A→B→C→A forms one SCC with 3 members
|
|
3217
|
+
* const sccArrays = [...SCCs.values()];
|
|
3218
|
+
* console.log(sccArrays.some(scc => scc.length === 3)); // true;
|
|
3219
|
+
*/
|
|
4613
3220
|
tarjan() {
|
|
4614
3221
|
const dfnMap = /* @__PURE__ */ new Map();
|
|
4615
3222
|
const lowMap = /* @__PURE__ */ new Map();
|
|
@@ -4667,60 +3274,21 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4667
3274
|
return this.tarjan().lowMap;
|
|
4668
3275
|
}
|
|
4669
3276
|
/**
|
|
4670
|
-
|
|
4671
|
-
|
|
4672
|
-
|
|
4673
|
-
|
|
4674
|
-
|
|
4675
|
-
|
|
4676
|
-
|
|
4677
|
-
|
|
4678
|
-
|
|
4679
|
-
|
|
4680
|
-
|
|
4681
|
-
|
|
4682
|
-
|
|
4683
|
-
|
|
4684
|
-
|
|
4685
|
-
|
|
4686
|
-
|
|
4687
|
-
|
|
4688
|
-
|
|
4689
|
-
|
|
4690
|
-
|
|
4691
|
-
|
|
4692
|
-
|
|
4693
|
-
|
|
4694
|
-
|
|
4695
|
-
|
|
4696
|
-
|
|
4697
|
-
|
|
4698
|
-
|
|
4699
|
-
|
|
4700
|
-
|
|
4701
|
-
|
|
4702
|
-
|
|
4703
|
-
|
|
4704
|
-
|
|
4705
|
-
|
|
4706
|
-
|
|
4707
|
-
|
|
4708
|
-
|
|
4709
|
-
|
|
4710
|
-
|
|
4711
|
-
|
|
4712
|
-
* @example
|
|
4713
|
-
* // Get strongly connected components
|
|
4714
|
-
* const g = new DirectedGraph();
|
|
4715
|
-
* g.addVertex(1);
|
|
4716
|
-
* g.addVertex(2);
|
|
4717
|
-
* g.addVertex(3);
|
|
4718
|
-
* g.addEdge(1, 2);
|
|
4719
|
-
* g.addEdge(2, 3);
|
|
4720
|
-
* g.addEdge(3, 1);
|
|
4721
|
-
* const sccs = g.getSCCs(); // Map<number, VO[]>
|
|
4722
|
-
* console.log(sccs.size); // >= 1;
|
|
4723
|
-
*/
|
|
3277
|
+
* Strongly connected components computed by `tarjan()`.
|
|
3278
|
+
* @returns Map from SCC id to vertices.
|
|
3279
|
+
* @remarks Time O(#SCC + V), Space O(V)
|
|
3280
|
+
* @example
|
|
3281
|
+
* // Get strongly connected components
|
|
3282
|
+
* const g = new DirectedGraph();
|
|
3283
|
+
* g.addVertex(1);
|
|
3284
|
+
* g.addVertex(2);
|
|
3285
|
+
* g.addVertex(3);
|
|
3286
|
+
* g.addEdge(1, 2);
|
|
3287
|
+
* g.addEdge(2, 3);
|
|
3288
|
+
* g.addEdge(3, 1);
|
|
3289
|
+
* const sccs = g.getSCCs(); // Map<number, VO[]>
|
|
3290
|
+
* console.log(sccs.size); // >= 1;
|
|
3291
|
+
*/
|
|
4724
3292
|
getSCCs() {
|
|
4725
3293
|
return this.tarjan().SCCs;
|
|
4726
3294
|
}
|
|
@@ -4842,58 +3410,19 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
4842
3410
|
return new UndirectedEdge(v1, v2, (_a = weight != null ? weight : this.options.defaultEdgeWeight) != null ? _a : 1, value);
|
|
4843
3411
|
}
|
|
4844
3412
|
/**
|
|
4845
|
-
|
|
4846
|
-
|
|
4847
|
-
|
|
4848
|
-
|
|
4849
|
-
|
|
4850
|
-
|
|
4851
|
-
|
|
4852
|
-
|
|
4853
|
-
|
|
4854
|
-
|
|
4855
|
-
|
|
4856
|
-
|
|
4857
|
-
|
|
4858
|
-
|
|
4859
|
-
|
|
4860
|
-
|
|
4861
|
-
|
|
4862
|
-
|
|
4863
|
-
|
|
4864
|
-
|
|
4865
|
-
|
|
4866
|
-
|
|
4867
|
-
|
|
4868
|
-
|
|
4869
|
-
|
|
4870
|
-
|
|
4871
|
-
|
|
4872
|
-
|
|
4873
|
-
|
|
4874
|
-
|
|
4875
|
-
|
|
4876
|
-
|
|
4877
|
-
|
|
4878
|
-
|
|
4879
|
-
|
|
4880
|
-
|
|
4881
|
-
|
|
4882
|
-
|
|
4883
|
-
|
|
4884
|
-
|
|
4885
|
-
|
|
4886
|
-
|
|
4887
|
-
|
|
4888
|
-
|
|
4889
|
-
* @example
|
|
4890
|
-
* // Get edge between vertices
|
|
4891
|
-
* const g = new UndirectedGraph();
|
|
4892
|
-
* g.addVertex('A');
|
|
4893
|
-
* g.addVertex('B');
|
|
4894
|
-
* g.addEdge('A', 'B', 7);
|
|
4895
|
-
* console.log(g.getEdge('A', 'B')?.weight); // 7;
|
|
4896
|
-
*/
|
|
3413
|
+
* Get an undirected edge between two vertices, if present.
|
|
3414
|
+
* @param v1 - One vertex or key.
|
|
3415
|
+
* @param v2 - The other vertex or key.
|
|
3416
|
+
* @returns Edge instance or `undefined`.
|
|
3417
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
3418
|
+
* @example
|
|
3419
|
+
* // Get edge between vertices
|
|
3420
|
+
* const g = new UndirectedGraph();
|
|
3421
|
+
* g.addVertex('A');
|
|
3422
|
+
* g.addVertex('B');
|
|
3423
|
+
* g.addEdge('A', 'B', 7);
|
|
3424
|
+
* console.log(g.getEdge('A', 'B')?.weight); // 7;
|
|
3425
|
+
*/
|
|
4897
3426
|
getEdge(v1, v2) {
|
|
4898
3427
|
var _a;
|
|
4899
3428
|
let edgeMap = [];
|
|
@@ -4931,81 +3460,39 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
4931
3460
|
return removed;
|
|
4932
3461
|
}
|
|
4933
3462
|
/**
|
|
4934
|
-
|
|
4935
|
-
|
|
4936
|
-
|
|
4937
|
-
|
|
4938
|
-
|
|
4939
|
-
|
|
4940
|
-
|
|
4941
|
-
|
|
4942
|
-
|
|
4943
|
-
|
|
4944
|
-
|
|
4945
|
-
|
|
4946
|
-
|
|
4947
|
-
|
|
4948
|
-
|
|
4949
|
-
|
|
4950
|
-
|
|
4951
|
-
|
|
4952
|
-
|
|
4953
|
-
|
|
4954
|
-
|
|
4955
|
-
|
|
4956
|
-
|
|
4957
|
-
|
|
4958
|
-
|
|
4959
|
-
|
|
4960
|
-
|
|
4961
|
-
|
|
4962
|
-
|
|
4963
|
-
|
|
4964
|
-
|
|
4965
|
-
|
|
4966
|
-
|
|
4967
|
-
|
|
4968
|
-
|
|
4969
|
-
|
|
4970
|
-
|
|
4971
|
-
|
|
4972
|
-
|
|
4973
|
-
|
|
4974
|
-
|
|
4975
|
-
|
|
4976
|
-
|
|
4977
|
-
|
|
4978
|
-
|
|
4979
|
-
|
|
4980
|
-
|
|
4981
|
-
* @example
|
|
4982
|
-
* // UndirectedGraph deleteEdge and vertex operations
|
|
4983
|
-
* const graph = new UndirectedGraph<string>();
|
|
4984
|
-
*
|
|
4985
|
-
* // Build a simple undirected graph
|
|
4986
|
-
* graph.addVertex('X');
|
|
4987
|
-
* graph.addVertex('Y');
|
|
4988
|
-
* graph.addVertex('Z');
|
|
4989
|
-
* graph.addEdge('X', 'Y', 1);
|
|
4990
|
-
* graph.addEdge('Y', 'Z', 2);
|
|
4991
|
-
* graph.addEdge('X', 'Z', 3);
|
|
4992
|
-
*
|
|
4993
|
-
* // Delete an edge
|
|
4994
|
-
* graph.deleteEdge('X', 'Y');
|
|
4995
|
-
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
4996
|
-
*
|
|
4997
|
-
* // Bidirectional deletion confirmed
|
|
4998
|
-
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
4999
|
-
*
|
|
5000
|
-
* // Other edges should remain
|
|
5001
|
-
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
5002
|
-
* console.log(graph.hasEdge('Z', 'Y')); // true;
|
|
5003
|
-
*
|
|
5004
|
-
* // Delete a vertex
|
|
5005
|
-
* graph.deleteVertex('Y');
|
|
5006
|
-
* console.log(graph.hasVertex('Y')); // false;
|
|
5007
|
-
* console.log(graph.size); // 2;
|
|
5008
|
-
*/
|
|
3463
|
+
* Delete an edge by instance or by a pair of keys.
|
|
3464
|
+
* @param edgeOrOneSideVertexKey - Edge instance or one endpoint vertex/key.
|
|
3465
|
+
* @param otherSideVertexKey - Required second endpoint when deleting by pair.
|
|
3466
|
+
* @returns Removed edge or `undefined`.
|
|
3467
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
3468
|
+
* @example
|
|
3469
|
+
* // UndirectedGraph deleteEdge and vertex operations
|
|
3470
|
+
* const graph = new UndirectedGraph<string>();
|
|
3471
|
+
*
|
|
3472
|
+
* // Build a simple undirected graph
|
|
3473
|
+
* graph.addVertex('X');
|
|
3474
|
+
* graph.addVertex('Y');
|
|
3475
|
+
* graph.addVertex('Z');
|
|
3476
|
+
* graph.addEdge('X', 'Y', 1);
|
|
3477
|
+
* graph.addEdge('Y', 'Z', 2);
|
|
3478
|
+
* graph.addEdge('X', 'Z', 3);
|
|
3479
|
+
*
|
|
3480
|
+
* // Delete an edge
|
|
3481
|
+
* graph.deleteEdge('X', 'Y');
|
|
3482
|
+
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
3483
|
+
*
|
|
3484
|
+
* // Bidirectional deletion confirmed
|
|
3485
|
+
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
3486
|
+
*
|
|
3487
|
+
* // Other edges should remain
|
|
3488
|
+
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
3489
|
+
* console.log(graph.hasEdge('Z', 'Y')); // true;
|
|
3490
|
+
*
|
|
3491
|
+
* // Delete a vertex
|
|
3492
|
+
* graph.deleteVertex('Y');
|
|
3493
|
+
* console.log(graph.hasVertex('Y')); // false;
|
|
3494
|
+
* console.log(graph.size); // 2;
|
|
3495
|
+
*/
|
|
5009
3496
|
deleteEdge(edgeOrOneSideVertexKey, otherSideVertexKey) {
|
|
5010
3497
|
let oneSide, otherSide;
|
|
5011
3498
|
if (this.isVertexKey(edgeOrOneSideVertexKey)) {
|
|
@@ -5026,58 +3513,19 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
5026
3513
|
}
|
|
5027
3514
|
}
|
|
5028
3515
|
/**
|
|
5029
|
-
|
|
5030
|
-
|
|
5031
|
-
|
|
5032
|
-
|
|
5033
|
-
|
|
5034
|
-
|
|
5035
|
-
|
|
5036
|
-
|
|
5037
|
-
|
|
5038
|
-
|
|
5039
|
-
|
|
5040
|
-
|
|
5041
|
-
|
|
5042
|
-
|
|
5043
|
-
|
|
5044
|
-
|
|
5045
|
-
|
|
5046
|
-
|
|
5047
|
-
|
|
5048
|
-
|
|
5049
|
-
|
|
5050
|
-
|
|
5051
|
-
|
|
5052
|
-
|
|
5053
|
-
|
|
5054
|
-
|
|
5055
|
-
|
|
5056
|
-
|
|
5057
|
-
|
|
5058
|
-
|
|
5059
|
-
|
|
5060
|
-
|
|
5061
|
-
|
|
5062
|
-
|
|
5063
|
-
|
|
5064
|
-
|
|
5065
|
-
|
|
5066
|
-
|
|
5067
|
-
|
|
5068
|
-
|
|
5069
|
-
|
|
5070
|
-
|
|
5071
|
-
|
|
5072
|
-
* @example
|
|
5073
|
-
* // Remove vertex and edges
|
|
5074
|
-
* const g = new UndirectedGraph();
|
|
5075
|
-
* g.addVertex('A');
|
|
5076
|
-
* g.addVertex('B');
|
|
5077
|
-
* g.addEdge('A', 'B');
|
|
5078
|
-
* g.deleteVertex('A');
|
|
5079
|
-
* console.log(g.hasVertex('A')); // false;
|
|
5080
|
-
*/
|
|
3516
|
+
* Delete a vertex and remove it from all neighbor lists.
|
|
3517
|
+
* @param vertexOrKey - Vertex or key.
|
|
3518
|
+
* @returns `true` if removed; otherwise `false`.
|
|
3519
|
+
* @remarks Time O(deg), Space O(1)
|
|
3520
|
+
* @example
|
|
3521
|
+
* // Remove vertex and edges
|
|
3522
|
+
* const g = new UndirectedGraph();
|
|
3523
|
+
* g.addVertex('A');
|
|
3524
|
+
* g.addVertex('B');
|
|
3525
|
+
* g.addEdge('A', 'B');
|
|
3526
|
+
* g.deleteVertex('A');
|
|
3527
|
+
* console.log(g.hasVertex('A')); // false;
|
|
3528
|
+
*/
|
|
5081
3529
|
deleteVertex(vertexOrKey) {
|
|
5082
3530
|
let vertexKey;
|
|
5083
3531
|
let vertex;
|
|
@@ -5133,56 +3581,17 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
5133
3581
|
}
|
|
5134
3582
|
}
|
|
5135
3583
|
/**
|
|
5136
|
-
|
|
5137
|
-
|
|
5138
|
-
|
|
5139
|
-
|
|
5140
|
-
|
|
5141
|
-
|
|
5142
|
-
|
|
5143
|
-
|
|
5144
|
-
|
|
5145
|
-
|
|
5146
|
-
|
|
5147
|
-
|
|
5148
|
-
|
|
5149
|
-
|
|
5150
|
-
|
|
5151
|
-
|
|
5152
|
-
|
|
5153
|
-
|
|
5154
|
-
|
|
5155
|
-
|
|
5156
|
-
|
|
5157
|
-
|
|
5158
|
-
|
|
5159
|
-
|
|
5160
|
-
|
|
5161
|
-
|
|
5162
|
-
|
|
5163
|
-
|
|
5164
|
-
|
|
5165
|
-
|
|
5166
|
-
|
|
5167
|
-
|
|
5168
|
-
|
|
5169
|
-
|
|
5170
|
-
|
|
5171
|
-
|
|
5172
|
-
|
|
5173
|
-
|
|
5174
|
-
|
|
5175
|
-
|
|
5176
|
-
|
|
5177
|
-
|
|
5178
|
-
* @example
|
|
5179
|
-
* // Get all edges
|
|
5180
|
-
* const g = new UndirectedGraph();
|
|
5181
|
-
* g.addVertex('A');
|
|
5182
|
-
* g.addVertex('B');
|
|
5183
|
-
* g.addEdge('A', 'B');
|
|
5184
|
-
* console.log(g.edgeSet().length); // 1;
|
|
5185
|
-
*/
|
|
3584
|
+
* Unique set of undirected edges across endpoints.
|
|
3585
|
+
* @returns Array of edges.
|
|
3586
|
+
* @remarks Time O(E), Space O(E)
|
|
3587
|
+
* @example
|
|
3588
|
+
* // Get all edges
|
|
3589
|
+
* const g = new UndirectedGraph();
|
|
3590
|
+
* g.addVertex('A');
|
|
3591
|
+
* g.addVertex('B');
|
|
3592
|
+
* g.addEdge('A', 'B');
|
|
3593
|
+
* console.log(g.edgeSet().length); // 1;
|
|
3594
|
+
*/
|
|
5186
3595
|
edgeSet() {
|
|
5187
3596
|
const edgeSet = /* @__PURE__ */ new Set();
|
|
5188
3597
|
this._edgeMap.forEach((edgeMap) => {
|
|
@@ -5193,82 +3602,40 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
5193
3602
|
return [...edgeSet];
|
|
5194
3603
|
}
|
|
5195
3604
|
/**
|
|
5196
|
-
|
|
5197
|
-
|
|
5198
|
-
|
|
5199
|
-
|
|
5200
|
-
|
|
5201
|
-
|
|
5202
|
-
|
|
5203
|
-
|
|
5204
|
-
|
|
5205
|
-
|
|
5206
|
-
|
|
5207
|
-
|
|
5208
|
-
|
|
5209
|
-
|
|
5210
|
-
|
|
5211
|
-
|
|
5212
|
-
|
|
5213
|
-
|
|
5214
|
-
|
|
5215
|
-
|
|
5216
|
-
|
|
5217
|
-
|
|
5218
|
-
|
|
5219
|
-
|
|
5220
|
-
|
|
5221
|
-
|
|
5222
|
-
|
|
5223
|
-
|
|
5224
|
-
|
|
5225
|
-
|
|
5226
|
-
|
|
5227
|
-
|
|
5228
|
-
|
|
5229
|
-
|
|
5230
|
-
|
|
5231
|
-
|
|
5232
|
-
|
|
5233
|
-
|
|
5234
|
-
|
|
5235
|
-
|
|
5236
|
-
|
|
5237
|
-
|
|
5238
|
-
|
|
5239
|
-
* @example
|
|
5240
|
-
* // UndirectedGraph connectivity and neighbors
|
|
5241
|
-
* const graph = new UndirectedGraph<string>();
|
|
5242
|
-
*
|
|
5243
|
-
* // Build a friendship network
|
|
5244
|
-
* const people = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'];
|
|
5245
|
-
* for (const person of people) {
|
|
5246
|
-
* graph.addVertex(person);
|
|
5247
|
-
* }
|
|
5248
|
-
*
|
|
5249
|
-
* // Add friendships (undirected edges)
|
|
5250
|
-
* graph.addEdge('Alice', 'Bob', 1);
|
|
5251
|
-
* graph.addEdge('Alice', 'Charlie', 1);
|
|
5252
|
-
* graph.addEdge('Bob', 'Diana', 1);
|
|
5253
|
-
* graph.addEdge('Charlie', 'Eve', 1);
|
|
5254
|
-
* graph.addEdge('Diana', 'Eve', 1);
|
|
5255
|
-
*
|
|
5256
|
-
* // Get friends of each person
|
|
5257
|
-
* const aliceFriends = graph.getNeighbors('Alice');
|
|
5258
|
-
* console.log(aliceFriends[0].key); // 'Bob';
|
|
5259
|
-
* console.log(aliceFriends[1].key); // 'Charlie';
|
|
5260
|
-
* console.log(aliceFriends.length); // 2;
|
|
5261
|
-
*
|
|
5262
|
-
* const dianaFriends = graph.getNeighbors('Diana');
|
|
5263
|
-
* console.log(dianaFriends[0].key); // 'Bob';
|
|
5264
|
-
* console.log(dianaFriends[1].key); // 'Eve';
|
|
5265
|
-
* console.log(dianaFriends.length); // 2;
|
|
5266
|
-
*
|
|
5267
|
-
* // Verify bidirectional friendship
|
|
5268
|
-
* const bobFriends = graph.getNeighbors('Bob');
|
|
5269
|
-
* console.log(bobFriends[0].key); // 'Alice'; // Alice -> Bob -> Alice ✓
|
|
5270
|
-
* console.log(bobFriends[1].key); // 'Diana';
|
|
5271
|
-
*/
|
|
3605
|
+
* UndirectedGraph connectivity and neighbors
|
|
3606
|
+
* @example
|
|
3607
|
+
* // UndirectedGraph connectivity and neighbors
|
|
3608
|
+
* const graph = new UndirectedGraph<string>();
|
|
3609
|
+
*
|
|
3610
|
+
* // Build a friendship network
|
|
3611
|
+
* const people = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'];
|
|
3612
|
+
* for (const person of people) {
|
|
3613
|
+
* graph.addVertex(person);
|
|
3614
|
+
* }
|
|
3615
|
+
*
|
|
3616
|
+
* // Add friendships (undirected edges)
|
|
3617
|
+
* graph.addEdge('Alice', 'Bob', 1);
|
|
3618
|
+
* graph.addEdge('Alice', 'Charlie', 1);
|
|
3619
|
+
* graph.addEdge('Bob', 'Diana', 1);
|
|
3620
|
+
* graph.addEdge('Charlie', 'Eve', 1);
|
|
3621
|
+
* graph.addEdge('Diana', 'Eve', 1);
|
|
3622
|
+
*
|
|
3623
|
+
* // Get friends of each person
|
|
3624
|
+
* const aliceFriends = graph.getNeighbors('Alice');
|
|
3625
|
+
* console.log(aliceFriends[0].key); // 'Bob';
|
|
3626
|
+
* console.log(aliceFriends[1].key); // 'Charlie';
|
|
3627
|
+
* console.log(aliceFriends.length); // 2;
|
|
3628
|
+
*
|
|
3629
|
+
* const dianaFriends = graph.getNeighbors('Diana');
|
|
3630
|
+
* console.log(dianaFriends[0].key); // 'Bob';
|
|
3631
|
+
* console.log(dianaFriends[1].key); // 'Eve';
|
|
3632
|
+
* console.log(dianaFriends.length); // 2;
|
|
3633
|
+
*
|
|
3634
|
+
* // Verify bidirectional friendship
|
|
3635
|
+
* const bobFriends = graph.getNeighbors('Bob');
|
|
3636
|
+
* console.log(bobFriends[0].key); // 'Alice'; // Alice -> Bob -> Alice ✓
|
|
3637
|
+
* console.log(bobFriends[1].key); // 'Diana';
|
|
3638
|
+
*/
|
|
5272
3639
|
getNeighbors(vertexOrKey) {
|
|
5273
3640
|
const neighbors = [];
|
|
5274
3641
|
const vertex = this._getVertex(vertexOrKey);
|
|
@@ -5325,59 +3692,20 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
5325
3692
|
return super.clone();
|
|
5326
3693
|
}
|
|
5327
3694
|
/**
|
|
5328
|
-
|
|
5329
|
-
|
|
5330
|
-
|
|
5331
|
-
|
|
5332
|
-
|
|
5333
|
-
|
|
5334
|
-
|
|
5335
|
-
|
|
5336
|
-
|
|
5337
|
-
|
|
5338
|
-
|
|
5339
|
-
|
|
5340
|
-
|
|
5341
|
-
|
|
5342
|
-
|
|
5343
|
-
|
|
5344
|
-
|
|
5345
|
-
|
|
5346
|
-
|
|
5347
|
-
|
|
5348
|
-
|
|
5349
|
-
|
|
5350
|
-
|
|
5351
|
-
|
|
5352
|
-
|
|
5353
|
-
|
|
5354
|
-
|
|
5355
|
-
|
|
5356
|
-
|
|
5357
|
-
|
|
5358
|
-
|
|
5359
|
-
|
|
5360
|
-
|
|
5361
|
-
|
|
5362
|
-
|
|
5363
|
-
|
|
5364
|
-
|
|
5365
|
-
|
|
5366
|
-
|
|
5367
|
-
|
|
5368
|
-
|
|
5369
|
-
|
|
5370
|
-
* @example
|
|
5371
|
-
* // Find articulation points and bridges
|
|
5372
|
-
* const g = new UndirectedGraph();
|
|
5373
|
-
* g.addVertex('A');
|
|
5374
|
-
* g.addVertex('B');
|
|
5375
|
-
* g.addVertex('C');
|
|
5376
|
-
* g.addEdge('A', 'B');
|
|
5377
|
-
* g.addEdge('B', 'C');
|
|
5378
|
-
* const result = g.tarjan();
|
|
5379
|
-
* console.log(result); // defined;
|
|
5380
|
-
*/
|
|
3695
|
+
* Tarjan-based bridge and articulation point detection.
|
|
3696
|
+
* @returns `{ dfnMap, lowMap, bridges, cutVertices }`.
|
|
3697
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
3698
|
+
* @example
|
|
3699
|
+
* // Find articulation points and bridges
|
|
3700
|
+
* const g = new UndirectedGraph();
|
|
3701
|
+
* g.addVertex('A');
|
|
3702
|
+
* g.addVertex('B');
|
|
3703
|
+
* g.addVertex('C');
|
|
3704
|
+
* g.addEdge('A', 'B');
|
|
3705
|
+
* g.addEdge('B', 'C');
|
|
3706
|
+
* const result = g.tarjan();
|
|
3707
|
+
* console.log(result); // defined;
|
|
3708
|
+
*/
|
|
5381
3709
|
tarjan() {
|
|
5382
3710
|
const dfnMap = /* @__PURE__ */ new Map();
|
|
5383
3711
|
const lowMap = /* @__PURE__ */ new Map();
|
|
@@ -5477,61 +3805,22 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
5477
3805
|
return components;
|
|
5478
3806
|
}
|
|
5479
3807
|
/**
|
|
5480
|
-
|
|
5481
|
-
|
|
5482
|
-
|
|
5483
|
-
|
|
5484
|
-
|
|
5485
|
-
|
|
5486
|
-
|
|
5487
|
-
|
|
5488
|
-
|
|
5489
|
-
|
|
5490
|
-
|
|
5491
|
-
|
|
5492
|
-
|
|
5493
|
-
|
|
5494
|
-
|
|
5495
|
-
|
|
5496
|
-
|
|
5497
|
-
|
|
5498
|
-
|
|
5499
|
-
|
|
5500
|
-
|
|
5501
|
-
|
|
5502
|
-
|
|
5503
|
-
|
|
5504
|
-
|
|
5505
|
-
|
|
5506
|
-
|
|
5507
|
-
|
|
5508
|
-
|
|
5509
|
-
|
|
5510
|
-
|
|
5511
|
-
|
|
5512
|
-
|
|
5513
|
-
|
|
5514
|
-
|
|
5515
|
-
|
|
5516
|
-
|
|
5517
|
-
|
|
5518
|
-
|
|
5519
|
-
|
|
5520
|
-
|
|
5521
|
-
|
|
5522
|
-
|
|
5523
|
-
* @example
|
|
5524
|
-
* // Detect cycle
|
|
5525
|
-
* const g = new UndirectedGraph();
|
|
5526
|
-
* g.addVertex('A');
|
|
5527
|
-
* g.addVertex('B');
|
|
5528
|
-
* g.addVertex('C');
|
|
5529
|
-
* g.addEdge('A', 'B');
|
|
5530
|
-
* g.addEdge('B', 'C');
|
|
5531
|
-
* console.log(g.hasCycle()); // false;
|
|
5532
|
-
* g.addEdge('C', 'A');
|
|
5533
|
-
* console.log(g.hasCycle()); // true;
|
|
5534
|
-
*/
|
|
3808
|
+
* Detect whether the graph contains a cycle.
|
|
3809
|
+
* Uses DFS with parent tracking.
|
|
3810
|
+
* @returns `true` if a cycle exists, `false` otherwise.
|
|
3811
|
+
* @remarks Time O(V + E), Space O(V)
|
|
3812
|
+
* @example
|
|
3813
|
+
* // Detect cycle
|
|
3814
|
+
* const g = new UndirectedGraph();
|
|
3815
|
+
* g.addVertex('A');
|
|
3816
|
+
* g.addVertex('B');
|
|
3817
|
+
* g.addVertex('C');
|
|
3818
|
+
* g.addEdge('A', 'B');
|
|
3819
|
+
* g.addEdge('B', 'C');
|
|
3820
|
+
* console.log(g.hasCycle()); // false;
|
|
3821
|
+
* g.addEdge('C', 'A');
|
|
3822
|
+
* console.log(g.hasCycle()); // true;
|
|
3823
|
+
*/
|
|
5535
3824
|
hasCycle() {
|
|
5536
3825
|
const visited = /* @__PURE__ */ new Set();
|
|
5537
3826
|
const dfs = /* @__PURE__ */ __name((vertex, parent) => {
|
|
@@ -5553,117 +3842,39 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
5553
3842
|
return false;
|
|
5554
3843
|
}
|
|
5555
3844
|
/**
|
|
5556
|
-
|
|
5557
|
-
|
|
5558
|
-
|
|
5559
|
-
|
|
5560
|
-
|
|
5561
|
-
|
|
5562
|
-
|
|
5563
|
-
|
|
5564
|
-
|
|
5565
|
-
|
|
5566
|
-
|
|
5567
|
-
|
|
5568
|
-
|
|
5569
|
-
|
|
5570
|
-
|
|
5571
|
-
|
|
5572
|
-
|
|
5573
|
-
|
|
5574
|
-
|
|
5575
|
-
|
|
5576
|
-
|
|
5577
|
-
|
|
5578
|
-
|
|
5579
|
-
|
|
5580
|
-
|
|
5581
|
-
|
|
5582
|
-
|
|
5583
|
-
|
|
5584
|
-
|
|
5585
|
-
|
|
5586
|
-
|
|
5587
|
-
|
|
5588
|
-
|
|
5589
|
-
|
|
5590
|
-
|
|
5591
|
-
|
|
5592
|
-
|
|
5593
|
-
|
|
5594
|
-
|
|
5595
|
-
|
|
5596
|
-
|
|
5597
|
-
|
|
5598
|
-
* @example
|
|
5599
|
-
* // Find bridge edges
|
|
5600
|
-
* const g = new UndirectedGraph();
|
|
5601
|
-
* g.addVertex('A');
|
|
5602
|
-
* g.addVertex('B');
|
|
5603
|
-
* g.addVertex('C');
|
|
5604
|
-
* g.addEdge('A', 'B');
|
|
5605
|
-
* g.addEdge('B', 'C');
|
|
5606
|
-
* const bridges = g.getBridges();
|
|
5607
|
-
* console.log(bridges.length); // 2;
|
|
5608
|
-
*/
|
|
3845
|
+
* Get bridges discovered by `tarjan()`.
|
|
3846
|
+
* @returns Array of edges that are bridges.
|
|
3847
|
+
* @remarks Time O(B), Space O(1)
|
|
3848
|
+
* @example
|
|
3849
|
+
* // Find bridge edges
|
|
3850
|
+
* const g = new UndirectedGraph();
|
|
3851
|
+
* g.addVertex('A');
|
|
3852
|
+
* g.addVertex('B');
|
|
3853
|
+
* g.addVertex('C');
|
|
3854
|
+
* g.addEdge('A', 'B');
|
|
3855
|
+
* g.addEdge('B', 'C');
|
|
3856
|
+
* const bridges = g.getBridges();
|
|
3857
|
+
* console.log(bridges.length); // 2;
|
|
3858
|
+
*/
|
|
5609
3859
|
getBridges() {
|
|
5610
3860
|
return this.tarjan().bridges;
|
|
5611
3861
|
}
|
|
5612
3862
|
/**
|
|
5613
|
-
|
|
5614
|
-
|
|
5615
|
-
|
|
5616
|
-
|
|
5617
|
-
|
|
5618
|
-
|
|
5619
|
-
|
|
5620
|
-
|
|
5621
|
-
|
|
5622
|
-
|
|
5623
|
-
|
|
5624
|
-
|
|
5625
|
-
|
|
5626
|
-
|
|
5627
|
-
|
|
5628
|
-
|
|
5629
|
-
|
|
5630
|
-
|
|
5631
|
-
|
|
5632
|
-
|
|
5633
|
-
|
|
5634
|
-
|
|
5635
|
-
|
|
5636
|
-
|
|
5637
|
-
|
|
5638
|
-
|
|
5639
|
-
|
|
5640
|
-
|
|
5641
|
-
|
|
5642
|
-
|
|
5643
|
-
|
|
5644
|
-
|
|
5645
|
-
|
|
5646
|
-
|
|
5647
|
-
|
|
5648
|
-
|
|
5649
|
-
|
|
5650
|
-
|
|
5651
|
-
|
|
5652
|
-
|
|
5653
|
-
|
|
5654
|
-
|
|
5655
|
-
* @example
|
|
5656
|
-
* // Find articulation points
|
|
5657
|
-
* const g = new UndirectedGraph();
|
|
5658
|
-
* g.addVertex('A');
|
|
5659
|
-
* g.addVertex('B');
|
|
5660
|
-
* g.addVertex('C');
|
|
5661
|
-
* g.addEdge('A', 'B');
|
|
5662
|
-
* g.addEdge('B', 'C');
|
|
5663
|
-
* const cuts = g.getCutVertices();
|
|
5664
|
-
* console.log(cuts.length); // 1;
|
|
5665
|
-
* console.log(cuts[0].key); // 'B';
|
|
5666
|
-
*/
|
|
3863
|
+
* Get articulation points discovered by `tarjan()`.
|
|
3864
|
+
* @returns Array of cut vertices.
|
|
3865
|
+
* @remarks Time O(C), Space O(1)
|
|
3866
|
+
* @example
|
|
3867
|
+
* // Find articulation points
|
|
3868
|
+
* const g = new UndirectedGraph();
|
|
3869
|
+
* g.addVertex('A');
|
|
3870
|
+
* g.addVertex('B');
|
|
3871
|
+
* g.addVertex('C');
|
|
3872
|
+
* g.addEdge('A', 'B');
|
|
3873
|
+
* g.addEdge('B', 'C');
|
|
3874
|
+
* const cuts = g.getCutVertices();
|
|
3875
|
+
* console.log(cuts.length); // 1;
|
|
3876
|
+
* console.log(cuts[0].key); // 'B';
|
|
3877
|
+
*/
|
|
5667
3878
|
getCutVertices() {
|
|
5668
3879
|
return this.tarjan().cutVertices;
|
|
5669
3880
|
}
|