@graphty/algorithms 1.3.1 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +358 -413
- package/dist/algorithms.js +564 -261
- package/dist/algorithms.js.map +1 -1
- package/dist/src/algorithms/centrality/betweenness.d.ts +10 -0
- package/dist/src/algorithms/centrality/betweenness.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/betweenness.js +27 -6
- package/dist/src/algorithms/centrality/betweenness.js.map +1 -1
- package/dist/src/algorithms/centrality/closeness.d.ts +11 -2
- package/dist/src/algorithms/centrality/closeness.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/closeness.js +17 -3
- package/dist/src/algorithms/centrality/closeness.js.map +1 -1
- package/dist/src/algorithms/centrality/degree.d.ts +7 -0
- package/dist/src/algorithms/centrality/degree.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/degree.js +9 -6
- package/dist/src/algorithms/centrality/degree.js.map +1 -1
- package/dist/src/algorithms/centrality/delta-pagerank-simple.d.ts +21 -3
- package/dist/src/algorithms/centrality/delta-pagerank-simple.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/delta-pagerank-simple.js +25 -7
- package/dist/src/algorithms/centrality/delta-pagerank-simple.js.map +1 -1
- package/dist/src/algorithms/centrality/delta-pagerank.d.ts +28 -2
- package/dist/src/algorithms/centrality/delta-pagerank.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/delta-pagerank.js +30 -4
- package/dist/src/algorithms/centrality/delta-pagerank.js.map +1 -1
- package/dist/src/algorithms/centrality/eigenvector.d.ts +10 -3
- package/dist/src/algorithms/centrality/eigenvector.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/eigenvector.js +11 -4
- package/dist/src/algorithms/centrality/eigenvector.js.map +1 -1
- package/dist/src/algorithms/centrality/hits.d.ts +10 -3
- package/dist/src/algorithms/centrality/hits.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/hits.js +11 -4
- package/dist/src/algorithms/centrality/hits.js.map +1 -1
- package/dist/src/algorithms/centrality/index.d.ts +1 -1
- package/dist/src/algorithms/centrality/index.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/index.js +1 -1
- package/dist/src/algorithms/centrality/index.js.map +1 -1
- package/dist/src/algorithms/centrality/katz.d.ts +10 -3
- package/dist/src/algorithms/centrality/katz.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/katz.js +13 -8
- package/dist/src/algorithms/centrality/katz.js.map +1 -1
- package/dist/src/algorithms/centrality/pagerank.d.ts +14 -0
- package/dist/src/algorithms/centrality/pagerank.d.ts.map +1 -1
- package/dist/src/algorithms/centrality/pagerank.js +16 -1
- package/dist/src/algorithms/centrality/pagerank.js.map +1 -1
- package/dist/src/algorithms/community/girvan-newman.d.ts +2 -3
- package/dist/src/algorithms/community/girvan-newman.d.ts.map +1 -1
- package/dist/src/algorithms/community/girvan-newman.js +21 -4
- package/dist/src/algorithms/community/girvan-newman.js.map +1 -1
- package/dist/src/algorithms/community/index.d.ts.map +1 -1
- package/dist/src/algorithms/community/index.js.map +1 -1
- package/dist/src/algorithms/community/label-propagation.d.ts +0 -3
- package/dist/src/algorithms/community/label-propagation.d.ts.map +1 -1
- package/dist/src/algorithms/community/label-propagation.js +13 -6
- package/dist/src/algorithms/community/label-propagation.js.map +1 -1
- package/dist/src/algorithms/community/leiden.d.ts +0 -1
- package/dist/src/algorithms/community/leiden.d.ts.map +1 -1
- package/dist/src/algorithms/community/leiden.js +37 -6
- package/dist/src/algorithms/community/leiden.js.map +1 -1
- package/dist/src/algorithms/community/louvain-optimized.d.ts +37 -0
- package/dist/src/algorithms/community/louvain-optimized.d.ts.map +1 -1
- package/dist/src/algorithms/community/louvain-optimized.js +42 -7
- package/dist/src/algorithms/community/louvain-optimized.js.map +1 -1
- package/dist/src/algorithms/community/louvain.d.ts +2 -3
- package/dist/src/algorithms/community/louvain.d.ts.map +1 -1
- package/dist/src/algorithms/community/louvain.js +19 -6
- package/dist/src/algorithms/community/louvain.js.map +1 -1
- package/dist/src/algorithms/community/modularity-utils.d.ts +0 -4
- package/dist/src/algorithms/community/modularity-utils.d.ts.map +1 -1
- package/dist/src/algorithms/community/modularity-utils.js +1 -5
- package/dist/src/algorithms/community/modularity-utils.js.map +1 -1
- package/dist/src/algorithms/components/connected.d.ts +23 -0
- package/dist/src/algorithms/components/connected.d.ts.map +1 -1
- package/dist/src/algorithms/components/connected.js +29 -3
- package/dist/src/algorithms/components/connected.js.map +1 -1
- package/dist/src/algorithms/matching/bipartite.d.ts +10 -0
- package/dist/src/algorithms/matching/bipartite.d.ts.map +1 -1
- package/dist/src/algorithms/matching/bipartite.js +10 -0
- package/dist/src/algorithms/matching/bipartite.js.map +1 -1
- package/dist/src/algorithms/matching/index.d.ts.map +1 -1
- package/dist/src/algorithms/matching/index.js.map +1 -1
- package/dist/src/algorithms/matching/isomorphism.d.ts +8 -0
- package/dist/src/algorithms/matching/isomorphism.d.ts.map +1 -1
- package/dist/src/algorithms/matching/isomorphism.js +36 -2
- package/dist/src/algorithms/matching/isomorphism.js.map +1 -1
- package/dist/src/algorithms/mst/index.d.ts.map +1 -1
- package/dist/src/algorithms/mst/index.js.map +1 -1
- package/dist/src/algorithms/mst/kruskal.d.ts +14 -0
- package/dist/src/algorithms/mst/kruskal.d.ts.map +1 -1
- package/dist/src/algorithms/mst/kruskal.js +17 -3
- package/dist/src/algorithms/mst/kruskal.js.map +1 -1
- package/dist/src/algorithms/mst/prim.d.ts +9 -0
- package/dist/src/algorithms/mst/prim.d.ts.map +1 -1
- package/dist/src/algorithms/mst/prim.js +9 -0
- package/dist/src/algorithms/mst/prim.js.map +1 -1
- package/dist/src/algorithms/shortest-path/bellman-ford.d.ts +10 -0
- package/dist/src/algorithms/shortest-path/bellman-ford.d.ts.map +1 -1
- package/dist/src/algorithms/shortest-path/bellman-ford.js +10 -0
- package/dist/src/algorithms/shortest-path/bellman-ford.js.map +1 -1
- package/dist/src/algorithms/shortest-path/bidirectional-dijkstra.d.ts +11 -4
- package/dist/src/algorithms/shortest-path/bidirectional-dijkstra.d.ts.map +1 -1
- package/dist/src/algorithms/shortest-path/bidirectional-dijkstra.js +18 -16
- package/dist/src/algorithms/shortest-path/bidirectional-dijkstra.js.map +1 -1
- package/dist/src/algorithms/shortest-path/dijkstra.d.ts +15 -0
- package/dist/src/algorithms/shortest-path/dijkstra.d.ts.map +1 -1
- package/dist/src/algorithms/shortest-path/dijkstra.js +15 -0
- package/dist/src/algorithms/shortest-path/dijkstra.js.map +1 -1
- package/dist/src/algorithms/shortest-path/floyd-warshall.d.ts +17 -0
- package/dist/src/algorithms/shortest-path/floyd-warshall.d.ts.map +1 -1
- package/dist/src/algorithms/shortest-path/floyd-warshall.js +17 -0
- package/dist/src/algorithms/shortest-path/floyd-warshall.js.map +1 -1
- package/dist/src/algorithms/shortest-path/index.d.ts.map +1 -1
- package/dist/src/algorithms/shortest-path/index.js.map +1 -1
- package/dist/src/algorithms/traversal/bfs-unified.d.ts +13 -0
- package/dist/src/algorithms/traversal/bfs-unified.d.ts.map +1 -1
- package/dist/src/algorithms/traversal/bfs-unified.js +37 -0
- package/dist/src/algorithms/traversal/bfs-unified.js.map +1 -1
- package/dist/src/algorithms/traversal/bfs-variants.d.ts +23 -0
- package/dist/src/algorithms/traversal/bfs-variants.d.ts.map +1 -1
- package/dist/src/algorithms/traversal/bfs-variants.js +34 -0
- package/dist/src/algorithms/traversal/bfs-variants.js.map +1 -1
- package/dist/src/algorithms/traversal/bfs.d.ts +1 -1
- package/dist/src/algorithms/traversal/bfs.d.ts.map +1 -1
- package/dist/src/algorithms/traversal/bfs.js +1 -1
- package/dist/src/algorithms/traversal/bfs.js.map +1 -1
- package/dist/src/algorithms/traversal/dfs.d.ts +10 -0
- package/dist/src/algorithms/traversal/dfs.d.ts.map +1 -1
- package/dist/src/algorithms/traversal/dfs.js +48 -0
- package/dist/src/algorithms/traversal/dfs.js.map +1 -1
- package/dist/src/algorithms/traversal/index.d.ts.map +1 -1
- package/dist/src/algorithms/traversal/index.js.map +1 -1
- package/dist/src/benchmark-all-algorithms.d.ts +2 -0
- package/dist/src/benchmark-all-algorithms.d.ts.map +1 -1
- package/dist/src/benchmark-all-algorithms.js +32 -3
- package/dist/src/benchmark-all-algorithms.js.map +1 -1
- package/dist/src/clustering/hierarchical.d.ts +8 -1
- package/dist/src/clustering/hierarchical.d.ts.map +1 -1
- package/dist/src/clustering/hierarchical.js +22 -6
- package/dist/src/clustering/hierarchical.js.map +1 -1
- package/dist/src/clustering/index.d.ts.map +1 -1
- package/dist/src/clustering/index.js.map +1 -1
- package/dist/src/clustering/k-core.d.ts +2 -5
- package/dist/src/clustering/k-core.d.ts.map +1 -1
- package/dist/src/clustering/k-core.js +14 -12
- package/dist/src/clustering/k-core.js.map +1 -1
- package/dist/src/clustering/mcl.d.ts +6 -0
- package/dist/src/clustering/mcl.d.ts.map +1 -1
- package/dist/src/clustering/mcl.js +34 -3
- package/dist/src/clustering/mcl.js.map +1 -1
- package/dist/src/clustering/spectral.d.ts +4 -1
- package/dist/src/clustering/spectral.d.ts.map +1 -1
- package/dist/src/clustering/spectral.js +61 -28
- package/dist/src/clustering/spectral.js.map +1 -1
- package/dist/src/core/graph.d.ts +45 -0
- package/dist/src/core/graph.d.ts.map +1 -1
- package/dist/src/core/graph.js +45 -1
- package/dist/src/core/graph.js.map +1 -1
- package/dist/src/data-structures/index.d.ts.map +1 -1
- package/dist/src/data-structures/index.js.map +1 -1
- package/dist/src/data-structures/priority-queue.d.ts +18 -1
- package/dist/src/data-structures/priority-queue.d.ts.map +1 -1
- package/dist/src/data-structures/priority-queue.js +26 -5
- package/dist/src/data-structures/priority-queue.js.map +1 -1
- package/dist/src/data-structures/union-find.d.ts +21 -0
- package/dist/src/data-structures/union-find.d.ts.map +1 -1
- package/dist/src/data-structures/union-find.js +21 -0
- package/dist/src/data-structures/union-find.js.map +1 -1
- package/dist/src/flow/ford-fulkerson.d.ts +4 -2
- package/dist/src/flow/ford-fulkerson.d.ts.map +1 -1
- package/dist/src/flow/ford-fulkerson.js +31 -2
- package/dist/src/flow/ford-fulkerson.js.map +1 -1
- package/dist/src/flow/min-cut.d.ts +0 -3
- package/dist/src/flow/min-cut.d.ts.map +1 -1
- package/dist/src/flow/min-cut.js +17 -4
- package/dist/src/flow/min-cut.js.map +1 -1
- package/dist/src/index.d.ts +2 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/link-prediction/adamic-adar.d.ts +26 -0
- package/dist/src/link-prediction/adamic-adar.d.ts.map +1 -1
- package/dist/src/link-prediction/adamic-adar.js +36 -10
- package/dist/src/link-prediction/adamic-adar.js.map +1 -1
- package/dist/src/link-prediction/common-neighbors.d.ts +21 -0
- package/dist/src/link-prediction/common-neighbors.d.ts.map +1 -1
- package/dist/src/link-prediction/common-neighbors.js +27 -6
- package/dist/src/link-prediction/common-neighbors.js.map +1 -1
- package/dist/src/link-prediction/index.d.ts.map +1 -1
- package/dist/src/optimized/bit-packed.d.ts +103 -53
- package/dist/src/optimized/bit-packed.d.ts.map +1 -1
- package/dist/src/optimized/bit-packed.js +104 -54
- package/dist/src/optimized/bit-packed.js.map +1 -1
- package/dist/src/optimized/csr-graph.d.ts +87 -24
- package/dist/src/optimized/csr-graph.d.ts.map +1 -1
- package/dist/src/optimized/csr-graph.js +87 -24
- package/dist/src/optimized/csr-graph.js.map +1 -1
- package/dist/src/optimized/direction-optimized-bfs.d.ts +37 -16
- package/dist/src/optimized/direction-optimized-bfs.d.ts.map +1 -1
- package/dist/src/optimized/direction-optimized-bfs.js +39 -20
- package/dist/src/optimized/direction-optimized-bfs.js.map +1 -1
- package/dist/src/optimized/graph-adapter.d.ts +58 -9
- package/dist/src/optimized/graph-adapter.d.ts.map +1 -1
- package/dist/src/optimized/graph-adapter.js +58 -10
- package/dist/src/optimized/graph-adapter.js.map +1 -1
- package/dist/src/optimized/index.d.ts +1 -1
- package/dist/src/optimized/index.d.ts.map +1 -1
- package/dist/src/optimized/index.js +3 -1
- package/dist/src/optimized/index.js.map +1 -1
- package/dist/src/pathfinding/astar.d.ts +25 -9
- package/dist/src/pathfinding/astar.d.ts.map +1 -1
- package/dist/src/pathfinding/astar.js +25 -10
- package/dist/src/pathfinding/astar.js.map +1 -1
- package/dist/src/pathfinding/utils.d.ts +15 -6
- package/dist/src/pathfinding/utils.d.ts.map +1 -1
- package/dist/src/pathfinding/utils.js +19 -7
- package/dist/src/pathfinding/utils.js.map +1 -1
- package/dist/src/research/grsbm.d.ts +0 -1
- package/dist/src/research/grsbm.d.ts.map +1 -1
- package/dist/src/research/grsbm.js +22 -9
- package/dist/src/research/grsbm.js.map +1 -1
- package/dist/src/research/index.d.ts.map +1 -1
- package/dist/src/research/index.js.map +1 -1
- package/dist/src/research/sync.d.ts +0 -1
- package/dist/src/research/sync.d.ts.map +1 -1
- package/dist/src/research/sync.js +28 -7
- package/dist/src/research/sync.js.map +1 -1
- package/dist/src/research/terahac.d.ts +0 -1
- package/dist/src/research/terahac.d.ts.map +1 -1
- package/dist/src/research/terahac.js +32 -4
- package/dist/src/research/terahac.js.map +1 -1
- package/dist/src/utils/graph-converters.d.ts +31 -0
- package/dist/src/utils/graph-converters.d.ts.map +1 -1
- package/dist/src/utils/graph-converters.js +31 -0
- package/dist/src/utils/graph-converters.js.map +1 -1
- package/dist/src/utils/graph-utilities.d.ts.map +1 -1
- package/dist/src/utils/graph-utilities.js.map +1 -1
- package/dist/src/utils/math-utilities.d.ts +11 -0
- package/dist/src/utils/math-utilities.d.ts.map +1 -1
- package/dist/src/utils/math-utilities.js +14 -5
- package/dist/src/utils/math-utilities.js.map +1 -1
- package/dist/src/utils/optimization-helpers.d.ts +9 -0
- package/dist/src/utils/optimization-helpers.d.ts.map +1 -1
- package/dist/src/utils/optimization-helpers.js +12 -0
- package/dist/src/utils/optimization-helpers.js.map +1 -1
- package/dist/src/utils/priorityQueue.d.ts +24 -0
- package/dist/src/utils/priorityQueue.d.ts.map +1 -1
- package/dist/src/utils/priorityQueue.js +34 -4
- package/dist/src/utils/priorityQueue.js.map +1 -1
- package/package.json +49 -45
- package/src/algorithms/centrality/betweenness.ts +34 -17
- package/src/algorithms/centrality/closeness.ts +34 -17
- package/src/algorithms/centrality/degree.ts +15 -19
- package/src/algorithms/centrality/delta-pagerank-simple.ts +36 -28
- package/src/algorithms/centrality/delta-pagerank.ts +34 -11
- package/src/algorithms/centrality/eigenvector.ts +14 -15
- package/src/algorithms/centrality/hits.ts +17 -17
- package/src/algorithms/centrality/index.ts +20 -15
- package/src/algorithms/centrality/katz.ts +17 -25
- package/src/algorithms/centrality/pagerank.ts +26 -17
- package/src/algorithms/community/girvan-newman.ts +39 -28
- package/src/algorithms/community/index.ts +6 -7
- package/src/algorithms/community/label-propagation.ts +20 -28
- package/src/algorithms/community/leiden.ts +61 -37
- package/src/algorithms/community/louvain-optimized.ts +49 -20
- package/src/algorithms/community/louvain.ts +35 -25
- package/src/algorithms/community/modularity-utils.ts +5 -17
- package/src/algorithms/components/connected.ts +40 -17
- package/src/algorithms/matching/bipartite.ts +17 -10
- package/src/algorithms/matching/index.ts +4 -4
- package/src/algorithms/matching/isomorphism.ts +53 -23
- package/src/algorithms/mst/index.ts +3 -4
- package/src/algorithms/mst/kruskal.ts +21 -6
- package/src/algorithms/mst/prim.ts +13 -4
- package/src/algorithms/shortest-path/bellman-ford.ts +16 -15
- package/src/algorithms/shortest-path/bidirectional-dijkstra.ts +28 -28
- package/src/algorithms/shortest-path/dijkstra.ts +23 -17
- package/src/algorithms/shortest-path/floyd-warshall.ts +21 -4
- package/src/algorithms/shortest-path/index.ts +5 -5
- package/src/algorithms/traversal/bfs-unified.ts +64 -60
- package/src/algorithms/traversal/bfs-variants.ts +61 -37
- package/src/algorithms/traversal/bfs.ts +1 -7
- package/src/algorithms/traversal/dfs.ts +61 -42
- package/src/algorithms/traversal/index.ts +3 -3
- package/src/benchmark-all-algorithms.ts +57 -25
- package/src/clustering/hierarchical.ts +40 -34
- package/src/clustering/index.ts +8 -8
- package/src/clustering/k-core.ts +26 -42
- package/src/clustering/mcl.ts +53 -23
- package/src/clustering/spectral.ts +91 -57
- package/src/core/graph.ts +54 -14
- package/src/data-structures/index.ts +2 -2
- package/src/data-structures/priority-queue.ts +36 -11
- package/src/data-structures/union-find.ts +22 -1
- package/src/flow/ford-fulkerson.ts +62 -35
- package/src/flow/min-cut.ts +43 -45
- package/src/index.ts +3 -2
- package/src/link-prediction/adamic-adar.ts +65 -51
- package/src/link-prediction/common-neighbors.ts +45 -36
- package/src/link-prediction/index.ts +1 -1
- package/src/optimized/bit-packed.ts +106 -56
- package/src/optimized/csr-graph.ts +93 -30
- package/src/optimized/direction-optimized-bfs.ts +44 -25
- package/src/optimized/graph-adapter.ts +64 -16
- package/src/optimized/index.ts +7 -9
- package/src/pathfinding/astar.ts +40 -25
- package/src/pathfinding/utils.ts +20 -13
- package/src/research/grsbm.ts +30 -24
- package/src/research/index.ts +4 -4
- package/src/research/sync.ts +39 -23
- package/src/research/terahac.ts +54 -20
- package/src/utils/graph-converters.ts +42 -14
- package/src/utils/graph-utilities.ts +11 -37
- package/src/utils/math-utilities.ts +17 -8
- package/src/utils/optimization-helpers.ts +27 -21
- package/src/utils/priorityQueue.ts +40 -6
|
@@ -5,9 +5,13 @@
|
|
|
5
5
|
* Uses a min-heap by default but supports custom comparison functions.
|
|
6
6
|
*/
|
|
7
7
|
export class PriorityQueue<T> {
|
|
8
|
-
private heap: {item: T
|
|
8
|
+
private heap: { item: T; priority: number }[];
|
|
9
9
|
private compareFn: (a: number, b: number) => number;
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new PriorityQueue instance.
|
|
13
|
+
* @param compareFn - Custom comparison function for priorities. Returns negative if a has higher priority than b.
|
|
14
|
+
*/
|
|
11
15
|
constructor(compareFn?: (a: number, b: number) => number) {
|
|
12
16
|
this.heap = [];
|
|
13
17
|
// Default to min-heap (smaller priority values have higher priority)
|
|
@@ -16,15 +20,18 @@ export class PriorityQueue<T> {
|
|
|
16
20
|
|
|
17
21
|
/**
|
|
18
22
|
* Add an item with the given priority to the queue
|
|
23
|
+
* @param item - The item to add to the queue
|
|
24
|
+
* @param priority - The priority value for the item (lower values have higher priority in min-heap)
|
|
19
25
|
*/
|
|
20
26
|
enqueue(item: T, priority: number): void {
|
|
21
|
-
const element = {item, priority};
|
|
27
|
+
const element = { item, priority };
|
|
22
28
|
this.heap.push(element);
|
|
23
29
|
this.heapifyUp(this.heap.length - 1);
|
|
24
30
|
}
|
|
25
31
|
|
|
26
32
|
/**
|
|
27
33
|
* Remove and return the item with the highest priority
|
|
34
|
+
* @returns The item with the highest priority, or undefined if the queue is empty
|
|
28
35
|
*/
|
|
29
36
|
dequeue(): T | undefined {
|
|
30
37
|
if (this.heap.length === 0) {
|
|
@@ -52,6 +59,7 @@ export class PriorityQueue<T> {
|
|
|
52
59
|
|
|
53
60
|
/**
|
|
54
61
|
* View the item with the highest priority without removing it
|
|
62
|
+
* @returns The item with the highest priority, or undefined if the queue is empty
|
|
55
63
|
*/
|
|
56
64
|
peek(): T | undefined {
|
|
57
65
|
const first = this.heap[0];
|
|
@@ -60,6 +68,7 @@ export class PriorityQueue<T> {
|
|
|
60
68
|
|
|
61
69
|
/**
|
|
62
70
|
* Check if the queue is empty
|
|
71
|
+
* @returns True if the queue has no items, false otherwise
|
|
63
72
|
*/
|
|
64
73
|
isEmpty(): boolean {
|
|
65
74
|
return this.heap.length === 0;
|
|
@@ -67,6 +76,7 @@ export class PriorityQueue<T> {
|
|
|
67
76
|
|
|
68
77
|
/**
|
|
69
78
|
* Get the number of items in the queue
|
|
79
|
+
* @returns The number of items currently in the queue
|
|
70
80
|
*/
|
|
71
81
|
size(): number {
|
|
72
82
|
return this.heap.length;
|
|
@@ -74,7 +84,9 @@ export class PriorityQueue<T> {
|
|
|
74
84
|
|
|
75
85
|
/**
|
|
76
86
|
* Update the priority of an item if it exists in the queue
|
|
77
|
-
*
|
|
87
|
+
* @param item - The item whose priority should be updated
|
|
88
|
+
* @param newPriority - The new priority value for the item
|
|
89
|
+
* @returns True if the item was found and updated, false otherwise
|
|
78
90
|
*/
|
|
79
91
|
updatePriority(item: T, newPriority: number): boolean {
|
|
80
92
|
const index = this.heap.findIndex((element) => element.item === item);
|
|
@@ -110,13 +122,15 @@ export class PriorityQueue<T> {
|
|
|
110
122
|
|
|
111
123
|
/**
|
|
112
124
|
* Convert queue to array (for testing/debugging)
|
|
125
|
+
* @returns An array of all items with their priorities
|
|
113
126
|
*/
|
|
114
|
-
toArray(): {item: T
|
|
115
|
-
return [...
|
|
127
|
+
toArray(): { item: T; priority: number }[] {
|
|
128
|
+
return [...this.heap];
|
|
116
129
|
}
|
|
117
130
|
|
|
118
131
|
/**
|
|
119
132
|
* Move element up the heap until heap property is satisfied
|
|
133
|
+
* @param index - The index of the element to heapify up
|
|
120
134
|
*/
|
|
121
135
|
private heapifyUp(index: number): void {
|
|
122
136
|
if (index === 0) {
|
|
@@ -136,26 +150,35 @@ export class PriorityQueue<T> {
|
|
|
136
150
|
|
|
137
151
|
/**
|
|
138
152
|
* Move element down the heap until heap property is satisfied
|
|
153
|
+
* @param index - The index of the element to heapify down
|
|
139
154
|
*/
|
|
140
155
|
private heapifyDown(index: number): void {
|
|
141
|
-
const leftChildIndex =
|
|
142
|
-
const rightChildIndex =
|
|
156
|
+
const leftChildIndex = 2 * index + 1;
|
|
157
|
+
const rightChildIndex = 2 * index + 2;
|
|
143
158
|
let targetIndex = index;
|
|
144
159
|
|
|
145
160
|
// Find the child with highest priority (lowest value for min-heap)
|
|
146
161
|
const leftChild = this.heap[leftChildIndex];
|
|
147
162
|
const target = this.heap[targetIndex];
|
|
148
163
|
|
|
149
|
-
if (
|
|
150
|
-
this.
|
|
164
|
+
if (
|
|
165
|
+
leftChildIndex < this.heap.length &&
|
|
166
|
+
leftChild &&
|
|
167
|
+
target &&
|
|
168
|
+
this.compareFn(leftChild.priority, target.priority) < 0
|
|
169
|
+
) {
|
|
151
170
|
targetIndex = leftChildIndex;
|
|
152
171
|
}
|
|
153
172
|
|
|
154
173
|
const rightChild = this.heap[rightChildIndex];
|
|
155
174
|
const newTarget = this.heap[targetIndex];
|
|
156
175
|
|
|
157
|
-
if (
|
|
158
|
-
this.
|
|
176
|
+
if (
|
|
177
|
+
rightChildIndex < this.heap.length &&
|
|
178
|
+
rightChild &&
|
|
179
|
+
newTarget &&
|
|
180
|
+
this.compareFn(rightChild.priority, newTarget.priority) < 0
|
|
181
|
+
) {
|
|
159
182
|
targetIndex = rightChildIndex;
|
|
160
183
|
}
|
|
161
184
|
|
|
@@ -168,6 +191,8 @@ export class PriorityQueue<T> {
|
|
|
168
191
|
|
|
169
192
|
/**
|
|
170
193
|
* Swap two elements in the heap
|
|
194
|
+
* @param i - Index of the first element
|
|
195
|
+
* @param j - Index of the second element
|
|
171
196
|
*/
|
|
172
197
|
private swap(i: number, j: number): void {
|
|
173
198
|
const temp = this.heap[i];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {NodeId} from "../types/index.js";
|
|
1
|
+
import type { NodeId } from "../types/index.js";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Union-Find (Disjoint Set) data structure with path compression and union by rank
|
|
@@ -11,6 +11,10 @@ export class UnionFind {
|
|
|
11
11
|
private rank: Map<NodeId, number>;
|
|
12
12
|
private componentCount: number;
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new UnionFind data structure with the given elements.
|
|
16
|
+
* @param elements - Array of elements to initialize the data structure with
|
|
17
|
+
*/
|
|
14
18
|
constructor(elements: NodeId[]) {
|
|
15
19
|
this.parent = new Map();
|
|
16
20
|
this.rank = new Map();
|
|
@@ -25,6 +29,8 @@ export class UnionFind {
|
|
|
25
29
|
|
|
26
30
|
/**
|
|
27
31
|
* Find the root of the set containing the element with path compression
|
|
32
|
+
* @param element - The element to find the root for
|
|
33
|
+
* @returns The root element of the set containing the given element
|
|
28
34
|
*/
|
|
29
35
|
find(element: NodeId): NodeId {
|
|
30
36
|
const parent = this.parent.get(element);
|
|
@@ -48,6 +54,8 @@ export class UnionFind {
|
|
|
48
54
|
|
|
49
55
|
/**
|
|
50
56
|
* Union two sets using union by rank
|
|
57
|
+
* @param elementA - The first element whose set should be merged
|
|
58
|
+
* @param elementB - The second element whose set should be merged
|
|
51
59
|
*/
|
|
52
60
|
union(elementA: NodeId, elementB: NodeId): void {
|
|
53
61
|
const rootA = this.find(elementA);
|
|
@@ -80,6 +88,9 @@ export class UnionFind {
|
|
|
80
88
|
|
|
81
89
|
/**
|
|
82
90
|
* Check if two elements are in the same connected component
|
|
91
|
+
* @param elementA - The first element to check
|
|
92
|
+
* @param elementB - The second element to check
|
|
93
|
+
* @returns True if both elements are in the same component, false otherwise
|
|
83
94
|
*/
|
|
84
95
|
connected(elementA: NodeId, elementB: NodeId): boolean {
|
|
85
96
|
try {
|
|
@@ -91,6 +102,7 @@ export class UnionFind {
|
|
|
91
102
|
|
|
92
103
|
/**
|
|
93
104
|
* Get the number of connected components
|
|
105
|
+
* @returns The current count of disjoint components
|
|
94
106
|
*/
|
|
95
107
|
getComponentCount(): number {
|
|
96
108
|
return this.componentCount;
|
|
@@ -98,6 +110,8 @@ export class UnionFind {
|
|
|
98
110
|
|
|
99
111
|
/**
|
|
100
112
|
* Get all elements that belong to the same component as the given element
|
|
113
|
+
* @param element - The element whose component should be retrieved
|
|
114
|
+
* @returns Array of all elements in the same component
|
|
101
115
|
*/
|
|
102
116
|
getComponent(element: NodeId): NodeId[] {
|
|
103
117
|
const root = this.find(element);
|
|
@@ -114,6 +128,7 @@ export class UnionFind {
|
|
|
114
128
|
|
|
115
129
|
/**
|
|
116
130
|
* Get all connected components as separate arrays
|
|
131
|
+
* @returns Array of arrays, where each inner array contains elements of one component
|
|
117
132
|
*/
|
|
118
133
|
getAllComponents(): NodeId[][] {
|
|
119
134
|
const componentMap = new Map<NodeId, NodeId[]>();
|
|
@@ -136,6 +151,8 @@ export class UnionFind {
|
|
|
136
151
|
|
|
137
152
|
/**
|
|
138
153
|
* Get the size of the component containing the given element
|
|
154
|
+
* @param element - The element whose component size should be retrieved
|
|
155
|
+
* @returns The number of elements in the component containing the given element
|
|
139
156
|
*/
|
|
140
157
|
getComponentSize(element: NodeId): number {
|
|
141
158
|
return this.getComponent(element).length;
|
|
@@ -143,6 +160,7 @@ export class UnionFind {
|
|
|
143
160
|
|
|
144
161
|
/**
|
|
145
162
|
* Add a new element to the data structure
|
|
163
|
+
* @param element - The element to add as a new singleton set
|
|
146
164
|
*/
|
|
147
165
|
addElement(element: NodeId): void {
|
|
148
166
|
if (this.parent.has(element)) {
|
|
@@ -156,6 +174,8 @@ export class UnionFind {
|
|
|
156
174
|
|
|
157
175
|
/**
|
|
158
176
|
* Check if an element exists in the data structure
|
|
177
|
+
* @param element - The element to check for
|
|
178
|
+
* @returns True if the element exists, false otherwise
|
|
159
179
|
*/
|
|
160
180
|
hasElement(element: NodeId): boolean {
|
|
161
181
|
return this.parent.has(element);
|
|
@@ -163,6 +183,7 @@ export class UnionFind {
|
|
|
163
183
|
|
|
164
184
|
/**
|
|
165
185
|
* Get the total number of elements
|
|
186
|
+
* @returns The total count of all elements in the data structure
|
|
166
187
|
*/
|
|
167
188
|
size(): number {
|
|
168
189
|
return this.parent.size;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {bfsAugmentingPath} from "../algorithms/traversal/bfs-variants.js";
|
|
2
|
-
import type {Graph} from "../core/graph.js";
|
|
3
|
-
import {graphToMap} from "../utils/graph-converters.js";
|
|
1
|
+
import { bfsAugmentingPath } from "../algorithms/traversal/bfs-variants.js";
|
|
2
|
+
import type { Graph } from "../core/graph.js";
|
|
3
|
+
import { graphToMap } from "../utils/graph-converters.js";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Ford-Fulkerson Algorithm for Maximum Flow
|
|
@@ -24,15 +24,15 @@ export interface FlowNetwork {
|
|
|
24
24
|
export interface MaxFlowResult {
|
|
25
25
|
maxFlow: number;
|
|
26
26
|
flowGraph: Map<string, Map<string, number>>;
|
|
27
|
-
minCut?: {source: Set<string
|
|
27
|
+
minCut?: { source: Set<string>; sink: Set<string>; edges: [string, string][] };
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
31
|
* Create residual graph from original graph
|
|
32
|
+
* @param graph - The original graph with capacity values
|
|
33
|
+
* @returns A residual graph initialized with the original capacities
|
|
32
34
|
*/
|
|
33
|
-
function createResidualGraph(
|
|
34
|
-
graph: Map<string, Map<string, number>>,
|
|
35
|
-
): Map<string, Map<string, number>> {
|
|
35
|
+
function createResidualGraph(graph: Map<string, Map<string, number>>): Map<string, Map<string, number>> {
|
|
36
36
|
const residual = new Map<string, Map<string, number>>();
|
|
37
37
|
|
|
38
38
|
for (const [u, neighbors] of graph) {
|
|
@@ -50,6 +50,10 @@ function createResidualGraph(
|
|
|
50
50
|
|
|
51
51
|
/**
|
|
52
52
|
* Find augmenting path using DFS
|
|
53
|
+
* @param residualGraph - The residual graph with remaining capacities
|
|
54
|
+
* @param source - The source node
|
|
55
|
+
* @param sink - The sink node
|
|
56
|
+
* @returns An array of nodes forming the path, or null if no path exists
|
|
53
57
|
*/
|
|
54
58
|
function findAugmentingPathDFS(
|
|
55
59
|
residualGraph: Map<string, Map<string, number>>,
|
|
@@ -92,6 +96,11 @@ function findAugmentingPathDFS(
|
|
|
92
96
|
|
|
93
97
|
/**
|
|
94
98
|
* Update flow along an augmenting path
|
|
99
|
+
* @param residualGraph - The residual graph to update
|
|
100
|
+
* @param flowGraph - The flow graph to update with new flow values
|
|
101
|
+
* @param originalGraph - The original graph for edge direction reference
|
|
102
|
+
* @param path - The augmenting path to update flow along
|
|
103
|
+
* @param pathFlow - The amount of flow to push through the path
|
|
95
104
|
*/
|
|
96
105
|
function updateFlow(
|
|
97
106
|
residualGraph: Map<string, Map<string, number>>,
|
|
@@ -146,11 +155,14 @@ function updateFlow(
|
|
|
146
155
|
|
|
147
156
|
/**
|
|
148
157
|
* Find minimum cut from source side
|
|
158
|
+
* @param residualGraph - The residual graph after max flow computation
|
|
159
|
+
* @param source - The source node
|
|
160
|
+
* @returns The minimum cut partition with source and sink sets and cut edges
|
|
149
161
|
*/
|
|
150
162
|
function findMinCut(
|
|
151
163
|
residualGraph: Map<string, Map<string, number>>,
|
|
152
164
|
source: string,
|
|
153
|
-
): {source: Set<string
|
|
165
|
+
): { source: Set<string>; sink: Set<string>; edges: [string, string][] } {
|
|
154
166
|
// Find all reachable nodes from source in residual graph
|
|
155
167
|
const sourceSet = new Set<string>();
|
|
156
168
|
const queue = [source];
|
|
@@ -195,20 +207,29 @@ function findMinCut(
|
|
|
195
207
|
}
|
|
196
208
|
}
|
|
197
209
|
|
|
198
|
-
return {source: sourceSet, sink: sinkSet, edges: cutEdges};
|
|
210
|
+
return { source: sourceSet, sink: sinkSet, edges: cutEdges };
|
|
199
211
|
}
|
|
200
212
|
|
|
201
213
|
/**
|
|
202
214
|
* Core max flow algorithm implementation
|
|
215
|
+
* @param graph - The graph with capacity values
|
|
216
|
+
* @param source - The source node
|
|
217
|
+
* @param sink - The sink node
|
|
218
|
+
* @param findPath - Function to find an augmenting path in the residual graph
|
|
219
|
+
* @returns The maximum flow result including flow value, flow graph, and minimum cut
|
|
203
220
|
*/
|
|
204
221
|
function maxFlowCore(
|
|
205
222
|
graph: Map<string, Map<string, number>>,
|
|
206
223
|
source: string,
|
|
207
224
|
sink: string,
|
|
208
|
-
findPath: (
|
|
225
|
+
findPath: (
|
|
226
|
+
residual: Map<string, Map<string, number>>,
|
|
227
|
+
src: string,
|
|
228
|
+
snk: string,
|
|
229
|
+
) => { path: string[]; pathCapacity: number } | null,
|
|
209
230
|
): MaxFlowResult {
|
|
210
231
|
if (!graph.has(source) || !graph.has(sink)) {
|
|
211
|
-
return {maxFlow: 0, flowGraph: new Map()};
|
|
232
|
+
return { maxFlow: 0, flowGraph: new Map() };
|
|
212
233
|
}
|
|
213
234
|
|
|
214
235
|
// Create residual graph
|
|
@@ -230,7 +251,7 @@ function maxFlowCore(
|
|
|
230
251
|
let pathResult = findPath(residualGraph, source, sink);
|
|
231
252
|
|
|
232
253
|
while (pathResult !== null) {
|
|
233
|
-
const {path, pathCapacity} = pathResult;
|
|
254
|
+
const { path, pathCapacity } = pathResult;
|
|
234
255
|
|
|
235
256
|
// Update flow
|
|
236
257
|
updateFlow(residualGraph, flowGraph, graph, path, pathCapacity);
|
|
@@ -243,19 +264,23 @@ function maxFlowCore(
|
|
|
243
264
|
// Find minimum cut
|
|
244
265
|
const minCut = findMinCut(residualGraph, source);
|
|
245
266
|
|
|
246
|
-
return {maxFlow, flowGraph, minCut};
|
|
267
|
+
return { maxFlow, flowGraph, minCut };
|
|
247
268
|
}
|
|
248
269
|
|
|
249
270
|
/**
|
|
250
271
|
* Internal implementation of Ford-Fulkerson algorithm using DFS
|
|
272
|
+
* @param graph - The graph with capacity values
|
|
273
|
+
* @param source - The source node
|
|
274
|
+
* @param sink - The sink node
|
|
275
|
+
* @returns The maximum flow result
|
|
251
276
|
*/
|
|
252
|
-
function fordFulkersonImpl(
|
|
253
|
-
graph: Map<string, Map<string, number>>,
|
|
254
|
-
source: string,
|
|
255
|
-
sink: string,
|
|
256
|
-
): MaxFlowResult {
|
|
277
|
+
function fordFulkersonImpl(graph: Map<string, Map<string, number>>, source: string, sink: string): MaxFlowResult {
|
|
257
278
|
// Adapter function for DFS path finding
|
|
258
|
-
const findPathDFS = (
|
|
279
|
+
const findPathDFS = (
|
|
280
|
+
residual: Map<string, Map<string, number>>,
|
|
281
|
+
src: string,
|
|
282
|
+
snk: string,
|
|
283
|
+
): { path: string[]; pathCapacity: number } | null => {
|
|
259
284
|
const path = findAugmentingPathDFS(residual, src, snk);
|
|
260
285
|
if (!path) {
|
|
261
286
|
return null;
|
|
@@ -274,7 +299,7 @@ function fordFulkersonImpl(
|
|
|
274
299
|
}
|
|
275
300
|
}
|
|
276
301
|
|
|
277
|
-
return {path, pathCapacity};
|
|
302
|
+
return { path, pathCapacity };
|
|
278
303
|
};
|
|
279
304
|
|
|
280
305
|
return maxFlowCore(graph, source, sink, findPathDFS);
|
|
@@ -282,14 +307,18 @@ function fordFulkersonImpl(
|
|
|
282
307
|
|
|
283
308
|
/**
|
|
284
309
|
* Internal implementation of Edmonds-Karp algorithm
|
|
310
|
+
* @param graph - The graph with capacity values
|
|
311
|
+
* @param source - The source node
|
|
312
|
+
* @param sink - The sink node
|
|
313
|
+
* @returns The maximum flow result
|
|
285
314
|
*/
|
|
286
|
-
function edmondsKarpImpl(
|
|
287
|
-
graph: Map<string, Map<string, number>>,
|
|
288
|
-
source: string,
|
|
289
|
-
sink: string,
|
|
290
|
-
): MaxFlowResult {
|
|
315
|
+
function edmondsKarpImpl(graph: Map<string, Map<string, number>>, source: string, sink: string): MaxFlowResult {
|
|
291
316
|
// Use BFS variant for path finding
|
|
292
|
-
const findPathBFS = (
|
|
317
|
+
const findPathBFS = (
|
|
318
|
+
residual: Map<string, Map<string, number>>,
|
|
319
|
+
src: string,
|
|
320
|
+
snk: string,
|
|
321
|
+
): { path: string[]; pathCapacity: number } | null => {
|
|
293
322
|
return bfsAugmentingPath(residual, src, snk);
|
|
294
323
|
};
|
|
295
324
|
|
|
@@ -298,12 +327,16 @@ function edmondsKarpImpl(
|
|
|
298
327
|
|
|
299
328
|
/**
|
|
300
329
|
* Utility function to create a flow network for bipartite matching
|
|
330
|
+
* @param leftNodes - Array of left partition node identifiers
|
|
331
|
+
* @param rightNodes - Array of right partition node identifiers
|
|
332
|
+
* @param edges - Array of edges connecting left nodes to right nodes
|
|
333
|
+
* @returns The flow network graph with source and sink nodes
|
|
301
334
|
*/
|
|
302
335
|
export function createBipartiteFlowNetwork(
|
|
303
336
|
leftNodes: string[],
|
|
304
337
|
rightNodes: string[],
|
|
305
338
|
edges: [string, string][],
|
|
306
|
-
): {graph: Map<string, Map<string, number
|
|
339
|
+
): { graph: Map<string, Map<string, number>>; source: string; sink: string } {
|
|
307
340
|
const graph = new Map<string, Map<string, number>>();
|
|
308
341
|
const source = "__source__";
|
|
309
342
|
const sink = "__sink__";
|
|
@@ -345,12 +378,11 @@ export function createBipartiteFlowNetwork(
|
|
|
345
378
|
|
|
346
379
|
graph.set(sink, new Map());
|
|
347
380
|
|
|
348
|
-
return {graph, source, sink};
|
|
381
|
+
return { graph, source, sink };
|
|
349
382
|
}
|
|
350
383
|
|
|
351
384
|
/**
|
|
352
385
|
* Ford-Fulkerson algorithm using DFS for finding augmenting paths
|
|
353
|
-
*
|
|
354
386
|
* @param graph - Adjacency list representation with capacities - accepts Graph class or Map
|
|
355
387
|
* @param source - Source node
|
|
356
388
|
* @param sink - Sink node
|
|
@@ -359,11 +391,7 @@ export function createBipartiteFlowNetwork(
|
|
|
359
391
|
* Time Complexity: O(E * f) where f is the maximum flow
|
|
360
392
|
* Space Complexity: O(V + E)
|
|
361
393
|
*/
|
|
362
|
-
export function fordFulkerson(
|
|
363
|
-
graph: Graph,
|
|
364
|
-
source: string,
|
|
365
|
-
sink: string,
|
|
366
|
-
): MaxFlowResult {
|
|
394
|
+
export function fordFulkerson(graph: Graph, source: string, sink: string): MaxFlowResult {
|
|
367
395
|
// Convert Graph to Map representation
|
|
368
396
|
const graphMap = graphToMap(graph);
|
|
369
397
|
return fordFulkersonImpl(graphMap, source, sink);
|
|
@@ -372,7 +400,6 @@ export function fordFulkerson(
|
|
|
372
400
|
/**
|
|
373
401
|
* Edmonds-Karp algorithm (Ford-Fulkerson with BFS)
|
|
374
402
|
* More efficient implementation with better time complexity
|
|
375
|
-
*
|
|
376
403
|
* @param graph - Adjacency list representation with capacities - accepts Graph class or Map
|
|
377
404
|
* @param source - Source node
|
|
378
405
|
* @param sink - Sink node
|