directed-graph-typed 2.4.5 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -51
- package/dist/cjs/index.cjs +870 -163
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +870 -163
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +870 -163
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +870 -163
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +272 -65
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/umd/directed-graph-typed.js +870 -163
- package/dist/umd/directed-graph-typed.js.map +1 -1
- package/dist/umd/directed-graph-typed.min.js +3 -3
- package/dist/umd/directed-graph-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/binary-tree/avl-tree.ts +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +302 -247
- package/src/data-structures/binary-tree/binary-tree.ts +429 -79
- package/src/data-structures/binary-tree/bst.ts +335 -34
- package/src/data-structures/binary-tree/red-black-tree.ts +290 -97
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1284 -6
- package/src/data-structures/binary-tree/tree-multi-map.ts +1094 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +858 -65
- package/src/data-structures/binary-tree/tree-set.ts +1136 -9
- package/src/data-structures/graph/directed-graph.ts +219 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +204 -59
- package/src/data-structures/hash/hash-map.ts +230 -77
- package/src/data-structures/heap/heap.ts +287 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
- package/src/data-structures/matrix/matrix.ts +416 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +272 -65
- package/src/data-structures/queue/queue.ts +211 -42
- package/src/data-structures/stack/stack.ts +174 -32
- package/src/data-structures/trie/trie.ts +213 -43
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
|
@@ -302,7 +302,7 @@ var directedGraphTyped = (() => {
|
|
|
302
302
|
if (options) {
|
|
303
303
|
const { toElementFn } = options;
|
|
304
304
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
305
|
-
else if (toElementFn) throw new TypeError(
|
|
305
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
306
306
|
}
|
|
307
307
|
}
|
|
308
308
|
/**
|
|
@@ -458,7 +458,7 @@ var directedGraphTyped = (() => {
|
|
|
458
458
|
acc = initialValue;
|
|
459
459
|
} else {
|
|
460
460
|
const first = iter.next();
|
|
461
|
-
if (first.done) throw new TypeError(
|
|
461
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
462
462
|
acc = first.value;
|
|
463
463
|
index = 1;
|
|
464
464
|
}
|
|
@@ -537,10 +537,30 @@ var directedGraphTyped = (() => {
|
|
|
537
537
|
return this._elements;
|
|
538
538
|
}
|
|
539
539
|
/**
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
540
|
+
* Get the number of elements.
|
|
541
|
+
* @remarks Time O(1), Space O(1)
|
|
542
|
+
* @returns Heap size.
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
* @example
|
|
555
|
+
* // Track heap capacity
|
|
556
|
+
* const heap = new Heap<number>();
|
|
557
|
+
* console.log(heap.size); // 0;
|
|
558
|
+
* heap.add(10);
|
|
559
|
+
* heap.add(20);
|
|
560
|
+
* console.log(heap.size); // 2;
|
|
561
|
+
* heap.poll();
|
|
562
|
+
* console.log(heap.size); // 1;
|
|
563
|
+
*/
|
|
544
564
|
get size() {
|
|
545
565
|
return this.elements.length;
|
|
546
566
|
}
|
|
@@ -579,21 +599,61 @@ var directedGraphTyped = (() => {
|
|
|
579
599
|
return new _Heap(elements, options);
|
|
580
600
|
}
|
|
581
601
|
/**
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
602
|
+
* Insert an element.
|
|
603
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
604
|
+
* @param element - Element to insert.
|
|
605
|
+
* @returns True.
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
* @example
|
|
618
|
+
* // basic Heap creation and add operation
|
|
619
|
+
* // Create a min heap (default)
|
|
620
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
621
|
+
*
|
|
622
|
+
* // Verify size
|
|
623
|
+
* console.log(minHeap.size); // 6;
|
|
624
|
+
*
|
|
625
|
+
* // Add new element
|
|
626
|
+
* minHeap.add(4);
|
|
627
|
+
* console.log(minHeap.size); // 7;
|
|
628
|
+
*
|
|
629
|
+
* // Min heap property: smallest element at root
|
|
630
|
+
* const min = minHeap.peek();
|
|
631
|
+
* console.log(min); // 1;
|
|
632
|
+
*/
|
|
587
633
|
add(element) {
|
|
588
634
|
this._elements.push(element);
|
|
589
635
|
return this._bubbleUp(this.elements.length - 1);
|
|
590
636
|
}
|
|
591
637
|
/**
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
638
|
+
* Insert many elements from an iterable.
|
|
639
|
+
* @remarks Time O(N log N), Space O(1)
|
|
640
|
+
* @param elements - Iterable of elements or raw values.
|
|
641
|
+
* @returns Array of per-element success flags.
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
|
|
650
|
+
* @example
|
|
651
|
+
* // Add multiple elements
|
|
652
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
653
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
654
|
+
* console.log(heap.peek()); // 1;
|
|
655
|
+
* console.log(heap.size); // 4;
|
|
656
|
+
*/
|
|
597
657
|
addMany(elements) {
|
|
598
658
|
const flags = [];
|
|
599
659
|
for (const el of elements) {
|
|
@@ -608,10 +668,46 @@ var directedGraphTyped = (() => {
|
|
|
608
668
|
return flags;
|
|
609
669
|
}
|
|
610
670
|
/**
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
671
|
+
* Remove and return the top element.
|
|
672
|
+
* @remarks Time O(log N), Space O(1)
|
|
673
|
+
* @returns Top element or undefined.
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
* @example
|
|
686
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
687
|
+
* interface Task {
|
|
688
|
+
* id: number;
|
|
689
|
+
* priority: number;
|
|
690
|
+
* name: string;
|
|
691
|
+
* }
|
|
692
|
+
*
|
|
693
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
694
|
+
* const tasks: Task[] = [
|
|
695
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
696
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
697
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
698
|
+
* ];
|
|
699
|
+
*
|
|
700
|
+
* const maxHeap = new Heap(tasks, {
|
|
701
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
702
|
+
* });
|
|
703
|
+
*
|
|
704
|
+
* console.log(maxHeap.size); // 3;
|
|
705
|
+
*
|
|
706
|
+
* // Peek returns highest priority task
|
|
707
|
+
* const topTask = maxHeap.peek();
|
|
708
|
+
* console.log(topTask?.priority); // 8;
|
|
709
|
+
* console.log(topTask?.name); // 'Alert';
|
|
710
|
+
*/
|
|
615
711
|
poll() {
|
|
616
712
|
if (this.elements.length === 0) return;
|
|
617
713
|
const value = this.elements[0];
|
|
@@ -623,26 +719,125 @@ var directedGraphTyped = (() => {
|
|
|
623
719
|
return value;
|
|
624
720
|
}
|
|
625
721
|
/**
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
722
|
+
* Get the current top element without removing it.
|
|
723
|
+
* @remarks Time O(1), Space O(1)
|
|
724
|
+
* @returns Top element or undefined.
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
* @example
|
|
737
|
+
* // Heap for event processing with priority
|
|
738
|
+
* interface Event {
|
|
739
|
+
* id: number;
|
|
740
|
+
* type: 'critical' | 'warning' | 'info';
|
|
741
|
+
* timestamp: number;
|
|
742
|
+
* message: string;
|
|
743
|
+
* }
|
|
744
|
+
*
|
|
745
|
+
* // Custom priority: critical > warning > info
|
|
746
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
747
|
+
*
|
|
748
|
+
* const eventHeap = new Heap<Event>([], {
|
|
749
|
+
* comparator: (a: Event, b: Event) => {
|
|
750
|
+
* const priorityA = priorityMap[a.type];
|
|
751
|
+
* const priorityB = priorityMap[b.type];
|
|
752
|
+
* return priorityB - priorityA; // Higher priority first
|
|
753
|
+
* }
|
|
754
|
+
* });
|
|
755
|
+
*
|
|
756
|
+
* // Add events in random order
|
|
757
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
758
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
759
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
760
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
761
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
762
|
+
*
|
|
763
|
+
* console.log(eventHeap.size); // 5;
|
|
764
|
+
*
|
|
765
|
+
* // Process events by priority (critical first)
|
|
766
|
+
* const processedOrder: Event[] = [];
|
|
767
|
+
* while (eventHeap.size > 0) {
|
|
768
|
+
* const event = eventHeap.poll();
|
|
769
|
+
* if (event) {
|
|
770
|
+
* processedOrder.push(event);
|
|
771
|
+
* }
|
|
772
|
+
* }
|
|
773
|
+
*
|
|
774
|
+
* // Verify critical events came first
|
|
775
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
776
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
777
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
778
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
779
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
780
|
+
*
|
|
781
|
+
* // Verify O(log n) operations
|
|
782
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
783
|
+
*
|
|
784
|
+
* // Add - O(log n)
|
|
785
|
+
* newHeap.add(2);
|
|
786
|
+
* console.log(newHeap.size); // 5;
|
|
787
|
+
*
|
|
788
|
+
* // Poll - O(log n)
|
|
789
|
+
* const removed = newHeap.poll();
|
|
790
|
+
* console.log(removed); // 1;
|
|
791
|
+
*
|
|
792
|
+
* // Peek - O(1)
|
|
793
|
+
* const top = newHeap.peek();
|
|
794
|
+
* console.log(top); // 2;
|
|
795
|
+
*/
|
|
630
796
|
peek() {
|
|
631
797
|
return this.elements[0];
|
|
632
798
|
}
|
|
633
799
|
/**
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
800
|
+
* Check whether the heap is empty.
|
|
801
|
+
* @remarks Time O(1), Space O(1)
|
|
802
|
+
* @returns True if size is 0.
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
* @example
|
|
813
|
+
* // Check if heap is empty
|
|
814
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
815
|
+
* console.log(heap.isEmpty()); // true;
|
|
816
|
+
* heap.add(1);
|
|
817
|
+
* console.log(heap.isEmpty()); // false;
|
|
818
|
+
*/
|
|
638
819
|
isEmpty() {
|
|
639
820
|
return this.size === 0;
|
|
640
821
|
}
|
|
641
822
|
/**
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
823
|
+
* Remove all elements.
|
|
824
|
+
* @remarks Time O(1), Space O(1)
|
|
825
|
+
* @returns void
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
* @example
|
|
836
|
+
* // Remove all elements
|
|
837
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
838
|
+
* heap.clear();
|
|
839
|
+
* console.log(heap.isEmpty()); // true;
|
|
840
|
+
*/
|
|
646
841
|
clear() {
|
|
647
842
|
this._elements = [];
|
|
648
843
|
}
|
|
@@ -657,21 +852,41 @@ var directedGraphTyped = (() => {
|
|
|
657
852
|
return this.fix();
|
|
658
853
|
}
|
|
659
854
|
/**
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
855
|
+
* Check if an equal element exists in the heap.
|
|
856
|
+
* @remarks Time O(N), Space O(1)
|
|
857
|
+
* @param element - Element to search for.
|
|
858
|
+
* @returns True if found.
|
|
859
|
+
|
|
860
|
+
|
|
861
|
+
* @example
|
|
862
|
+
* // Check element existence
|
|
863
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
864
|
+
* console.log(heap.has(1)); // true;
|
|
865
|
+
* console.log(heap.has(99)); // false;
|
|
866
|
+
*/
|
|
665
867
|
has(element) {
|
|
666
868
|
for (const el of this.elements) if (this._equals(el, element)) return true;
|
|
667
869
|
return false;
|
|
668
870
|
}
|
|
669
871
|
/**
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
872
|
+
* Delete one occurrence of an element.
|
|
873
|
+
* @remarks Time O(N), Space O(1)
|
|
874
|
+
* @param element - Element to delete.
|
|
875
|
+
* @returns True if an element was removed.
|
|
876
|
+
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
* @example
|
|
885
|
+
* // Remove specific element
|
|
886
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
887
|
+
* heap.delete(4);
|
|
888
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
889
|
+
*/
|
|
675
890
|
delete(element) {
|
|
676
891
|
let index = -1;
|
|
677
892
|
for (let i = 0; i < this.elements.length; i++) {
|
|
@@ -729,11 +944,18 @@ var directedGraphTyped = (() => {
|
|
|
729
944
|
return this;
|
|
730
945
|
}
|
|
731
946
|
/**
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
947
|
+
* Traverse the binary heap as a complete binary tree and collect elements.
|
|
948
|
+
* @remarks Time O(N), Space O(H)
|
|
949
|
+
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
950
|
+
* @returns Array of visited elements.
|
|
951
|
+
|
|
952
|
+
|
|
953
|
+
* @example
|
|
954
|
+
* // Depth-first traversal
|
|
955
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
956
|
+
* const result = heap.dfs('IN');
|
|
957
|
+
* console.log(result.length); // 3;
|
|
958
|
+
*/
|
|
737
959
|
dfs(order = "PRE") {
|
|
738
960
|
const result = [];
|
|
739
961
|
const _dfs = (index) => {
|
|
@@ -770,10 +992,26 @@ var directedGraphTyped = (() => {
|
|
|
770
992
|
return results;
|
|
771
993
|
}
|
|
772
994
|
/**
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
995
|
+
* Return all elements in ascending order by repeatedly polling.
|
|
996
|
+
* @remarks Time O(N log N), Space O(N)
|
|
997
|
+
* @returns Sorted array of elements.
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
|
|
1001
|
+
|
|
1002
|
+
|
|
1003
|
+
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
* @example
|
|
1010
|
+
* // Sort elements using heap
|
|
1011
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
1012
|
+
* const sorted = heap.sort();
|
|
1013
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
1014
|
+
*/
|
|
777
1015
|
sort() {
|
|
778
1016
|
const visited = [];
|
|
779
1017
|
const cloned = this._createInstance();
|
|
@@ -785,22 +1023,52 @@ var directedGraphTyped = (() => {
|
|
|
785
1023
|
return visited;
|
|
786
1024
|
}
|
|
787
1025
|
/**
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
1026
|
+
* Deep clone this heap.
|
|
1027
|
+
* @remarks Time O(N), Space O(N)
|
|
1028
|
+
* @returns A new heap with the same elements.
|
|
1029
|
+
|
|
1030
|
+
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
|
|
1037
|
+
|
|
1038
|
+
* @example
|
|
1039
|
+
* // Create independent copy
|
|
1040
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
1041
|
+
* const copy = heap.clone();
|
|
1042
|
+
* copy.poll();
|
|
1043
|
+
* console.log(heap.size); // 3;
|
|
1044
|
+
* console.log(copy.size); // 2;
|
|
1045
|
+
*/
|
|
792
1046
|
clone() {
|
|
793
1047
|
const next = this._createInstance();
|
|
794
1048
|
for (const x of this.elements) next.add(x);
|
|
795
1049
|
return next;
|
|
796
1050
|
}
|
|
797
1051
|
/**
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
1052
|
+
* Filter elements into a new heap of the same class.
|
|
1053
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1054
|
+
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
1055
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1056
|
+
* @returns A new heap with the kept elements.
|
|
1057
|
+
|
|
1058
|
+
|
|
1059
|
+
|
|
1060
|
+
|
|
1061
|
+
|
|
1062
|
+
|
|
1063
|
+
|
|
1064
|
+
|
|
1065
|
+
|
|
1066
|
+
* @example
|
|
1067
|
+
* // Filter elements
|
|
1068
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
1069
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
1070
|
+
* console.log(evens.size); // 2;
|
|
1071
|
+
*/
|
|
804
1072
|
filter(callback, thisArg) {
|
|
805
1073
|
const out = this._createInstance();
|
|
806
1074
|
let i = 0;
|
|
@@ -814,15 +1082,28 @@ var directedGraphTyped = (() => {
|
|
|
814
1082
|
return out;
|
|
815
1083
|
}
|
|
816
1084
|
/**
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
1085
|
+
* Map elements into a new heap of possibly different element type.
|
|
1086
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1087
|
+
* @template EM
|
|
1088
|
+
* @template RM
|
|
1089
|
+
* @param callback - Mapping function (element, index, heap) → newElement.
|
|
1090
|
+
* @param options - Options for the output heap, including comparator for EM.
|
|
1091
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1092
|
+
* @returns A new heap with mapped elements.
|
|
1093
|
+
|
|
1094
|
+
|
|
1095
|
+
|
|
1096
|
+
|
|
1097
|
+
|
|
1098
|
+
|
|
1099
|
+
|
|
1100
|
+
|
|
1101
|
+
* @example
|
|
1102
|
+
* // Transform elements
|
|
1103
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1104
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
1105
|
+
* console.log(doubled.peek()); // 2;
|
|
1106
|
+
*/
|
|
826
1107
|
map(callback, options, thisArg) {
|
|
827
1108
|
const { comparator, toElementFn, ...rest } = options != null ? options : {};
|
|
828
1109
|
if (!comparator) throw new TypeError(ERR.comparatorRequired("Heap.map"));
|
|
@@ -1170,18 +1451,52 @@ var directedGraphTyped = (() => {
|
|
|
1170
1451
|
this._autoCompactRatio = value;
|
|
1171
1452
|
}
|
|
1172
1453
|
/**
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1454
|
+
* Get the number of elements currently in the queue.
|
|
1455
|
+
* @remarks Time O(1), Space O(1)
|
|
1456
|
+
* @returns Current length.
|
|
1457
|
+
|
|
1458
|
+
|
|
1459
|
+
|
|
1460
|
+
|
|
1461
|
+
|
|
1462
|
+
|
|
1463
|
+
|
|
1464
|
+
|
|
1465
|
+
|
|
1466
|
+
|
|
1467
|
+
|
|
1468
|
+
* @example
|
|
1469
|
+
* // Track queue length
|
|
1470
|
+
* const q = new Queue<number>();
|
|
1471
|
+
* console.log(q.length); // 0;
|
|
1472
|
+
* q.push(1);
|
|
1473
|
+
* q.push(2);
|
|
1474
|
+
* console.log(q.length); // 2;
|
|
1475
|
+
*/
|
|
1177
1476
|
get length() {
|
|
1178
1477
|
return this.elements.length - this._offset;
|
|
1179
1478
|
}
|
|
1180
1479
|
/**
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1480
|
+
* Get the first element (front) without removing it.
|
|
1481
|
+
* @remarks Time O(1), Space O(1)
|
|
1482
|
+
* @returns Front element or undefined.
|
|
1483
|
+
|
|
1484
|
+
|
|
1485
|
+
|
|
1486
|
+
|
|
1487
|
+
|
|
1488
|
+
|
|
1489
|
+
|
|
1490
|
+
|
|
1491
|
+
|
|
1492
|
+
|
|
1493
|
+
|
|
1494
|
+
* @example
|
|
1495
|
+
* // View the front element
|
|
1496
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
1497
|
+
* console.log(q.first); // 'first';
|
|
1498
|
+
* console.log(q.length); // 3;
|
|
1499
|
+
*/
|
|
1185
1500
|
get first() {
|
|
1186
1501
|
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
1187
1502
|
}
|
|
@@ -1204,19 +1519,69 @@ var directedGraphTyped = (() => {
|
|
|
1204
1519
|
return new _Queue(elements);
|
|
1205
1520
|
}
|
|
1206
1521
|
/**
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1522
|
+
* Check whether the queue is empty.
|
|
1523
|
+
* @remarks Time O(1), Space O(1)
|
|
1524
|
+
* @returns True if length is 0.
|
|
1525
|
+
|
|
1526
|
+
|
|
1527
|
+
|
|
1528
|
+
|
|
1529
|
+
|
|
1530
|
+
|
|
1531
|
+
|
|
1532
|
+
|
|
1533
|
+
|
|
1534
|
+
|
|
1535
|
+
|
|
1536
|
+
* @example
|
|
1537
|
+
* // Queue for...of iteration and isEmpty check
|
|
1538
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
1539
|
+
*
|
|
1540
|
+
* const elements: string[] = [];
|
|
1541
|
+
* for (const item of queue) {
|
|
1542
|
+
* elements.push(item);
|
|
1543
|
+
* }
|
|
1544
|
+
*
|
|
1545
|
+
* // Verify all elements are iterated in order
|
|
1546
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
1547
|
+
*
|
|
1548
|
+
* // Process all elements
|
|
1549
|
+
* while (queue.length > 0) {
|
|
1550
|
+
* queue.shift();
|
|
1551
|
+
* }
|
|
1552
|
+
*
|
|
1553
|
+
* console.log(queue.length); // 0;
|
|
1554
|
+
*/
|
|
1211
1555
|
isEmpty() {
|
|
1212
1556
|
return this.length === 0;
|
|
1213
1557
|
}
|
|
1214
1558
|
/**
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1559
|
+
* Enqueue one element at the back.
|
|
1560
|
+
* @remarks Time O(1), Space O(1)
|
|
1561
|
+
* @param element - Element to enqueue.
|
|
1562
|
+
* @returns True on success.
|
|
1563
|
+
|
|
1564
|
+
|
|
1565
|
+
|
|
1566
|
+
|
|
1567
|
+
|
|
1568
|
+
|
|
1569
|
+
|
|
1570
|
+
|
|
1571
|
+
|
|
1572
|
+
|
|
1573
|
+
|
|
1574
|
+
* @example
|
|
1575
|
+
* // basic Queue creation and push operation
|
|
1576
|
+
* // Create a simple Queue with initial values
|
|
1577
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
1578
|
+
*
|
|
1579
|
+
* // Verify the queue maintains insertion order
|
|
1580
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
1581
|
+
*
|
|
1582
|
+
* // Check length
|
|
1583
|
+
* console.log(queue.length); // 5;
|
|
1584
|
+
*/
|
|
1220
1585
|
push(element) {
|
|
1221
1586
|
this.elements.push(element);
|
|
1222
1587
|
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
@@ -1237,10 +1602,35 @@ var directedGraphTyped = (() => {
|
|
|
1237
1602
|
return ans;
|
|
1238
1603
|
}
|
|
1239
1604
|
/**
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1605
|
+
* Dequeue one element from the front (amortized via offset).
|
|
1606
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
1607
|
+
* @returns Removed element or undefined.
|
|
1608
|
+
|
|
1609
|
+
|
|
1610
|
+
|
|
1611
|
+
|
|
1612
|
+
|
|
1613
|
+
|
|
1614
|
+
|
|
1615
|
+
|
|
1616
|
+
|
|
1617
|
+
|
|
1618
|
+
|
|
1619
|
+
* @example
|
|
1620
|
+
* // Queue shift and peek operations
|
|
1621
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
1622
|
+
*
|
|
1623
|
+
* // Peek at the front element without removing it
|
|
1624
|
+
* console.log(queue.first); // 10;
|
|
1625
|
+
*
|
|
1626
|
+
* // Remove and get the first element (FIFO)
|
|
1627
|
+
* const first = queue.shift();
|
|
1628
|
+
* console.log(first); // 10;
|
|
1629
|
+
*
|
|
1630
|
+
* // Verify remaining elements and length decreased
|
|
1631
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
1632
|
+
* console.log(queue.length); // 3;
|
|
1633
|
+
*/
|
|
1244
1634
|
shift() {
|
|
1245
1635
|
if (this.length === 0) return void 0;
|
|
1246
1636
|
const first = this.first;
|
|
@@ -1249,11 +1639,24 @@ var directedGraphTyped = (() => {
|
|
|
1249
1639
|
return first;
|
|
1250
1640
|
}
|
|
1251
1641
|
/**
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1642
|
+
* Delete the first occurrence of a specific element.
|
|
1643
|
+
* @remarks Time O(N), Space O(1)
|
|
1644
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
1645
|
+
* @returns True if an element was removed.
|
|
1646
|
+
|
|
1647
|
+
|
|
1648
|
+
|
|
1649
|
+
|
|
1650
|
+
|
|
1651
|
+
|
|
1652
|
+
|
|
1653
|
+
|
|
1654
|
+
* @example
|
|
1655
|
+
* // Remove specific element
|
|
1656
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
1657
|
+
* q.delete(2);
|
|
1658
|
+
* console.log(q.length); // 3;
|
|
1659
|
+
*/
|
|
1257
1660
|
delete(element) {
|
|
1258
1661
|
for (let i = this._offset; i < this.elements.length; i++) {
|
|
1259
1662
|
if (Object.is(this.elements[i], element)) {
|
|
@@ -1264,11 +1667,24 @@ var directedGraphTyped = (() => {
|
|
|
1264
1667
|
return false;
|
|
1265
1668
|
}
|
|
1266
1669
|
/**
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1670
|
+
* Get the element at a given logical index.
|
|
1671
|
+
* @remarks Time O(1), Space O(1)
|
|
1672
|
+
* @param index - Zero-based index from the front.
|
|
1673
|
+
* @returns Element or undefined.
|
|
1674
|
+
|
|
1675
|
+
|
|
1676
|
+
|
|
1677
|
+
|
|
1678
|
+
|
|
1679
|
+
|
|
1680
|
+
|
|
1681
|
+
|
|
1682
|
+
* @example
|
|
1683
|
+
* // Access element by index
|
|
1684
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
1685
|
+
* console.log(q.at(0)); // 'a';
|
|
1686
|
+
* console.log(q.at(2)); // 'c';
|
|
1687
|
+
*/
|
|
1272
1688
|
at(index) {
|
|
1273
1689
|
if (index < 0 || index >= this.length) return void 0;
|
|
1274
1690
|
return this._elements[this._offset + index];
|
|
@@ -1320,19 +1736,48 @@ var directedGraphTyped = (() => {
|
|
|
1320
1736
|
return this;
|
|
1321
1737
|
}
|
|
1322
1738
|
/**
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1739
|
+
* Remove all elements and reset offset.
|
|
1740
|
+
* @remarks Time O(1), Space O(1)
|
|
1741
|
+
* @returns void
|
|
1742
|
+
|
|
1743
|
+
|
|
1744
|
+
|
|
1745
|
+
|
|
1746
|
+
|
|
1747
|
+
|
|
1748
|
+
|
|
1749
|
+
|
|
1750
|
+
|
|
1751
|
+
* @example
|
|
1752
|
+
* // Remove all elements
|
|
1753
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1754
|
+
* q.clear();
|
|
1755
|
+
* console.log(q.length); // 0;
|
|
1756
|
+
*/
|
|
1327
1757
|
clear() {
|
|
1328
1758
|
this._elements = [];
|
|
1329
1759
|
this._offset = 0;
|
|
1330
1760
|
}
|
|
1331
1761
|
/**
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1762
|
+
* Compact storage by discarding consumed head elements.
|
|
1763
|
+
* @remarks Time O(N), Space O(N)
|
|
1764
|
+
* @returns True when compaction performed.
|
|
1765
|
+
|
|
1766
|
+
|
|
1767
|
+
|
|
1768
|
+
|
|
1769
|
+
|
|
1770
|
+
|
|
1771
|
+
|
|
1772
|
+
|
|
1773
|
+
* @example
|
|
1774
|
+
* // Reclaim unused memory
|
|
1775
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1776
|
+
* q.shift();
|
|
1777
|
+
* q.shift();
|
|
1778
|
+
* q.compact();
|
|
1779
|
+
* console.log(q.length); // 3;
|
|
1780
|
+
*/
|
|
1336
1781
|
compact() {
|
|
1337
1782
|
this._elements = this.elements.slice(this._offset);
|
|
1338
1783
|
this._offset = 0;
|
|
@@ -1358,10 +1803,26 @@ var directedGraphTyped = (() => {
|
|
|
1358
1803
|
return removed;
|
|
1359
1804
|
}
|
|
1360
1805
|
/**
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1806
|
+
* Deep clone this queue and its parameters.
|
|
1807
|
+
* @remarks Time O(N), Space O(N)
|
|
1808
|
+
* @returns A new queue with the same content and options.
|
|
1809
|
+
|
|
1810
|
+
|
|
1811
|
+
|
|
1812
|
+
|
|
1813
|
+
|
|
1814
|
+
|
|
1815
|
+
|
|
1816
|
+
|
|
1817
|
+
|
|
1818
|
+
* @example
|
|
1819
|
+
* // Create independent copy
|
|
1820
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1821
|
+
* const copy = q.clone();
|
|
1822
|
+
* copy.shift();
|
|
1823
|
+
* console.log(q.length); // 3;
|
|
1824
|
+
* console.log(copy.length); // 2;
|
|
1825
|
+
*/
|
|
1365
1826
|
clone() {
|
|
1366
1827
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1367
1828
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -1369,12 +1830,26 @@ var directedGraphTyped = (() => {
|
|
|
1369
1830
|
return out;
|
|
1370
1831
|
}
|
|
1371
1832
|
/**
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1833
|
+
* Filter elements into a new queue of the same class.
|
|
1834
|
+
* @remarks Time O(N), Space O(N)
|
|
1835
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
1836
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
1837
|
+
* @returns A new queue with kept elements.
|
|
1838
|
+
|
|
1839
|
+
|
|
1840
|
+
|
|
1841
|
+
|
|
1842
|
+
|
|
1843
|
+
|
|
1844
|
+
|
|
1845
|
+
|
|
1846
|
+
|
|
1847
|
+
* @example
|
|
1848
|
+
* // Filter elements
|
|
1849
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1850
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
1851
|
+
* console.log(evens.length); // 2;
|
|
1852
|
+
*/
|
|
1378
1853
|
filter(predicate, thisArg) {
|
|
1379
1854
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1380
1855
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -1386,15 +1861,28 @@ var directedGraphTyped = (() => {
|
|
|
1386
1861
|
return out;
|
|
1387
1862
|
}
|
|
1388
1863
|
/**
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1864
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
1865
|
+
* @remarks Time O(N), Space O(N)
|
|
1866
|
+
* @template EM
|
|
1867
|
+
* @template RM
|
|
1868
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
1869
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
1870
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1871
|
+
* @returns A new Queue with mapped elements.
|
|
1872
|
+
|
|
1873
|
+
|
|
1874
|
+
|
|
1875
|
+
|
|
1876
|
+
|
|
1877
|
+
|
|
1878
|
+
|
|
1879
|
+
|
|
1880
|
+
* @example
|
|
1881
|
+
* // Transform elements
|
|
1882
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1883
|
+
* const doubled = q.map(x => x * 2);
|
|
1884
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
1885
|
+
*/
|
|
1398
1886
|
map(callback, options, thisArg) {
|
|
1399
1887
|
var _a, _b;
|
|
1400
1888
|
const out = new this.constructor([], {
|
|
@@ -2482,12 +2970,28 @@ var directedGraphTyped = (() => {
|
|
|
2482
2970
|
return new DirectedEdge(src, dest, (_a = weight != null ? weight : this.options.defaultEdgeWeight) != null ? _a : 1, value);
|
|
2483
2971
|
}
|
|
2484
2972
|
/**
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2973
|
+
* Get the unique edge from `src` to `dest`, if present.
|
|
2974
|
+
* @param srcOrKey - Source vertex or key.
|
|
2975
|
+
* @param destOrKey - Destination vertex or key.
|
|
2976
|
+
* @returns Edge instance or `undefined`.
|
|
2977
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
2978
|
+
|
|
2979
|
+
|
|
2980
|
+
|
|
2981
|
+
|
|
2982
|
+
|
|
2983
|
+
|
|
2984
|
+
|
|
2985
|
+
|
|
2986
|
+
* @example
|
|
2987
|
+
* // Get edge between vertices
|
|
2988
|
+
* const g = new DirectedGraph();
|
|
2989
|
+
* g.addVertex('A');
|
|
2990
|
+
* g.addVertex('B');
|
|
2991
|
+
* g.addEdge('A', 'B', 5);
|
|
2992
|
+
* const edge = g.getEdge('A', 'B');
|
|
2993
|
+
* console.log(edge?.weight); // 5;
|
|
2994
|
+
*/
|
|
2491
2995
|
getEdge(srcOrKey, destOrKey) {
|
|
2492
2996
|
let edgeMap = [];
|
|
2493
2997
|
if (srcOrKey !== void 0 && destOrKey !== void 0) {
|
|
@@ -2527,12 +3031,48 @@ var directedGraphTyped = (() => {
|
|
|
2527
3031
|
return removed;
|
|
2528
3032
|
}
|
|
2529
3033
|
/**
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
3034
|
+
* Delete an edge by instance or by `(srcKey, destKey)`.
|
|
3035
|
+
* @param edgeOrSrcVertexKey - Edge instance or source vertex/key.
|
|
3036
|
+
* @param destVertexKey - Optional destination vertex/key when deleting by pair.
|
|
3037
|
+
* @returns Removed edge or `undefined`.
|
|
3038
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
3039
|
+
|
|
3040
|
+
|
|
3041
|
+
|
|
3042
|
+
|
|
3043
|
+
|
|
3044
|
+
|
|
3045
|
+
|
|
3046
|
+
|
|
3047
|
+
|
|
3048
|
+
|
|
3049
|
+
|
|
3050
|
+
* @example
|
|
3051
|
+
* // DirectedGraph deleteEdge and vertex operations
|
|
3052
|
+
* const graph = new DirectedGraph<string>();
|
|
3053
|
+
*
|
|
3054
|
+
* // Build a small graph
|
|
3055
|
+
* graph.addVertex('X');
|
|
3056
|
+
* graph.addVertex('Y');
|
|
3057
|
+
* graph.addVertex('Z');
|
|
3058
|
+
* graph.addEdge('X', 'Y', 1);
|
|
3059
|
+
* graph.addEdge('Y', 'Z', 2);
|
|
3060
|
+
*
|
|
3061
|
+
* // Delete an edge
|
|
3062
|
+
* graph.deleteEdgeSrcToDest('X', 'Y');
|
|
3063
|
+
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
3064
|
+
*
|
|
3065
|
+
* // Edge in other direction should not exist
|
|
3066
|
+
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
3067
|
+
*
|
|
3068
|
+
* // Other edges should remain
|
|
3069
|
+
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
3070
|
+
*
|
|
3071
|
+
* // Delete a vertex
|
|
3072
|
+
* graph.deleteVertex('Y');
|
|
3073
|
+
* console.log(graph.hasVertex('Y')); // false;
|
|
3074
|
+
* console.log(graph.size); // 2;
|
|
3075
|
+
*/
|
|
2536
3076
|
deleteEdge(edgeOrSrcVertexKey, destVertexKey) {
|
|
2537
3077
|
let removed = void 0;
|
|
2538
3078
|
let src, dest;
|
|
@@ -2559,6 +3099,26 @@ var directedGraphTyped = (() => {
|
|
|
2559
3099
|
}
|
|
2560
3100
|
return removed;
|
|
2561
3101
|
}
|
|
3102
|
+
/**
|
|
3103
|
+
* Remove a vertex
|
|
3104
|
+
|
|
3105
|
+
|
|
3106
|
+
|
|
3107
|
+
|
|
3108
|
+
|
|
3109
|
+
|
|
3110
|
+
|
|
3111
|
+
|
|
3112
|
+
* @example
|
|
3113
|
+
* // Remove a vertex
|
|
3114
|
+
* const g = new DirectedGraph();
|
|
3115
|
+
* g.addVertex('A');
|
|
3116
|
+
* g.addVertex('B');
|
|
3117
|
+
* g.addEdge('A', 'B');
|
|
3118
|
+
* g.deleteVertex('A');
|
|
3119
|
+
* console.log(g.hasVertex('A')); // false;
|
|
3120
|
+
* console.log(g.hasEdge('A', 'B')); // false;
|
|
3121
|
+
*/
|
|
2562
3122
|
deleteVertex(vertexOrKey) {
|
|
2563
3123
|
let vertexKey;
|
|
2564
3124
|
let vertex;
|
|
@@ -2590,11 +3150,28 @@ var directedGraphTyped = (() => {
|
|
|
2590
3150
|
return removed;
|
|
2591
3151
|
}
|
|
2592
3152
|
/**
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
3153
|
+
* Incoming edges of a vertex.
|
|
3154
|
+
* @param vertexOrKey - Vertex or key.
|
|
3155
|
+
* @returns Array of incoming edges.
|
|
3156
|
+
* @remarks Time O(deg_in), Space O(deg_in)
|
|
3157
|
+
|
|
3158
|
+
|
|
3159
|
+
|
|
3160
|
+
|
|
3161
|
+
|
|
3162
|
+
|
|
3163
|
+
|
|
3164
|
+
|
|
3165
|
+
* @example
|
|
3166
|
+
* // Get incoming edges
|
|
3167
|
+
* const g = new DirectedGraph();
|
|
3168
|
+
* g.addVertex('A');
|
|
3169
|
+
* g.addVertex('B');
|
|
3170
|
+
* g.addVertex('C');
|
|
3171
|
+
* g.addEdge('A', 'C');
|
|
3172
|
+
* g.addEdge('B', 'C');
|
|
3173
|
+
* console.log(g.incomingEdgesOf('C').length); // 2;
|
|
3174
|
+
*/
|
|
2598
3175
|
incomingEdgesOf(vertexOrKey) {
|
|
2599
3176
|
const target = this._getVertex(vertexOrKey);
|
|
2600
3177
|
if (target) {
|
|
@@ -2603,11 +3180,28 @@ var directedGraphTyped = (() => {
|
|
|
2603
3180
|
return [];
|
|
2604
3181
|
}
|
|
2605
3182
|
/**
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
|
|
3183
|
+
* Outgoing edges of a vertex.
|
|
3184
|
+
* @param vertexOrKey - Vertex or key.
|
|
3185
|
+
* @returns Array of outgoing edges.
|
|
3186
|
+
* @remarks Time O(deg_out), Space O(deg_out)
|
|
3187
|
+
|
|
3188
|
+
|
|
3189
|
+
|
|
3190
|
+
|
|
3191
|
+
|
|
3192
|
+
|
|
3193
|
+
|
|
3194
|
+
|
|
3195
|
+
* @example
|
|
3196
|
+
* // Get outgoing edges
|
|
3197
|
+
* const g = new DirectedGraph();
|
|
3198
|
+
* g.addVertex('A');
|
|
3199
|
+
* g.addVertex('B');
|
|
3200
|
+
* g.addVertex('C');
|
|
3201
|
+
* g.addEdge('A', 'B');
|
|
3202
|
+
* g.addEdge('A', 'C');
|
|
3203
|
+
* console.log(g.outgoingEdgesOf('A').length); // 2;
|
|
3204
|
+
*/
|
|
2611
3205
|
outgoingEdgesOf(vertexOrKey) {
|
|
2612
3206
|
const target = this._getVertex(vertexOrKey);
|
|
2613
3207
|
if (target) {
|
|
@@ -2666,11 +3260,44 @@ var directedGraphTyped = (() => {
|
|
|
2666
3260
|
return destinations;
|
|
2667
3261
|
}
|
|
2668
3262
|
/**
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
3263
|
+
* Topological sort if DAG; returns `undefined` if a cycle exists.
|
|
3264
|
+
* @param propertyName - `'key'` to map to keys; `'vertex'` to keep instances.
|
|
3265
|
+
* @returns Array of keys/vertices, or `undefined` when cycle is found.
|
|
3266
|
+
* @remarks Time O(V + E), Space O(V)
|
|
3267
|
+
|
|
3268
|
+
|
|
3269
|
+
|
|
3270
|
+
|
|
3271
|
+
|
|
3272
|
+
|
|
3273
|
+
|
|
3274
|
+
|
|
3275
|
+
|
|
3276
|
+
|
|
3277
|
+
|
|
3278
|
+
* @example
|
|
3279
|
+
* // DirectedGraph topologicalSort for task scheduling
|
|
3280
|
+
* const graph = new DirectedGraph<string>();
|
|
3281
|
+
*
|
|
3282
|
+
* // Build a DAG (Directed Acyclic Graph) for task dependencies
|
|
3283
|
+
* graph.addVertex('Design');
|
|
3284
|
+
* graph.addVertex('Implement');
|
|
3285
|
+
* graph.addVertex('Test');
|
|
3286
|
+
* graph.addVertex('Deploy');
|
|
3287
|
+
*
|
|
3288
|
+
* // Add dependency edges
|
|
3289
|
+
* graph.addEdge('Design', 'Implement', 1); // Design must come before Implement
|
|
3290
|
+
* graph.addEdge('Implement', 'Test', 1); // Implement must come before Test
|
|
3291
|
+
* graph.addEdge('Test', 'Deploy', 1); // Test must come before Deploy
|
|
3292
|
+
*
|
|
3293
|
+
* // Topological sort gives valid execution order
|
|
3294
|
+
* const executionOrder = graph.topologicalSort();
|
|
3295
|
+
* console.log(executionOrder); // defined;
|
|
3296
|
+
* console.log(executionOrder); // ['Design', 'Implement', 'Test', 'Deploy'];
|
|
3297
|
+
*
|
|
3298
|
+
* // All vertices should be included
|
|
3299
|
+
* console.log(executionOrder?.length); // 4;
|
|
3300
|
+
*/
|
|
2674
3301
|
topologicalSort(propertyName) {
|
|
2675
3302
|
propertyName = propertyName != null ? propertyName : "key";
|
|
2676
3303
|
const statusMap = /* @__PURE__ */ new Map();
|
|
@@ -2702,6 +3329,24 @@ var directedGraphTyped = (() => {
|
|
|
2702
3329
|
if (propertyName === "key") sorted = sorted.map((vertex) => vertex instanceof DirectedVertex ? vertex.key : vertex);
|
|
2703
3330
|
return sorted.reverse();
|
|
2704
3331
|
}
|
|
3332
|
+
/**
|
|
3333
|
+
* Get all edges
|
|
3334
|
+
|
|
3335
|
+
|
|
3336
|
+
|
|
3337
|
+
|
|
3338
|
+
|
|
3339
|
+
|
|
3340
|
+
|
|
3341
|
+
|
|
3342
|
+
* @example
|
|
3343
|
+
* // Get all edges
|
|
3344
|
+
* const g = new DirectedGraph();
|
|
3345
|
+
* g.addVertex('A');
|
|
3346
|
+
* g.addVertex('B');
|
|
3347
|
+
* g.addEdge('A', 'B', 3);
|
|
3348
|
+
* console.log(g.edgeSet().length); // 1;
|
|
3349
|
+
*/
|
|
2705
3350
|
edgeSet() {
|
|
2706
3351
|
let edgeMap = [];
|
|
2707
3352
|
this._outEdgeMap.forEach((outEdges) => {
|
|
@@ -2709,6 +3354,28 @@ var directedGraphTyped = (() => {
|
|
|
2709
3354
|
});
|
|
2710
3355
|
return edgeMap;
|
|
2711
3356
|
}
|
|
3357
|
+
/**
|
|
3358
|
+
* Get outgoing neighbors
|
|
3359
|
+
|
|
3360
|
+
|
|
3361
|
+
|
|
3362
|
+
|
|
3363
|
+
|
|
3364
|
+
|
|
3365
|
+
|
|
3366
|
+
|
|
3367
|
+
|
|
3368
|
+
* @example
|
|
3369
|
+
* // Get outgoing neighbors
|
|
3370
|
+
* const g = new DirectedGraph();
|
|
3371
|
+
* g.addVertex('A');
|
|
3372
|
+
* g.addVertex('B');
|
|
3373
|
+
* g.addVertex('C');
|
|
3374
|
+
* g.addEdge('A', 'B');
|
|
3375
|
+
* g.addEdge('A', 'C');
|
|
3376
|
+
* const neighbors = g.getNeighbors('A');
|
|
3377
|
+
* console.log(neighbors.map(v => v.key).sort()); // ['B', 'C'];
|
|
3378
|
+
*/
|
|
2712
3379
|
getNeighbors(vertexOrKey) {
|
|
2713
3380
|
const neighbors = [];
|
|
2714
3381
|
const vertex = this._getVertex(vertexOrKey);
|
|
@@ -2766,10 +3433,31 @@ var directedGraphTyped = (() => {
|
|
|
2766
3433
|
return super.clone();
|
|
2767
3434
|
}
|
|
2768
3435
|
/**
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
3436
|
+
* Tarjan's algorithm for strongly connected components.
|
|
3437
|
+
* @returns `{ dfnMap, lowMap, SCCs }`.
|
|
3438
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
3439
|
+
|
|
3440
|
+
|
|
3441
|
+
|
|
3442
|
+
|
|
3443
|
+
|
|
3444
|
+
|
|
3445
|
+
|
|
3446
|
+
|
|
3447
|
+
* @example
|
|
3448
|
+
* // Find strongly connected components
|
|
3449
|
+
* const g = new DirectedGraph();
|
|
3450
|
+
* g.addVertex('A');
|
|
3451
|
+
* g.addVertex('B');
|
|
3452
|
+
* g.addVertex('C');
|
|
3453
|
+
* g.addEdge('A', 'B');
|
|
3454
|
+
* g.addEdge('B', 'C');
|
|
3455
|
+
* g.addEdge('C', 'A');
|
|
3456
|
+
* const { SCCs } = g.tarjan();
|
|
3457
|
+
* // A→B→C→A forms one SCC with 3 members
|
|
3458
|
+
* const sccArrays = [...SCCs.values()];
|
|
3459
|
+
* console.log(sccArrays.some(scc => scc.length === 3)); // true;
|
|
3460
|
+
*/
|
|
2773
3461
|
tarjan() {
|
|
2774
3462
|
const dfnMap = /* @__PURE__ */ new Map();
|
|
2775
3463
|
const lowMap = /* @__PURE__ */ new Map();
|
|
@@ -2827,10 +3515,29 @@ var directedGraphTyped = (() => {
|
|
|
2827
3515
|
return this.tarjan().lowMap;
|
|
2828
3516
|
}
|
|
2829
3517
|
/**
|
|
2830
|
-
|
|
2831
|
-
|
|
2832
|
-
|
|
2833
|
-
|
|
3518
|
+
* Strongly connected components computed by `tarjan()`.
|
|
3519
|
+
* @returns Map from SCC id to vertices.
|
|
3520
|
+
* @remarks Time O(#SCC + V), Space O(V)
|
|
3521
|
+
|
|
3522
|
+
|
|
3523
|
+
|
|
3524
|
+
|
|
3525
|
+
|
|
3526
|
+
|
|
3527
|
+
|
|
3528
|
+
|
|
3529
|
+
* @example
|
|
3530
|
+
* // Get strongly connected components
|
|
3531
|
+
* const g = new DirectedGraph();
|
|
3532
|
+
* g.addVertex(1);
|
|
3533
|
+
* g.addVertex(2);
|
|
3534
|
+
* g.addVertex(3);
|
|
3535
|
+
* g.addEdge(1, 2);
|
|
3536
|
+
* g.addEdge(2, 3);
|
|
3537
|
+
* g.addEdge(3, 1);
|
|
3538
|
+
* const sccs = g.getSCCs(); // Map<number, VO[]>
|
|
3539
|
+
* console.log(sccs.size); // >= 1;
|
|
3540
|
+
*/
|
|
2834
3541
|
getSCCs() {
|
|
2835
3542
|
return this.tarjan().SCCs;
|
|
2836
3543
|
}
|