directed-graph-typed 2.4.4 → 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 +1010 -188
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +1011 -187
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +1010 -189
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +1011 -188
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/common/error.d.ts +23 -0
- package/dist/types/common/index.d.ts +1 -0
- 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 +439 -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 +217 -31
- 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/abstract-graph.d.ts +44 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -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 +313 -66
- 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/types/types/data-structures/queue/deque.d.ts +6 -0
- package/dist/umd/directed-graph-typed.js +1009 -185
- package/dist/umd/directed-graph-typed.js.map +1 -1
- package/dist/umd/directed-graph-typed.min.js +3 -1
- package/dist/umd/directed-graph-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/common/error.ts +60 -0
- package/src/common/index.ts +2 -0
- package/src/data-structures/base/iterable-element-base.ts +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +303 -247
- package/src/data-structures/binary-tree/binary-tree.ts +542 -121
- package/src/data-structures/binary-tree/bst.ts +346 -37
- package/src/data-structures/binary-tree/red-black-tree.ts +309 -96
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1292 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +1098 -215
- package/src/data-structures/binary-tree/tree-multi-set.ts +863 -69
- package/src/data-structures/binary-tree/tree-set.ts +1143 -15
- package/src/data-structures/graph/abstract-graph.ts +106 -1
- package/src/data-structures/graph/directed-graph.ts +223 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +299 -59
- package/src/data-structures/hash/hash-map.ts +243 -79
- package/src/data-structures/heap/heap.ts +291 -102
- package/src/data-structures/heap/max-heap.ts +48 -3
- 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 +425 -22
- package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
- 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 +343 -68
- 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 +215 -44
- 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
- package/src/types/data-structures/queue/deque.ts +7 -0
- package/src/utils/utils.ts +4 -2
|
@@ -30,6 +30,7 @@ var directedGraphTyped = (() => {
|
|
|
30
30
|
DirectedEdge: () => DirectedEdge,
|
|
31
31
|
DirectedGraph: () => DirectedGraph,
|
|
32
32
|
DirectedVertex: () => DirectedVertex,
|
|
33
|
+
ERR: () => ERR,
|
|
33
34
|
Range: () => Range
|
|
34
35
|
});
|
|
35
36
|
|
|
@@ -54,6 +55,52 @@ var directedGraphTyped = (() => {
|
|
|
54
55
|
return result;
|
|
55
56
|
};
|
|
56
57
|
|
|
58
|
+
// src/common/error.ts
|
|
59
|
+
var ERR = {
|
|
60
|
+
// Range / index
|
|
61
|
+
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
62
|
+
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
63
|
+
// Type / argument
|
|
64
|
+
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
65
|
+
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
66
|
+
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
67
|
+
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
68
|
+
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
69
|
+
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
70
|
+
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
71
|
+
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
72
|
+
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
73
|
+
// State / operation
|
|
74
|
+
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
75
|
+
// Matrix
|
|
76
|
+
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
77
|
+
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
78
|
+
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
79
|
+
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
80
|
+
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// src/common/index.ts
|
|
84
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
85
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
86
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
87
|
+
return DFSOperation2;
|
|
88
|
+
})(DFSOperation || {});
|
|
89
|
+
var Range = class {
|
|
90
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
91
|
+
this.low = low;
|
|
92
|
+
this.high = high;
|
|
93
|
+
this.includeLow = includeLow;
|
|
94
|
+
this.includeHigh = includeHigh;
|
|
95
|
+
}
|
|
96
|
+
// Determine whether a key is within the range
|
|
97
|
+
isInRange(key, comparator) {
|
|
98
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
99
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
100
|
+
return lowCheck && highCheck;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
57
104
|
// src/data-structures/base/iterable-entry-base.ts
|
|
58
105
|
var IterableEntryBase = class {
|
|
59
106
|
/**
|
|
@@ -468,7 +515,7 @@ var directedGraphTyped = (() => {
|
|
|
468
515
|
__publicField(this, "_elements", []);
|
|
469
516
|
__publicField(this, "_DEFAULT_COMPARATOR", (a, b) => {
|
|
470
517
|
if (typeof a === "object" || typeof b === "object") {
|
|
471
|
-
throw TypeError(
|
|
518
|
+
throw new TypeError(ERR.comparatorRequired("Heap"));
|
|
472
519
|
}
|
|
473
520
|
if (a > b) return 1;
|
|
474
521
|
if (a < b) return -1;
|
|
@@ -490,10 +537,30 @@ var directedGraphTyped = (() => {
|
|
|
490
537
|
return this._elements;
|
|
491
538
|
}
|
|
492
539
|
/**
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
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
|
+
*/
|
|
497
564
|
get size() {
|
|
498
565
|
return this.elements.length;
|
|
499
566
|
}
|
|
@@ -532,21 +599,61 @@ var directedGraphTyped = (() => {
|
|
|
532
599
|
return new _Heap(elements, options);
|
|
533
600
|
}
|
|
534
601
|
/**
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
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
|
+
*/
|
|
540
633
|
add(element) {
|
|
541
634
|
this._elements.push(element);
|
|
542
635
|
return this._bubbleUp(this.elements.length - 1);
|
|
543
636
|
}
|
|
544
637
|
/**
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
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
|
+
*/
|
|
550
657
|
addMany(elements) {
|
|
551
658
|
const flags = [];
|
|
552
659
|
for (const el of elements) {
|
|
@@ -561,10 +668,46 @@ var directedGraphTyped = (() => {
|
|
|
561
668
|
return flags;
|
|
562
669
|
}
|
|
563
670
|
/**
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
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
|
+
*/
|
|
568
711
|
poll() {
|
|
569
712
|
if (this.elements.length === 0) return;
|
|
570
713
|
const value = this.elements[0];
|
|
@@ -576,26 +719,125 @@ var directedGraphTyped = (() => {
|
|
|
576
719
|
return value;
|
|
577
720
|
}
|
|
578
721
|
/**
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
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
|
+
*/
|
|
583
796
|
peek() {
|
|
584
797
|
return this.elements[0];
|
|
585
798
|
}
|
|
586
799
|
/**
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
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
|
+
*/
|
|
591
819
|
isEmpty() {
|
|
592
820
|
return this.size === 0;
|
|
593
821
|
}
|
|
594
822
|
/**
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
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
|
+
*/
|
|
599
841
|
clear() {
|
|
600
842
|
this._elements = [];
|
|
601
843
|
}
|
|
@@ -610,21 +852,41 @@ var directedGraphTyped = (() => {
|
|
|
610
852
|
return this.fix();
|
|
611
853
|
}
|
|
612
854
|
/**
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
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
|
+
*/
|
|
618
867
|
has(element) {
|
|
619
868
|
for (const el of this.elements) if (this._equals(el, element)) return true;
|
|
620
869
|
return false;
|
|
621
870
|
}
|
|
622
871
|
/**
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
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
|
+
*/
|
|
628
890
|
delete(element) {
|
|
629
891
|
let index = -1;
|
|
630
892
|
for (let i = 0; i < this.elements.length; i++) {
|
|
@@ -682,11 +944,18 @@ var directedGraphTyped = (() => {
|
|
|
682
944
|
return this;
|
|
683
945
|
}
|
|
684
946
|
/**
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
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
|
+
*/
|
|
690
959
|
dfs(order = "PRE") {
|
|
691
960
|
const result = [];
|
|
692
961
|
const _dfs = (index) => {
|
|
@@ -723,10 +992,26 @@ var directedGraphTyped = (() => {
|
|
|
723
992
|
return results;
|
|
724
993
|
}
|
|
725
994
|
/**
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
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
|
+
*/
|
|
730
1015
|
sort() {
|
|
731
1016
|
const visited = [];
|
|
732
1017
|
const cloned = this._createInstance();
|
|
@@ -738,22 +1023,52 @@ var directedGraphTyped = (() => {
|
|
|
738
1023
|
return visited;
|
|
739
1024
|
}
|
|
740
1025
|
/**
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
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
|
+
*/
|
|
745
1046
|
clone() {
|
|
746
1047
|
const next = this._createInstance();
|
|
747
1048
|
for (const x of this.elements) next.add(x);
|
|
748
1049
|
return next;
|
|
749
1050
|
}
|
|
750
1051
|
/**
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
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
|
+
*/
|
|
757
1072
|
filter(callback, thisArg) {
|
|
758
1073
|
const out = this._createInstance();
|
|
759
1074
|
let i = 0;
|
|
@@ -767,18 +1082,31 @@ var directedGraphTyped = (() => {
|
|
|
767
1082
|
return out;
|
|
768
1083
|
}
|
|
769
1084
|
/**
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
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
|
+
*/
|
|
779
1107
|
map(callback, options, thisArg) {
|
|
780
1108
|
const { comparator, toElementFn, ...rest } = options != null ? options : {};
|
|
781
|
-
if (!comparator) throw new TypeError("Heap.map
|
|
1109
|
+
if (!comparator) throw new TypeError(ERR.comparatorRequired("Heap.map"));
|
|
782
1110
|
const out = this._createLike([], { ...rest, comparator, toElementFn });
|
|
783
1111
|
let i = 0;
|
|
784
1112
|
for (const x of this) {
|
|
@@ -1123,18 +1451,52 @@ var directedGraphTyped = (() => {
|
|
|
1123
1451
|
this._autoCompactRatio = value;
|
|
1124
1452
|
}
|
|
1125
1453
|
/**
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
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
|
+
*/
|
|
1130
1476
|
get length() {
|
|
1131
1477
|
return this.elements.length - this._offset;
|
|
1132
1478
|
}
|
|
1133
1479
|
/**
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
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
|
+
*/
|
|
1138
1500
|
get first() {
|
|
1139
1501
|
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
1140
1502
|
}
|
|
@@ -1157,19 +1519,69 @@ var directedGraphTyped = (() => {
|
|
|
1157
1519
|
return new _Queue(elements);
|
|
1158
1520
|
}
|
|
1159
1521
|
/**
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
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
|
+
*/
|
|
1164
1555
|
isEmpty() {
|
|
1165
1556
|
return this.length === 0;
|
|
1166
1557
|
}
|
|
1167
1558
|
/**
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
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
|
+
*/
|
|
1173
1585
|
push(element) {
|
|
1174
1586
|
this.elements.push(element);
|
|
1175
1587
|
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
@@ -1190,10 +1602,35 @@ var directedGraphTyped = (() => {
|
|
|
1190
1602
|
return ans;
|
|
1191
1603
|
}
|
|
1192
1604
|
/**
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
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
|
+
*/
|
|
1197
1634
|
shift() {
|
|
1198
1635
|
if (this.length === 0) return void 0;
|
|
1199
1636
|
const first = this.first;
|
|
@@ -1202,11 +1639,24 @@ var directedGraphTyped = (() => {
|
|
|
1202
1639
|
return first;
|
|
1203
1640
|
}
|
|
1204
1641
|
/**
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
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
|
+
*/
|
|
1210
1660
|
delete(element) {
|
|
1211
1661
|
for (let i = this._offset; i < this.elements.length; i++) {
|
|
1212
1662
|
if (Object.is(this.elements[i], element)) {
|
|
@@ -1217,11 +1667,24 @@ var directedGraphTyped = (() => {
|
|
|
1217
1667
|
return false;
|
|
1218
1668
|
}
|
|
1219
1669
|
/**
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
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
|
+
*/
|
|
1225
1688
|
at(index) {
|
|
1226
1689
|
if (index < 0 || index >= this.length) return void 0;
|
|
1227
1690
|
return this._elements[this._offset + index];
|
|
@@ -1273,19 +1736,48 @@ var directedGraphTyped = (() => {
|
|
|
1273
1736
|
return this;
|
|
1274
1737
|
}
|
|
1275
1738
|
/**
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
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
|
+
*/
|
|
1280
1757
|
clear() {
|
|
1281
1758
|
this._elements = [];
|
|
1282
1759
|
this._offset = 0;
|
|
1283
1760
|
}
|
|
1284
1761
|
/**
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
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
|
+
*/
|
|
1289
1781
|
compact() {
|
|
1290
1782
|
this._elements = this.elements.slice(this._offset);
|
|
1291
1783
|
this._offset = 0;
|
|
@@ -1311,10 +1803,26 @@ var directedGraphTyped = (() => {
|
|
|
1311
1803
|
return removed;
|
|
1312
1804
|
}
|
|
1313
1805
|
/**
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
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
|
+
*/
|
|
1318
1826
|
clone() {
|
|
1319
1827
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1320
1828
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -1322,12 +1830,26 @@ var directedGraphTyped = (() => {
|
|
|
1322
1830
|
return out;
|
|
1323
1831
|
}
|
|
1324
1832
|
/**
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
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
|
+
*/
|
|
1331
1853
|
filter(predicate, thisArg) {
|
|
1332
1854
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1333
1855
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -1339,15 +1861,28 @@ var directedGraphTyped = (() => {
|
|
|
1339
1861
|
return out;
|
|
1340
1862
|
}
|
|
1341
1863
|
/**
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
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
|
+
*/
|
|
1351
1886
|
map(callback, options, thisArg) {
|
|
1352
1887
|
var _a, _b;
|
|
1353
1888
|
const out = new this.constructor([], {
|
|
@@ -1570,7 +2105,7 @@ var directedGraphTyped = (() => {
|
|
|
1570
2105
|
const newEdge = this.createEdge(srcOrEdge, dest, weight, value);
|
|
1571
2106
|
return this._addEdge(newEdge);
|
|
1572
2107
|
} else {
|
|
1573
|
-
throw new
|
|
2108
|
+
throw new TypeError(ERR.invalidArgument("dest must be a Vertex or vertex key when srcOrEdge is an Edge.", "Graph"));
|
|
1574
2109
|
}
|
|
1575
2110
|
}
|
|
1576
2111
|
}
|
|
@@ -2254,6 +2789,94 @@ var directedGraphTyped = (() => {
|
|
|
2254
2789
|
_getVertexKey(vertexOrKey) {
|
|
2255
2790
|
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|
|
2256
2791
|
}
|
|
2792
|
+
/**
|
|
2793
|
+
* The edge connector string used in visual output.
|
|
2794
|
+
* Override in subclasses (e.g., '--' for undirected, '->' for directed).
|
|
2795
|
+
*/
|
|
2796
|
+
get _edgeConnector() {
|
|
2797
|
+
return "--";
|
|
2798
|
+
}
|
|
2799
|
+
/**
|
|
2800
|
+
* Generate a text-based visual representation of the graph.
|
|
2801
|
+
*
|
|
2802
|
+
* **Adjacency list format:**
|
|
2803
|
+
* ```
|
|
2804
|
+
* Graph (5 vertices, 6 edges):
|
|
2805
|
+
* A -> B (1), C (2)
|
|
2806
|
+
* B -> D (3)
|
|
2807
|
+
* C -> (no outgoing edges)
|
|
2808
|
+
* D -> A (1)
|
|
2809
|
+
* E (isolated)
|
|
2810
|
+
* ```
|
|
2811
|
+
*
|
|
2812
|
+
* @param options - Optional display settings.
|
|
2813
|
+
* @param options.showWeight - Whether to show edge weights (default: true).
|
|
2814
|
+
* @returns The visual string.
|
|
2815
|
+
*/
|
|
2816
|
+
toVisual(options) {
|
|
2817
|
+
var _a;
|
|
2818
|
+
const showWeight = (_a = options == null ? void 0 : options.showWeight) != null ? _a : true;
|
|
2819
|
+
const vertices = [...this._vertexMap.values()];
|
|
2820
|
+
const vertexCount = vertices.length;
|
|
2821
|
+
const edgeCount = this.edgeSet().length;
|
|
2822
|
+
const lines = [`Graph (${vertexCount} vertices, ${edgeCount} edges):`];
|
|
2823
|
+
for (const vertex of vertices) {
|
|
2824
|
+
const neighbors = this.getNeighbors(vertex);
|
|
2825
|
+
if (neighbors.length === 0) {
|
|
2826
|
+
lines.push(` ${vertex.key} (isolated)`);
|
|
2827
|
+
} else {
|
|
2828
|
+
const edgeStrs = neighbors.map((neighbor) => {
|
|
2829
|
+
const edge = this.getEdge(vertex, neighbor);
|
|
2830
|
+
if (edge && showWeight && edge.weight !== void 0 && edge.weight !== 1) {
|
|
2831
|
+
return `${neighbor.key} (${edge.weight})`;
|
|
2832
|
+
}
|
|
2833
|
+
return `${neighbor.key}`;
|
|
2834
|
+
});
|
|
2835
|
+
lines.push(` ${vertex.key} ${this._edgeConnector} ${edgeStrs.join(", ")}`);
|
|
2836
|
+
}
|
|
2837
|
+
}
|
|
2838
|
+
return lines.join("\n");
|
|
2839
|
+
}
|
|
2840
|
+
/**
|
|
2841
|
+
* Generate DOT language representation for Graphviz.
|
|
2842
|
+
*
|
|
2843
|
+
* @param options - Optional display settings.
|
|
2844
|
+
* @param options.name - Graph name (default: 'G').
|
|
2845
|
+
* @param options.showWeight - Whether to label edges with weight (default: true).
|
|
2846
|
+
* @returns DOT format string.
|
|
2847
|
+
*/
|
|
2848
|
+
toDot(options) {
|
|
2849
|
+
var _a, _b;
|
|
2850
|
+
const name = (_a = options == null ? void 0 : options.name) != null ? _a : "G";
|
|
2851
|
+
const showWeight = (_b = options == null ? void 0 : options.showWeight) != null ? _b : true;
|
|
2852
|
+
const isDirected = this._edgeConnector === "->";
|
|
2853
|
+
const graphType = isDirected ? "digraph" : "graph";
|
|
2854
|
+
const edgeOp = isDirected ? "->" : "--";
|
|
2855
|
+
const lines = [`${graphType} ${name} {`];
|
|
2856
|
+
for (const vertex of this._vertexMap.values()) {
|
|
2857
|
+
lines.push(` "${vertex.key}";`);
|
|
2858
|
+
}
|
|
2859
|
+
const visited = /* @__PURE__ */ new Set();
|
|
2860
|
+
for (const vertex of this._vertexMap.values()) {
|
|
2861
|
+
for (const neighbor of this.getNeighbors(vertex)) {
|
|
2862
|
+
const edgeId = isDirected ? `${vertex.key}->${neighbor.key}` : [vertex.key, neighbor.key].sort().join("--");
|
|
2863
|
+
if (visited.has(edgeId)) continue;
|
|
2864
|
+
visited.add(edgeId);
|
|
2865
|
+
const edge = this.getEdge(vertex, neighbor);
|
|
2866
|
+
const label = edge && showWeight && edge.weight !== void 0 && edge.weight !== 1 ? ` [label="${edge.weight}"]` : "";
|
|
2867
|
+
lines.push(` "${vertex.key}" ${edgeOp} "${neighbor.key}"${label};`);
|
|
2868
|
+
}
|
|
2869
|
+
}
|
|
2870
|
+
lines.push("}");
|
|
2871
|
+
return lines.join("\n");
|
|
2872
|
+
}
|
|
2873
|
+
/**
|
|
2874
|
+
* Print the graph to console.
|
|
2875
|
+
* @param options - Display settings passed to `toVisual`.
|
|
2876
|
+
*/
|
|
2877
|
+
print(options) {
|
|
2878
|
+
console.log(this.toVisual(options));
|
|
2879
|
+
}
|
|
2257
2880
|
};
|
|
2258
2881
|
|
|
2259
2882
|
// src/data-structures/graph/directed-graph.ts
|
|
@@ -2282,6 +2905,9 @@ var directedGraphTyped = (() => {
|
|
|
2282
2905
|
__publicField(this, "_outEdgeMap", /* @__PURE__ */ new Map());
|
|
2283
2906
|
__publicField(this, "_inEdgeMap", /* @__PURE__ */ new Map());
|
|
2284
2907
|
}
|
|
2908
|
+
get _edgeConnector() {
|
|
2909
|
+
return "->";
|
|
2910
|
+
}
|
|
2285
2911
|
get outEdgeMap() {
|
|
2286
2912
|
return this._outEdgeMap;
|
|
2287
2913
|
}
|
|
@@ -2344,12 +2970,28 @@ var directedGraphTyped = (() => {
|
|
|
2344
2970
|
return new DirectedEdge(src, dest, (_a = weight != null ? weight : this.options.defaultEdgeWeight) != null ? _a : 1, value);
|
|
2345
2971
|
}
|
|
2346
2972
|
/**
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
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
|
+
*/
|
|
2353
2995
|
getEdge(srcOrKey, destOrKey) {
|
|
2354
2996
|
let edgeMap = [];
|
|
2355
2997
|
if (srcOrKey !== void 0 && destOrKey !== void 0) {
|
|
@@ -2389,12 +3031,48 @@ var directedGraphTyped = (() => {
|
|
|
2389
3031
|
return removed;
|
|
2390
3032
|
}
|
|
2391
3033
|
/**
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
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
|
+
*/
|
|
2398
3076
|
deleteEdge(edgeOrSrcVertexKey, destVertexKey) {
|
|
2399
3077
|
let removed = void 0;
|
|
2400
3078
|
let src, dest;
|
|
@@ -2421,6 +3099,26 @@ var directedGraphTyped = (() => {
|
|
|
2421
3099
|
}
|
|
2422
3100
|
return removed;
|
|
2423
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
|
+
*/
|
|
2424
3122
|
deleteVertex(vertexOrKey) {
|
|
2425
3123
|
let vertexKey;
|
|
2426
3124
|
let vertex;
|
|
@@ -2452,11 +3150,28 @@ var directedGraphTyped = (() => {
|
|
|
2452
3150
|
return removed;
|
|
2453
3151
|
}
|
|
2454
3152
|
/**
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
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
|
+
*/
|
|
2460
3175
|
incomingEdgesOf(vertexOrKey) {
|
|
2461
3176
|
const target = this._getVertex(vertexOrKey);
|
|
2462
3177
|
if (target) {
|
|
@@ -2465,11 +3180,28 @@ var directedGraphTyped = (() => {
|
|
|
2465
3180
|
return [];
|
|
2466
3181
|
}
|
|
2467
3182
|
/**
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
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
|
+
*/
|
|
2473
3205
|
outgoingEdgesOf(vertexOrKey) {
|
|
2474
3206
|
const target = this._getVertex(vertexOrKey);
|
|
2475
3207
|
if (target) {
|
|
@@ -2528,11 +3260,44 @@ var directedGraphTyped = (() => {
|
|
|
2528
3260
|
return destinations;
|
|
2529
3261
|
}
|
|
2530
3262
|
/**
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
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
|
+
*/
|
|
2536
3301
|
topologicalSort(propertyName) {
|
|
2537
3302
|
propertyName = propertyName != null ? propertyName : "key";
|
|
2538
3303
|
const statusMap = /* @__PURE__ */ new Map();
|
|
@@ -2564,6 +3329,24 @@ var directedGraphTyped = (() => {
|
|
|
2564
3329
|
if (propertyName === "key") sorted = sorted.map((vertex) => vertex instanceof DirectedVertex ? vertex.key : vertex);
|
|
2565
3330
|
return sorted.reverse();
|
|
2566
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
|
+
*/
|
|
2567
3350
|
edgeSet() {
|
|
2568
3351
|
let edgeMap = [];
|
|
2569
3352
|
this._outEdgeMap.forEach((outEdges) => {
|
|
@@ -2571,6 +3354,28 @@ var directedGraphTyped = (() => {
|
|
|
2571
3354
|
});
|
|
2572
3355
|
return edgeMap;
|
|
2573
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
|
+
*/
|
|
2574
3379
|
getNeighbors(vertexOrKey) {
|
|
2575
3380
|
const neighbors = [];
|
|
2576
3381
|
const vertex = this._getVertex(vertexOrKey);
|
|
@@ -2628,10 +3433,31 @@ var directedGraphTyped = (() => {
|
|
|
2628
3433
|
return super.clone();
|
|
2629
3434
|
}
|
|
2630
3435
|
/**
|
|
2631
|
-
|
|
2632
|
-
|
|
2633
|
-
|
|
2634
|
-
|
|
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
|
+
*/
|
|
2635
3461
|
tarjan() {
|
|
2636
3462
|
const dfnMap = /* @__PURE__ */ new Map();
|
|
2637
3463
|
const lowMap = /* @__PURE__ */ new Map();
|
|
@@ -2689,10 +3515,29 @@ var directedGraphTyped = (() => {
|
|
|
2689
3515
|
return this.tarjan().lowMap;
|
|
2690
3516
|
}
|
|
2691
3517
|
/**
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
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
|
+
*/
|
|
2696
3541
|
getSCCs() {
|
|
2697
3542
|
return this.tarjan().SCCs;
|
|
2698
3543
|
}
|
|
@@ -2727,27 +3572,6 @@ var directedGraphTyped = (() => {
|
|
|
2727
3572
|
}
|
|
2728
3573
|
}
|
|
2729
3574
|
};
|
|
2730
|
-
|
|
2731
|
-
// src/common/index.ts
|
|
2732
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
2733
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
2734
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
2735
|
-
return DFSOperation2;
|
|
2736
|
-
})(DFSOperation || {});
|
|
2737
|
-
var Range = class {
|
|
2738
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
2739
|
-
this.low = low;
|
|
2740
|
-
this.high = high;
|
|
2741
|
-
this.includeLow = includeLow;
|
|
2742
|
-
this.includeHigh = includeHigh;
|
|
2743
|
-
}
|
|
2744
|
-
// Determine whether a key is within the range
|
|
2745
|
-
isInRange(key, comparator) {
|
|
2746
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
2747
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
2748
|
-
return lowCheck && highCheck;
|
|
2749
|
-
}
|
|
2750
|
-
};
|
|
2751
3575
|
return __toCommonJS(src_exports);
|
|
2752
3576
|
})();
|
|
2753
3577
|
/**
|