data-structure-typed 2.6.1 → 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/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 +72 -104
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +60 -80
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +67 -101
- 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 +40 -68
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +17 -17
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +38 -38
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +45 -45
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +68 -96
- 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 +38 -66
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +41 -74
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +38 -66
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +46 -79
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +40 -68
- package/docs-site-docusaurus/docs/api/classes/Queue.md +54 -78
- 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 +68 -98
- 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 +33 -53
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +104 -129
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +57 -124
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +45 -116
- package/docs-site-docusaurus/docs/api/classes/Trie.md +37 -61
- 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 +1 -1
- package/src/data-structures/binary-tree/tree-multi-set.ts +15 -11
|
@@ -728,61 +728,19 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
728
728
|
return this._elements;
|
|
729
729
|
}
|
|
730
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
|
-
|
|
775
|
-
|
|
776
|
-
* @example
|
|
777
|
-
* // Track heap capacity
|
|
778
|
-
* const heap = new Heap<number>();
|
|
779
|
-
* console.log(heap.size); // 0;
|
|
780
|
-
* heap.add(10);
|
|
781
|
-
* heap.add(20);
|
|
782
|
-
* console.log(heap.size); // 2;
|
|
783
|
-
* heap.poll();
|
|
784
|
-
* console.log(heap.size); // 1;
|
|
785
|
-
*/
|
|
731
|
+
* Get the number of elements.
|
|
732
|
+
* @remarks Time O(1), Space O(1)
|
|
733
|
+
* @returns Heap size.
|
|
734
|
+
* @example
|
|
735
|
+
* // Track heap capacity
|
|
736
|
+
* const heap = new Heap<number>();
|
|
737
|
+
* console.log(heap.size); // 0;
|
|
738
|
+
* heap.add(10);
|
|
739
|
+
* heap.add(20);
|
|
740
|
+
* console.log(heap.size); // 2;
|
|
741
|
+
* heap.poll();
|
|
742
|
+
* console.log(heap.size); // 1;
|
|
743
|
+
*/
|
|
786
744
|
get size() {
|
|
787
745
|
return this.elements.length;
|
|
788
746
|
}
|
|
@@ -795,6 +753,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
795
753
|
var _a;
|
|
796
754
|
return (_a = this.elements[this.size - 1]) != null ? _a : void 0;
|
|
797
755
|
}
|
|
756
|
+
/**
|
|
757
|
+
* Get the comparator used to order elements.
|
|
758
|
+
* @remarks Time O(1), Space O(1)
|
|
759
|
+
* @returns Comparator function.
|
|
760
|
+
*/
|
|
761
|
+
get comparator() {
|
|
762
|
+
return this._comparator;
|
|
763
|
+
}
|
|
798
764
|
/**
|
|
799
765
|
* Create a heap of the same class from an iterable.
|
|
800
766
|
* @remarks Time O(N), Space O(N)
|
|
@@ -821,123 +787,42 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
821
787
|
return new _Heap(elements, options);
|
|
822
788
|
}
|
|
823
789
|
/**
|
|
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
|
-
|
|
869
|
-
|
|
870
|
-
* @example
|
|
871
|
-
* // basic Heap creation and add operation
|
|
872
|
-
* // Create a min heap (default)
|
|
873
|
-
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
874
|
-
*
|
|
875
|
-
* // Verify size
|
|
876
|
-
* console.log(minHeap.size); // 6;
|
|
877
|
-
*
|
|
878
|
-
* // Add new element
|
|
879
|
-
* minHeap.add(4);
|
|
880
|
-
* console.log(minHeap.size); // 7;
|
|
881
|
-
*
|
|
882
|
-
* // Min heap property: smallest element at root
|
|
883
|
-
* const min = minHeap.peek();
|
|
884
|
-
* console.log(min); // 1;
|
|
885
|
-
*/
|
|
790
|
+
* Insert an element.
|
|
791
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
792
|
+
* @param element - Element to insert.
|
|
793
|
+
* @returns True.
|
|
794
|
+
* @example
|
|
795
|
+
* // basic Heap creation and add operation
|
|
796
|
+
* // Create a min heap (default)
|
|
797
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
798
|
+
*
|
|
799
|
+
* // Verify size
|
|
800
|
+
* console.log(minHeap.size); // 6;
|
|
801
|
+
*
|
|
802
|
+
* // Add new element
|
|
803
|
+
* minHeap.add(4);
|
|
804
|
+
* console.log(minHeap.size); // 7;
|
|
805
|
+
*
|
|
806
|
+
* // Min heap property: smallest element at root
|
|
807
|
+
* const min = minHeap.peek();
|
|
808
|
+
* console.log(min); // 1;
|
|
809
|
+
*/
|
|
886
810
|
add(element) {
|
|
887
811
|
this._elements.push(element);
|
|
888
812
|
return this._bubbleUp(this.elements.length - 1);
|
|
889
813
|
}
|
|
890
814
|
/**
|
|
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
|
-
|
|
933
|
-
|
|
934
|
-
* @example
|
|
935
|
-
* // Add multiple elements
|
|
936
|
-
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
937
|
-
* heap.addMany([5, 3, 7, 1]);
|
|
938
|
-
* console.log(heap.peek()); // 1;
|
|
939
|
-
* console.log(heap.size); // 4;
|
|
940
|
-
*/
|
|
815
|
+
* Insert many elements from an iterable.
|
|
816
|
+
* @remarks Time O(N log N), Space O(1)
|
|
817
|
+
* @param elements - Iterable of elements or raw values.
|
|
818
|
+
* @returns Array of per-element success flags.
|
|
819
|
+
* @example
|
|
820
|
+
* // Add multiple elements
|
|
821
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
822
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
823
|
+
* console.log(heap.peek()); // 1;
|
|
824
|
+
* console.log(heap.size); // 4;
|
|
825
|
+
*/
|
|
941
826
|
addMany(elements) {
|
|
942
827
|
const flags = [];
|
|
943
828
|
for (const el of elements) {
|
|
@@ -952,104 +837,63 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
952
837
|
return flags;
|
|
953
838
|
}
|
|
954
839
|
/**
|
|
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
|
-
|
|
1011
|
-
|
|
1012
|
-
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
1013
|
-
* });
|
|
1014
|
-
*
|
|
1015
|
-
* console.log(maxHeap.size); // 3;
|
|
1016
|
-
*
|
|
1017
|
-
* // Peek returns highest priority task
|
|
1018
|
-
* const topTask = maxHeap.peek();
|
|
1019
|
-
* console.log(topTask?.priority); // 8;
|
|
1020
|
-
* console.log(topTask?.name); // 'Alert';
|
|
1021
|
-
*/
|
|
1022
|
-
/**
|
|
1023
|
-
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
* @example
|
|
1028
|
-
* // Heap with custom comparator (MaxHeap behavior)
|
|
1029
|
-
* interface Task {
|
|
1030
|
-
* id: number;
|
|
1031
|
-
* priority: number;
|
|
1032
|
-
* name: string;
|
|
1033
|
-
* }
|
|
1034
|
-
*
|
|
1035
|
-
* // Custom comparator for max heap behavior (higher priority first)
|
|
1036
|
-
* const tasks: Task[] = [
|
|
1037
|
-
* { id: 1, priority: 5, name: 'Email' },
|
|
1038
|
-
* { id: 2, priority: 3, name: 'Chat' },
|
|
1039
|
-
* { id: 3, priority: 8, name: 'Alert' }
|
|
1040
|
-
* ];
|
|
1041
|
-
*
|
|
1042
|
-
* const maxHeap = new Heap(tasks, {
|
|
1043
|
-
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
1044
|
-
* });
|
|
1045
|
-
*
|
|
1046
|
-
* console.log(maxHeap.size); // 3;
|
|
1047
|
-
*
|
|
1048
|
-
* // Peek returns highest priority task
|
|
1049
|
-
* const topTask = maxHeap.peek();
|
|
1050
|
-
* console.log(topTask?.priority); // 8;
|
|
1051
|
-
* console.log(topTask?.name); // 'Alert';
|
|
1052
|
-
*/
|
|
840
|
+
* Remove and return the top element.
|
|
841
|
+
* @remarks Time O(log N), Space O(1)
|
|
842
|
+
* @returns Top element or undefined.
|
|
843
|
+
* @example
|
|
844
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
845
|
+
* interface Task {
|
|
846
|
+
* id: number;
|
|
847
|
+
* priority: number;
|
|
848
|
+
* name: string;
|
|
849
|
+
* }
|
|
850
|
+
*
|
|
851
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
852
|
+
* const tasks: Task[] = [
|
|
853
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
854
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
855
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
856
|
+
* ];
|
|
857
|
+
*
|
|
858
|
+
* const maxHeap = new Heap(tasks, {
|
|
859
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
860
|
+
* });
|
|
861
|
+
*
|
|
862
|
+
* console.log(maxHeap.size); // 3;
|
|
863
|
+
*
|
|
864
|
+
* // Peek returns highest priority task
|
|
865
|
+
* const topTask = maxHeap.peek();
|
|
866
|
+
* console.log(topTask?.priority); // 8;
|
|
867
|
+
* console.log(topTask?.name); // 'Alert';
|
|
868
|
+
*/
|
|
869
|
+
/**
|
|
870
|
+
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
871
|
+
* @example
|
|
872
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
873
|
+
* interface Task {
|
|
874
|
+
* id: number;
|
|
875
|
+
* priority: number;
|
|
876
|
+
* name: string;
|
|
877
|
+
* }
|
|
878
|
+
*
|
|
879
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
880
|
+
* const tasks: Task[] = [
|
|
881
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
882
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
883
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
884
|
+
* ];
|
|
885
|
+
*
|
|
886
|
+
* const maxHeap = new Heap(tasks, {
|
|
887
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
888
|
+
* });
|
|
889
|
+
*
|
|
890
|
+
* console.log(maxHeap.size); // 3;
|
|
891
|
+
*
|
|
892
|
+
* // Peek returns highest priority task
|
|
893
|
+
* const topTask = maxHeap.peek();
|
|
894
|
+
* console.log(topTask?.priority); // 8;
|
|
895
|
+
* console.log(topTask?.name); // 'Alert';
|
|
896
|
+
*/
|
|
1053
897
|
poll() {
|
|
1054
898
|
return this.pop();
|
|
1055
899
|
}
|
|
@@ -1069,319 +913,125 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1069
913
|
return value;
|
|
1070
914
|
}
|
|
1071
915
|
/**
|
|
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
|
-
* });
|
|
1136
|
-
*
|
|
1137
|
-
* // Add events in random order
|
|
1138
|
-
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
1139
|
-
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
1140
|
-
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
1141
|
-
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
1142
|
-
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
1143
|
-
*
|
|
1144
|
-
* console.log(eventHeap.size); // 5;
|
|
1145
|
-
*
|
|
1146
|
-
* // Process events by priority (critical first)
|
|
1147
|
-
* const processedOrder: Event[] = [];
|
|
1148
|
-
* while (eventHeap.size > 0) {
|
|
1149
|
-
* const event = eventHeap.poll();
|
|
1150
|
-
* if (event) {
|
|
1151
|
-
* processedOrder.push(event);
|
|
1152
|
-
* }
|
|
1153
|
-
* }
|
|
1154
|
-
*
|
|
1155
|
-
* // Verify critical events came first
|
|
1156
|
-
* console.log(processedOrder[0].type); // 'critical';
|
|
1157
|
-
* console.log(processedOrder[1].type); // 'critical';
|
|
1158
|
-
* console.log(processedOrder[2].type); // 'warning';
|
|
1159
|
-
* console.log(processedOrder[3].type); // 'info';
|
|
1160
|
-
* console.log(processedOrder[4].type); // 'info';
|
|
1161
|
-
*
|
|
1162
|
-
* // Verify O(log n) operations
|
|
1163
|
-
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
1164
|
-
*
|
|
1165
|
-
* // Add - O(log n)
|
|
1166
|
-
* newHeap.add(2);
|
|
1167
|
-
* console.log(newHeap.size); // 5;
|
|
1168
|
-
*
|
|
1169
|
-
* // Poll - O(log n)
|
|
1170
|
-
* const removed = newHeap.poll();
|
|
1171
|
-
* console.log(removed); // 1;
|
|
1172
|
-
*
|
|
1173
|
-
* // Peek - O(1)
|
|
1174
|
-
* const top = newHeap.peek();
|
|
1175
|
-
* console.log(top); // 2;
|
|
1176
|
-
*/
|
|
916
|
+
* Get the current top element without removing it.
|
|
917
|
+
* @remarks Time O(1), Space O(1)
|
|
918
|
+
* @returns Top element or undefined.
|
|
919
|
+
* @example
|
|
920
|
+
* // Heap for event processing with priority
|
|
921
|
+
* interface Event {
|
|
922
|
+
* id: number;
|
|
923
|
+
* type: 'critical' | 'warning' | 'info';
|
|
924
|
+
* timestamp: number;
|
|
925
|
+
* message: string;
|
|
926
|
+
* }
|
|
927
|
+
*
|
|
928
|
+
* // Custom priority: critical > warning > info
|
|
929
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
930
|
+
*
|
|
931
|
+
* const eventHeap = new Heap<Event>([], {
|
|
932
|
+
* comparator: (a: Event, b: Event) => {
|
|
933
|
+
* const priorityA = priorityMap[a.type];
|
|
934
|
+
* const priorityB = priorityMap[b.type];
|
|
935
|
+
* return priorityB - priorityA; // Higher priority first
|
|
936
|
+
* }
|
|
937
|
+
* });
|
|
938
|
+
*
|
|
939
|
+
* // Add events in random order
|
|
940
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
941
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
942
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
943
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
944
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
945
|
+
*
|
|
946
|
+
* console.log(eventHeap.size); // 5;
|
|
947
|
+
*
|
|
948
|
+
* // Process events by priority (critical first)
|
|
949
|
+
* const processedOrder: Event[] = [];
|
|
950
|
+
* while (eventHeap.size > 0) {
|
|
951
|
+
* const event = eventHeap.poll();
|
|
952
|
+
* if (event) {
|
|
953
|
+
* processedOrder.push(event);
|
|
954
|
+
* }
|
|
955
|
+
* }
|
|
956
|
+
*
|
|
957
|
+
* // Verify critical events came first
|
|
958
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
959
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
960
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
961
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
962
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
963
|
+
*
|
|
964
|
+
* // Verify O(log n) operations
|
|
965
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
966
|
+
*
|
|
967
|
+
* // Add - O(log n)
|
|
968
|
+
* newHeap.add(2);
|
|
969
|
+
* console.log(newHeap.size); // 5;
|
|
970
|
+
*
|
|
971
|
+
* // Poll - O(log n)
|
|
972
|
+
* const removed = newHeap.poll();
|
|
973
|
+
* console.log(removed); // 1;
|
|
974
|
+
*
|
|
975
|
+
* // Peek - O(1)
|
|
976
|
+
* const top = newHeap.peek();
|
|
977
|
+
* console.log(top); // 2;
|
|
978
|
+
*/
|
|
1177
979
|
peek() {
|
|
1178
980
|
return this.elements[0];
|
|
1179
981
|
}
|
|
1180
982
|
/**
|
|
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
|
-
|
|
1223
|
-
|
|
1224
|
-
* @example
|
|
1225
|
-
* // Check if heap is empty
|
|
1226
|
-
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
1227
|
-
* console.log(heap.isEmpty()); // true;
|
|
1228
|
-
* heap.add(1);
|
|
1229
|
-
* console.log(heap.isEmpty()); // false;
|
|
1230
|
-
*/
|
|
983
|
+
* Check whether the heap is empty.
|
|
984
|
+
* @remarks Time O(1), Space O(1)
|
|
985
|
+
* @returns True if size is 0.
|
|
986
|
+
* @example
|
|
987
|
+
* // Check if heap is empty
|
|
988
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
989
|
+
* console.log(heap.isEmpty()); // true;
|
|
990
|
+
* heap.add(1);
|
|
991
|
+
* console.log(heap.isEmpty()); // false;
|
|
992
|
+
*/
|
|
1231
993
|
isEmpty() {
|
|
1232
994
|
return this.size === 0;
|
|
1233
995
|
}
|
|
1234
996
|
/**
|
|
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
|
-
|
|
1277
|
-
|
|
1278
|
-
* @example
|
|
1279
|
-
* // Remove all elements
|
|
1280
|
-
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1281
|
-
* heap.clear();
|
|
1282
|
-
* console.log(heap.isEmpty()); // true;
|
|
1283
|
-
*/
|
|
997
|
+
* Remove all elements.
|
|
998
|
+
* @remarks Time O(1), Space O(1)
|
|
999
|
+
* @returns void
|
|
1000
|
+
* @example
|
|
1001
|
+
* // Remove all elements
|
|
1002
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1003
|
+
* heap.clear();
|
|
1004
|
+
* console.log(heap.isEmpty()); // true;
|
|
1005
|
+
*/
|
|
1284
1006
|
clear() {
|
|
1285
1007
|
this._elements = [];
|
|
1286
1008
|
}
|
|
1287
1009
|
/**
|
|
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
|
-
|
|
1324
|
-
|
|
1325
|
-
* @example
|
|
1326
|
-
* // Check element existence
|
|
1327
|
-
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
1328
|
-
* console.log(heap.has(1)); // true;
|
|
1329
|
-
* console.log(heap.has(99)); // false;
|
|
1330
|
-
*/
|
|
1010
|
+
* Check if an equal element exists in the heap.
|
|
1011
|
+
* @remarks Time O(N), Space O(1)
|
|
1012
|
+
* @param element - Element to search for.
|
|
1013
|
+
* @returns True if found.
|
|
1014
|
+
* @example
|
|
1015
|
+
* // Check element existence
|
|
1016
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
1017
|
+
* console.log(heap.has(1)); // true;
|
|
1018
|
+
* console.log(heap.has(99)); // false;
|
|
1019
|
+
*/
|
|
1331
1020
|
has(element) {
|
|
1332
1021
|
for (const el of this.elements) if (this._equals(el, element)) return true;
|
|
1333
1022
|
return false;
|
|
1334
1023
|
}
|
|
1335
1024
|
/**
|
|
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
|
-
|
|
1378
|
-
|
|
1379
|
-
* @example
|
|
1380
|
-
* // Remove specific element
|
|
1381
|
-
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
1382
|
-
* heap.delete(4);
|
|
1383
|
-
* console.log(heap.toArray().includes(4)); // false;
|
|
1384
|
-
*/
|
|
1025
|
+
* Delete one occurrence of an element.
|
|
1026
|
+
* @remarks Time O(N), Space O(1)
|
|
1027
|
+
* @param element - Element to delete.
|
|
1028
|
+
* @returns True if an element was removed.
|
|
1029
|
+
* @example
|
|
1030
|
+
* // Remove specific element
|
|
1031
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
1032
|
+
* heap.delete(4);
|
|
1033
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
1034
|
+
*/
|
|
1385
1035
|
delete(element) {
|
|
1386
1036
|
let index = -1;
|
|
1387
1037
|
for (let i = 0; i < this.elements.length; i++) {
|
|
@@ -1445,49 +1095,16 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1445
1095
|
return this;
|
|
1446
1096
|
}
|
|
1447
1097
|
/**
|
|
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
|
-
|
|
1484
|
-
|
|
1485
|
-
* @example
|
|
1486
|
-
* // Depth-first traversal
|
|
1487
|
-
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
1488
|
-
* const result = heap.dfs('IN');
|
|
1489
|
-
* console.log(result.length); // 3;
|
|
1490
|
-
*/
|
|
1098
|
+
* Traverse the binary heap as a complete binary tree and collect elements.
|
|
1099
|
+
* @remarks Time O(N), Space O(H)
|
|
1100
|
+
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
1101
|
+
* @returns Array of visited elements.
|
|
1102
|
+
* @example
|
|
1103
|
+
* // Depth-first traversal
|
|
1104
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
1105
|
+
* const result = heap.dfs('IN');
|
|
1106
|
+
* console.log(result.length); // 3;
|
|
1107
|
+
*/
|
|
1491
1108
|
dfs(order = "PRE") {
|
|
1492
1109
|
const result = [];
|
|
1493
1110
|
const _dfs = /* @__PURE__ */ __name((index) => {
|
|
@@ -1524,57 +1141,15 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1524
1141
|
return results;
|
|
1525
1142
|
}
|
|
1526
1143
|
/**
|
|
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
|
-
|
|
1571
|
-
|
|
1572
|
-
* @example
|
|
1573
|
-
* // Sort elements using heap
|
|
1574
|
-
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
1575
|
-
* const sorted = heap.sort();
|
|
1576
|
-
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
1577
|
-
*/
|
|
1144
|
+
* Return all elements in ascending order by repeatedly polling.
|
|
1145
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1146
|
+
* @returns Sorted array of elements.
|
|
1147
|
+
* @example
|
|
1148
|
+
* // Sort elements using heap
|
|
1149
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
1150
|
+
* const sorted = heap.sort();
|
|
1151
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
1152
|
+
*/
|
|
1578
1153
|
sort() {
|
|
1579
1154
|
const visited = [];
|
|
1580
1155
|
const cloned = this._createInstance();
|
|
@@ -1586,114 +1161,34 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1586
1161
|
return visited;
|
|
1587
1162
|
}
|
|
1588
1163
|
/**
|
|
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
|
-
|
|
1631
|
-
|
|
1632
|
-
* @example
|
|
1633
|
-
* // Create independent copy
|
|
1634
|
-
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
1635
|
-
* const copy = heap.clone();
|
|
1636
|
-
* copy.poll();
|
|
1637
|
-
* console.log(heap.size); // 3;
|
|
1638
|
-
* console.log(copy.size); // 2;
|
|
1639
|
-
*/
|
|
1164
|
+
* Deep clone this heap.
|
|
1165
|
+
* @remarks Time O(N), Space O(N)
|
|
1166
|
+
* @returns A new heap with the same elements.
|
|
1167
|
+
* @example
|
|
1168
|
+
* // Create independent copy
|
|
1169
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
1170
|
+
* const copy = heap.clone();
|
|
1171
|
+
* copy.poll();
|
|
1172
|
+
* console.log(heap.size); // 3;
|
|
1173
|
+
* console.log(copy.size); // 2;
|
|
1174
|
+
*/
|
|
1640
1175
|
clone() {
|
|
1641
1176
|
const next = this._createInstance();
|
|
1642
1177
|
for (const x of this.elements) next.add(x);
|
|
1643
1178
|
return next;
|
|
1644
1179
|
}
|
|
1645
1180
|
/**
|
|
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
|
-
|
|
1690
|
-
|
|
1691
|
-
* @example
|
|
1692
|
-
* // Filter elements
|
|
1693
|
-
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
1694
|
-
* const evens = heap.filter(x => x % 2 === 0);
|
|
1695
|
-
* console.log(evens.size); // 2;
|
|
1696
|
-
*/
|
|
1181
|
+
* Filter elements into a new heap of the same class.
|
|
1182
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1183
|
+
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
1184
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1185
|
+
* @returns A new heap with the kept elements.
|
|
1186
|
+
* @example
|
|
1187
|
+
* // Filter elements
|
|
1188
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
1189
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
1190
|
+
* console.log(evens.size); // 2;
|
|
1191
|
+
*/
|
|
1697
1192
|
filter(callback, thisArg) {
|
|
1698
1193
|
const out = this._createInstance();
|
|
1699
1194
|
let i = 0;
|
|
@@ -1707,59 +1202,20 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1707
1202
|
return out;
|
|
1708
1203
|
}
|
|
1709
1204
|
/**
|
|
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
|
-
|
|
1756
|
-
|
|
1757
|
-
* @example
|
|
1758
|
-
* // Transform elements
|
|
1759
|
-
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1760
|
-
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
1761
|
-
* console.log(doubled.peek()); // 2;
|
|
1762
|
-
*/
|
|
1205
|
+
* Map elements into a new heap of possibly different element type.
|
|
1206
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1207
|
+
* @template EM
|
|
1208
|
+
* @template RM
|
|
1209
|
+
* @param callback - Mapping function (element, index, heap) → newElement.
|
|
1210
|
+
* @param options - Options for the output heap, including comparator for EM.
|
|
1211
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1212
|
+
* @returns A new heap with mapped elements.
|
|
1213
|
+
* @example
|
|
1214
|
+
* // Transform elements
|
|
1215
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1216
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
1217
|
+
* console.log(doubled.peek()); // 2;
|
|
1218
|
+
*/
|
|
1763
1219
|
map(callback, options, thisArg) {
|
|
1764
1220
|
const { comparator, toElementFn, ...rest } = options != null ? options : {};
|
|
1765
1221
|
if (!comparator) raise(TypeError, ERR.comparatorRequired("Heap.map"));
|
|
@@ -1787,14 +1243,6 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1787
1243
|
}
|
|
1788
1244
|
return out;
|
|
1789
1245
|
}
|
|
1790
|
-
/**
|
|
1791
|
-
* Get the comparator used to order elements.
|
|
1792
|
-
* @remarks Time O(1), Space O(1)
|
|
1793
|
-
* @returns Comparator function.
|
|
1794
|
-
*/
|
|
1795
|
-
get comparator() {
|
|
1796
|
-
return this._comparator;
|
|
1797
|
-
}
|
|
1798
1246
|
*_getIterator() {
|
|
1799
1247
|
for (const element of this.elements) yield element;
|
|
1800
1248
|
}
|
|
@@ -1919,124 +1367,32 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
1919
1367
|
this._autoCompactRatio = value;
|
|
1920
1368
|
}
|
|
1921
1369
|
/**
|
|
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
|
-
|
|
1966
|
-
|
|
1967
|
-
* @example
|
|
1968
|
-
* // Track queue length
|
|
1969
|
-
* const q = new Queue<number>();
|
|
1970
|
-
* console.log(q.length); // 0;
|
|
1971
|
-
* q.push(1);
|
|
1972
|
-
* q.push(2);
|
|
1973
|
-
* console.log(q.length); // 2;
|
|
1974
|
-
*/
|
|
1370
|
+
* Get the number of elements currently in the queue.
|
|
1371
|
+
* @remarks Time O(1), Space O(1)
|
|
1372
|
+
* @returns Current length.
|
|
1373
|
+
* @example
|
|
1374
|
+
* // Track queue length
|
|
1375
|
+
* const q = new Queue<number>();
|
|
1376
|
+
* console.log(q.length); // 0;
|
|
1377
|
+
* q.push(1);
|
|
1378
|
+
* q.push(2);
|
|
1379
|
+
* console.log(q.length); // 2;
|
|
1380
|
+
*/
|
|
1975
1381
|
get length() {
|
|
1976
1382
|
return this.elements.length - this._offset;
|
|
1977
1383
|
}
|
|
1978
1384
|
/**
|
|
1979
|
-
|
|
1980
|
-
* @remarks Time O(1), Space O(1)
|
|
1981
|
-
* @returns Front element or undefined.
|
|
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
|
-
|
|
2023
|
-
|
|
2024
|
-
* @example
|
|
2025
|
-
* // View the front element
|
|
2026
|
-
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
2027
|
-
* console.log(q.first); // 'first';
|
|
2028
|
-
* console.log(q.length); // 3;
|
|
2029
|
-
*/
|
|
2030
|
-
get first() {
|
|
2031
|
-
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
2032
|
-
}
|
|
2033
|
-
/**
|
|
2034
|
-
* Peek at the front element without removing it (alias for `first`).
|
|
1385
|
+
* Get the first element (front) without removing it.
|
|
2035
1386
|
* @remarks Time O(1), Space O(1)
|
|
2036
1387
|
* @returns Front element or undefined.
|
|
1388
|
+
* @example
|
|
1389
|
+
* // View the front element
|
|
1390
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
1391
|
+
* console.log(q.first); // 'first';
|
|
1392
|
+
* console.log(q.length); // 3;
|
|
2037
1393
|
*/
|
|
2038
|
-
|
|
2039
|
-
return this.
|
|
1394
|
+
get first() {
|
|
1395
|
+
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
2040
1396
|
}
|
|
2041
1397
|
/**
|
|
2042
1398
|
* Get the last element (back) without removing it.
|
|
@@ -2057,131 +1413,55 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2057
1413
|
return new _Queue(elements);
|
|
2058
1414
|
}
|
|
2059
1415
|
/**
|
|
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
|
-
|
|
2104
|
-
|
|
2105
|
-
* @example
|
|
2106
|
-
* // Queue for...of iteration and isEmpty check
|
|
2107
|
-
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
2108
|
-
*
|
|
2109
|
-
* const elements: string[] = [];
|
|
2110
|
-
* for (const item of queue) {
|
|
2111
|
-
* elements.push(item);
|
|
2112
|
-
* }
|
|
2113
|
-
*
|
|
2114
|
-
* // Verify all elements are iterated in order
|
|
2115
|
-
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
2116
|
-
*
|
|
2117
|
-
* // Process all elements
|
|
2118
|
-
* while (queue.length > 0) {
|
|
2119
|
-
* queue.shift();
|
|
2120
|
-
* }
|
|
2121
|
-
*
|
|
2122
|
-
* console.log(queue.length); // 0;
|
|
2123
|
-
*/
|
|
1416
|
+
* Peek at the front element without removing it (alias for `first`).
|
|
1417
|
+
* @remarks Time O(1), Space O(1)
|
|
1418
|
+
* @returns Front element or undefined.
|
|
1419
|
+
*/
|
|
1420
|
+
peek() {
|
|
1421
|
+
return this.first;
|
|
1422
|
+
}
|
|
1423
|
+
/**
|
|
1424
|
+
* Check whether the queue is empty.
|
|
1425
|
+
* @remarks Time O(1), Space O(1)
|
|
1426
|
+
* @returns True if length is 0.
|
|
1427
|
+
* @example
|
|
1428
|
+
* // Queue for...of iteration and isEmpty check
|
|
1429
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
1430
|
+
*
|
|
1431
|
+
* const elements: string[] = [];
|
|
1432
|
+
* for (const item of queue) {
|
|
1433
|
+
* elements.push(item);
|
|
1434
|
+
* }
|
|
1435
|
+
*
|
|
1436
|
+
* // Verify all elements are iterated in order
|
|
1437
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
1438
|
+
*
|
|
1439
|
+
* // Process all elements
|
|
1440
|
+
* while (queue.length > 0) {
|
|
1441
|
+
* queue.shift();
|
|
1442
|
+
* }
|
|
1443
|
+
*
|
|
1444
|
+
* console.log(queue.length); // 0;
|
|
1445
|
+
*/
|
|
2124
1446
|
isEmpty() {
|
|
2125
1447
|
return this.length === 0;
|
|
2126
1448
|
}
|
|
2127
1449
|
/**
|
|
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
|
-
|
|
2173
|
-
|
|
2174
|
-
* @example
|
|
2175
|
-
* // basic Queue creation and push operation
|
|
2176
|
-
* // Create a simple Queue with initial values
|
|
2177
|
-
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
2178
|
-
*
|
|
2179
|
-
* // Verify the queue maintains insertion order
|
|
2180
|
-
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
2181
|
-
*
|
|
2182
|
-
* // Check length
|
|
2183
|
-
* console.log(queue.length); // 5;
|
|
2184
|
-
*/
|
|
1450
|
+
* Enqueue one element at the back.
|
|
1451
|
+
* @remarks Time O(1), Space O(1)
|
|
1452
|
+
* @param element - Element to enqueue.
|
|
1453
|
+
* @returns True on success.
|
|
1454
|
+
* @example
|
|
1455
|
+
* // basic Queue creation and push operation
|
|
1456
|
+
* // Create a simple Queue with initial values
|
|
1457
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
1458
|
+
*
|
|
1459
|
+
* // Verify the queue maintains insertion order
|
|
1460
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
1461
|
+
*
|
|
1462
|
+
* // Check length
|
|
1463
|
+
* console.log(queue.length); // 5;
|
|
1464
|
+
*/
|
|
2185
1465
|
push(element) {
|
|
2186
1466
|
this.elements.push(element);
|
|
2187
1467
|
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
@@ -2202,66 +1482,24 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2202
1482
|
return ans;
|
|
2203
1483
|
}
|
|
2204
1484
|
/**
|
|
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
|
-
|
|
2249
|
-
|
|
2250
|
-
* @example
|
|
2251
|
-
* // Queue shift and peek operations
|
|
2252
|
-
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
2253
|
-
*
|
|
2254
|
-
* // Peek at the front element without removing it
|
|
2255
|
-
* console.log(queue.first); // 10;
|
|
2256
|
-
*
|
|
2257
|
-
* // Remove and get the first element (FIFO)
|
|
2258
|
-
* const first = queue.shift();
|
|
2259
|
-
* console.log(first); // 10;
|
|
2260
|
-
*
|
|
2261
|
-
* // Verify remaining elements and length decreased
|
|
2262
|
-
* console.log([...queue]); // [20, 30, 40];
|
|
2263
|
-
* console.log(queue.length); // 3;
|
|
2264
|
-
*/
|
|
1485
|
+
* Dequeue one element from the front (amortized via offset).
|
|
1486
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
1487
|
+
* @returns Removed element or undefined.
|
|
1488
|
+
* @example
|
|
1489
|
+
* // Queue shift and peek operations
|
|
1490
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
1491
|
+
*
|
|
1492
|
+
* // Peek at the front element without removing it
|
|
1493
|
+
* console.log(queue.first); // 10;
|
|
1494
|
+
*
|
|
1495
|
+
* // Remove and get the first element (FIFO)
|
|
1496
|
+
* const first = queue.shift();
|
|
1497
|
+
* console.log(first); // 10;
|
|
1498
|
+
*
|
|
1499
|
+
* // Verify remaining elements and length decreased
|
|
1500
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
1501
|
+
* console.log(queue.length); // 3;
|
|
1502
|
+
*/
|
|
2265
1503
|
shift() {
|
|
2266
1504
|
if (this.length === 0) return void 0;
|
|
2267
1505
|
const first = this.first;
|
|
@@ -2270,55 +1508,16 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2270
1508
|
return first;
|
|
2271
1509
|
}
|
|
2272
1510
|
/**
|
|
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
|
-
|
|
2315
|
-
|
|
2316
|
-
* @example
|
|
2317
|
-
* // Remove specific element
|
|
2318
|
-
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
2319
|
-
* q.delete(2);
|
|
2320
|
-
* console.log(q.length); // 3;
|
|
2321
|
-
*/
|
|
1511
|
+
* Delete the first occurrence of a specific element.
|
|
1512
|
+
* @remarks Time O(N), Space O(1)
|
|
1513
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
1514
|
+
* @returns True if an element was removed.
|
|
1515
|
+
* @example
|
|
1516
|
+
* // Remove specific element
|
|
1517
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
1518
|
+
* q.delete(2);
|
|
1519
|
+
* console.log(q.length); // 3;
|
|
1520
|
+
*/
|
|
2322
1521
|
delete(element) {
|
|
2323
1522
|
for (let i = this._offset; i < this.elements.length; i++) {
|
|
2324
1523
|
if (Object.is(this.elements[i], element)) {
|
|
@@ -2329,55 +1528,16 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2329
1528
|
return false;
|
|
2330
1529
|
}
|
|
2331
1530
|
/**
|
|
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
|
-
|
|
2374
|
-
|
|
2375
|
-
* @example
|
|
2376
|
-
* // Access element by index
|
|
2377
|
-
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
2378
|
-
* console.log(q.at(0)); // 'a';
|
|
2379
|
-
* console.log(q.at(2)); // 'c';
|
|
2380
|
-
*/
|
|
1531
|
+
* Get the element at a given logical index.
|
|
1532
|
+
* @remarks Time O(1), Space O(1)
|
|
1533
|
+
* @param index - Zero-based index from the front.
|
|
1534
|
+
* @returns Element or undefined.
|
|
1535
|
+
* @example
|
|
1536
|
+
* // Access element by index
|
|
1537
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
1538
|
+
* console.log(q.at(0)); // 'a';
|
|
1539
|
+
* console.log(q.at(2)); // 'c';
|
|
1540
|
+
*/
|
|
2381
1541
|
at(index) {
|
|
2382
1542
|
if (index < 0 || index >= this.length) return void 0;
|
|
2383
1543
|
return this._elements[this._offset + index];
|
|
@@ -2444,110 +1604,31 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2444
1604
|
return this;
|
|
2445
1605
|
}
|
|
2446
1606
|
/**
|
|
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
|
-
|
|
2489
|
-
|
|
2490
|
-
* @example
|
|
2491
|
-
* // Remove all elements
|
|
2492
|
-
* const q = new Queue<number>([1, 2, 3]);
|
|
2493
|
-
* q.clear();
|
|
2494
|
-
* console.log(q.length); // 0;
|
|
2495
|
-
*/
|
|
1607
|
+
* Remove all elements and reset offset.
|
|
1608
|
+
* @remarks Time O(1), Space O(1)
|
|
1609
|
+
* @returns void
|
|
1610
|
+
* @example
|
|
1611
|
+
* // Remove all elements
|
|
1612
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1613
|
+
* q.clear();
|
|
1614
|
+
* console.log(q.length); // 0;
|
|
1615
|
+
*/
|
|
2496
1616
|
clear() {
|
|
2497
1617
|
this._elements = [];
|
|
2498
1618
|
this._offset = 0;
|
|
2499
1619
|
}
|
|
2500
1620
|
/**
|
|
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
|
-
|
|
2542
|
-
|
|
2543
|
-
* @example
|
|
2544
|
-
* // Reclaim unused memory
|
|
2545
|
-
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2546
|
-
* q.shift();
|
|
2547
|
-
* q.shift();
|
|
2548
|
-
* q.compact();
|
|
2549
|
-
* console.log(q.length); // 3;
|
|
2550
|
-
*/
|
|
1621
|
+
* Compact storage by discarding consumed head elements.
|
|
1622
|
+
* @remarks Time O(N), Space O(N)
|
|
1623
|
+
* @returns True when compaction performed.
|
|
1624
|
+
* @example
|
|
1625
|
+
* // Reclaim unused memory
|
|
1626
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1627
|
+
* q.shift();
|
|
1628
|
+
* q.shift();
|
|
1629
|
+
* q.compact();
|
|
1630
|
+
* console.log(q.length); // 3;
|
|
1631
|
+
*/
|
|
2551
1632
|
compact() {
|
|
2552
1633
|
this._elements = this.elements.slice(this._offset);
|
|
2553
1634
|
this._offset = 0;
|
|
@@ -2573,57 +1654,17 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2573
1654
|
return removed;
|
|
2574
1655
|
}
|
|
2575
1656
|
/**
|
|
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
|
-
|
|
2618
|
-
|
|
2619
|
-
* @example
|
|
2620
|
-
* // Create independent copy
|
|
2621
|
-
* const q = new Queue<number>([1, 2, 3]);
|
|
2622
|
-
* const copy = q.clone();
|
|
2623
|
-
* copy.shift();
|
|
2624
|
-
* console.log(q.length); // 3;
|
|
2625
|
-
* console.log(copy.length); // 2;
|
|
2626
|
-
*/
|
|
1657
|
+
* Deep clone this queue and its parameters.
|
|
1658
|
+
* @remarks Time O(N), Space O(N)
|
|
1659
|
+
* @returns A new queue with the same content and options.
|
|
1660
|
+
* @example
|
|
1661
|
+
* // Create independent copy
|
|
1662
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1663
|
+
* const copy = q.clone();
|
|
1664
|
+
* copy.shift();
|
|
1665
|
+
* console.log(q.length); // 3;
|
|
1666
|
+
* console.log(copy.length); // 2;
|
|
1667
|
+
*/
|
|
2627
1668
|
clone() {
|
|
2628
1669
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2629
1670
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -2631,57 +1672,17 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2631
1672
|
return out;
|
|
2632
1673
|
}
|
|
2633
1674
|
/**
|
|
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
|
-
|
|
2678
|
-
|
|
2679
|
-
* @example
|
|
2680
|
-
* // Filter elements
|
|
2681
|
-
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2682
|
-
* const evens = q.filter(x => x % 2 === 0);
|
|
2683
|
-
* console.log(evens.length); // 2;
|
|
2684
|
-
*/
|
|
1675
|
+
* Filter elements into a new queue of the same class.
|
|
1676
|
+
* @remarks Time O(N), Space O(N)
|
|
1677
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
1678
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
1679
|
+
* @returns A new queue with kept elements.
|
|
1680
|
+
* @example
|
|
1681
|
+
* // Filter elements
|
|
1682
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1683
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
1684
|
+
* console.log(evens.length); // 2;
|
|
1685
|
+
*/
|
|
2685
1686
|
filter(predicate, thisArg) {
|
|
2686
1687
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2687
1688
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -2693,59 +1694,20 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
2693
1694
|
return out;
|
|
2694
1695
|
}
|
|
2695
1696
|
/**
|
|
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
|
-
|
|
2742
|
-
|
|
2743
|
-
* @example
|
|
2744
|
-
* // Transform elements
|
|
2745
|
-
* const q = new Queue<number>([1, 2, 3]);
|
|
2746
|
-
* const doubled = q.map(x => x * 2);
|
|
2747
|
-
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
2748
|
-
*/
|
|
1697
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
1698
|
+
* @remarks Time O(N), Space O(N)
|
|
1699
|
+
* @template EM
|
|
1700
|
+
* @template RM
|
|
1701
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
1702
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
1703
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1704
|
+
* @returns A new Queue with mapped elements.
|
|
1705
|
+
* @example
|
|
1706
|
+
* // Transform elements
|
|
1707
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1708
|
+
* const doubled = q.map(x => x * 2);
|
|
1709
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
1710
|
+
*/
|
|
2749
1711
|
map(callback, options, thisArg) {
|
|
2750
1712
|
var _a, _b;
|
|
2751
1713
|
const out = new this.constructor([], {
|
|
@@ -2887,6 +1849,13 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
2887
1849
|
get size() {
|
|
2888
1850
|
return this._vertexMap.size;
|
|
2889
1851
|
}
|
|
1852
|
+
/**
|
|
1853
|
+
* The edge connector string used in visual output.
|
|
1854
|
+
* Override in subclasses (e.g., '--' for undirected, '->' for directed).
|
|
1855
|
+
*/
|
|
1856
|
+
get _edgeConnector() {
|
|
1857
|
+
return "--";
|
|
1858
|
+
}
|
|
2890
1859
|
/**
|
|
2891
1860
|
* Get vertex instance by key.
|
|
2892
1861
|
* @param vertexKey - Vertex key.
|
|
@@ -2974,7 +1943,10 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
2974
1943
|
const newEdge = this.createEdge(srcOrEdge, dest, weight, value);
|
|
2975
1944
|
return this._addEdge(newEdge);
|
|
2976
1945
|
} else {
|
|
2977
|
-
raise(
|
|
1946
|
+
raise(
|
|
1947
|
+
TypeError,
|
|
1948
|
+
ERR.invalidArgument("dest must be a Vertex or vertex key when srcOrEdge is an Edge.", "Graph")
|
|
1949
|
+
);
|
|
2978
1950
|
}
|
|
2979
1951
|
}
|
|
2980
1952
|
}
|
|
@@ -3534,6 +2506,7 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
3534
2506
|
}
|
|
3535
2507
|
return mapped;
|
|
3536
2508
|
}
|
|
2509
|
+
// ===== Same-species factory & cloning helpers =====
|
|
3537
2510
|
/**
|
|
3538
2511
|
* Create a deep clone of the graph with the same species.
|
|
3539
2512
|
* @remarks Time O(V + E), Space O(V + E)
|
|
@@ -3546,7 +2519,87 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
3546
2519
|
clone() {
|
|
3547
2520
|
return this._createLike(void 0, this._snapshotOptions());
|
|
3548
2521
|
}
|
|
3549
|
-
|
|
2522
|
+
/**
|
|
2523
|
+
* Generate a text-based visual representation of the graph.
|
|
2524
|
+
*
|
|
2525
|
+
* **Adjacency list format:**
|
|
2526
|
+
* ```
|
|
2527
|
+
* Graph (5 vertices, 6 edges):
|
|
2528
|
+
* A -> B (1), C (2)
|
|
2529
|
+
* B -> D (3)
|
|
2530
|
+
* C -> (no outgoing edges)
|
|
2531
|
+
* D -> A (1)
|
|
2532
|
+
* E (isolated)
|
|
2533
|
+
* ```
|
|
2534
|
+
*
|
|
2535
|
+
* @param options - Optional display settings.
|
|
2536
|
+
* @param options.showWeight - Whether to show edge weights (default: true).
|
|
2537
|
+
* @returns The visual string.
|
|
2538
|
+
*/
|
|
2539
|
+
toVisual(options) {
|
|
2540
|
+
var _a;
|
|
2541
|
+
const showWeight = (_a = options == null ? void 0 : options.showWeight) != null ? _a : true;
|
|
2542
|
+
const vertices = [...this._vertexMap.values()];
|
|
2543
|
+
const vertexCount = vertices.length;
|
|
2544
|
+
const edgeCount = this.edgeSet().length;
|
|
2545
|
+
const lines = [`Graph (${vertexCount} vertices, ${edgeCount} edges):`];
|
|
2546
|
+
for (const vertex of vertices) {
|
|
2547
|
+
const neighbors = this.getNeighbors(vertex);
|
|
2548
|
+
if (neighbors.length === 0) {
|
|
2549
|
+
lines.push(` ${vertex.key} (isolated)`);
|
|
2550
|
+
} else {
|
|
2551
|
+
const edgeStrs = neighbors.map((neighbor) => {
|
|
2552
|
+
const edge = this.getEdge(vertex, neighbor);
|
|
2553
|
+
if (edge && showWeight && edge.weight !== void 0 && edge.weight !== 1) {
|
|
2554
|
+
return `${neighbor.key} (${edge.weight})`;
|
|
2555
|
+
}
|
|
2556
|
+
return `${neighbor.key}`;
|
|
2557
|
+
});
|
|
2558
|
+
lines.push(` ${vertex.key} ${this._edgeConnector} ${edgeStrs.join(", ")}`);
|
|
2559
|
+
}
|
|
2560
|
+
}
|
|
2561
|
+
return lines.join("\n");
|
|
2562
|
+
}
|
|
2563
|
+
/**
|
|
2564
|
+
* Generate DOT language representation for Graphviz.
|
|
2565
|
+
*
|
|
2566
|
+
* @param options - Optional display settings.
|
|
2567
|
+
* @param options.name - Graph name (default: 'G').
|
|
2568
|
+
* @param options.showWeight - Whether to label edges with weight (default: true).
|
|
2569
|
+
* @returns DOT format string.
|
|
2570
|
+
*/
|
|
2571
|
+
toDot(options) {
|
|
2572
|
+
var _a, _b;
|
|
2573
|
+
const name = (_a = options == null ? void 0 : options.name) != null ? _a : "G";
|
|
2574
|
+
const showWeight = (_b = options == null ? void 0 : options.showWeight) != null ? _b : true;
|
|
2575
|
+
const isDirected = this._edgeConnector === "->";
|
|
2576
|
+
const graphType = isDirected ? "digraph" : "graph";
|
|
2577
|
+
const edgeOp = isDirected ? "->" : "--";
|
|
2578
|
+
const lines = [`${graphType} ${name} {`];
|
|
2579
|
+
for (const vertex of this._vertexMap.values()) {
|
|
2580
|
+
lines.push(` "${vertex.key}";`);
|
|
2581
|
+
}
|
|
2582
|
+
const visited = /* @__PURE__ */ new Set();
|
|
2583
|
+
for (const vertex of this._vertexMap.values()) {
|
|
2584
|
+
for (const neighbor of this.getNeighbors(vertex)) {
|
|
2585
|
+
const edgeId = isDirected ? `${vertex.key}->${neighbor.key}` : [vertex.key, neighbor.key].sort().join("--");
|
|
2586
|
+
if (visited.has(edgeId)) continue;
|
|
2587
|
+
visited.add(edgeId);
|
|
2588
|
+
const edge = this.getEdge(vertex, neighbor);
|
|
2589
|
+
const label = edge && showWeight && edge.weight !== void 0 && edge.weight !== 1 ? ` [label="${edge.weight}"]` : "";
|
|
2590
|
+
lines.push(` "${vertex.key}" ${edgeOp} "${neighbor.key}"${label};`);
|
|
2591
|
+
}
|
|
2592
|
+
}
|
|
2593
|
+
lines.push("}");
|
|
2594
|
+
return lines.join("\n");
|
|
2595
|
+
}
|
|
2596
|
+
/**
|
|
2597
|
+
* Print the graph to console.
|
|
2598
|
+
* @param options - Display settings passed to `toVisual`.
|
|
2599
|
+
*/
|
|
2600
|
+
print(options) {
|
|
2601
|
+
console.log(this.toVisual(options));
|
|
2602
|
+
}
|
|
3550
2603
|
/**
|
|
3551
2604
|
* Internal iterator over `[key, value]` entries in insertion order.
|
|
3552
2605
|
* @returns Iterator of `[VertexKey, V | undefined]`.
|
|
@@ -3658,94 +2711,6 @@ var _AbstractGraph = class _AbstractGraph extends IterableEntryBase {
|
|
|
3658
2711
|
_getVertexKey(vertexOrKey) {
|
|
3659
2712
|
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|
|
3660
2713
|
}
|
|
3661
|
-
/**
|
|
3662
|
-
* The edge connector string used in visual output.
|
|
3663
|
-
* Override in subclasses (e.g., '--' for undirected, '->' for directed).
|
|
3664
|
-
*/
|
|
3665
|
-
get _edgeConnector() {
|
|
3666
|
-
return "--";
|
|
3667
|
-
}
|
|
3668
|
-
/**
|
|
3669
|
-
* Generate a text-based visual representation of the graph.
|
|
3670
|
-
*
|
|
3671
|
-
* **Adjacency list format:**
|
|
3672
|
-
* ```
|
|
3673
|
-
* Graph (5 vertices, 6 edges):
|
|
3674
|
-
* A -> B (1), C (2)
|
|
3675
|
-
* B -> D (3)
|
|
3676
|
-
* C -> (no outgoing edges)
|
|
3677
|
-
* D -> A (1)
|
|
3678
|
-
* E (isolated)
|
|
3679
|
-
* ```
|
|
3680
|
-
*
|
|
3681
|
-
* @param options - Optional display settings.
|
|
3682
|
-
* @param options.showWeight - Whether to show edge weights (default: true).
|
|
3683
|
-
* @returns The visual string.
|
|
3684
|
-
*/
|
|
3685
|
-
toVisual(options) {
|
|
3686
|
-
var _a;
|
|
3687
|
-
const showWeight = (_a = options == null ? void 0 : options.showWeight) != null ? _a : true;
|
|
3688
|
-
const vertices = [...this._vertexMap.values()];
|
|
3689
|
-
const vertexCount = vertices.length;
|
|
3690
|
-
const edgeCount = this.edgeSet().length;
|
|
3691
|
-
const lines = [`Graph (${vertexCount} vertices, ${edgeCount} edges):`];
|
|
3692
|
-
for (const vertex of vertices) {
|
|
3693
|
-
const neighbors = this.getNeighbors(vertex);
|
|
3694
|
-
if (neighbors.length === 0) {
|
|
3695
|
-
lines.push(` ${vertex.key} (isolated)`);
|
|
3696
|
-
} else {
|
|
3697
|
-
const edgeStrs = neighbors.map((neighbor) => {
|
|
3698
|
-
const edge = this.getEdge(vertex, neighbor);
|
|
3699
|
-
if (edge && showWeight && edge.weight !== void 0 && edge.weight !== 1) {
|
|
3700
|
-
return `${neighbor.key} (${edge.weight})`;
|
|
3701
|
-
}
|
|
3702
|
-
return `${neighbor.key}`;
|
|
3703
|
-
});
|
|
3704
|
-
lines.push(` ${vertex.key} ${this._edgeConnector} ${edgeStrs.join(", ")}`);
|
|
3705
|
-
}
|
|
3706
|
-
}
|
|
3707
|
-
return lines.join("\n");
|
|
3708
|
-
}
|
|
3709
|
-
/**
|
|
3710
|
-
* Generate DOT language representation for Graphviz.
|
|
3711
|
-
*
|
|
3712
|
-
* @param options - Optional display settings.
|
|
3713
|
-
* @param options.name - Graph name (default: 'G').
|
|
3714
|
-
* @param options.showWeight - Whether to label edges with weight (default: true).
|
|
3715
|
-
* @returns DOT format string.
|
|
3716
|
-
*/
|
|
3717
|
-
toDot(options) {
|
|
3718
|
-
var _a, _b;
|
|
3719
|
-
const name = (_a = options == null ? void 0 : options.name) != null ? _a : "G";
|
|
3720
|
-
const showWeight = (_b = options == null ? void 0 : options.showWeight) != null ? _b : true;
|
|
3721
|
-
const isDirected = this._edgeConnector === "->";
|
|
3722
|
-
const graphType = isDirected ? "digraph" : "graph";
|
|
3723
|
-
const edgeOp = isDirected ? "->" : "--";
|
|
3724
|
-
const lines = [`${graphType} ${name} {`];
|
|
3725
|
-
for (const vertex of this._vertexMap.values()) {
|
|
3726
|
-
lines.push(` "${vertex.key}";`);
|
|
3727
|
-
}
|
|
3728
|
-
const visited = /* @__PURE__ */ new Set();
|
|
3729
|
-
for (const vertex of this._vertexMap.values()) {
|
|
3730
|
-
for (const neighbor of this.getNeighbors(vertex)) {
|
|
3731
|
-
const edgeId = isDirected ? `${vertex.key}->${neighbor.key}` : [vertex.key, neighbor.key].sort().join("--");
|
|
3732
|
-
if (visited.has(edgeId)) continue;
|
|
3733
|
-
visited.add(edgeId);
|
|
3734
|
-
const edge = this.getEdge(vertex, neighbor);
|
|
3735
|
-
const label = edge && showWeight && edge.weight !== void 0 && edge.weight !== 1 ? ` [label="${edge.weight}"]` : "";
|
|
3736
|
-
lines.push(` "${vertex.key}" ${edgeOp} "${neighbor.key}"${label};`);
|
|
3737
|
-
}
|
|
3738
|
-
}
|
|
3739
|
-
lines.push("}");
|
|
3740
|
-
return lines.join("\n");
|
|
3741
|
-
}
|
|
3742
|
-
/**
|
|
3743
|
-
* Print the graph to console.
|
|
3744
|
-
* @param options - Display settings passed to `toVisual`.
|
|
3745
|
-
*/
|
|
3746
|
-
print(options) {
|
|
3747
|
-
console.log(this.toVisual(options));
|
|
3748
|
-
}
|
|
3749
2714
|
};
|
|
3750
2715
|
__name(_AbstractGraph, "AbstractGraph");
|
|
3751
2716
|
var AbstractGraph = _AbstractGraph;
|
|
@@ -3780,9 +2745,6 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
3780
2745
|
__publicField(this, "_outEdgeMap", /* @__PURE__ */ new Map());
|
|
3781
2746
|
__publicField(this, "_inEdgeMap", /* @__PURE__ */ new Map());
|
|
3782
2747
|
}
|
|
3783
|
-
get _edgeConnector() {
|
|
3784
|
-
return "->";
|
|
3785
|
-
}
|
|
3786
2748
|
get outEdgeMap() {
|
|
3787
2749
|
return this._outEdgeMap;
|
|
3788
2750
|
}
|
|
@@ -3795,6 +2757,9 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
3795
2757
|
set inEdgeMap(v) {
|
|
3796
2758
|
this._inEdgeMap = v;
|
|
3797
2759
|
}
|
|
2760
|
+
get _edgeConnector() {
|
|
2761
|
+
return "->";
|
|
2762
|
+
}
|
|
3798
2763
|
/**
|
|
3799
2764
|
* Construct a directed graph from keys with value initializer `v => v`.
|
|
3800
2765
|
* @template K - Vertex key type.
|
|
@@ -3845,59 +2810,20 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
3845
2810
|
return new DirectedEdge(src, dest, (_a = weight != null ? weight : this.options.defaultEdgeWeight) != null ? _a : 1, value);
|
|
3846
2811
|
}
|
|
3847
2812
|
/**
|
|
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
|
-
|
|
3891
|
-
|
|
3892
|
-
* @example
|
|
3893
|
-
* // Get edge between vertices
|
|
3894
|
-
* const g = new DirectedGraph();
|
|
3895
|
-
* g.addVertex('A');
|
|
3896
|
-
* g.addVertex('B');
|
|
3897
|
-
* g.addEdge('A', 'B', 5);
|
|
3898
|
-
* const edge = g.getEdge('A', 'B');
|
|
3899
|
-
* console.log(edge?.weight); // 5;
|
|
3900
|
-
*/
|
|
2813
|
+
* Get the unique edge from `src` to `dest`, if present.
|
|
2814
|
+
* @param srcOrKey - Source vertex or key.
|
|
2815
|
+
* @param destOrKey - Destination vertex or key.
|
|
2816
|
+
* @returns Edge instance or `undefined`.
|
|
2817
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
2818
|
+
* @example
|
|
2819
|
+
* // Get edge between vertices
|
|
2820
|
+
* const g = new DirectedGraph();
|
|
2821
|
+
* g.addVertex('A');
|
|
2822
|
+
* g.addVertex('B');
|
|
2823
|
+
* g.addEdge('A', 'B', 5);
|
|
2824
|
+
* const edge = g.getEdge('A', 'B');
|
|
2825
|
+
* console.log(edge?.weight); // 5;
|
|
2826
|
+
*/
|
|
3901
2827
|
getEdge(srcOrKey, destOrKey) {
|
|
3902
2828
|
let edgeMap = [];
|
|
3903
2829
|
if (srcOrKey !== void 0 && destOrKey !== void 0) {
|
|
@@ -3937,79 +2863,37 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
3937
2863
|
return removed;
|
|
3938
2864
|
}
|
|
3939
2865
|
/**
|
|
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
|
-
|
|
3986
|
-
|
|
3987
|
-
* @example
|
|
3988
|
-
* // DirectedGraph deleteEdge and vertex operations
|
|
3989
|
-
* const graph = new DirectedGraph<string>();
|
|
3990
|
-
*
|
|
3991
|
-
* // Build a small graph
|
|
3992
|
-
* graph.addVertex('X');
|
|
3993
|
-
* graph.addVertex('Y');
|
|
3994
|
-
* graph.addVertex('Z');
|
|
3995
|
-
* graph.addEdge('X', 'Y', 1);
|
|
3996
|
-
* graph.addEdge('Y', 'Z', 2);
|
|
3997
|
-
*
|
|
3998
|
-
* // Delete an edge
|
|
3999
|
-
* graph.deleteEdgeSrcToDest('X', 'Y');
|
|
4000
|
-
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
4001
|
-
*
|
|
4002
|
-
* // Edge in other direction should not exist
|
|
4003
|
-
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
4004
|
-
*
|
|
4005
|
-
* // Other edges should remain
|
|
4006
|
-
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
4007
|
-
*
|
|
4008
|
-
* // Delete a vertex
|
|
4009
|
-
* graph.deleteVertex('Y');
|
|
4010
|
-
* console.log(graph.hasVertex('Y')); // false;
|
|
4011
|
-
* console.log(graph.size); // 2;
|
|
4012
|
-
*/
|
|
2866
|
+
* Delete an edge by instance or by `(srcKey, destKey)`.
|
|
2867
|
+
* @param edgeOrSrcVertexKey - Edge instance or source vertex/key.
|
|
2868
|
+
* @param destVertexKey - Optional destination vertex/key when deleting by pair.
|
|
2869
|
+
* @returns Removed edge or `undefined`.
|
|
2870
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
2871
|
+
* @example
|
|
2872
|
+
* // DirectedGraph deleteEdge and vertex operations
|
|
2873
|
+
* const graph = new DirectedGraph<string>();
|
|
2874
|
+
*
|
|
2875
|
+
* // Build a small graph
|
|
2876
|
+
* graph.addVertex('X');
|
|
2877
|
+
* graph.addVertex('Y');
|
|
2878
|
+
* graph.addVertex('Z');
|
|
2879
|
+
* graph.addEdge('X', 'Y', 1);
|
|
2880
|
+
* graph.addEdge('Y', 'Z', 2);
|
|
2881
|
+
*
|
|
2882
|
+
* // Delete an edge
|
|
2883
|
+
* graph.deleteEdgeSrcToDest('X', 'Y');
|
|
2884
|
+
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
2885
|
+
*
|
|
2886
|
+
* // Edge in other direction should not exist
|
|
2887
|
+
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
2888
|
+
*
|
|
2889
|
+
* // Other edges should remain
|
|
2890
|
+
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
2891
|
+
*
|
|
2892
|
+
* // Delete a vertex
|
|
2893
|
+
* graph.deleteVertex('Y');
|
|
2894
|
+
* console.log(graph.hasVertex('Y')); // false;
|
|
2895
|
+
* console.log(graph.size); // 2;
|
|
2896
|
+
*/
|
|
4013
2897
|
deleteEdge(edgeOrSrcVertexKey, destVertexKey) {
|
|
4014
2898
|
let removed = void 0;
|
|
4015
2899
|
let src, dest;
|
|
@@ -4037,56 +2921,17 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4037
2921
|
return removed;
|
|
4038
2922
|
}
|
|
4039
2923
|
/**
|
|
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
|
-
|
|
4079
|
-
|
|
4080
|
-
* @example
|
|
4081
|
-
* // Remove a vertex
|
|
4082
|
-
* const g = new DirectedGraph();
|
|
4083
|
-
* g.addVertex('A');
|
|
4084
|
-
* g.addVertex('B');
|
|
4085
|
-
* g.addEdge('A', 'B');
|
|
4086
|
-
* g.deleteVertex('A');
|
|
4087
|
-
* console.log(g.hasVertex('A')); // false;
|
|
4088
|
-
* console.log(g.hasEdge('A', 'B')); // false;
|
|
4089
|
-
*/
|
|
2924
|
+
* Remove a vertex
|
|
2925
|
+
* @example
|
|
2926
|
+
* // Remove a vertex
|
|
2927
|
+
* const g = new DirectedGraph();
|
|
2928
|
+
* g.addVertex('A');
|
|
2929
|
+
* g.addVertex('B');
|
|
2930
|
+
* g.addEdge('A', 'B');
|
|
2931
|
+
* g.deleteVertex('A');
|
|
2932
|
+
* console.log(g.hasVertex('A')); // false;
|
|
2933
|
+
* console.log(g.hasEdge('A', 'B')); // false;
|
|
2934
|
+
*/
|
|
4090
2935
|
deleteVertex(vertexOrKey) {
|
|
4091
2936
|
let vertexKey;
|
|
4092
2937
|
let vertex;
|
|
@@ -4118,59 +2963,20 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4118
2963
|
return removed;
|
|
4119
2964
|
}
|
|
4120
2965
|
/**
|
|
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
|
-
|
|
4163
|
-
|
|
4164
|
-
* @example
|
|
4165
|
-
* // Get incoming edges
|
|
4166
|
-
* const g = new DirectedGraph();
|
|
4167
|
-
* g.addVertex('A');
|
|
4168
|
-
* g.addVertex('B');
|
|
4169
|
-
* g.addVertex('C');
|
|
4170
|
-
* g.addEdge('A', 'C');
|
|
4171
|
-
* g.addEdge('B', 'C');
|
|
4172
|
-
* console.log(g.incomingEdgesOf('C').length); // 2;
|
|
4173
|
-
*/
|
|
2966
|
+
* Incoming edges of a vertex.
|
|
2967
|
+
* @param vertexOrKey - Vertex or key.
|
|
2968
|
+
* @returns Array of incoming edges.
|
|
2969
|
+
* @remarks Time O(deg_in), Space O(deg_in)
|
|
2970
|
+
* @example
|
|
2971
|
+
* // Get incoming edges
|
|
2972
|
+
* const g = new DirectedGraph();
|
|
2973
|
+
* g.addVertex('A');
|
|
2974
|
+
* g.addVertex('B');
|
|
2975
|
+
* g.addVertex('C');
|
|
2976
|
+
* g.addEdge('A', 'C');
|
|
2977
|
+
* g.addEdge('B', 'C');
|
|
2978
|
+
* console.log(g.incomingEdgesOf('C').length); // 2;
|
|
2979
|
+
*/
|
|
4174
2980
|
incomingEdgesOf(vertexOrKey) {
|
|
4175
2981
|
const target = this._getVertex(vertexOrKey);
|
|
4176
2982
|
if (target) {
|
|
@@ -4179,59 +2985,20 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4179
2985
|
return [];
|
|
4180
2986
|
}
|
|
4181
2987
|
/**
|
|
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
|
-
|
|
4224
|
-
|
|
4225
|
-
* @example
|
|
4226
|
-
* // Get outgoing edges
|
|
4227
|
-
* const g = new DirectedGraph();
|
|
4228
|
-
* g.addVertex('A');
|
|
4229
|
-
* g.addVertex('B');
|
|
4230
|
-
* g.addVertex('C');
|
|
4231
|
-
* g.addEdge('A', 'B');
|
|
4232
|
-
* g.addEdge('A', 'C');
|
|
4233
|
-
* console.log(g.outgoingEdgesOf('A').length); // 2;
|
|
4234
|
-
*/
|
|
2988
|
+
* Outgoing edges of a vertex.
|
|
2989
|
+
* @param vertexOrKey - Vertex or key.
|
|
2990
|
+
* @returns Array of outgoing edges.
|
|
2991
|
+
* @remarks Time O(deg_out), Space O(deg_out)
|
|
2992
|
+
* @example
|
|
2993
|
+
* // Get outgoing edges
|
|
2994
|
+
* const g = new DirectedGraph();
|
|
2995
|
+
* g.addVertex('A');
|
|
2996
|
+
* g.addVertex('B');
|
|
2997
|
+
* g.addVertex('C');
|
|
2998
|
+
* g.addEdge('A', 'B');
|
|
2999
|
+
* g.addEdge('A', 'C');
|
|
3000
|
+
* console.log(g.outgoingEdgesOf('A').length); // 2;
|
|
3001
|
+
*/
|
|
4235
3002
|
outgoingEdgesOf(vertexOrKey) {
|
|
4236
3003
|
const target = this._getVertex(vertexOrKey);
|
|
4237
3004
|
if (target) {
|
|
@@ -4290,75 +3057,33 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4290
3057
|
return destinations;
|
|
4291
3058
|
}
|
|
4292
3059
|
/**
|
|
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
|
-
|
|
4338
|
-
|
|
4339
|
-
* @example
|
|
4340
|
-
* // DirectedGraph topologicalSort for task scheduling
|
|
4341
|
-
* const graph = new DirectedGraph<string>();
|
|
4342
|
-
*
|
|
4343
|
-
* // Build a DAG (Directed Acyclic Graph) for task dependencies
|
|
4344
|
-
* graph.addVertex('Design');
|
|
4345
|
-
* graph.addVertex('Implement');
|
|
4346
|
-
* graph.addVertex('Test');
|
|
4347
|
-
* graph.addVertex('Deploy');
|
|
4348
|
-
*
|
|
4349
|
-
* // Add dependency edges
|
|
4350
|
-
* graph.addEdge('Design', 'Implement', 1); // Design must come before Implement
|
|
4351
|
-
* graph.addEdge('Implement', 'Test', 1); // Implement must come before Test
|
|
4352
|
-
* graph.addEdge('Test', 'Deploy', 1); // Test must come before Deploy
|
|
4353
|
-
*
|
|
4354
|
-
* // Topological sort gives valid execution order
|
|
4355
|
-
* const executionOrder = graph.topologicalSort();
|
|
4356
|
-
* console.log(executionOrder); // defined;
|
|
4357
|
-
* console.log(executionOrder); // ['Design', 'Implement', 'Test', 'Deploy'];
|
|
4358
|
-
*
|
|
4359
|
-
* // All vertices should be included
|
|
4360
|
-
* console.log(executionOrder?.length); // 4;
|
|
4361
|
-
*/
|
|
3060
|
+
* Topological sort if DAG; returns `undefined` if a cycle exists.
|
|
3061
|
+
* @param propertyName - `'key'` to map to keys; `'vertex'` to keep instances.
|
|
3062
|
+
* @returns Array of keys/vertices, or `undefined` when cycle is found.
|
|
3063
|
+
* @remarks Time O(V + E), Space O(V)
|
|
3064
|
+
* @example
|
|
3065
|
+
* // DirectedGraph topologicalSort for task scheduling
|
|
3066
|
+
* const graph = new DirectedGraph<string>();
|
|
3067
|
+
*
|
|
3068
|
+
* // Build a DAG (Directed Acyclic Graph) for task dependencies
|
|
3069
|
+
* graph.addVertex('Design');
|
|
3070
|
+
* graph.addVertex('Implement');
|
|
3071
|
+
* graph.addVertex('Test');
|
|
3072
|
+
* graph.addVertex('Deploy');
|
|
3073
|
+
*
|
|
3074
|
+
* // Add dependency edges
|
|
3075
|
+
* graph.addEdge('Design', 'Implement', 1); // Design must come before Implement
|
|
3076
|
+
* graph.addEdge('Implement', 'Test', 1); // Implement must come before Test
|
|
3077
|
+
* graph.addEdge('Test', 'Deploy', 1); // Test must come before Deploy
|
|
3078
|
+
*
|
|
3079
|
+
* // Topological sort gives valid execution order
|
|
3080
|
+
* const executionOrder = graph.topologicalSort();
|
|
3081
|
+
* console.log(executionOrder); // defined;
|
|
3082
|
+
* console.log(executionOrder); // ['Design', 'Implement', 'Test', 'Deploy'];
|
|
3083
|
+
*
|
|
3084
|
+
* // All vertices should be included
|
|
3085
|
+
* console.log(executionOrder?.length); // 4;
|
|
3086
|
+
*/
|
|
4362
3087
|
topologicalSort(propertyName) {
|
|
4363
3088
|
propertyName = propertyName != null ? propertyName : "key";
|
|
4364
3089
|
const statusMap = /* @__PURE__ */ new Map();
|
|
@@ -4391,54 +3116,15 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4391
3116
|
return sorted.reverse();
|
|
4392
3117
|
}
|
|
4393
3118
|
/**
|
|
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
|
-
|
|
4433
|
-
|
|
4434
|
-
* @example
|
|
4435
|
-
* // Get all edges
|
|
4436
|
-
* const g = new DirectedGraph();
|
|
4437
|
-
* g.addVertex('A');
|
|
4438
|
-
* g.addVertex('B');
|
|
4439
|
-
* g.addEdge('A', 'B', 3);
|
|
4440
|
-
* console.log(g.edgeSet().length); // 1;
|
|
4441
|
-
*/
|
|
3119
|
+
* Get all edges
|
|
3120
|
+
* @example
|
|
3121
|
+
* // Get all edges
|
|
3122
|
+
* const g = new DirectedGraph();
|
|
3123
|
+
* g.addVertex('A');
|
|
3124
|
+
* g.addVertex('B');
|
|
3125
|
+
* g.addEdge('A', 'B', 3);
|
|
3126
|
+
* console.log(g.edgeSet().length); // 1;
|
|
3127
|
+
*/
|
|
4442
3128
|
edgeSet() {
|
|
4443
3129
|
let edgeMap = [];
|
|
4444
3130
|
this._outEdgeMap.forEach((outEdges) => {
|
|
@@ -4447,58 +3133,18 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4447
3133
|
return edgeMap;
|
|
4448
3134
|
}
|
|
4449
3135
|
/**
|
|
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
|
-
|
|
4490
|
-
|
|
4491
|
-
* @example
|
|
4492
|
-
* // Get outgoing neighbors
|
|
4493
|
-
* const g = new DirectedGraph();
|
|
4494
|
-
* g.addVertex('A');
|
|
4495
|
-
* g.addVertex('B');
|
|
4496
|
-
* g.addVertex('C');
|
|
4497
|
-
* g.addEdge('A', 'B');
|
|
4498
|
-
* g.addEdge('A', 'C');
|
|
4499
|
-
* const neighbors = g.getNeighbors('A');
|
|
4500
|
-
* console.log(neighbors.map(v => v.key).sort()); // ['B', 'C'];
|
|
4501
|
-
*/
|
|
3136
|
+
* Get outgoing neighbors
|
|
3137
|
+
* @example
|
|
3138
|
+
* // Get outgoing neighbors
|
|
3139
|
+
* const g = new DirectedGraph();
|
|
3140
|
+
* g.addVertex('A');
|
|
3141
|
+
* g.addVertex('B');
|
|
3142
|
+
* g.addVertex('C');
|
|
3143
|
+
* g.addEdge('A', 'B');
|
|
3144
|
+
* g.addEdge('A', 'C');
|
|
3145
|
+
* const neighbors = g.getNeighbors('A');
|
|
3146
|
+
* console.log(neighbors.map(v => v.key).sort()); // ['B', 'C'];
|
|
3147
|
+
*/
|
|
4502
3148
|
getNeighbors(vertexOrKey) {
|
|
4503
3149
|
const neighbors = [];
|
|
4504
3150
|
const vertex = this._getVertex(vertexOrKey);
|
|
@@ -4556,62 +3202,23 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4556
3202
|
return super.clone();
|
|
4557
3203
|
}
|
|
4558
3204
|
/**
|
|
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
|
-
|
|
4600
|
-
|
|
4601
|
-
* @example
|
|
4602
|
-
* // Find strongly connected components
|
|
4603
|
-
* const g = new DirectedGraph();
|
|
4604
|
-
* g.addVertex('A');
|
|
4605
|
-
* g.addVertex('B');
|
|
4606
|
-
* g.addVertex('C');
|
|
4607
|
-
* g.addEdge('A', 'B');
|
|
4608
|
-
* g.addEdge('B', 'C');
|
|
4609
|
-
* g.addEdge('C', 'A');
|
|
4610
|
-
* const { SCCs } = g.tarjan();
|
|
4611
|
-
* // A→B→C→A forms one SCC with 3 members
|
|
4612
|
-
* const sccArrays = [...SCCs.values()];
|
|
4613
|
-
* console.log(sccArrays.some(scc => scc.length === 3)); // true;
|
|
4614
|
-
*/
|
|
3205
|
+
* Tarjan's algorithm for strongly connected components.
|
|
3206
|
+
* @returns `{ dfnMap, lowMap, SCCs }`.
|
|
3207
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
3208
|
+
* @example
|
|
3209
|
+
* // Find strongly connected components
|
|
3210
|
+
* const g = new DirectedGraph();
|
|
3211
|
+
* g.addVertex('A');
|
|
3212
|
+
* g.addVertex('B');
|
|
3213
|
+
* g.addVertex('C');
|
|
3214
|
+
* g.addEdge('A', 'B');
|
|
3215
|
+
* g.addEdge('B', 'C');
|
|
3216
|
+
* g.addEdge('C', 'A');
|
|
3217
|
+
* const { SCCs } = g.tarjan();
|
|
3218
|
+
* // A→B→C→A forms one SCC with 3 members
|
|
3219
|
+
* const sccArrays = [...SCCs.values()];
|
|
3220
|
+
* console.log(sccArrays.some(scc => scc.length === 3)); // true;
|
|
3221
|
+
*/
|
|
4615
3222
|
tarjan() {
|
|
4616
3223
|
const dfnMap = /* @__PURE__ */ new Map();
|
|
4617
3224
|
const lowMap = /* @__PURE__ */ new Map();
|
|
@@ -4669,60 +3276,21 @@ var _DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4669
3276
|
return this.tarjan().lowMap;
|
|
4670
3277
|
}
|
|
4671
3278
|
/**
|
|
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
|
-
|
|
4713
|
-
|
|
4714
|
-
* @example
|
|
4715
|
-
* // Get strongly connected components
|
|
4716
|
-
* const g = new DirectedGraph();
|
|
4717
|
-
* g.addVertex(1);
|
|
4718
|
-
* g.addVertex(2);
|
|
4719
|
-
* g.addVertex(3);
|
|
4720
|
-
* g.addEdge(1, 2);
|
|
4721
|
-
* g.addEdge(2, 3);
|
|
4722
|
-
* g.addEdge(3, 1);
|
|
4723
|
-
* const sccs = g.getSCCs(); // Map<number, VO[]>
|
|
4724
|
-
* console.log(sccs.size); // >= 1;
|
|
4725
|
-
*/
|
|
3279
|
+
* Strongly connected components computed by `tarjan()`.
|
|
3280
|
+
* @returns Map from SCC id to vertices.
|
|
3281
|
+
* @remarks Time O(#SCC + V), Space O(V)
|
|
3282
|
+
* @example
|
|
3283
|
+
* // Get strongly connected components
|
|
3284
|
+
* const g = new DirectedGraph();
|
|
3285
|
+
* g.addVertex(1);
|
|
3286
|
+
* g.addVertex(2);
|
|
3287
|
+
* g.addVertex(3);
|
|
3288
|
+
* g.addEdge(1, 2);
|
|
3289
|
+
* g.addEdge(2, 3);
|
|
3290
|
+
* g.addEdge(3, 1);
|
|
3291
|
+
* const sccs = g.getSCCs(); // Map<number, VO[]>
|
|
3292
|
+
* console.log(sccs.size); // >= 1;
|
|
3293
|
+
*/
|
|
4726
3294
|
getSCCs() {
|
|
4727
3295
|
return this.tarjan().SCCs;
|
|
4728
3296
|
}
|
|
@@ -4844,58 +3412,19 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
4844
3412
|
return new UndirectedEdge(v1, v2, (_a = weight != null ? weight : this.options.defaultEdgeWeight) != null ? _a : 1, value);
|
|
4845
3413
|
}
|
|
4846
3414
|
/**
|
|
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
|
-
|
|
4890
|
-
|
|
4891
|
-
* @example
|
|
4892
|
-
* // Get edge between vertices
|
|
4893
|
-
* const g = new UndirectedGraph();
|
|
4894
|
-
* g.addVertex('A');
|
|
4895
|
-
* g.addVertex('B');
|
|
4896
|
-
* g.addEdge('A', 'B', 7);
|
|
4897
|
-
* console.log(g.getEdge('A', 'B')?.weight); // 7;
|
|
4898
|
-
*/
|
|
3415
|
+
* Get an undirected edge between two vertices, if present.
|
|
3416
|
+
* @param v1 - One vertex or key.
|
|
3417
|
+
* @param v2 - The other vertex or key.
|
|
3418
|
+
* @returns Edge instance or `undefined`.
|
|
3419
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
3420
|
+
* @example
|
|
3421
|
+
* // Get edge between vertices
|
|
3422
|
+
* const g = new UndirectedGraph();
|
|
3423
|
+
* g.addVertex('A');
|
|
3424
|
+
* g.addVertex('B');
|
|
3425
|
+
* g.addEdge('A', 'B', 7);
|
|
3426
|
+
* console.log(g.getEdge('A', 'B')?.weight); // 7;
|
|
3427
|
+
*/
|
|
4899
3428
|
getEdge(v1, v2) {
|
|
4900
3429
|
var _a;
|
|
4901
3430
|
let edgeMap = [];
|
|
@@ -4933,81 +3462,39 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
4933
3462
|
return removed;
|
|
4934
3463
|
}
|
|
4935
3464
|
/**
|
|
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
|
-
|
|
4982
|
-
|
|
4983
|
-
* @example
|
|
4984
|
-
* // UndirectedGraph deleteEdge and vertex operations
|
|
4985
|
-
* const graph = new UndirectedGraph<string>();
|
|
4986
|
-
*
|
|
4987
|
-
* // Build a simple undirected graph
|
|
4988
|
-
* graph.addVertex('X');
|
|
4989
|
-
* graph.addVertex('Y');
|
|
4990
|
-
* graph.addVertex('Z');
|
|
4991
|
-
* graph.addEdge('X', 'Y', 1);
|
|
4992
|
-
* graph.addEdge('Y', 'Z', 2);
|
|
4993
|
-
* graph.addEdge('X', 'Z', 3);
|
|
4994
|
-
*
|
|
4995
|
-
* // Delete an edge
|
|
4996
|
-
* graph.deleteEdge('X', 'Y');
|
|
4997
|
-
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
4998
|
-
*
|
|
4999
|
-
* // Bidirectional deletion confirmed
|
|
5000
|
-
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
5001
|
-
*
|
|
5002
|
-
* // Other edges should remain
|
|
5003
|
-
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
5004
|
-
* console.log(graph.hasEdge('Z', 'Y')); // true;
|
|
5005
|
-
*
|
|
5006
|
-
* // Delete a vertex
|
|
5007
|
-
* graph.deleteVertex('Y');
|
|
5008
|
-
* console.log(graph.hasVertex('Y')); // false;
|
|
5009
|
-
* console.log(graph.size); // 2;
|
|
5010
|
-
*/
|
|
3465
|
+
* Delete an edge by instance or by a pair of keys.
|
|
3466
|
+
* @param edgeOrOneSideVertexKey - Edge instance or one endpoint vertex/key.
|
|
3467
|
+
* @param otherSideVertexKey - Required second endpoint when deleting by pair.
|
|
3468
|
+
* @returns Removed edge or `undefined`.
|
|
3469
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
3470
|
+
* @example
|
|
3471
|
+
* // UndirectedGraph deleteEdge and vertex operations
|
|
3472
|
+
* const graph = new UndirectedGraph<string>();
|
|
3473
|
+
*
|
|
3474
|
+
* // Build a simple undirected graph
|
|
3475
|
+
* graph.addVertex('X');
|
|
3476
|
+
* graph.addVertex('Y');
|
|
3477
|
+
* graph.addVertex('Z');
|
|
3478
|
+
* graph.addEdge('X', 'Y', 1);
|
|
3479
|
+
* graph.addEdge('Y', 'Z', 2);
|
|
3480
|
+
* graph.addEdge('X', 'Z', 3);
|
|
3481
|
+
*
|
|
3482
|
+
* // Delete an edge
|
|
3483
|
+
* graph.deleteEdge('X', 'Y');
|
|
3484
|
+
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
3485
|
+
*
|
|
3486
|
+
* // Bidirectional deletion confirmed
|
|
3487
|
+
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
3488
|
+
*
|
|
3489
|
+
* // Other edges should remain
|
|
3490
|
+
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
3491
|
+
* console.log(graph.hasEdge('Z', 'Y')); // true;
|
|
3492
|
+
*
|
|
3493
|
+
* // Delete a vertex
|
|
3494
|
+
* graph.deleteVertex('Y');
|
|
3495
|
+
* console.log(graph.hasVertex('Y')); // false;
|
|
3496
|
+
* console.log(graph.size); // 2;
|
|
3497
|
+
*/
|
|
5011
3498
|
deleteEdge(edgeOrOneSideVertexKey, otherSideVertexKey) {
|
|
5012
3499
|
let oneSide, otherSide;
|
|
5013
3500
|
if (this.isVertexKey(edgeOrOneSideVertexKey)) {
|
|
@@ -5028,58 +3515,19 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
5028
3515
|
}
|
|
5029
3516
|
}
|
|
5030
3517
|
/**
|
|
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
|
-
|
|
5073
|
-
|
|
5074
|
-
* @example
|
|
5075
|
-
* // Remove vertex and edges
|
|
5076
|
-
* const g = new UndirectedGraph();
|
|
5077
|
-
* g.addVertex('A');
|
|
5078
|
-
* g.addVertex('B');
|
|
5079
|
-
* g.addEdge('A', 'B');
|
|
5080
|
-
* g.deleteVertex('A');
|
|
5081
|
-
* console.log(g.hasVertex('A')); // false;
|
|
5082
|
-
*/
|
|
3518
|
+
* Delete a vertex and remove it from all neighbor lists.
|
|
3519
|
+
* @param vertexOrKey - Vertex or key.
|
|
3520
|
+
* @returns `true` if removed; otherwise `false`.
|
|
3521
|
+
* @remarks Time O(deg), Space O(1)
|
|
3522
|
+
* @example
|
|
3523
|
+
* // Remove vertex and edges
|
|
3524
|
+
* const g = new UndirectedGraph();
|
|
3525
|
+
* g.addVertex('A');
|
|
3526
|
+
* g.addVertex('B');
|
|
3527
|
+
* g.addEdge('A', 'B');
|
|
3528
|
+
* g.deleteVertex('A');
|
|
3529
|
+
* console.log(g.hasVertex('A')); // false;
|
|
3530
|
+
*/
|
|
5083
3531
|
deleteVertex(vertexOrKey) {
|
|
5084
3532
|
let vertexKey;
|
|
5085
3533
|
let vertex;
|
|
@@ -5135,56 +3583,17 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
5135
3583
|
}
|
|
5136
3584
|
}
|
|
5137
3585
|
/**
|
|
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
|
-
|
|
5179
|
-
|
|
5180
|
-
* @example
|
|
5181
|
-
* // Get all edges
|
|
5182
|
-
* const g = new UndirectedGraph();
|
|
5183
|
-
* g.addVertex('A');
|
|
5184
|
-
* g.addVertex('B');
|
|
5185
|
-
* g.addEdge('A', 'B');
|
|
5186
|
-
* console.log(g.edgeSet().length); // 1;
|
|
5187
|
-
*/
|
|
3586
|
+
* Unique set of undirected edges across endpoints.
|
|
3587
|
+
* @returns Array of edges.
|
|
3588
|
+
* @remarks Time O(E), Space O(E)
|
|
3589
|
+
* @example
|
|
3590
|
+
* // Get all edges
|
|
3591
|
+
* const g = new UndirectedGraph();
|
|
3592
|
+
* g.addVertex('A');
|
|
3593
|
+
* g.addVertex('B');
|
|
3594
|
+
* g.addEdge('A', 'B');
|
|
3595
|
+
* console.log(g.edgeSet().length); // 1;
|
|
3596
|
+
*/
|
|
5188
3597
|
edgeSet() {
|
|
5189
3598
|
const edgeSet = /* @__PURE__ */ new Set();
|
|
5190
3599
|
this._edgeMap.forEach((edgeMap) => {
|
|
@@ -5195,82 +3604,40 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
5195
3604
|
return [...edgeSet];
|
|
5196
3605
|
}
|
|
5197
3606
|
/**
|
|
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
|
-
|
|
5240
|
-
|
|
5241
|
-
* @example
|
|
5242
|
-
* // UndirectedGraph connectivity and neighbors
|
|
5243
|
-
* const graph = new UndirectedGraph<string>();
|
|
5244
|
-
*
|
|
5245
|
-
* // Build a friendship network
|
|
5246
|
-
* const people = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'];
|
|
5247
|
-
* for (const person of people) {
|
|
5248
|
-
* graph.addVertex(person);
|
|
5249
|
-
* }
|
|
5250
|
-
*
|
|
5251
|
-
* // Add friendships (undirected edges)
|
|
5252
|
-
* graph.addEdge('Alice', 'Bob', 1);
|
|
5253
|
-
* graph.addEdge('Alice', 'Charlie', 1);
|
|
5254
|
-
* graph.addEdge('Bob', 'Diana', 1);
|
|
5255
|
-
* graph.addEdge('Charlie', 'Eve', 1);
|
|
5256
|
-
* graph.addEdge('Diana', 'Eve', 1);
|
|
5257
|
-
*
|
|
5258
|
-
* // Get friends of each person
|
|
5259
|
-
* const aliceFriends = graph.getNeighbors('Alice');
|
|
5260
|
-
* console.log(aliceFriends[0].key); // 'Bob';
|
|
5261
|
-
* console.log(aliceFriends[1].key); // 'Charlie';
|
|
5262
|
-
* console.log(aliceFriends.length); // 2;
|
|
5263
|
-
*
|
|
5264
|
-
* const dianaFriends = graph.getNeighbors('Diana');
|
|
5265
|
-
* console.log(dianaFriends[0].key); // 'Bob';
|
|
5266
|
-
* console.log(dianaFriends[1].key); // 'Eve';
|
|
5267
|
-
* console.log(dianaFriends.length); // 2;
|
|
5268
|
-
*
|
|
5269
|
-
* // Verify bidirectional friendship
|
|
5270
|
-
* const bobFriends = graph.getNeighbors('Bob');
|
|
5271
|
-
* console.log(bobFriends[0].key); // 'Alice'; // Alice -> Bob -> Alice ✓
|
|
5272
|
-
* console.log(bobFriends[1].key); // 'Diana';
|
|
5273
|
-
*/
|
|
3607
|
+
* UndirectedGraph connectivity and neighbors
|
|
3608
|
+
* @example
|
|
3609
|
+
* // UndirectedGraph connectivity and neighbors
|
|
3610
|
+
* const graph = new UndirectedGraph<string>();
|
|
3611
|
+
*
|
|
3612
|
+
* // Build a friendship network
|
|
3613
|
+
* const people = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'];
|
|
3614
|
+
* for (const person of people) {
|
|
3615
|
+
* graph.addVertex(person);
|
|
3616
|
+
* }
|
|
3617
|
+
*
|
|
3618
|
+
* // Add friendships (undirected edges)
|
|
3619
|
+
* graph.addEdge('Alice', 'Bob', 1);
|
|
3620
|
+
* graph.addEdge('Alice', 'Charlie', 1);
|
|
3621
|
+
* graph.addEdge('Bob', 'Diana', 1);
|
|
3622
|
+
* graph.addEdge('Charlie', 'Eve', 1);
|
|
3623
|
+
* graph.addEdge('Diana', 'Eve', 1);
|
|
3624
|
+
*
|
|
3625
|
+
* // Get friends of each person
|
|
3626
|
+
* const aliceFriends = graph.getNeighbors('Alice');
|
|
3627
|
+
* console.log(aliceFriends[0].key); // 'Bob';
|
|
3628
|
+
* console.log(aliceFriends[1].key); // 'Charlie';
|
|
3629
|
+
* console.log(aliceFriends.length); // 2;
|
|
3630
|
+
*
|
|
3631
|
+
* const dianaFriends = graph.getNeighbors('Diana');
|
|
3632
|
+
* console.log(dianaFriends[0].key); // 'Bob';
|
|
3633
|
+
* console.log(dianaFriends[1].key); // 'Eve';
|
|
3634
|
+
* console.log(dianaFriends.length); // 2;
|
|
3635
|
+
*
|
|
3636
|
+
* // Verify bidirectional friendship
|
|
3637
|
+
* const bobFriends = graph.getNeighbors('Bob');
|
|
3638
|
+
* console.log(bobFriends[0].key); // 'Alice'; // Alice -> Bob -> Alice ✓
|
|
3639
|
+
* console.log(bobFriends[1].key); // 'Diana';
|
|
3640
|
+
*/
|
|
5274
3641
|
getNeighbors(vertexOrKey) {
|
|
5275
3642
|
const neighbors = [];
|
|
5276
3643
|
const vertex = this._getVertex(vertexOrKey);
|
|
@@ -5327,59 +3694,20 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
5327
3694
|
return super.clone();
|
|
5328
3695
|
}
|
|
5329
3696
|
/**
|
|
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
|
-
|
|
5371
|
-
|
|
5372
|
-
* @example
|
|
5373
|
-
* // Find articulation points and bridges
|
|
5374
|
-
* const g = new UndirectedGraph();
|
|
5375
|
-
* g.addVertex('A');
|
|
5376
|
-
* g.addVertex('B');
|
|
5377
|
-
* g.addVertex('C');
|
|
5378
|
-
* g.addEdge('A', 'B');
|
|
5379
|
-
* g.addEdge('B', 'C');
|
|
5380
|
-
* const result = g.tarjan();
|
|
5381
|
-
* console.log(result); // defined;
|
|
5382
|
-
*/
|
|
3697
|
+
* Tarjan-based bridge and articulation point detection.
|
|
3698
|
+
* @returns `{ dfnMap, lowMap, bridges, cutVertices }`.
|
|
3699
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
3700
|
+
* @example
|
|
3701
|
+
* // Find articulation points and bridges
|
|
3702
|
+
* const g = new UndirectedGraph();
|
|
3703
|
+
* g.addVertex('A');
|
|
3704
|
+
* g.addVertex('B');
|
|
3705
|
+
* g.addVertex('C');
|
|
3706
|
+
* g.addEdge('A', 'B');
|
|
3707
|
+
* g.addEdge('B', 'C');
|
|
3708
|
+
* const result = g.tarjan();
|
|
3709
|
+
* console.log(result); // defined;
|
|
3710
|
+
*/
|
|
5383
3711
|
tarjan() {
|
|
5384
3712
|
const dfnMap = /* @__PURE__ */ new Map();
|
|
5385
3713
|
const lowMap = /* @__PURE__ */ new Map();
|
|
@@ -5479,61 +3807,22 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
5479
3807
|
return components;
|
|
5480
3808
|
}
|
|
5481
3809
|
/**
|
|
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
|
-
|
|
5524
|
-
|
|
5525
|
-
* @example
|
|
5526
|
-
* // Detect cycle
|
|
5527
|
-
* const g = new UndirectedGraph();
|
|
5528
|
-
* g.addVertex('A');
|
|
5529
|
-
* g.addVertex('B');
|
|
5530
|
-
* g.addVertex('C');
|
|
5531
|
-
* g.addEdge('A', 'B');
|
|
5532
|
-
* g.addEdge('B', 'C');
|
|
5533
|
-
* console.log(g.hasCycle()); // false;
|
|
5534
|
-
* g.addEdge('C', 'A');
|
|
5535
|
-
* console.log(g.hasCycle()); // true;
|
|
5536
|
-
*/
|
|
3810
|
+
* Detect whether the graph contains a cycle.
|
|
3811
|
+
* Uses DFS with parent tracking.
|
|
3812
|
+
* @returns `true` if a cycle exists, `false` otherwise.
|
|
3813
|
+
* @remarks Time O(V + E), Space O(V)
|
|
3814
|
+
* @example
|
|
3815
|
+
* // Detect cycle
|
|
3816
|
+
* const g = new UndirectedGraph();
|
|
3817
|
+
* g.addVertex('A');
|
|
3818
|
+
* g.addVertex('B');
|
|
3819
|
+
* g.addVertex('C');
|
|
3820
|
+
* g.addEdge('A', 'B');
|
|
3821
|
+
* g.addEdge('B', 'C');
|
|
3822
|
+
* console.log(g.hasCycle()); // false;
|
|
3823
|
+
* g.addEdge('C', 'A');
|
|
3824
|
+
* console.log(g.hasCycle()); // true;
|
|
3825
|
+
*/
|
|
5537
3826
|
hasCycle() {
|
|
5538
3827
|
const visited = /* @__PURE__ */ new Set();
|
|
5539
3828
|
const dfs = /* @__PURE__ */ __name((vertex, parent) => {
|
|
@@ -5555,117 +3844,39 @@ var _UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
5555
3844
|
return false;
|
|
5556
3845
|
}
|
|
5557
3846
|
/**
|
|
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
|
-
|
|
5599
|
-
|
|
5600
|
-
* @example
|
|
5601
|
-
* // Find bridge edges
|
|
5602
|
-
* const g = new UndirectedGraph();
|
|
5603
|
-
* g.addVertex('A');
|
|
5604
|
-
* g.addVertex('B');
|
|
5605
|
-
* g.addVertex('C');
|
|
5606
|
-
* g.addEdge('A', 'B');
|
|
5607
|
-
* g.addEdge('B', 'C');
|
|
5608
|
-
* const bridges = g.getBridges();
|
|
5609
|
-
* console.log(bridges.length); // 2;
|
|
5610
|
-
*/
|
|
3847
|
+
* Get bridges discovered by `tarjan()`.
|
|
3848
|
+
* @returns Array of edges that are bridges.
|
|
3849
|
+
* @remarks Time O(B), Space O(1)
|
|
3850
|
+
* @example
|
|
3851
|
+
* // Find bridge edges
|
|
3852
|
+
* const g = new UndirectedGraph();
|
|
3853
|
+
* g.addVertex('A');
|
|
3854
|
+
* g.addVertex('B');
|
|
3855
|
+
* g.addVertex('C');
|
|
3856
|
+
* g.addEdge('A', 'B');
|
|
3857
|
+
* g.addEdge('B', 'C');
|
|
3858
|
+
* const bridges = g.getBridges();
|
|
3859
|
+
* console.log(bridges.length); // 2;
|
|
3860
|
+
*/
|
|
5611
3861
|
getBridges() {
|
|
5612
3862
|
return this.tarjan().bridges;
|
|
5613
3863
|
}
|
|
5614
3864
|
/**
|
|
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
|
-
|
|
5656
|
-
|
|
5657
|
-
* @example
|
|
5658
|
-
* // Find articulation points
|
|
5659
|
-
* const g = new UndirectedGraph();
|
|
5660
|
-
* g.addVertex('A');
|
|
5661
|
-
* g.addVertex('B');
|
|
5662
|
-
* g.addVertex('C');
|
|
5663
|
-
* g.addEdge('A', 'B');
|
|
5664
|
-
* g.addEdge('B', 'C');
|
|
5665
|
-
* const cuts = g.getCutVertices();
|
|
5666
|
-
* console.log(cuts.length); // 1;
|
|
5667
|
-
* console.log(cuts[0].key); // 'B';
|
|
5668
|
-
*/
|
|
3865
|
+
* Get articulation points discovered by `tarjan()`.
|
|
3866
|
+
* @returns Array of cut vertices.
|
|
3867
|
+
* @remarks Time O(C), Space O(1)
|
|
3868
|
+
* @example
|
|
3869
|
+
* // Find articulation points
|
|
3870
|
+
* const g = new UndirectedGraph();
|
|
3871
|
+
* g.addVertex('A');
|
|
3872
|
+
* g.addVertex('B');
|
|
3873
|
+
* g.addVertex('C');
|
|
3874
|
+
* g.addEdge('A', 'B');
|
|
3875
|
+
* g.addEdge('B', 'C');
|
|
3876
|
+
* const cuts = g.getCutVertices();
|
|
3877
|
+
* console.log(cuts.length); // 1;
|
|
3878
|
+
* console.log(cuts[0].key); // 'B';
|
|
3879
|
+
*/
|
|
5669
3880
|
getCutVertices() {
|
|
5670
3881
|
return this.tarjan().cutVertices;
|
|
5671
3882
|
}
|