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