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
package/dist/esm/graph.mjs
CHANGED
|
@@ -721,61 +721,19 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
721
721
|
return this._elements;
|
|
722
722
|
}
|
|
723
723
|
/**
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
* @example
|
|
770
|
-
* // Track heap capacity
|
|
771
|
-
* const heap = new Heap<number>();
|
|
772
|
-
* console.log(heap.size); // 0;
|
|
773
|
-
* heap.add(10);
|
|
774
|
-
* heap.add(20);
|
|
775
|
-
* console.log(heap.size); // 2;
|
|
776
|
-
* heap.poll();
|
|
777
|
-
* console.log(heap.size); // 1;
|
|
778
|
-
*/
|
|
724
|
+
* Get the number of elements.
|
|
725
|
+
* @remarks Time O(1), Space O(1)
|
|
726
|
+
* @returns Heap size.
|
|
727
|
+
* @example
|
|
728
|
+
* // Track heap capacity
|
|
729
|
+
* const heap = new Heap<number>();
|
|
730
|
+
* console.log(heap.size); // 0;
|
|
731
|
+
* heap.add(10);
|
|
732
|
+
* heap.add(20);
|
|
733
|
+
* console.log(heap.size); // 2;
|
|
734
|
+
* heap.poll();
|
|
735
|
+
* console.log(heap.size); // 1;
|
|
736
|
+
*/
|
|
779
737
|
get size() {
|
|
780
738
|
return this.elements.length;
|
|
781
739
|
}
|
|
@@ -787,6 +745,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
787
745
|
get leaf() {
|
|
788
746
|
return this.elements[this.size - 1] ?? void 0;
|
|
789
747
|
}
|
|
748
|
+
/**
|
|
749
|
+
* Get the comparator used to order elements.
|
|
750
|
+
* @remarks Time O(1), Space O(1)
|
|
751
|
+
* @returns Comparator function.
|
|
752
|
+
*/
|
|
753
|
+
get comparator() {
|
|
754
|
+
return this._comparator;
|
|
755
|
+
}
|
|
790
756
|
/**
|
|
791
757
|
* Create a heap of the same class from an iterable.
|
|
792
758
|
* @remarks Time O(N), Space O(N)
|
|
@@ -813,123 +779,42 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
813
779
|
return new _Heap(elements, options);
|
|
814
780
|
}
|
|
815
781
|
/**
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
* @example
|
|
863
|
-
* // basic Heap creation and add operation
|
|
864
|
-
* // Create a min heap (default)
|
|
865
|
-
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
866
|
-
*
|
|
867
|
-
* // Verify size
|
|
868
|
-
* console.log(minHeap.size); // 6;
|
|
869
|
-
*
|
|
870
|
-
* // Add new element
|
|
871
|
-
* minHeap.add(4);
|
|
872
|
-
* console.log(minHeap.size); // 7;
|
|
873
|
-
*
|
|
874
|
-
* // Min heap property: smallest element at root
|
|
875
|
-
* const min = minHeap.peek();
|
|
876
|
-
* console.log(min); // 1;
|
|
877
|
-
*/
|
|
782
|
+
* Insert an element.
|
|
783
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
784
|
+
* @param element - Element to insert.
|
|
785
|
+
* @returns True.
|
|
786
|
+
* @example
|
|
787
|
+
* // basic Heap creation and add operation
|
|
788
|
+
* // Create a min heap (default)
|
|
789
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
790
|
+
*
|
|
791
|
+
* // Verify size
|
|
792
|
+
* console.log(minHeap.size); // 6;
|
|
793
|
+
*
|
|
794
|
+
* // Add new element
|
|
795
|
+
* minHeap.add(4);
|
|
796
|
+
* console.log(minHeap.size); // 7;
|
|
797
|
+
*
|
|
798
|
+
* // Min heap property: smallest element at root
|
|
799
|
+
* const min = minHeap.peek();
|
|
800
|
+
* console.log(min); // 1;
|
|
801
|
+
*/
|
|
878
802
|
add(element) {
|
|
879
803
|
this._elements.push(element);
|
|
880
804
|
return this._bubbleUp(this.elements.length - 1);
|
|
881
805
|
}
|
|
882
806
|
/**
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
* @example
|
|
927
|
-
* // Add multiple elements
|
|
928
|
-
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
929
|
-
* heap.addMany([5, 3, 7, 1]);
|
|
930
|
-
* console.log(heap.peek()); // 1;
|
|
931
|
-
* console.log(heap.size); // 4;
|
|
932
|
-
*/
|
|
807
|
+
* Insert many elements from an iterable.
|
|
808
|
+
* @remarks Time O(N log N), Space O(1)
|
|
809
|
+
* @param elements - Iterable of elements or raw values.
|
|
810
|
+
* @returns Array of per-element success flags.
|
|
811
|
+
* @example
|
|
812
|
+
* // Add multiple elements
|
|
813
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
814
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
815
|
+
* console.log(heap.peek()); // 1;
|
|
816
|
+
* console.log(heap.size); // 4;
|
|
817
|
+
*/
|
|
933
818
|
addMany(elements) {
|
|
934
819
|
const flags = [];
|
|
935
820
|
for (const el of elements) {
|
|
@@ -944,104 +829,63 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
944
829
|
return flags;
|
|
945
830
|
}
|
|
946
831
|
/**
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
1005
|
-
* });
|
|
1006
|
-
*
|
|
1007
|
-
* console.log(maxHeap.size); // 3;
|
|
1008
|
-
*
|
|
1009
|
-
* // Peek returns highest priority task
|
|
1010
|
-
* const topTask = maxHeap.peek();
|
|
1011
|
-
* console.log(topTask?.priority); // 8;
|
|
1012
|
-
* console.log(topTask?.name); // 'Alert';
|
|
1013
|
-
*/
|
|
1014
|
-
/**
|
|
1015
|
-
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
* @example
|
|
1020
|
-
* // Heap with custom comparator (MaxHeap behavior)
|
|
1021
|
-
* interface Task {
|
|
1022
|
-
* id: number;
|
|
1023
|
-
* priority: number;
|
|
1024
|
-
* name: string;
|
|
1025
|
-
* }
|
|
1026
|
-
*
|
|
1027
|
-
* // Custom comparator for max heap behavior (higher priority first)
|
|
1028
|
-
* const tasks: Task[] = [
|
|
1029
|
-
* { id: 1, priority: 5, name: 'Email' },
|
|
1030
|
-
* { id: 2, priority: 3, name: 'Chat' },
|
|
1031
|
-
* { id: 3, priority: 8, name: 'Alert' }
|
|
1032
|
-
* ];
|
|
1033
|
-
*
|
|
1034
|
-
* const maxHeap = new Heap(tasks, {
|
|
1035
|
-
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
1036
|
-
* });
|
|
1037
|
-
*
|
|
1038
|
-
* console.log(maxHeap.size); // 3;
|
|
1039
|
-
*
|
|
1040
|
-
* // Peek returns highest priority task
|
|
1041
|
-
* const topTask = maxHeap.peek();
|
|
1042
|
-
* console.log(topTask?.priority); // 8;
|
|
1043
|
-
* console.log(topTask?.name); // 'Alert';
|
|
1044
|
-
*/
|
|
832
|
+
* Remove and return the top element.
|
|
833
|
+
* @remarks Time O(log N), Space O(1)
|
|
834
|
+
* @returns Top element or undefined.
|
|
835
|
+
* @example
|
|
836
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
837
|
+
* interface Task {
|
|
838
|
+
* id: number;
|
|
839
|
+
* priority: number;
|
|
840
|
+
* name: string;
|
|
841
|
+
* }
|
|
842
|
+
*
|
|
843
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
844
|
+
* const tasks: Task[] = [
|
|
845
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
846
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
847
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
848
|
+
* ];
|
|
849
|
+
*
|
|
850
|
+
* const maxHeap = new Heap(tasks, {
|
|
851
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
852
|
+
* });
|
|
853
|
+
*
|
|
854
|
+
* console.log(maxHeap.size); // 3;
|
|
855
|
+
*
|
|
856
|
+
* // Peek returns highest priority task
|
|
857
|
+
* const topTask = maxHeap.peek();
|
|
858
|
+
* console.log(topTask?.priority); // 8;
|
|
859
|
+
* console.log(topTask?.name); // 'Alert';
|
|
860
|
+
*/
|
|
861
|
+
/**
|
|
862
|
+
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
863
|
+
* @example
|
|
864
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
865
|
+
* interface Task {
|
|
866
|
+
* id: number;
|
|
867
|
+
* priority: number;
|
|
868
|
+
* name: string;
|
|
869
|
+
* }
|
|
870
|
+
*
|
|
871
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
872
|
+
* const tasks: Task[] = [
|
|
873
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
874
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
875
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
876
|
+
* ];
|
|
877
|
+
*
|
|
878
|
+
* const maxHeap = new Heap(tasks, {
|
|
879
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
880
|
+
* });
|
|
881
|
+
*
|
|
882
|
+
* console.log(maxHeap.size); // 3;
|
|
883
|
+
*
|
|
884
|
+
* // Peek returns highest priority task
|
|
885
|
+
* const topTask = maxHeap.peek();
|
|
886
|
+
* console.log(topTask?.priority); // 8;
|
|
887
|
+
* console.log(topTask?.name); // 'Alert';
|
|
888
|
+
*/
|
|
1045
889
|
poll() {
|
|
1046
890
|
return this.pop();
|
|
1047
891
|
}
|
|
@@ -1061,319 +905,125 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1061
905
|
return value;
|
|
1062
906
|
}
|
|
1063
907
|
/**
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
* });
|
|
1128
|
-
*
|
|
1129
|
-
* // Add events in random order
|
|
1130
|
-
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
1131
|
-
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
1132
|
-
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
1133
|
-
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
1134
|
-
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
1135
|
-
*
|
|
1136
|
-
* console.log(eventHeap.size); // 5;
|
|
1137
|
-
*
|
|
1138
|
-
* // Process events by priority (critical first)
|
|
1139
|
-
* const processedOrder: Event[] = [];
|
|
1140
|
-
* while (eventHeap.size > 0) {
|
|
1141
|
-
* const event = eventHeap.poll();
|
|
1142
|
-
* if (event) {
|
|
1143
|
-
* processedOrder.push(event);
|
|
1144
|
-
* }
|
|
1145
|
-
* }
|
|
1146
|
-
*
|
|
1147
|
-
* // Verify critical events came first
|
|
1148
|
-
* console.log(processedOrder[0].type); // 'critical';
|
|
1149
|
-
* console.log(processedOrder[1].type); // 'critical';
|
|
1150
|
-
* console.log(processedOrder[2].type); // 'warning';
|
|
1151
|
-
* console.log(processedOrder[3].type); // 'info';
|
|
1152
|
-
* console.log(processedOrder[4].type); // 'info';
|
|
1153
|
-
*
|
|
1154
|
-
* // Verify O(log n) operations
|
|
1155
|
-
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
1156
|
-
*
|
|
1157
|
-
* // Add - O(log n)
|
|
1158
|
-
* newHeap.add(2);
|
|
1159
|
-
* console.log(newHeap.size); // 5;
|
|
1160
|
-
*
|
|
1161
|
-
* // Poll - O(log n)
|
|
1162
|
-
* const removed = newHeap.poll();
|
|
1163
|
-
* console.log(removed); // 1;
|
|
1164
|
-
*
|
|
1165
|
-
* // Peek - O(1)
|
|
1166
|
-
* const top = newHeap.peek();
|
|
1167
|
-
* console.log(top); // 2;
|
|
1168
|
-
*/
|
|
908
|
+
* Get the current top element without removing it.
|
|
909
|
+
* @remarks Time O(1), Space O(1)
|
|
910
|
+
* @returns Top element or undefined.
|
|
911
|
+
* @example
|
|
912
|
+
* // Heap for event processing with priority
|
|
913
|
+
* interface Event {
|
|
914
|
+
* id: number;
|
|
915
|
+
* type: 'critical' | 'warning' | 'info';
|
|
916
|
+
* timestamp: number;
|
|
917
|
+
* message: string;
|
|
918
|
+
* }
|
|
919
|
+
*
|
|
920
|
+
* // Custom priority: critical > warning > info
|
|
921
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
922
|
+
*
|
|
923
|
+
* const eventHeap = new Heap<Event>([], {
|
|
924
|
+
* comparator: (a: Event, b: Event) => {
|
|
925
|
+
* const priorityA = priorityMap[a.type];
|
|
926
|
+
* const priorityB = priorityMap[b.type];
|
|
927
|
+
* return priorityB - priorityA; // Higher priority first
|
|
928
|
+
* }
|
|
929
|
+
* });
|
|
930
|
+
*
|
|
931
|
+
* // Add events in random order
|
|
932
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
933
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
934
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
935
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
936
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
937
|
+
*
|
|
938
|
+
* console.log(eventHeap.size); // 5;
|
|
939
|
+
*
|
|
940
|
+
* // Process events by priority (critical first)
|
|
941
|
+
* const processedOrder: Event[] = [];
|
|
942
|
+
* while (eventHeap.size > 0) {
|
|
943
|
+
* const event = eventHeap.poll();
|
|
944
|
+
* if (event) {
|
|
945
|
+
* processedOrder.push(event);
|
|
946
|
+
* }
|
|
947
|
+
* }
|
|
948
|
+
*
|
|
949
|
+
* // Verify critical events came first
|
|
950
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
951
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
952
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
953
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
954
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
955
|
+
*
|
|
956
|
+
* // Verify O(log n) operations
|
|
957
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
958
|
+
*
|
|
959
|
+
* // Add - O(log n)
|
|
960
|
+
* newHeap.add(2);
|
|
961
|
+
* console.log(newHeap.size); // 5;
|
|
962
|
+
*
|
|
963
|
+
* // Poll - O(log n)
|
|
964
|
+
* const removed = newHeap.poll();
|
|
965
|
+
* console.log(removed); // 1;
|
|
966
|
+
*
|
|
967
|
+
* // Peek - O(1)
|
|
968
|
+
* const top = newHeap.peek();
|
|
969
|
+
* console.log(top); // 2;
|
|
970
|
+
*/
|
|
1169
971
|
peek() {
|
|
1170
972
|
return this.elements[0];
|
|
1171
973
|
}
|
|
1172
974
|
/**
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
* @example
|
|
1217
|
-
* // Check if heap is empty
|
|
1218
|
-
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
1219
|
-
* console.log(heap.isEmpty()); // true;
|
|
1220
|
-
* heap.add(1);
|
|
1221
|
-
* console.log(heap.isEmpty()); // false;
|
|
1222
|
-
*/
|
|
975
|
+
* Check whether the heap is empty.
|
|
976
|
+
* @remarks Time O(1), Space O(1)
|
|
977
|
+
* @returns True if size is 0.
|
|
978
|
+
* @example
|
|
979
|
+
* // Check if heap is empty
|
|
980
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
981
|
+
* console.log(heap.isEmpty()); // true;
|
|
982
|
+
* heap.add(1);
|
|
983
|
+
* console.log(heap.isEmpty()); // false;
|
|
984
|
+
*/
|
|
1223
985
|
isEmpty() {
|
|
1224
986
|
return this.size === 0;
|
|
1225
987
|
}
|
|
1226
988
|
/**
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
* @example
|
|
1271
|
-
* // Remove all elements
|
|
1272
|
-
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1273
|
-
* heap.clear();
|
|
1274
|
-
* console.log(heap.isEmpty()); // true;
|
|
1275
|
-
*/
|
|
989
|
+
* Remove all elements.
|
|
990
|
+
* @remarks Time O(1), Space O(1)
|
|
991
|
+
* @returns void
|
|
992
|
+
* @example
|
|
993
|
+
* // Remove all elements
|
|
994
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
995
|
+
* heap.clear();
|
|
996
|
+
* console.log(heap.isEmpty()); // true;
|
|
997
|
+
*/
|
|
1276
998
|
clear() {
|
|
1277
999
|
this._elements = [];
|
|
1278
1000
|
}
|
|
1279
1001
|
/**
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
* @example
|
|
1318
|
-
* // Check element existence
|
|
1319
|
-
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
1320
|
-
* console.log(heap.has(1)); // true;
|
|
1321
|
-
* console.log(heap.has(99)); // false;
|
|
1322
|
-
*/
|
|
1002
|
+
* Check if an equal element exists in the heap.
|
|
1003
|
+
* @remarks Time O(N), Space O(1)
|
|
1004
|
+
* @param element - Element to search for.
|
|
1005
|
+
* @returns True if found.
|
|
1006
|
+
* @example
|
|
1007
|
+
* // Check element existence
|
|
1008
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
1009
|
+
* console.log(heap.has(1)); // true;
|
|
1010
|
+
* console.log(heap.has(99)); // false;
|
|
1011
|
+
*/
|
|
1323
1012
|
has(element) {
|
|
1324
1013
|
for (const el of this.elements) if (this._equals(el, element)) return true;
|
|
1325
1014
|
return false;
|
|
1326
1015
|
}
|
|
1327
1016
|
/**
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
* @example
|
|
1372
|
-
* // Remove specific element
|
|
1373
|
-
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
1374
|
-
* heap.delete(4);
|
|
1375
|
-
* console.log(heap.toArray().includes(4)); // false;
|
|
1376
|
-
*/
|
|
1017
|
+
* Delete one occurrence of an element.
|
|
1018
|
+
* @remarks Time O(N), Space O(1)
|
|
1019
|
+
* @param element - Element to delete.
|
|
1020
|
+
* @returns True if an element was removed.
|
|
1021
|
+
* @example
|
|
1022
|
+
* // Remove specific element
|
|
1023
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
1024
|
+
* heap.delete(4);
|
|
1025
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
1026
|
+
*/
|
|
1377
1027
|
delete(element) {
|
|
1378
1028
|
let index = -1;
|
|
1379
1029
|
for (let i = 0; i < this.elements.length; i++) {
|
|
@@ -1437,49 +1087,16 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1437
1087
|
return this;
|
|
1438
1088
|
}
|
|
1439
1089
|
/**
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
* @example
|
|
1478
|
-
* // Depth-first traversal
|
|
1479
|
-
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
1480
|
-
* const result = heap.dfs('IN');
|
|
1481
|
-
* console.log(result.length); // 3;
|
|
1482
|
-
*/
|
|
1090
|
+
* Traverse the binary heap as a complete binary tree and collect elements.
|
|
1091
|
+
* @remarks Time O(N), Space O(H)
|
|
1092
|
+
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
1093
|
+
* @returns Array of visited elements.
|
|
1094
|
+
* @example
|
|
1095
|
+
* // Depth-first traversal
|
|
1096
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
1097
|
+
* const result = heap.dfs('IN');
|
|
1098
|
+
* console.log(result.length); // 3;
|
|
1099
|
+
*/
|
|
1483
1100
|
dfs(order = "PRE") {
|
|
1484
1101
|
const result = [];
|
|
1485
1102
|
const _dfs = /* @__PURE__ */ __name((index) => {
|
|
@@ -1516,57 +1133,15 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1516
1133
|
return results;
|
|
1517
1134
|
}
|
|
1518
1135
|
/**
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
* @example
|
|
1565
|
-
* // Sort elements using heap
|
|
1566
|
-
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
1567
|
-
* const sorted = heap.sort();
|
|
1568
|
-
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
1569
|
-
*/
|
|
1136
|
+
* Return all elements in ascending order by repeatedly polling.
|
|
1137
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1138
|
+
* @returns Sorted array of elements.
|
|
1139
|
+
* @example
|
|
1140
|
+
* // Sort elements using heap
|
|
1141
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
1142
|
+
* const sorted = heap.sort();
|
|
1143
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
1144
|
+
*/
|
|
1570
1145
|
sort() {
|
|
1571
1146
|
const visited = [];
|
|
1572
1147
|
const cloned = this._createInstance();
|
|
@@ -1578,114 +1153,34 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1578
1153
|
return visited;
|
|
1579
1154
|
}
|
|
1580
1155
|
/**
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
* @example
|
|
1625
|
-
* // Create independent copy
|
|
1626
|
-
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
1627
|
-
* const copy = heap.clone();
|
|
1628
|
-
* copy.poll();
|
|
1629
|
-
* console.log(heap.size); // 3;
|
|
1630
|
-
* console.log(copy.size); // 2;
|
|
1631
|
-
*/
|
|
1156
|
+
* Deep clone this heap.
|
|
1157
|
+
* @remarks Time O(N), Space O(N)
|
|
1158
|
+
* @returns A new heap with the same elements.
|
|
1159
|
+
* @example
|
|
1160
|
+
* // Create independent copy
|
|
1161
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
1162
|
+
* const copy = heap.clone();
|
|
1163
|
+
* copy.poll();
|
|
1164
|
+
* console.log(heap.size); // 3;
|
|
1165
|
+
* console.log(copy.size); // 2;
|
|
1166
|
+
*/
|
|
1632
1167
|
clone() {
|
|
1633
1168
|
const next = this._createInstance();
|
|
1634
1169
|
for (const x of this.elements) next.add(x);
|
|
1635
1170
|
return next;
|
|
1636
1171
|
}
|
|
1637
1172
|
/**
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
* @example
|
|
1684
|
-
* // Filter elements
|
|
1685
|
-
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
1686
|
-
* const evens = heap.filter(x => x % 2 === 0);
|
|
1687
|
-
* console.log(evens.size); // 2;
|
|
1688
|
-
*/
|
|
1173
|
+
* Filter elements into a new heap of the same class.
|
|
1174
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1175
|
+
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
1176
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1177
|
+
* @returns A new heap with the kept elements.
|
|
1178
|
+
* @example
|
|
1179
|
+
* // Filter elements
|
|
1180
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
1181
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
1182
|
+
* console.log(evens.size); // 2;
|
|
1183
|
+
*/
|
|
1689
1184
|
filter(callback, thisArg) {
|
|
1690
1185
|
const out = this._createInstance();
|
|
1691
1186
|
let i = 0;
|
|
@@ -1699,59 +1194,20 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1699
1194
|
return out;
|
|
1700
1195
|
}
|
|
1701
1196
|
/**
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
* @example
|
|
1750
|
-
* // Transform elements
|
|
1751
|
-
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1752
|
-
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
1753
|
-
* console.log(doubled.peek()); // 2;
|
|
1754
|
-
*/
|
|
1197
|
+
* Map elements into a new heap of possibly different element type.
|
|
1198
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1199
|
+
* @template EM
|
|
1200
|
+
* @template RM
|
|
1201
|
+
* @param callback - Mapping function (element, index, heap) → newElement.
|
|
1202
|
+
* @param options - Options for the output heap, including comparator for EM.
|
|
1203
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1204
|
+
* @returns A new heap with mapped elements.
|
|
1205
|
+
* @example
|
|
1206
|
+
* // Transform elements
|
|
1207
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1208
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
1209
|
+
* console.log(doubled.peek()); // 2;
|
|
1210
|
+
*/
|
|
1755
1211
|
map(callback, options, thisArg) {
|
|
1756
1212
|
const { comparator, toElementFn, ...rest } = options ?? {};
|
|
1757
1213
|
if (!comparator) raise(TypeError, ERR.comparatorRequired("Heap.map"));
|
|
@@ -1788,14 +1244,6 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1788
1244
|
return 0;
|
|
1789
1245
|
}, "_DEFAULT_COMPARATOR");
|
|
1790
1246
|
_comparator = this._DEFAULT_COMPARATOR;
|
|
1791
|
-
/**
|
|
1792
|
-
* Get the comparator used to order elements.
|
|
1793
|
-
* @remarks Time O(1), Space O(1)
|
|
1794
|
-
* @returns Comparator function.
|
|
1795
|
-
*/
|
|
1796
|
-
get comparator() {
|
|
1797
|
-
return this._comparator;
|
|
1798
|
-
}
|
|
1799
1247
|
*_getIterator() {
|
|
1800
1248
|
for (const element of this.elements) yield element;
|
|
1801
1249
|
}
|
|
@@ -1921,124 +1369,32 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1921
1369
|
this._autoCompactRatio = value;
|
|
1922
1370
|
}
|
|
1923
1371
|
/**
|
|
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
|
-
|
|
1968
|
-
|
|
1969
|
-
* @example
|
|
1970
|
-
* // Track queue length
|
|
1971
|
-
* const q = new Queue<number>();
|
|
1972
|
-
* console.log(q.length); // 0;
|
|
1973
|
-
* q.push(1);
|
|
1974
|
-
* q.push(2);
|
|
1975
|
-
* console.log(q.length); // 2;
|
|
1976
|
-
*/
|
|
1372
|
+
* Get the number of elements currently in the queue.
|
|
1373
|
+
* @remarks Time O(1), Space O(1)
|
|
1374
|
+
* @returns Current length.
|
|
1375
|
+
* @example
|
|
1376
|
+
* // Track queue length
|
|
1377
|
+
* const q = new Queue<number>();
|
|
1378
|
+
* console.log(q.length); // 0;
|
|
1379
|
+
* q.push(1);
|
|
1380
|
+
* q.push(2);
|
|
1381
|
+
* console.log(q.length); // 2;
|
|
1382
|
+
*/
|
|
1977
1383
|
get length() {
|
|
1978
1384
|
return this.elements.length - this._offset;
|
|
1979
1385
|
}
|
|
1980
1386
|
/**
|
|
1981
|
-
|
|
1982
|
-
* @remarks Time O(1), Space O(1)
|
|
1983
|
-
* @returns Front element or undefined.
|
|
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
|
-
|
|
2025
|
-
|
|
2026
|
-
* @example
|
|
2027
|
-
* // View the front element
|
|
2028
|
-
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
2029
|
-
* console.log(q.first); // 'first';
|
|
2030
|
-
* console.log(q.length); // 3;
|
|
2031
|
-
*/
|
|
2032
|
-
get first() {
|
|
2033
|
-
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
2034
|
-
}
|
|
2035
|
-
/**
|
|
2036
|
-
* Peek at the front element without removing it (alias for `first`).
|
|
1387
|
+
* Get the first element (front) without removing it.
|
|
2037
1388
|
* @remarks Time O(1), Space O(1)
|
|
2038
1389
|
* @returns Front element or undefined.
|
|
1390
|
+
* @example
|
|
1391
|
+
* // View the front element
|
|
1392
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
1393
|
+
* console.log(q.first); // 'first';
|
|
1394
|
+
* console.log(q.length); // 3;
|
|
2039
1395
|
*/
|
|
2040
|
-
|
|
2041
|
-
return this.
|
|
1396
|
+
get first() {
|
|
1397
|
+
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
2042
1398
|
}
|
|
2043
1399
|
/**
|
|
2044
1400
|
* Get the last element (back) without removing it.
|
|
@@ -2059,131 +1415,55 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2059
1415
|
return new _Queue(elements);
|
|
2060
1416
|
}
|
|
2061
1417
|
/**
|
|
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
|
-
|
|
2106
|
-
|
|
2107
|
-
* @example
|
|
2108
|
-
* // Queue for...of iteration and isEmpty check
|
|
2109
|
-
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
2110
|
-
*
|
|
2111
|
-
* const elements: string[] = [];
|
|
2112
|
-
* for (const item of queue) {
|
|
2113
|
-
* elements.push(item);
|
|
2114
|
-
* }
|
|
2115
|
-
*
|
|
2116
|
-
* // Verify all elements are iterated in order
|
|
2117
|
-
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
2118
|
-
*
|
|
2119
|
-
* // Process all elements
|
|
2120
|
-
* while (queue.length > 0) {
|
|
2121
|
-
* queue.shift();
|
|
2122
|
-
* }
|
|
2123
|
-
*
|
|
2124
|
-
* console.log(queue.length); // 0;
|
|
2125
|
-
*/
|
|
1418
|
+
* Peek at the front element without removing it (alias for `first`).
|
|
1419
|
+
* @remarks Time O(1), Space O(1)
|
|
1420
|
+
* @returns Front element or undefined.
|
|
1421
|
+
*/
|
|
1422
|
+
peek() {
|
|
1423
|
+
return this.first;
|
|
1424
|
+
}
|
|
1425
|
+
/**
|
|
1426
|
+
* Check whether the queue is empty.
|
|
1427
|
+
* @remarks Time O(1), Space O(1)
|
|
1428
|
+
* @returns True if length is 0.
|
|
1429
|
+
* @example
|
|
1430
|
+
* // Queue for...of iteration and isEmpty check
|
|
1431
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
1432
|
+
*
|
|
1433
|
+
* const elements: string[] = [];
|
|
1434
|
+
* for (const item of queue) {
|
|
1435
|
+
* elements.push(item);
|
|
1436
|
+
* }
|
|
1437
|
+
*
|
|
1438
|
+
* // Verify all elements are iterated in order
|
|
1439
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
1440
|
+
*
|
|
1441
|
+
* // Process all elements
|
|
1442
|
+
* while (queue.length > 0) {
|
|
1443
|
+
* queue.shift();
|
|
1444
|
+
* }
|
|
1445
|
+
*
|
|
1446
|
+
* console.log(queue.length); // 0;
|
|
1447
|
+
*/
|
|
2126
1448
|
isEmpty() {
|
|
2127
1449
|
return this.length === 0;
|
|
2128
1450
|
}
|
|
2129
1451
|
/**
|
|
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
|
-
|
|
2175
|
-
|
|
2176
|
-
* @example
|
|
2177
|
-
* // basic Queue creation and push operation
|
|
2178
|
-
* // Create a simple Queue with initial values
|
|
2179
|
-
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
2180
|
-
*
|
|
2181
|
-
* // Verify the queue maintains insertion order
|
|
2182
|
-
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
2183
|
-
*
|
|
2184
|
-
* // Check length
|
|
2185
|
-
* console.log(queue.length); // 5;
|
|
2186
|
-
*/
|
|
1452
|
+
* Enqueue one element at the back.
|
|
1453
|
+
* @remarks Time O(1), Space O(1)
|
|
1454
|
+
* @param element - Element to enqueue.
|
|
1455
|
+
* @returns True on success.
|
|
1456
|
+
* @example
|
|
1457
|
+
* // basic Queue creation and push operation
|
|
1458
|
+
* // Create a simple Queue with initial values
|
|
1459
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
1460
|
+
*
|
|
1461
|
+
* // Verify the queue maintains insertion order
|
|
1462
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
1463
|
+
*
|
|
1464
|
+
* // Check length
|
|
1465
|
+
* console.log(queue.length); // 5;
|
|
1466
|
+
*/
|
|
2187
1467
|
push(element) {
|
|
2188
1468
|
this.elements.push(element);
|
|
2189
1469
|
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
@@ -2204,66 +1484,24 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2204
1484
|
return ans;
|
|
2205
1485
|
}
|
|
2206
1486
|
/**
|
|
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
|
-
|
|
2251
|
-
|
|
2252
|
-
* @example
|
|
2253
|
-
* // Queue shift and peek operations
|
|
2254
|
-
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
2255
|
-
*
|
|
2256
|
-
* // Peek at the front element without removing it
|
|
2257
|
-
* console.log(queue.first); // 10;
|
|
2258
|
-
*
|
|
2259
|
-
* // Remove and get the first element (FIFO)
|
|
2260
|
-
* const first = queue.shift();
|
|
2261
|
-
* console.log(first); // 10;
|
|
2262
|
-
*
|
|
2263
|
-
* // Verify remaining elements and length decreased
|
|
2264
|
-
* console.log([...queue]); // [20, 30, 40];
|
|
2265
|
-
* console.log(queue.length); // 3;
|
|
2266
|
-
*/
|
|
1487
|
+
* Dequeue one element from the front (amortized via offset).
|
|
1488
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
1489
|
+
* @returns Removed element or undefined.
|
|
1490
|
+
* @example
|
|
1491
|
+
* // Queue shift and peek operations
|
|
1492
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
1493
|
+
*
|
|
1494
|
+
* // Peek at the front element without removing it
|
|
1495
|
+
* console.log(queue.first); // 10;
|
|
1496
|
+
*
|
|
1497
|
+
* // Remove and get the first element (FIFO)
|
|
1498
|
+
* const first = queue.shift();
|
|
1499
|
+
* console.log(first); // 10;
|
|
1500
|
+
*
|
|
1501
|
+
* // Verify remaining elements and length decreased
|
|
1502
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
1503
|
+
* console.log(queue.length); // 3;
|
|
1504
|
+
*/
|
|
2267
1505
|
shift() {
|
|
2268
1506
|
if (this.length === 0) return void 0;
|
|
2269
1507
|
const first = this.first;
|
|
@@ -2272,55 +1510,16 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2272
1510
|
return first;
|
|
2273
1511
|
}
|
|
2274
1512
|
/**
|
|
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
|
-
|
|
2317
|
-
|
|
2318
|
-
* @example
|
|
2319
|
-
* // Remove specific element
|
|
2320
|
-
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
2321
|
-
* q.delete(2);
|
|
2322
|
-
* console.log(q.length); // 3;
|
|
2323
|
-
*/
|
|
1513
|
+
* Delete the first occurrence of a specific element.
|
|
1514
|
+
* @remarks Time O(N), Space O(1)
|
|
1515
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
1516
|
+
* @returns True if an element was removed.
|
|
1517
|
+
* @example
|
|
1518
|
+
* // Remove specific element
|
|
1519
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
1520
|
+
* q.delete(2);
|
|
1521
|
+
* console.log(q.length); // 3;
|
|
1522
|
+
*/
|
|
2324
1523
|
delete(element) {
|
|
2325
1524
|
for (let i = this._offset; i < this.elements.length; i++) {
|
|
2326
1525
|
if (Object.is(this.elements[i], element)) {
|
|
@@ -2331,55 +1530,16 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2331
1530
|
return false;
|
|
2332
1531
|
}
|
|
2333
1532
|
/**
|
|
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
|
-
|
|
2376
|
-
|
|
2377
|
-
* @example
|
|
2378
|
-
* // Access element by index
|
|
2379
|
-
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
2380
|
-
* console.log(q.at(0)); // 'a';
|
|
2381
|
-
* console.log(q.at(2)); // 'c';
|
|
2382
|
-
*/
|
|
1533
|
+
* Get the element at a given logical index.
|
|
1534
|
+
* @remarks Time O(1), Space O(1)
|
|
1535
|
+
* @param index - Zero-based index from the front.
|
|
1536
|
+
* @returns Element or undefined.
|
|
1537
|
+
* @example
|
|
1538
|
+
* // Access element by index
|
|
1539
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
1540
|
+
* console.log(q.at(0)); // 'a';
|
|
1541
|
+
* console.log(q.at(2)); // 'c';
|
|
1542
|
+
*/
|
|
2383
1543
|
at(index) {
|
|
2384
1544
|
if (index < 0 || index >= this.length) return void 0;
|
|
2385
1545
|
return this._elements[this._offset + index];
|
|
@@ -2446,110 +1606,31 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2446
1606
|
return this;
|
|
2447
1607
|
}
|
|
2448
1608
|
/**
|
|
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
|
-
|
|
2491
|
-
|
|
2492
|
-
* @example
|
|
2493
|
-
* // Remove all elements
|
|
2494
|
-
* const q = new Queue<number>([1, 2, 3]);
|
|
2495
|
-
* q.clear();
|
|
2496
|
-
* console.log(q.length); // 0;
|
|
2497
|
-
*/
|
|
1609
|
+
* Remove all elements and reset offset.
|
|
1610
|
+
* @remarks Time O(1), Space O(1)
|
|
1611
|
+
* @returns void
|
|
1612
|
+
* @example
|
|
1613
|
+
* // Remove all elements
|
|
1614
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1615
|
+
* q.clear();
|
|
1616
|
+
* console.log(q.length); // 0;
|
|
1617
|
+
*/
|
|
2498
1618
|
clear() {
|
|
2499
1619
|
this._elements = [];
|
|
2500
1620
|
this._offset = 0;
|
|
2501
1621
|
}
|
|
2502
1622
|
/**
|
|
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
|
-
|
|
2544
|
-
|
|
2545
|
-
* @example
|
|
2546
|
-
* // Reclaim unused memory
|
|
2547
|
-
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2548
|
-
* q.shift();
|
|
2549
|
-
* q.shift();
|
|
2550
|
-
* q.compact();
|
|
2551
|
-
* console.log(q.length); // 3;
|
|
2552
|
-
*/
|
|
1623
|
+
* Compact storage by discarding consumed head elements.
|
|
1624
|
+
* @remarks Time O(N), Space O(N)
|
|
1625
|
+
* @returns True when compaction performed.
|
|
1626
|
+
* @example
|
|
1627
|
+
* // Reclaim unused memory
|
|
1628
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1629
|
+
* q.shift();
|
|
1630
|
+
* q.shift();
|
|
1631
|
+
* q.compact();
|
|
1632
|
+
* console.log(q.length); // 3;
|
|
1633
|
+
*/
|
|
2553
1634
|
compact() {
|
|
2554
1635
|
this._elements = this.elements.slice(this._offset);
|
|
2555
1636
|
this._offset = 0;
|
|
@@ -2575,57 +1656,17 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2575
1656
|
return removed;
|
|
2576
1657
|
}
|
|
2577
1658
|
/**
|
|
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
|
-
|
|
2620
|
-
|
|
2621
|
-
* @example
|
|
2622
|
-
* // Create independent copy
|
|
2623
|
-
* const q = new Queue<number>([1, 2, 3]);
|
|
2624
|
-
* const copy = q.clone();
|
|
2625
|
-
* copy.shift();
|
|
2626
|
-
* console.log(q.length); // 3;
|
|
2627
|
-
* console.log(copy.length); // 2;
|
|
2628
|
-
*/
|
|
1659
|
+
* Deep clone this queue and its parameters.
|
|
1660
|
+
* @remarks Time O(N), Space O(N)
|
|
1661
|
+
* @returns A new queue with the same content and options.
|
|
1662
|
+
* @example
|
|
1663
|
+
* // Create independent copy
|
|
1664
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1665
|
+
* const copy = q.clone();
|
|
1666
|
+
* copy.shift();
|
|
1667
|
+
* console.log(q.length); // 3;
|
|
1668
|
+
* console.log(copy.length); // 2;
|
|
1669
|
+
*/
|
|
2629
1670
|
clone() {
|
|
2630
1671
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2631
1672
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -2633,57 +1674,17 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2633
1674
|
return out;
|
|
2634
1675
|
}
|
|
2635
1676
|
/**
|
|
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
|
-
|
|
2680
|
-
|
|
2681
|
-
* @example
|
|
2682
|
-
* // Filter elements
|
|
2683
|
-
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
2684
|
-
* const evens = q.filter(x => x % 2 === 0);
|
|
2685
|
-
* console.log(evens.length); // 2;
|
|
2686
|
-
*/
|
|
1677
|
+
* Filter elements into a new queue of the same class.
|
|
1678
|
+
* @remarks Time O(N), Space O(N)
|
|
1679
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
1680
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
1681
|
+
* @returns A new queue with kept elements.
|
|
1682
|
+
* @example
|
|
1683
|
+
* // Filter elements
|
|
1684
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1685
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
1686
|
+
* console.log(evens.length); // 2;
|
|
1687
|
+
*/
|
|
2687
1688
|
filter(predicate, thisArg) {
|
|
2688
1689
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2689
1690
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -2695,59 +1696,20 @@ var Queue = class _Queue extends LinearBase {
|
|
|
2695
1696
|
return out;
|
|
2696
1697
|
}
|
|
2697
1698
|
/**
|
|
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
|
-
|
|
2744
|
-
|
|
2745
|
-
* @example
|
|
2746
|
-
* // Transform elements
|
|
2747
|
-
* const q = new Queue<number>([1, 2, 3]);
|
|
2748
|
-
* const doubled = q.map(x => x * 2);
|
|
2749
|
-
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
2750
|
-
*/
|
|
1699
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
1700
|
+
* @remarks Time O(N), Space O(N)
|
|
1701
|
+
* @template EM
|
|
1702
|
+
* @template RM
|
|
1703
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
1704
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
1705
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1706
|
+
* @returns A new Queue with mapped elements.
|
|
1707
|
+
* @example
|
|
1708
|
+
* // Transform elements
|
|
1709
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1710
|
+
* const doubled = q.map(x => x * 2);
|
|
1711
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
1712
|
+
*/
|
|
2751
1713
|
map(callback, options, thisArg) {
|
|
2752
1714
|
const out = new this.constructor([], {
|
|
2753
1715
|
toElementFn: options?.toElementFn,
|
|
@@ -2890,6 +1852,13 @@ var AbstractGraph = class extends IterableEntryBase {
|
|
|
2890
1852
|
get size() {
|
|
2891
1853
|
return this._vertexMap.size;
|
|
2892
1854
|
}
|
|
1855
|
+
/**
|
|
1856
|
+
* The edge connector string used in visual output.
|
|
1857
|
+
* Override in subclasses (e.g., '--' for undirected, '->' for directed).
|
|
1858
|
+
*/
|
|
1859
|
+
get _edgeConnector() {
|
|
1860
|
+
return "--";
|
|
1861
|
+
}
|
|
2893
1862
|
/**
|
|
2894
1863
|
* Get vertex instance by key.
|
|
2895
1864
|
* @param vertexKey - Vertex key.
|
|
@@ -2977,7 +1946,10 @@ var AbstractGraph = class extends IterableEntryBase {
|
|
|
2977
1946
|
const newEdge = this.createEdge(srcOrEdge, dest, weight, value);
|
|
2978
1947
|
return this._addEdge(newEdge);
|
|
2979
1948
|
} else {
|
|
2980
|
-
raise(
|
|
1949
|
+
raise(
|
|
1950
|
+
TypeError,
|
|
1951
|
+
ERR.invalidArgument("dest must be a Vertex or vertex key when srcOrEdge is an Edge.", "Graph")
|
|
1952
|
+
);
|
|
2981
1953
|
}
|
|
2982
1954
|
}
|
|
2983
1955
|
}
|
|
@@ -3533,6 +2505,7 @@ var AbstractGraph = class extends IterableEntryBase {
|
|
|
3533
2505
|
}
|
|
3534
2506
|
return mapped;
|
|
3535
2507
|
}
|
|
2508
|
+
// ===== Same-species factory & cloning helpers =====
|
|
3536
2509
|
/**
|
|
3537
2510
|
* Create a deep clone of the graph with the same species.
|
|
3538
2511
|
* @remarks Time O(V + E), Space O(V + E)
|
|
@@ -3545,7 +2518,85 @@ var AbstractGraph = class extends IterableEntryBase {
|
|
|
3545
2518
|
clone() {
|
|
3546
2519
|
return this._createLike(void 0, this._snapshotOptions());
|
|
3547
2520
|
}
|
|
3548
|
-
|
|
2521
|
+
/**
|
|
2522
|
+
* Generate a text-based visual representation of the graph.
|
|
2523
|
+
*
|
|
2524
|
+
* **Adjacency list format:**
|
|
2525
|
+
* ```
|
|
2526
|
+
* Graph (5 vertices, 6 edges):
|
|
2527
|
+
* A -> B (1), C (2)
|
|
2528
|
+
* B -> D (3)
|
|
2529
|
+
* C -> (no outgoing edges)
|
|
2530
|
+
* D -> A (1)
|
|
2531
|
+
* E (isolated)
|
|
2532
|
+
* ```
|
|
2533
|
+
*
|
|
2534
|
+
* @param options - Optional display settings.
|
|
2535
|
+
* @param options.showWeight - Whether to show edge weights (default: true).
|
|
2536
|
+
* @returns The visual string.
|
|
2537
|
+
*/
|
|
2538
|
+
toVisual(options) {
|
|
2539
|
+
const showWeight = options?.showWeight ?? true;
|
|
2540
|
+
const vertices = [...this._vertexMap.values()];
|
|
2541
|
+
const vertexCount = vertices.length;
|
|
2542
|
+
const edgeCount = this.edgeSet().length;
|
|
2543
|
+
const lines = [`Graph (${vertexCount} vertices, ${edgeCount} edges):`];
|
|
2544
|
+
for (const vertex of vertices) {
|
|
2545
|
+
const neighbors = this.getNeighbors(vertex);
|
|
2546
|
+
if (neighbors.length === 0) {
|
|
2547
|
+
lines.push(` ${vertex.key} (isolated)`);
|
|
2548
|
+
} else {
|
|
2549
|
+
const edgeStrs = neighbors.map((neighbor) => {
|
|
2550
|
+
const edge = this.getEdge(vertex, neighbor);
|
|
2551
|
+
if (edge && showWeight && edge.weight !== void 0 && edge.weight !== 1) {
|
|
2552
|
+
return `${neighbor.key} (${edge.weight})`;
|
|
2553
|
+
}
|
|
2554
|
+
return `${neighbor.key}`;
|
|
2555
|
+
});
|
|
2556
|
+
lines.push(` ${vertex.key} ${this._edgeConnector} ${edgeStrs.join(", ")}`);
|
|
2557
|
+
}
|
|
2558
|
+
}
|
|
2559
|
+
return lines.join("\n");
|
|
2560
|
+
}
|
|
2561
|
+
/**
|
|
2562
|
+
* Generate DOT language representation for Graphviz.
|
|
2563
|
+
*
|
|
2564
|
+
* @param options - Optional display settings.
|
|
2565
|
+
* @param options.name - Graph name (default: 'G').
|
|
2566
|
+
* @param options.showWeight - Whether to label edges with weight (default: true).
|
|
2567
|
+
* @returns DOT format string.
|
|
2568
|
+
*/
|
|
2569
|
+
toDot(options) {
|
|
2570
|
+
const name = options?.name ?? "G";
|
|
2571
|
+
const showWeight = options?.showWeight ?? true;
|
|
2572
|
+
const isDirected = this._edgeConnector === "->";
|
|
2573
|
+
const graphType = isDirected ? "digraph" : "graph";
|
|
2574
|
+
const edgeOp = isDirected ? "->" : "--";
|
|
2575
|
+
const lines = [`${graphType} ${name} {`];
|
|
2576
|
+
for (const vertex of this._vertexMap.values()) {
|
|
2577
|
+
lines.push(` "${vertex.key}";`);
|
|
2578
|
+
}
|
|
2579
|
+
const visited = /* @__PURE__ */ new Set();
|
|
2580
|
+
for (const vertex of this._vertexMap.values()) {
|
|
2581
|
+
for (const neighbor of this.getNeighbors(vertex)) {
|
|
2582
|
+
const edgeId = isDirected ? `${vertex.key}->${neighbor.key}` : [vertex.key, neighbor.key].sort().join("--");
|
|
2583
|
+
if (visited.has(edgeId)) continue;
|
|
2584
|
+
visited.add(edgeId);
|
|
2585
|
+
const edge = this.getEdge(vertex, neighbor);
|
|
2586
|
+
const label = edge && showWeight && edge.weight !== void 0 && edge.weight !== 1 ? ` [label="${edge.weight}"]` : "";
|
|
2587
|
+
lines.push(` "${vertex.key}" ${edgeOp} "${neighbor.key}"${label};`);
|
|
2588
|
+
}
|
|
2589
|
+
}
|
|
2590
|
+
lines.push("}");
|
|
2591
|
+
return lines.join("\n");
|
|
2592
|
+
}
|
|
2593
|
+
/**
|
|
2594
|
+
* Print the graph to console.
|
|
2595
|
+
* @param options - Display settings passed to `toVisual`.
|
|
2596
|
+
*/
|
|
2597
|
+
print(options) {
|
|
2598
|
+
console.log(this.toVisual(options));
|
|
2599
|
+
}
|
|
3549
2600
|
/**
|
|
3550
2601
|
* Internal iterator over `[key, value]` entries in insertion order.
|
|
3551
2602
|
* @returns Iterator of `[VertexKey, V | undefined]`.
|
|
@@ -3657,92 +2708,6 @@ var AbstractGraph = class extends IterableEntryBase {
|
|
|
3657
2708
|
_getVertexKey(vertexOrKey) {
|
|
3658
2709
|
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|
|
3659
2710
|
}
|
|
3660
|
-
/**
|
|
3661
|
-
* The edge connector string used in visual output.
|
|
3662
|
-
* Override in subclasses (e.g., '--' for undirected, '->' for directed).
|
|
3663
|
-
*/
|
|
3664
|
-
get _edgeConnector() {
|
|
3665
|
-
return "--";
|
|
3666
|
-
}
|
|
3667
|
-
/**
|
|
3668
|
-
* Generate a text-based visual representation of the graph.
|
|
3669
|
-
*
|
|
3670
|
-
* **Adjacency list format:**
|
|
3671
|
-
* ```
|
|
3672
|
-
* Graph (5 vertices, 6 edges):
|
|
3673
|
-
* A -> B (1), C (2)
|
|
3674
|
-
* B -> D (3)
|
|
3675
|
-
* C -> (no outgoing edges)
|
|
3676
|
-
* D -> A (1)
|
|
3677
|
-
* E (isolated)
|
|
3678
|
-
* ```
|
|
3679
|
-
*
|
|
3680
|
-
* @param options - Optional display settings.
|
|
3681
|
-
* @param options.showWeight - Whether to show edge weights (default: true).
|
|
3682
|
-
* @returns The visual string.
|
|
3683
|
-
*/
|
|
3684
|
-
toVisual(options) {
|
|
3685
|
-
const showWeight = options?.showWeight ?? true;
|
|
3686
|
-
const vertices = [...this._vertexMap.values()];
|
|
3687
|
-
const vertexCount = vertices.length;
|
|
3688
|
-
const edgeCount = this.edgeSet().length;
|
|
3689
|
-
const lines = [`Graph (${vertexCount} vertices, ${edgeCount} edges):`];
|
|
3690
|
-
for (const vertex of vertices) {
|
|
3691
|
-
const neighbors = this.getNeighbors(vertex);
|
|
3692
|
-
if (neighbors.length === 0) {
|
|
3693
|
-
lines.push(` ${vertex.key} (isolated)`);
|
|
3694
|
-
} else {
|
|
3695
|
-
const edgeStrs = neighbors.map((neighbor) => {
|
|
3696
|
-
const edge = this.getEdge(vertex, neighbor);
|
|
3697
|
-
if (edge && showWeight && edge.weight !== void 0 && edge.weight !== 1) {
|
|
3698
|
-
return `${neighbor.key} (${edge.weight})`;
|
|
3699
|
-
}
|
|
3700
|
-
return `${neighbor.key}`;
|
|
3701
|
-
});
|
|
3702
|
-
lines.push(` ${vertex.key} ${this._edgeConnector} ${edgeStrs.join(", ")}`);
|
|
3703
|
-
}
|
|
3704
|
-
}
|
|
3705
|
-
return lines.join("\n");
|
|
3706
|
-
}
|
|
3707
|
-
/**
|
|
3708
|
-
* Generate DOT language representation for Graphviz.
|
|
3709
|
-
*
|
|
3710
|
-
* @param options - Optional display settings.
|
|
3711
|
-
* @param options.name - Graph name (default: 'G').
|
|
3712
|
-
* @param options.showWeight - Whether to label edges with weight (default: true).
|
|
3713
|
-
* @returns DOT format string.
|
|
3714
|
-
*/
|
|
3715
|
-
toDot(options) {
|
|
3716
|
-
const name = options?.name ?? "G";
|
|
3717
|
-
const showWeight = options?.showWeight ?? true;
|
|
3718
|
-
const isDirected = this._edgeConnector === "->";
|
|
3719
|
-
const graphType = isDirected ? "digraph" : "graph";
|
|
3720
|
-
const edgeOp = isDirected ? "->" : "--";
|
|
3721
|
-
const lines = [`${graphType} ${name} {`];
|
|
3722
|
-
for (const vertex of this._vertexMap.values()) {
|
|
3723
|
-
lines.push(` "${vertex.key}";`);
|
|
3724
|
-
}
|
|
3725
|
-
const visited = /* @__PURE__ */ new Set();
|
|
3726
|
-
for (const vertex of this._vertexMap.values()) {
|
|
3727
|
-
for (const neighbor of this.getNeighbors(vertex)) {
|
|
3728
|
-
const edgeId = isDirected ? `${vertex.key}->${neighbor.key}` : [vertex.key, neighbor.key].sort().join("--");
|
|
3729
|
-
if (visited.has(edgeId)) continue;
|
|
3730
|
-
visited.add(edgeId);
|
|
3731
|
-
const edge = this.getEdge(vertex, neighbor);
|
|
3732
|
-
const label = edge && showWeight && edge.weight !== void 0 && edge.weight !== 1 ? ` [label="${edge.weight}"]` : "";
|
|
3733
|
-
lines.push(` "${vertex.key}" ${edgeOp} "${neighbor.key}"${label};`);
|
|
3734
|
-
}
|
|
3735
|
-
}
|
|
3736
|
-
lines.push("}");
|
|
3737
|
-
return lines.join("\n");
|
|
3738
|
-
}
|
|
3739
|
-
/**
|
|
3740
|
-
* Print the graph to console.
|
|
3741
|
-
* @param options - Display settings passed to `toVisual`.
|
|
3742
|
-
*/
|
|
3743
|
-
print(options) {
|
|
3744
|
-
console.log(this.toVisual(options));
|
|
3745
|
-
}
|
|
3746
2711
|
};
|
|
3747
2712
|
|
|
3748
2713
|
// src/data-structures/graph/directed-graph.ts
|
|
@@ -3778,9 +2743,6 @@ var DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
3778
2743
|
constructor(options) {
|
|
3779
2744
|
super(options);
|
|
3780
2745
|
}
|
|
3781
|
-
get _edgeConnector() {
|
|
3782
|
-
return "->";
|
|
3783
|
-
}
|
|
3784
2746
|
_outEdgeMap = /* @__PURE__ */ new Map();
|
|
3785
2747
|
get outEdgeMap() {
|
|
3786
2748
|
return this._outEdgeMap;
|
|
@@ -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.
|
|
@@ -3844,59 +2809,20 @@ var DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
3844
2809
|
return new DirectedEdge(src, dest, weight ?? this.options.defaultEdgeWeight ?? 1, value);
|
|
3845
2810
|
}
|
|
3846
2811
|
/**
|
|
3847
|
-
|
|
3848
|
-
|
|
3849
|
-
|
|
3850
|
-
|
|
3851
|
-
|
|
3852
|
-
|
|
3853
|
-
|
|
3854
|
-
|
|
3855
|
-
|
|
3856
|
-
|
|
3857
|
-
|
|
3858
|
-
|
|
3859
|
-
|
|
3860
|
-
|
|
3861
|
-
|
|
3862
|
-
|
|
3863
|
-
|
|
3864
|
-
|
|
3865
|
-
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
|
|
3869
|
-
|
|
3870
|
-
|
|
3871
|
-
|
|
3872
|
-
|
|
3873
|
-
|
|
3874
|
-
|
|
3875
|
-
|
|
3876
|
-
|
|
3877
|
-
|
|
3878
|
-
|
|
3879
|
-
|
|
3880
|
-
|
|
3881
|
-
|
|
3882
|
-
|
|
3883
|
-
|
|
3884
|
-
|
|
3885
|
-
|
|
3886
|
-
|
|
3887
|
-
|
|
3888
|
-
|
|
3889
|
-
|
|
3890
|
-
|
|
3891
|
-
* @example
|
|
3892
|
-
* // Get edge between vertices
|
|
3893
|
-
* const g = new DirectedGraph();
|
|
3894
|
-
* g.addVertex('A');
|
|
3895
|
-
* g.addVertex('B');
|
|
3896
|
-
* g.addEdge('A', 'B', 5);
|
|
3897
|
-
* const edge = g.getEdge('A', 'B');
|
|
3898
|
-
* console.log(edge?.weight); // 5;
|
|
3899
|
-
*/
|
|
2812
|
+
* Get the unique edge from `src` to `dest`, if present.
|
|
2813
|
+
* @param srcOrKey - Source vertex or key.
|
|
2814
|
+
* @param destOrKey - Destination vertex or key.
|
|
2815
|
+
* @returns Edge instance or `undefined`.
|
|
2816
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
2817
|
+
* @example
|
|
2818
|
+
* // Get edge between vertices
|
|
2819
|
+
* const g = new DirectedGraph();
|
|
2820
|
+
* g.addVertex('A');
|
|
2821
|
+
* g.addVertex('B');
|
|
2822
|
+
* g.addEdge('A', 'B', 5);
|
|
2823
|
+
* const edge = g.getEdge('A', 'B');
|
|
2824
|
+
* console.log(edge?.weight); // 5;
|
|
2825
|
+
*/
|
|
3900
2826
|
getEdge(srcOrKey, destOrKey) {
|
|
3901
2827
|
let edgeMap = [];
|
|
3902
2828
|
if (srcOrKey !== void 0 && destOrKey !== void 0) {
|
|
@@ -3936,79 +2862,37 @@ var DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
3936
2862
|
return removed;
|
|
3937
2863
|
}
|
|
3938
2864
|
/**
|
|
3939
|
-
|
|
3940
|
-
|
|
3941
|
-
|
|
3942
|
-
|
|
3943
|
-
|
|
3944
|
-
|
|
3945
|
-
|
|
3946
|
-
|
|
3947
|
-
|
|
3948
|
-
|
|
3949
|
-
|
|
3950
|
-
|
|
3951
|
-
|
|
3952
|
-
|
|
3953
|
-
|
|
3954
|
-
|
|
3955
|
-
|
|
3956
|
-
|
|
3957
|
-
|
|
3958
|
-
|
|
3959
|
-
|
|
3960
|
-
|
|
3961
|
-
|
|
3962
|
-
|
|
3963
|
-
|
|
3964
|
-
|
|
3965
|
-
|
|
3966
|
-
|
|
3967
|
-
|
|
3968
|
-
|
|
3969
|
-
|
|
3970
|
-
|
|
3971
|
-
|
|
3972
|
-
|
|
3973
|
-
|
|
3974
|
-
|
|
3975
|
-
|
|
3976
|
-
|
|
3977
|
-
|
|
3978
|
-
|
|
3979
|
-
|
|
3980
|
-
|
|
3981
|
-
|
|
3982
|
-
|
|
3983
|
-
|
|
3984
|
-
|
|
3985
|
-
|
|
3986
|
-
* @example
|
|
3987
|
-
* // DirectedGraph deleteEdge and vertex operations
|
|
3988
|
-
* const graph = new DirectedGraph<string>();
|
|
3989
|
-
*
|
|
3990
|
-
* // Build a small graph
|
|
3991
|
-
* graph.addVertex('X');
|
|
3992
|
-
* graph.addVertex('Y');
|
|
3993
|
-
* graph.addVertex('Z');
|
|
3994
|
-
* graph.addEdge('X', 'Y', 1);
|
|
3995
|
-
* graph.addEdge('Y', 'Z', 2);
|
|
3996
|
-
*
|
|
3997
|
-
* // Delete an edge
|
|
3998
|
-
* graph.deleteEdgeSrcToDest('X', 'Y');
|
|
3999
|
-
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
4000
|
-
*
|
|
4001
|
-
* // Edge in other direction should not exist
|
|
4002
|
-
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
4003
|
-
*
|
|
4004
|
-
* // Other edges should remain
|
|
4005
|
-
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
4006
|
-
*
|
|
4007
|
-
* // Delete a vertex
|
|
4008
|
-
* graph.deleteVertex('Y');
|
|
4009
|
-
* console.log(graph.hasVertex('Y')); // false;
|
|
4010
|
-
* console.log(graph.size); // 2;
|
|
4011
|
-
*/
|
|
2865
|
+
* Delete an edge by instance or by `(srcKey, destKey)`.
|
|
2866
|
+
* @param edgeOrSrcVertexKey - Edge instance or source vertex/key.
|
|
2867
|
+
* @param destVertexKey - Optional destination vertex/key when deleting by pair.
|
|
2868
|
+
* @returns Removed edge or `undefined`.
|
|
2869
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
2870
|
+
* @example
|
|
2871
|
+
* // DirectedGraph deleteEdge and vertex operations
|
|
2872
|
+
* const graph = new DirectedGraph<string>();
|
|
2873
|
+
*
|
|
2874
|
+
* // Build a small graph
|
|
2875
|
+
* graph.addVertex('X');
|
|
2876
|
+
* graph.addVertex('Y');
|
|
2877
|
+
* graph.addVertex('Z');
|
|
2878
|
+
* graph.addEdge('X', 'Y', 1);
|
|
2879
|
+
* graph.addEdge('Y', 'Z', 2);
|
|
2880
|
+
*
|
|
2881
|
+
* // Delete an edge
|
|
2882
|
+
* graph.deleteEdgeSrcToDest('X', 'Y');
|
|
2883
|
+
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
2884
|
+
*
|
|
2885
|
+
* // Edge in other direction should not exist
|
|
2886
|
+
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
2887
|
+
*
|
|
2888
|
+
* // Other edges should remain
|
|
2889
|
+
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
2890
|
+
*
|
|
2891
|
+
* // Delete a vertex
|
|
2892
|
+
* graph.deleteVertex('Y');
|
|
2893
|
+
* console.log(graph.hasVertex('Y')); // false;
|
|
2894
|
+
* console.log(graph.size); // 2;
|
|
2895
|
+
*/
|
|
4012
2896
|
deleteEdge(edgeOrSrcVertexKey, destVertexKey) {
|
|
4013
2897
|
let removed = void 0;
|
|
4014
2898
|
let src, dest;
|
|
@@ -4036,56 +2920,17 @@ var DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4036
2920
|
return removed;
|
|
4037
2921
|
}
|
|
4038
2922
|
/**
|
|
4039
|
-
|
|
4040
|
-
|
|
4041
|
-
|
|
4042
|
-
|
|
4043
|
-
|
|
4044
|
-
|
|
4045
|
-
|
|
4046
|
-
|
|
4047
|
-
|
|
4048
|
-
|
|
4049
|
-
|
|
4050
|
-
|
|
4051
|
-
|
|
4052
|
-
|
|
4053
|
-
|
|
4054
|
-
|
|
4055
|
-
|
|
4056
|
-
|
|
4057
|
-
|
|
4058
|
-
|
|
4059
|
-
|
|
4060
|
-
|
|
4061
|
-
|
|
4062
|
-
|
|
4063
|
-
|
|
4064
|
-
|
|
4065
|
-
|
|
4066
|
-
|
|
4067
|
-
|
|
4068
|
-
|
|
4069
|
-
|
|
4070
|
-
|
|
4071
|
-
|
|
4072
|
-
|
|
4073
|
-
|
|
4074
|
-
|
|
4075
|
-
|
|
4076
|
-
|
|
4077
|
-
|
|
4078
|
-
|
|
4079
|
-
* @example
|
|
4080
|
-
* // Remove a vertex
|
|
4081
|
-
* const g = new DirectedGraph();
|
|
4082
|
-
* g.addVertex('A');
|
|
4083
|
-
* g.addVertex('B');
|
|
4084
|
-
* g.addEdge('A', 'B');
|
|
4085
|
-
* g.deleteVertex('A');
|
|
4086
|
-
* console.log(g.hasVertex('A')); // false;
|
|
4087
|
-
* console.log(g.hasEdge('A', 'B')); // false;
|
|
4088
|
-
*/
|
|
2923
|
+
* Remove a vertex
|
|
2924
|
+
* @example
|
|
2925
|
+
* // Remove a vertex
|
|
2926
|
+
* const g = new DirectedGraph();
|
|
2927
|
+
* g.addVertex('A');
|
|
2928
|
+
* g.addVertex('B');
|
|
2929
|
+
* g.addEdge('A', 'B');
|
|
2930
|
+
* g.deleteVertex('A');
|
|
2931
|
+
* console.log(g.hasVertex('A')); // false;
|
|
2932
|
+
* console.log(g.hasEdge('A', 'B')); // false;
|
|
2933
|
+
*/
|
|
4089
2934
|
deleteVertex(vertexOrKey) {
|
|
4090
2935
|
let vertexKey;
|
|
4091
2936
|
let vertex;
|
|
@@ -4117,59 +2962,20 @@ var DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4117
2962
|
return removed;
|
|
4118
2963
|
}
|
|
4119
2964
|
/**
|
|
4120
|
-
|
|
4121
|
-
|
|
4122
|
-
|
|
4123
|
-
|
|
4124
|
-
|
|
4125
|
-
|
|
4126
|
-
|
|
4127
|
-
|
|
4128
|
-
|
|
4129
|
-
|
|
4130
|
-
|
|
4131
|
-
|
|
4132
|
-
|
|
4133
|
-
|
|
4134
|
-
|
|
4135
|
-
|
|
4136
|
-
|
|
4137
|
-
|
|
4138
|
-
|
|
4139
|
-
|
|
4140
|
-
|
|
4141
|
-
|
|
4142
|
-
|
|
4143
|
-
|
|
4144
|
-
|
|
4145
|
-
|
|
4146
|
-
|
|
4147
|
-
|
|
4148
|
-
|
|
4149
|
-
|
|
4150
|
-
|
|
4151
|
-
|
|
4152
|
-
|
|
4153
|
-
|
|
4154
|
-
|
|
4155
|
-
|
|
4156
|
-
|
|
4157
|
-
|
|
4158
|
-
|
|
4159
|
-
|
|
4160
|
-
|
|
4161
|
-
|
|
4162
|
-
|
|
4163
|
-
* @example
|
|
4164
|
-
* // Get incoming edges
|
|
4165
|
-
* const g = new DirectedGraph();
|
|
4166
|
-
* g.addVertex('A');
|
|
4167
|
-
* g.addVertex('B');
|
|
4168
|
-
* g.addVertex('C');
|
|
4169
|
-
* g.addEdge('A', 'C');
|
|
4170
|
-
* g.addEdge('B', 'C');
|
|
4171
|
-
* console.log(g.incomingEdgesOf('C').length); // 2;
|
|
4172
|
-
*/
|
|
2965
|
+
* Incoming edges of a vertex.
|
|
2966
|
+
* @param vertexOrKey - Vertex or key.
|
|
2967
|
+
* @returns Array of incoming edges.
|
|
2968
|
+
* @remarks Time O(deg_in), Space O(deg_in)
|
|
2969
|
+
* @example
|
|
2970
|
+
* // Get incoming edges
|
|
2971
|
+
* const g = new DirectedGraph();
|
|
2972
|
+
* g.addVertex('A');
|
|
2973
|
+
* g.addVertex('B');
|
|
2974
|
+
* g.addVertex('C');
|
|
2975
|
+
* g.addEdge('A', 'C');
|
|
2976
|
+
* g.addEdge('B', 'C');
|
|
2977
|
+
* console.log(g.incomingEdgesOf('C').length); // 2;
|
|
2978
|
+
*/
|
|
4173
2979
|
incomingEdgesOf(vertexOrKey) {
|
|
4174
2980
|
const target = this._getVertex(vertexOrKey);
|
|
4175
2981
|
if (target) {
|
|
@@ -4178,59 +2984,20 @@ var DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4178
2984
|
return [];
|
|
4179
2985
|
}
|
|
4180
2986
|
/**
|
|
4181
|
-
|
|
4182
|
-
|
|
4183
|
-
|
|
4184
|
-
|
|
4185
|
-
|
|
4186
|
-
|
|
4187
|
-
|
|
4188
|
-
|
|
4189
|
-
|
|
4190
|
-
|
|
4191
|
-
|
|
4192
|
-
|
|
4193
|
-
|
|
4194
|
-
|
|
4195
|
-
|
|
4196
|
-
|
|
4197
|
-
|
|
4198
|
-
|
|
4199
|
-
|
|
4200
|
-
|
|
4201
|
-
|
|
4202
|
-
|
|
4203
|
-
|
|
4204
|
-
|
|
4205
|
-
|
|
4206
|
-
|
|
4207
|
-
|
|
4208
|
-
|
|
4209
|
-
|
|
4210
|
-
|
|
4211
|
-
|
|
4212
|
-
|
|
4213
|
-
|
|
4214
|
-
|
|
4215
|
-
|
|
4216
|
-
|
|
4217
|
-
|
|
4218
|
-
|
|
4219
|
-
|
|
4220
|
-
|
|
4221
|
-
|
|
4222
|
-
|
|
4223
|
-
|
|
4224
|
-
* @example
|
|
4225
|
-
* // Get outgoing edges
|
|
4226
|
-
* const g = new DirectedGraph();
|
|
4227
|
-
* g.addVertex('A');
|
|
4228
|
-
* g.addVertex('B');
|
|
4229
|
-
* g.addVertex('C');
|
|
4230
|
-
* g.addEdge('A', 'B');
|
|
4231
|
-
* g.addEdge('A', 'C');
|
|
4232
|
-
* console.log(g.outgoingEdgesOf('A').length); // 2;
|
|
4233
|
-
*/
|
|
2987
|
+
* Outgoing edges of a vertex.
|
|
2988
|
+
* @param vertexOrKey - Vertex or key.
|
|
2989
|
+
* @returns Array of outgoing edges.
|
|
2990
|
+
* @remarks Time O(deg_out), Space O(deg_out)
|
|
2991
|
+
* @example
|
|
2992
|
+
* // Get outgoing edges
|
|
2993
|
+
* const g = new DirectedGraph();
|
|
2994
|
+
* g.addVertex('A');
|
|
2995
|
+
* g.addVertex('B');
|
|
2996
|
+
* g.addVertex('C');
|
|
2997
|
+
* g.addEdge('A', 'B');
|
|
2998
|
+
* g.addEdge('A', 'C');
|
|
2999
|
+
* console.log(g.outgoingEdgesOf('A').length); // 2;
|
|
3000
|
+
*/
|
|
4234
3001
|
outgoingEdgesOf(vertexOrKey) {
|
|
4235
3002
|
const target = this._getVertex(vertexOrKey);
|
|
4236
3003
|
if (target) {
|
|
@@ -4289,75 +3056,33 @@ var DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4289
3056
|
return destinations;
|
|
4290
3057
|
}
|
|
4291
3058
|
/**
|
|
4292
|
-
|
|
4293
|
-
|
|
4294
|
-
|
|
4295
|
-
|
|
4296
|
-
|
|
4297
|
-
|
|
4298
|
-
|
|
4299
|
-
|
|
4300
|
-
|
|
4301
|
-
|
|
4302
|
-
|
|
4303
|
-
|
|
4304
|
-
|
|
4305
|
-
|
|
4306
|
-
|
|
4307
|
-
|
|
4308
|
-
|
|
4309
|
-
|
|
4310
|
-
|
|
4311
|
-
|
|
4312
|
-
|
|
4313
|
-
|
|
4314
|
-
|
|
4315
|
-
|
|
4316
|
-
|
|
4317
|
-
|
|
4318
|
-
|
|
4319
|
-
|
|
4320
|
-
|
|
4321
|
-
|
|
4322
|
-
|
|
4323
|
-
|
|
4324
|
-
|
|
4325
|
-
|
|
4326
|
-
|
|
4327
|
-
|
|
4328
|
-
|
|
4329
|
-
|
|
4330
|
-
|
|
4331
|
-
|
|
4332
|
-
|
|
4333
|
-
|
|
4334
|
-
|
|
4335
|
-
|
|
4336
|
-
|
|
4337
|
-
|
|
4338
|
-
* @example
|
|
4339
|
-
* // DirectedGraph topologicalSort for task scheduling
|
|
4340
|
-
* const graph = new DirectedGraph<string>();
|
|
4341
|
-
*
|
|
4342
|
-
* // Build a DAG (Directed Acyclic Graph) for task dependencies
|
|
4343
|
-
* graph.addVertex('Design');
|
|
4344
|
-
* graph.addVertex('Implement');
|
|
4345
|
-
* graph.addVertex('Test');
|
|
4346
|
-
* graph.addVertex('Deploy');
|
|
4347
|
-
*
|
|
4348
|
-
* // Add dependency edges
|
|
4349
|
-
* graph.addEdge('Design', 'Implement', 1); // Design must come before Implement
|
|
4350
|
-
* graph.addEdge('Implement', 'Test', 1); // Implement must come before Test
|
|
4351
|
-
* graph.addEdge('Test', 'Deploy', 1); // Test must come before Deploy
|
|
4352
|
-
*
|
|
4353
|
-
* // Topological sort gives valid execution order
|
|
4354
|
-
* const executionOrder = graph.topologicalSort();
|
|
4355
|
-
* console.log(executionOrder); // defined;
|
|
4356
|
-
* console.log(executionOrder); // ['Design', 'Implement', 'Test', 'Deploy'];
|
|
4357
|
-
*
|
|
4358
|
-
* // All vertices should be included
|
|
4359
|
-
* console.log(executionOrder?.length); // 4;
|
|
4360
|
-
*/
|
|
3059
|
+
* Topological sort if DAG; returns `undefined` if a cycle exists.
|
|
3060
|
+
* @param propertyName - `'key'` to map to keys; `'vertex'` to keep instances.
|
|
3061
|
+
* @returns Array of keys/vertices, or `undefined` when cycle is found.
|
|
3062
|
+
* @remarks Time O(V + E), Space O(V)
|
|
3063
|
+
* @example
|
|
3064
|
+
* // DirectedGraph topologicalSort for task scheduling
|
|
3065
|
+
* const graph = new DirectedGraph<string>();
|
|
3066
|
+
*
|
|
3067
|
+
* // Build a DAG (Directed Acyclic Graph) for task dependencies
|
|
3068
|
+
* graph.addVertex('Design');
|
|
3069
|
+
* graph.addVertex('Implement');
|
|
3070
|
+
* graph.addVertex('Test');
|
|
3071
|
+
* graph.addVertex('Deploy');
|
|
3072
|
+
*
|
|
3073
|
+
* // Add dependency edges
|
|
3074
|
+
* graph.addEdge('Design', 'Implement', 1); // Design must come before Implement
|
|
3075
|
+
* graph.addEdge('Implement', 'Test', 1); // Implement must come before Test
|
|
3076
|
+
* graph.addEdge('Test', 'Deploy', 1); // Test must come before Deploy
|
|
3077
|
+
*
|
|
3078
|
+
* // Topological sort gives valid execution order
|
|
3079
|
+
* const executionOrder = graph.topologicalSort();
|
|
3080
|
+
* console.log(executionOrder); // defined;
|
|
3081
|
+
* console.log(executionOrder); // ['Design', 'Implement', 'Test', 'Deploy'];
|
|
3082
|
+
*
|
|
3083
|
+
* // All vertices should be included
|
|
3084
|
+
* console.log(executionOrder?.length); // 4;
|
|
3085
|
+
*/
|
|
4361
3086
|
topologicalSort(propertyName) {
|
|
4362
3087
|
propertyName = propertyName ?? "key";
|
|
4363
3088
|
const statusMap = /* @__PURE__ */ new Map();
|
|
@@ -4390,54 +3115,15 @@ var DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4390
3115
|
return sorted.reverse();
|
|
4391
3116
|
}
|
|
4392
3117
|
/**
|
|
4393
|
-
|
|
4394
|
-
|
|
4395
|
-
|
|
4396
|
-
|
|
4397
|
-
|
|
4398
|
-
|
|
4399
|
-
|
|
4400
|
-
|
|
4401
|
-
|
|
4402
|
-
|
|
4403
|
-
|
|
4404
|
-
|
|
4405
|
-
|
|
4406
|
-
|
|
4407
|
-
|
|
4408
|
-
|
|
4409
|
-
|
|
4410
|
-
|
|
4411
|
-
|
|
4412
|
-
|
|
4413
|
-
|
|
4414
|
-
|
|
4415
|
-
|
|
4416
|
-
|
|
4417
|
-
|
|
4418
|
-
|
|
4419
|
-
|
|
4420
|
-
|
|
4421
|
-
|
|
4422
|
-
|
|
4423
|
-
|
|
4424
|
-
|
|
4425
|
-
|
|
4426
|
-
|
|
4427
|
-
|
|
4428
|
-
|
|
4429
|
-
|
|
4430
|
-
|
|
4431
|
-
|
|
4432
|
-
|
|
4433
|
-
* @example
|
|
4434
|
-
* // Get all edges
|
|
4435
|
-
* const g = new DirectedGraph();
|
|
4436
|
-
* g.addVertex('A');
|
|
4437
|
-
* g.addVertex('B');
|
|
4438
|
-
* g.addEdge('A', 'B', 3);
|
|
4439
|
-
* console.log(g.edgeSet().length); // 1;
|
|
4440
|
-
*/
|
|
3118
|
+
* Get all edges
|
|
3119
|
+
* @example
|
|
3120
|
+
* // Get all edges
|
|
3121
|
+
* const g = new DirectedGraph();
|
|
3122
|
+
* g.addVertex('A');
|
|
3123
|
+
* g.addVertex('B');
|
|
3124
|
+
* g.addEdge('A', 'B', 3);
|
|
3125
|
+
* console.log(g.edgeSet().length); // 1;
|
|
3126
|
+
*/
|
|
4441
3127
|
edgeSet() {
|
|
4442
3128
|
let edgeMap = [];
|
|
4443
3129
|
this._outEdgeMap.forEach((outEdges) => {
|
|
@@ -4446,58 +3132,18 @@ var DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4446
3132
|
return edgeMap;
|
|
4447
3133
|
}
|
|
4448
3134
|
/**
|
|
4449
|
-
|
|
4450
|
-
|
|
4451
|
-
|
|
4452
|
-
|
|
4453
|
-
|
|
4454
|
-
|
|
4455
|
-
|
|
4456
|
-
|
|
4457
|
-
|
|
4458
|
-
|
|
4459
|
-
|
|
4460
|
-
|
|
4461
|
-
|
|
4462
|
-
|
|
4463
|
-
|
|
4464
|
-
|
|
4465
|
-
|
|
4466
|
-
|
|
4467
|
-
|
|
4468
|
-
|
|
4469
|
-
|
|
4470
|
-
|
|
4471
|
-
|
|
4472
|
-
|
|
4473
|
-
|
|
4474
|
-
|
|
4475
|
-
|
|
4476
|
-
|
|
4477
|
-
|
|
4478
|
-
|
|
4479
|
-
|
|
4480
|
-
|
|
4481
|
-
|
|
4482
|
-
|
|
4483
|
-
|
|
4484
|
-
|
|
4485
|
-
|
|
4486
|
-
|
|
4487
|
-
|
|
4488
|
-
|
|
4489
|
-
|
|
4490
|
-
* @example
|
|
4491
|
-
* // Get outgoing neighbors
|
|
4492
|
-
* const g = new DirectedGraph();
|
|
4493
|
-
* g.addVertex('A');
|
|
4494
|
-
* g.addVertex('B');
|
|
4495
|
-
* g.addVertex('C');
|
|
4496
|
-
* g.addEdge('A', 'B');
|
|
4497
|
-
* g.addEdge('A', 'C');
|
|
4498
|
-
* const neighbors = g.getNeighbors('A');
|
|
4499
|
-
* console.log(neighbors.map(v => v.key).sort()); // ['B', 'C'];
|
|
4500
|
-
*/
|
|
3135
|
+
* Get outgoing neighbors
|
|
3136
|
+
* @example
|
|
3137
|
+
* // Get outgoing neighbors
|
|
3138
|
+
* const g = new DirectedGraph();
|
|
3139
|
+
* g.addVertex('A');
|
|
3140
|
+
* g.addVertex('B');
|
|
3141
|
+
* g.addVertex('C');
|
|
3142
|
+
* g.addEdge('A', 'B');
|
|
3143
|
+
* g.addEdge('A', 'C');
|
|
3144
|
+
* const neighbors = g.getNeighbors('A');
|
|
3145
|
+
* console.log(neighbors.map(v => v.key).sort()); // ['B', 'C'];
|
|
3146
|
+
*/
|
|
4501
3147
|
getNeighbors(vertexOrKey) {
|
|
4502
3148
|
const neighbors = [];
|
|
4503
3149
|
const vertex = this._getVertex(vertexOrKey);
|
|
@@ -4555,62 +3201,23 @@ var DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4555
3201
|
return super.clone();
|
|
4556
3202
|
}
|
|
4557
3203
|
/**
|
|
4558
|
-
|
|
4559
|
-
|
|
4560
|
-
|
|
4561
|
-
|
|
4562
|
-
|
|
4563
|
-
|
|
4564
|
-
|
|
4565
|
-
|
|
4566
|
-
|
|
4567
|
-
|
|
4568
|
-
|
|
4569
|
-
|
|
4570
|
-
|
|
4571
|
-
|
|
4572
|
-
|
|
4573
|
-
|
|
4574
|
-
|
|
4575
|
-
|
|
4576
|
-
|
|
4577
|
-
|
|
4578
|
-
|
|
4579
|
-
|
|
4580
|
-
|
|
4581
|
-
|
|
4582
|
-
|
|
4583
|
-
|
|
4584
|
-
|
|
4585
|
-
|
|
4586
|
-
|
|
4587
|
-
|
|
4588
|
-
|
|
4589
|
-
|
|
4590
|
-
|
|
4591
|
-
|
|
4592
|
-
|
|
4593
|
-
|
|
4594
|
-
|
|
4595
|
-
|
|
4596
|
-
|
|
4597
|
-
|
|
4598
|
-
|
|
4599
|
-
|
|
4600
|
-
* @example
|
|
4601
|
-
* // Find strongly connected components
|
|
4602
|
-
* const g = new DirectedGraph();
|
|
4603
|
-
* g.addVertex('A');
|
|
4604
|
-
* g.addVertex('B');
|
|
4605
|
-
* g.addVertex('C');
|
|
4606
|
-
* g.addEdge('A', 'B');
|
|
4607
|
-
* g.addEdge('B', 'C');
|
|
4608
|
-
* g.addEdge('C', 'A');
|
|
4609
|
-
* const { SCCs } = g.tarjan();
|
|
4610
|
-
* // A→B→C→A forms one SCC with 3 members
|
|
4611
|
-
* const sccArrays = [...SCCs.values()];
|
|
4612
|
-
* console.log(sccArrays.some(scc => scc.length === 3)); // true;
|
|
4613
|
-
*/
|
|
3204
|
+
* Tarjan's algorithm for strongly connected components.
|
|
3205
|
+
* @returns `{ dfnMap, lowMap, SCCs }`.
|
|
3206
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
3207
|
+
* @example
|
|
3208
|
+
* // Find strongly connected components
|
|
3209
|
+
* const g = new DirectedGraph();
|
|
3210
|
+
* g.addVertex('A');
|
|
3211
|
+
* g.addVertex('B');
|
|
3212
|
+
* g.addVertex('C');
|
|
3213
|
+
* g.addEdge('A', 'B');
|
|
3214
|
+
* g.addEdge('B', 'C');
|
|
3215
|
+
* g.addEdge('C', 'A');
|
|
3216
|
+
* const { SCCs } = g.tarjan();
|
|
3217
|
+
* // A→B→C→A forms one SCC with 3 members
|
|
3218
|
+
* const sccArrays = [...SCCs.values()];
|
|
3219
|
+
* console.log(sccArrays.some(scc => scc.length === 3)); // true;
|
|
3220
|
+
*/
|
|
4614
3221
|
tarjan() {
|
|
4615
3222
|
const dfnMap = /* @__PURE__ */ new Map();
|
|
4616
3223
|
const lowMap = /* @__PURE__ */ new Map();
|
|
@@ -4668,60 +3275,21 @@ var DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
|
4668
3275
|
return this.tarjan().lowMap;
|
|
4669
3276
|
}
|
|
4670
3277
|
/**
|
|
4671
|
-
|
|
4672
|
-
|
|
4673
|
-
|
|
4674
|
-
|
|
4675
|
-
|
|
4676
|
-
|
|
4677
|
-
|
|
4678
|
-
|
|
4679
|
-
|
|
4680
|
-
|
|
4681
|
-
|
|
4682
|
-
|
|
4683
|
-
|
|
4684
|
-
|
|
4685
|
-
|
|
4686
|
-
|
|
4687
|
-
|
|
4688
|
-
|
|
4689
|
-
|
|
4690
|
-
|
|
4691
|
-
|
|
4692
|
-
|
|
4693
|
-
|
|
4694
|
-
|
|
4695
|
-
|
|
4696
|
-
|
|
4697
|
-
|
|
4698
|
-
|
|
4699
|
-
|
|
4700
|
-
|
|
4701
|
-
|
|
4702
|
-
|
|
4703
|
-
|
|
4704
|
-
|
|
4705
|
-
|
|
4706
|
-
|
|
4707
|
-
|
|
4708
|
-
|
|
4709
|
-
|
|
4710
|
-
|
|
4711
|
-
|
|
4712
|
-
|
|
4713
|
-
* @example
|
|
4714
|
-
* // Get strongly connected components
|
|
4715
|
-
* const g = new DirectedGraph();
|
|
4716
|
-
* g.addVertex(1);
|
|
4717
|
-
* g.addVertex(2);
|
|
4718
|
-
* g.addVertex(3);
|
|
4719
|
-
* g.addEdge(1, 2);
|
|
4720
|
-
* g.addEdge(2, 3);
|
|
4721
|
-
* g.addEdge(3, 1);
|
|
4722
|
-
* const sccs = g.getSCCs(); // Map<number, VO[]>
|
|
4723
|
-
* console.log(sccs.size); // >= 1;
|
|
4724
|
-
*/
|
|
3278
|
+
* Strongly connected components computed by `tarjan()`.
|
|
3279
|
+
* @returns Map from SCC id to vertices.
|
|
3280
|
+
* @remarks Time O(#SCC + V), Space O(V)
|
|
3281
|
+
* @example
|
|
3282
|
+
* // Get strongly connected components
|
|
3283
|
+
* const g = new DirectedGraph();
|
|
3284
|
+
* g.addVertex(1);
|
|
3285
|
+
* g.addVertex(2);
|
|
3286
|
+
* g.addVertex(3);
|
|
3287
|
+
* g.addEdge(1, 2);
|
|
3288
|
+
* g.addEdge(2, 3);
|
|
3289
|
+
* g.addEdge(3, 1);
|
|
3290
|
+
* const sccs = g.getSCCs(); // Map<number, VO[]>
|
|
3291
|
+
* console.log(sccs.size); // >= 1;
|
|
3292
|
+
*/
|
|
4725
3293
|
getSCCs() {
|
|
4726
3294
|
return this.tarjan().SCCs;
|
|
4727
3295
|
}
|
|
@@ -4845,58 +3413,19 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
4845
3413
|
return new UndirectedEdge(v1, v2, weight ?? this.options.defaultEdgeWeight ?? 1, value);
|
|
4846
3414
|
}
|
|
4847
3415
|
/**
|
|
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
|
-
|
|
4892
|
-
* @example
|
|
4893
|
-
* // Get edge between vertices
|
|
4894
|
-
* const g = new UndirectedGraph();
|
|
4895
|
-
* g.addVertex('A');
|
|
4896
|
-
* g.addVertex('B');
|
|
4897
|
-
* g.addEdge('A', 'B', 7);
|
|
4898
|
-
* console.log(g.getEdge('A', 'B')?.weight); // 7;
|
|
4899
|
-
*/
|
|
3416
|
+
* Get an undirected edge between two vertices, if present.
|
|
3417
|
+
* @param v1 - One vertex or key.
|
|
3418
|
+
* @param v2 - The other vertex or key.
|
|
3419
|
+
* @returns Edge instance or `undefined`.
|
|
3420
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
3421
|
+
* @example
|
|
3422
|
+
* // Get edge between vertices
|
|
3423
|
+
* const g = new UndirectedGraph();
|
|
3424
|
+
* g.addVertex('A');
|
|
3425
|
+
* g.addVertex('B');
|
|
3426
|
+
* g.addEdge('A', 'B', 7);
|
|
3427
|
+
* console.log(g.getEdge('A', 'B')?.weight); // 7;
|
|
3428
|
+
*/
|
|
4900
3429
|
getEdge(v1, v2) {
|
|
4901
3430
|
let edgeMap = [];
|
|
4902
3431
|
if (v1 !== void 0 && v2 !== void 0) {
|
|
@@ -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;
|
|
@@ -5134,56 +3582,17 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
5134
3582
|
}
|
|
5135
3583
|
}
|
|
5136
3584
|
/**
|
|
5137
|
-
|
|
5138
|
-
|
|
5139
|
-
|
|
5140
|
-
|
|
5141
|
-
|
|
5142
|
-
|
|
5143
|
-
|
|
5144
|
-
|
|
5145
|
-
|
|
5146
|
-
|
|
5147
|
-
|
|
5148
|
-
|
|
5149
|
-
|
|
5150
|
-
|
|
5151
|
-
|
|
5152
|
-
|
|
5153
|
-
|
|
5154
|
-
|
|
5155
|
-
|
|
5156
|
-
|
|
5157
|
-
|
|
5158
|
-
|
|
5159
|
-
|
|
5160
|
-
|
|
5161
|
-
|
|
5162
|
-
|
|
5163
|
-
|
|
5164
|
-
|
|
5165
|
-
|
|
5166
|
-
|
|
5167
|
-
|
|
5168
|
-
|
|
5169
|
-
|
|
5170
|
-
|
|
5171
|
-
|
|
5172
|
-
|
|
5173
|
-
|
|
5174
|
-
|
|
5175
|
-
|
|
5176
|
-
|
|
5177
|
-
|
|
5178
|
-
|
|
5179
|
-
* @example
|
|
5180
|
-
* // Get all edges
|
|
5181
|
-
* const g = new UndirectedGraph();
|
|
5182
|
-
* g.addVertex('A');
|
|
5183
|
-
* g.addVertex('B');
|
|
5184
|
-
* g.addEdge('A', 'B');
|
|
5185
|
-
* console.log(g.edgeSet().length); // 1;
|
|
5186
|
-
*/
|
|
3585
|
+
* Unique set of undirected edges across endpoints.
|
|
3586
|
+
* @returns Array of edges.
|
|
3587
|
+
* @remarks Time O(E), Space O(E)
|
|
3588
|
+
* @example
|
|
3589
|
+
* // Get all edges
|
|
3590
|
+
* const g = new UndirectedGraph();
|
|
3591
|
+
* g.addVertex('A');
|
|
3592
|
+
* g.addVertex('B');
|
|
3593
|
+
* g.addEdge('A', 'B');
|
|
3594
|
+
* console.log(g.edgeSet().length); // 1;
|
|
3595
|
+
*/
|
|
5187
3596
|
edgeSet() {
|
|
5188
3597
|
const edgeSet = /* @__PURE__ */ new Set();
|
|
5189
3598
|
this._edgeMap.forEach((edgeMap) => {
|
|
@@ -5194,82 +3603,40 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
5194
3603
|
return [...edgeSet];
|
|
5195
3604
|
}
|
|
5196
3605
|
/**
|
|
5197
|
-
|
|
5198
|
-
|
|
5199
|
-
|
|
5200
|
-
|
|
5201
|
-
|
|
5202
|
-
|
|
5203
|
-
|
|
5204
|
-
|
|
5205
|
-
|
|
5206
|
-
|
|
5207
|
-
|
|
5208
|
-
|
|
5209
|
-
|
|
5210
|
-
|
|
5211
|
-
|
|
5212
|
-
|
|
5213
|
-
|
|
5214
|
-
|
|
5215
|
-
|
|
5216
|
-
|
|
5217
|
-
|
|
5218
|
-
|
|
5219
|
-
|
|
5220
|
-
|
|
5221
|
-
|
|
5222
|
-
|
|
5223
|
-
|
|
5224
|
-
|
|
5225
|
-
|
|
5226
|
-
|
|
5227
|
-
|
|
5228
|
-
|
|
5229
|
-
|
|
5230
|
-
|
|
5231
|
-
|
|
5232
|
-
|
|
5233
|
-
|
|
5234
|
-
|
|
5235
|
-
|
|
5236
|
-
|
|
5237
|
-
|
|
5238
|
-
|
|
5239
|
-
|
|
5240
|
-
* @example
|
|
5241
|
-
* // UndirectedGraph connectivity and neighbors
|
|
5242
|
-
* const graph = new UndirectedGraph<string>();
|
|
5243
|
-
*
|
|
5244
|
-
* // Build a friendship network
|
|
5245
|
-
* const people = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'];
|
|
5246
|
-
* for (const person of people) {
|
|
5247
|
-
* graph.addVertex(person);
|
|
5248
|
-
* }
|
|
5249
|
-
*
|
|
5250
|
-
* // Add friendships (undirected edges)
|
|
5251
|
-
* graph.addEdge('Alice', 'Bob', 1);
|
|
5252
|
-
* graph.addEdge('Alice', 'Charlie', 1);
|
|
5253
|
-
* graph.addEdge('Bob', 'Diana', 1);
|
|
5254
|
-
* graph.addEdge('Charlie', 'Eve', 1);
|
|
5255
|
-
* graph.addEdge('Diana', 'Eve', 1);
|
|
5256
|
-
*
|
|
5257
|
-
* // Get friends of each person
|
|
5258
|
-
* const aliceFriends = graph.getNeighbors('Alice');
|
|
5259
|
-
* console.log(aliceFriends[0].key); // 'Bob';
|
|
5260
|
-
* console.log(aliceFriends[1].key); // 'Charlie';
|
|
5261
|
-
* console.log(aliceFriends.length); // 2;
|
|
5262
|
-
*
|
|
5263
|
-
* const dianaFriends = graph.getNeighbors('Diana');
|
|
5264
|
-
* console.log(dianaFriends[0].key); // 'Bob';
|
|
5265
|
-
* console.log(dianaFriends[1].key); // 'Eve';
|
|
5266
|
-
* console.log(dianaFriends.length); // 2;
|
|
5267
|
-
*
|
|
5268
|
-
* // Verify bidirectional friendship
|
|
5269
|
-
* const bobFriends = graph.getNeighbors('Bob');
|
|
5270
|
-
* console.log(bobFriends[0].key); // 'Alice'; // Alice -> Bob -> Alice ✓
|
|
5271
|
-
* console.log(bobFriends[1].key); // 'Diana';
|
|
5272
|
-
*/
|
|
3606
|
+
* UndirectedGraph connectivity and neighbors
|
|
3607
|
+
* @example
|
|
3608
|
+
* // UndirectedGraph connectivity and neighbors
|
|
3609
|
+
* const graph = new UndirectedGraph<string>();
|
|
3610
|
+
*
|
|
3611
|
+
* // Build a friendship network
|
|
3612
|
+
* const people = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'];
|
|
3613
|
+
* for (const person of people) {
|
|
3614
|
+
* graph.addVertex(person);
|
|
3615
|
+
* }
|
|
3616
|
+
*
|
|
3617
|
+
* // Add friendships (undirected edges)
|
|
3618
|
+
* graph.addEdge('Alice', 'Bob', 1);
|
|
3619
|
+
* graph.addEdge('Alice', 'Charlie', 1);
|
|
3620
|
+
* graph.addEdge('Bob', 'Diana', 1);
|
|
3621
|
+
* graph.addEdge('Charlie', 'Eve', 1);
|
|
3622
|
+
* graph.addEdge('Diana', 'Eve', 1);
|
|
3623
|
+
*
|
|
3624
|
+
* // Get friends of each person
|
|
3625
|
+
* const aliceFriends = graph.getNeighbors('Alice');
|
|
3626
|
+
* console.log(aliceFriends[0].key); // 'Bob';
|
|
3627
|
+
* console.log(aliceFriends[1].key); // 'Charlie';
|
|
3628
|
+
* console.log(aliceFriends.length); // 2;
|
|
3629
|
+
*
|
|
3630
|
+
* const dianaFriends = graph.getNeighbors('Diana');
|
|
3631
|
+
* console.log(dianaFriends[0].key); // 'Bob';
|
|
3632
|
+
* console.log(dianaFriends[1].key); // 'Eve';
|
|
3633
|
+
* console.log(dianaFriends.length); // 2;
|
|
3634
|
+
*
|
|
3635
|
+
* // Verify bidirectional friendship
|
|
3636
|
+
* const bobFriends = graph.getNeighbors('Bob');
|
|
3637
|
+
* console.log(bobFriends[0].key); // 'Alice'; // Alice -> Bob -> Alice ✓
|
|
3638
|
+
* console.log(bobFriends[1].key); // 'Diana';
|
|
3639
|
+
*/
|
|
5273
3640
|
getNeighbors(vertexOrKey) {
|
|
5274
3641
|
const neighbors = [];
|
|
5275
3642
|
const vertex = this._getVertex(vertexOrKey);
|
|
@@ -5326,59 +3693,20 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
5326
3693
|
return super.clone();
|
|
5327
3694
|
}
|
|
5328
3695
|
/**
|
|
5329
|
-
|
|
5330
|
-
|
|
5331
|
-
|
|
5332
|
-
|
|
5333
|
-
|
|
5334
|
-
|
|
5335
|
-
|
|
5336
|
-
|
|
5337
|
-
|
|
5338
|
-
|
|
5339
|
-
|
|
5340
|
-
|
|
5341
|
-
|
|
5342
|
-
|
|
5343
|
-
|
|
5344
|
-
|
|
5345
|
-
|
|
5346
|
-
|
|
5347
|
-
|
|
5348
|
-
|
|
5349
|
-
|
|
5350
|
-
|
|
5351
|
-
|
|
5352
|
-
|
|
5353
|
-
|
|
5354
|
-
|
|
5355
|
-
|
|
5356
|
-
|
|
5357
|
-
|
|
5358
|
-
|
|
5359
|
-
|
|
5360
|
-
|
|
5361
|
-
|
|
5362
|
-
|
|
5363
|
-
|
|
5364
|
-
|
|
5365
|
-
|
|
5366
|
-
|
|
5367
|
-
|
|
5368
|
-
|
|
5369
|
-
|
|
5370
|
-
|
|
5371
|
-
* @example
|
|
5372
|
-
* // Find articulation points and bridges
|
|
5373
|
-
* const g = new UndirectedGraph();
|
|
5374
|
-
* g.addVertex('A');
|
|
5375
|
-
* g.addVertex('B');
|
|
5376
|
-
* g.addVertex('C');
|
|
5377
|
-
* g.addEdge('A', 'B');
|
|
5378
|
-
* g.addEdge('B', 'C');
|
|
5379
|
-
* const result = g.tarjan();
|
|
5380
|
-
* console.log(result); // defined;
|
|
5381
|
-
*/
|
|
3696
|
+
* Tarjan-based bridge and articulation point detection.
|
|
3697
|
+
* @returns `{ dfnMap, lowMap, bridges, cutVertices }`.
|
|
3698
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
3699
|
+
* @example
|
|
3700
|
+
* // Find articulation points and bridges
|
|
3701
|
+
* const g = new UndirectedGraph();
|
|
3702
|
+
* g.addVertex('A');
|
|
3703
|
+
* g.addVertex('B');
|
|
3704
|
+
* g.addVertex('C');
|
|
3705
|
+
* g.addEdge('A', 'B');
|
|
3706
|
+
* g.addEdge('B', 'C');
|
|
3707
|
+
* const result = g.tarjan();
|
|
3708
|
+
* console.log(result); // defined;
|
|
3709
|
+
*/
|
|
5382
3710
|
tarjan() {
|
|
5383
3711
|
const dfnMap = /* @__PURE__ */ new Map();
|
|
5384
3712
|
const lowMap = /* @__PURE__ */ new Map();
|
|
@@ -5478,61 +3806,22 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
5478
3806
|
return components;
|
|
5479
3807
|
}
|
|
5480
3808
|
/**
|
|
5481
|
-
|
|
5482
|
-
|
|
5483
|
-
|
|
5484
|
-
|
|
5485
|
-
|
|
5486
|
-
|
|
5487
|
-
|
|
5488
|
-
|
|
5489
|
-
|
|
5490
|
-
|
|
5491
|
-
|
|
5492
|
-
|
|
5493
|
-
|
|
5494
|
-
|
|
5495
|
-
|
|
5496
|
-
|
|
5497
|
-
|
|
5498
|
-
|
|
5499
|
-
|
|
5500
|
-
|
|
5501
|
-
|
|
5502
|
-
|
|
5503
|
-
|
|
5504
|
-
|
|
5505
|
-
|
|
5506
|
-
|
|
5507
|
-
|
|
5508
|
-
|
|
5509
|
-
|
|
5510
|
-
|
|
5511
|
-
|
|
5512
|
-
|
|
5513
|
-
|
|
5514
|
-
|
|
5515
|
-
|
|
5516
|
-
|
|
5517
|
-
|
|
5518
|
-
|
|
5519
|
-
|
|
5520
|
-
|
|
5521
|
-
|
|
5522
|
-
|
|
5523
|
-
|
|
5524
|
-
* @example
|
|
5525
|
-
* // Detect cycle
|
|
5526
|
-
* const g = new UndirectedGraph();
|
|
5527
|
-
* g.addVertex('A');
|
|
5528
|
-
* g.addVertex('B');
|
|
5529
|
-
* g.addVertex('C');
|
|
5530
|
-
* g.addEdge('A', 'B');
|
|
5531
|
-
* g.addEdge('B', 'C');
|
|
5532
|
-
* console.log(g.hasCycle()); // false;
|
|
5533
|
-
* g.addEdge('C', 'A');
|
|
5534
|
-
* console.log(g.hasCycle()); // true;
|
|
5535
|
-
*/
|
|
3809
|
+
* Detect whether the graph contains a cycle.
|
|
3810
|
+
* Uses DFS with parent tracking.
|
|
3811
|
+
* @returns `true` if a cycle exists, `false` otherwise.
|
|
3812
|
+
* @remarks Time O(V + E), Space O(V)
|
|
3813
|
+
* @example
|
|
3814
|
+
* // Detect cycle
|
|
3815
|
+
* const g = new UndirectedGraph();
|
|
3816
|
+
* g.addVertex('A');
|
|
3817
|
+
* g.addVertex('B');
|
|
3818
|
+
* g.addVertex('C');
|
|
3819
|
+
* g.addEdge('A', 'B');
|
|
3820
|
+
* g.addEdge('B', 'C');
|
|
3821
|
+
* console.log(g.hasCycle()); // false;
|
|
3822
|
+
* g.addEdge('C', 'A');
|
|
3823
|
+
* console.log(g.hasCycle()); // true;
|
|
3824
|
+
*/
|
|
5536
3825
|
hasCycle() {
|
|
5537
3826
|
const visited = /* @__PURE__ */ new Set();
|
|
5538
3827
|
const dfs = /* @__PURE__ */ __name((vertex, parent) => {
|
|
@@ -5554,117 +3843,39 @@ var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
|
5554
3843
|
return false;
|
|
5555
3844
|
}
|
|
5556
3845
|
/**
|
|
5557
|
-
|
|
5558
|
-
|
|
5559
|
-
|
|
5560
|
-
|
|
5561
|
-
|
|
5562
|
-
|
|
5563
|
-
|
|
5564
|
-
|
|
5565
|
-
|
|
5566
|
-
|
|
5567
|
-
|
|
5568
|
-
|
|
5569
|
-
|
|
5570
|
-
|
|
5571
|
-
|
|
5572
|
-
|
|
5573
|
-
|
|
5574
|
-
|
|
5575
|
-
|
|
5576
|
-
|
|
5577
|
-
|
|
5578
|
-
|
|
5579
|
-
|
|
5580
|
-
|
|
5581
|
-
|
|
5582
|
-
|
|
5583
|
-
|
|
5584
|
-
|
|
5585
|
-
|
|
5586
|
-
|
|
5587
|
-
|
|
5588
|
-
|
|
5589
|
-
|
|
5590
|
-
|
|
5591
|
-
|
|
5592
|
-
|
|
5593
|
-
|
|
5594
|
-
|
|
5595
|
-
|
|
5596
|
-
|
|
5597
|
-
|
|
5598
|
-
|
|
5599
|
-
* @example
|
|
5600
|
-
* // Find bridge edges
|
|
5601
|
-
* const g = new UndirectedGraph();
|
|
5602
|
-
* g.addVertex('A');
|
|
5603
|
-
* g.addVertex('B');
|
|
5604
|
-
* g.addVertex('C');
|
|
5605
|
-
* g.addEdge('A', 'B');
|
|
5606
|
-
* g.addEdge('B', 'C');
|
|
5607
|
-
* const bridges = g.getBridges();
|
|
5608
|
-
* console.log(bridges.length); // 2;
|
|
5609
|
-
*/
|
|
3846
|
+
* Get bridges discovered by `tarjan()`.
|
|
3847
|
+
* @returns Array of edges that are bridges.
|
|
3848
|
+
* @remarks Time O(B), Space O(1)
|
|
3849
|
+
* @example
|
|
3850
|
+
* // Find bridge edges
|
|
3851
|
+
* const g = new UndirectedGraph();
|
|
3852
|
+
* g.addVertex('A');
|
|
3853
|
+
* g.addVertex('B');
|
|
3854
|
+
* g.addVertex('C');
|
|
3855
|
+
* g.addEdge('A', 'B');
|
|
3856
|
+
* g.addEdge('B', 'C');
|
|
3857
|
+
* const bridges = g.getBridges();
|
|
3858
|
+
* console.log(bridges.length); // 2;
|
|
3859
|
+
*/
|
|
5610
3860
|
getBridges() {
|
|
5611
3861
|
return this.tarjan().bridges;
|
|
5612
3862
|
}
|
|
5613
3863
|
/**
|
|
5614
|
-
|
|
5615
|
-
|
|
5616
|
-
|
|
5617
|
-
|
|
5618
|
-
|
|
5619
|
-
|
|
5620
|
-
|
|
5621
|
-
|
|
5622
|
-
|
|
5623
|
-
|
|
5624
|
-
|
|
5625
|
-
|
|
5626
|
-
|
|
5627
|
-
|
|
5628
|
-
|
|
5629
|
-
|
|
5630
|
-
|
|
5631
|
-
|
|
5632
|
-
|
|
5633
|
-
|
|
5634
|
-
|
|
5635
|
-
|
|
5636
|
-
|
|
5637
|
-
|
|
5638
|
-
|
|
5639
|
-
|
|
5640
|
-
|
|
5641
|
-
|
|
5642
|
-
|
|
5643
|
-
|
|
5644
|
-
|
|
5645
|
-
|
|
5646
|
-
|
|
5647
|
-
|
|
5648
|
-
|
|
5649
|
-
|
|
5650
|
-
|
|
5651
|
-
|
|
5652
|
-
|
|
5653
|
-
|
|
5654
|
-
|
|
5655
|
-
|
|
5656
|
-
* @example
|
|
5657
|
-
* // Find articulation points
|
|
5658
|
-
* const g = new UndirectedGraph();
|
|
5659
|
-
* g.addVertex('A');
|
|
5660
|
-
* g.addVertex('B');
|
|
5661
|
-
* g.addVertex('C');
|
|
5662
|
-
* g.addEdge('A', 'B');
|
|
5663
|
-
* g.addEdge('B', 'C');
|
|
5664
|
-
* const cuts = g.getCutVertices();
|
|
5665
|
-
* console.log(cuts.length); // 1;
|
|
5666
|
-
* console.log(cuts[0].key); // 'B';
|
|
5667
|
-
*/
|
|
3864
|
+
* Get articulation points discovered by `tarjan()`.
|
|
3865
|
+
* @returns Array of cut vertices.
|
|
3866
|
+
* @remarks Time O(C), Space O(1)
|
|
3867
|
+
* @example
|
|
3868
|
+
* // Find articulation points
|
|
3869
|
+
* const g = new UndirectedGraph();
|
|
3870
|
+
* g.addVertex('A');
|
|
3871
|
+
* g.addVertex('B');
|
|
3872
|
+
* g.addVertex('C');
|
|
3873
|
+
* g.addEdge('A', 'B');
|
|
3874
|
+
* g.addEdge('B', 'C');
|
|
3875
|
+
* const cuts = g.getCutVertices();
|
|
3876
|
+
* console.log(cuts.length); // 1;
|
|
3877
|
+
* console.log(cuts[0].key); // 'B';
|
|
3878
|
+
*/
|
|
5668
3879
|
getCutVertices() {
|
|
5669
3880
|
return this.tarjan().cutVertices;
|
|
5670
3881
|
}
|