@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
package/dist/src/core/graph.js
CHANGED
|
@@ -5,6 +5,10 @@
|
|
|
5
5
|
* Uses adjacency lists for optimal performance with sparse graphs.
|
|
6
6
|
*/
|
|
7
7
|
export class Graph {
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new Graph instance.
|
|
10
|
+
* @param config - Configuration options for the graph
|
|
11
|
+
*/
|
|
8
12
|
constructor(config = {}) {
|
|
9
13
|
this.config = {
|
|
10
14
|
directed: false,
|
|
@@ -19,6 +23,8 @@ export class Graph {
|
|
|
19
23
|
}
|
|
20
24
|
/**
|
|
21
25
|
* Add a node to the graph
|
|
26
|
+
* @param id - The unique identifier for the node
|
|
27
|
+
* @param data - Optional key-value data to attach to the node
|
|
22
28
|
*/
|
|
23
29
|
addNode(id, data) {
|
|
24
30
|
if (!this.nodeMap.has(id)) {
|
|
@@ -31,6 +37,8 @@ export class Graph {
|
|
|
31
37
|
}
|
|
32
38
|
/**
|
|
33
39
|
* Remove a node from the graph
|
|
40
|
+
* @param id - The unique identifier of the node to remove
|
|
41
|
+
* @returns True if the node was removed, false if it did not exist
|
|
34
42
|
*/
|
|
35
43
|
removeNode(id) {
|
|
36
44
|
if (!this.nodeMap.has(id)) {
|
|
@@ -69,6 +77,10 @@ export class Graph {
|
|
|
69
77
|
}
|
|
70
78
|
/**
|
|
71
79
|
* Add an edge to the graph
|
|
80
|
+
* @param source - The source node identifier
|
|
81
|
+
* @param target - The target node identifier
|
|
82
|
+
* @param weight - The weight of the edge (defaults to 1)
|
|
83
|
+
* @param data - Optional key-value data to attach to the edge
|
|
72
84
|
*/
|
|
73
85
|
addEdge(source, target, weight = 1, data) {
|
|
74
86
|
// Ensure both nodes exist
|
|
@@ -109,6 +121,9 @@ export class Graph {
|
|
|
109
121
|
}
|
|
110
122
|
/**
|
|
111
123
|
* Remove an edge from the graph
|
|
124
|
+
* @param source - The source node identifier
|
|
125
|
+
* @param target - The target node identifier
|
|
126
|
+
* @returns True if the edge was removed, false if it did not exist
|
|
112
127
|
*/
|
|
113
128
|
removeEdge(source, target) {
|
|
114
129
|
const sourceEdges = this.adjacencyList.get(source);
|
|
@@ -134,12 +149,17 @@ export class Graph {
|
|
|
134
149
|
}
|
|
135
150
|
/**
|
|
136
151
|
* Check if a node exists in the graph
|
|
152
|
+
* @param id - The unique identifier of the node to check
|
|
153
|
+
* @returns True if the node exists, false otherwise
|
|
137
154
|
*/
|
|
138
155
|
hasNode(id) {
|
|
139
156
|
return this.nodeMap.has(id);
|
|
140
157
|
}
|
|
141
158
|
/**
|
|
142
159
|
* Check if an edge exists in the graph
|
|
160
|
+
* @param source - The source node identifier
|
|
161
|
+
* @param target - The target node identifier
|
|
162
|
+
* @returns True if the edge exists, false otherwise
|
|
143
163
|
*/
|
|
144
164
|
hasEdge(source, target) {
|
|
145
165
|
const sourceEdges = this.adjacencyList.get(source);
|
|
@@ -147,12 +167,17 @@ export class Graph {
|
|
|
147
167
|
}
|
|
148
168
|
/**
|
|
149
169
|
* Get a node by ID
|
|
170
|
+
* @param id - The unique identifier of the node to retrieve
|
|
171
|
+
* @returns The node if found, undefined otherwise
|
|
150
172
|
*/
|
|
151
173
|
getNode(id) {
|
|
152
174
|
return this.nodeMap.get(id);
|
|
153
175
|
}
|
|
154
176
|
/**
|
|
155
177
|
* Get an edge by source and target
|
|
178
|
+
* @param source - The source node identifier
|
|
179
|
+
* @param target - The target node identifier
|
|
180
|
+
* @returns The edge if found, undefined otherwise
|
|
156
181
|
*/
|
|
157
182
|
getEdge(source, target) {
|
|
158
183
|
const sourceEdges = this.adjacencyList.get(source);
|
|
@@ -160,30 +185,35 @@ export class Graph {
|
|
|
160
185
|
}
|
|
161
186
|
/**
|
|
162
187
|
* Get the number of nodes in the graph
|
|
188
|
+
* @returns The total count of nodes
|
|
163
189
|
*/
|
|
164
190
|
get nodeCount() {
|
|
165
191
|
return this.nodeMap.size;
|
|
166
192
|
}
|
|
167
193
|
/**
|
|
168
194
|
* Get the number of edges in the graph
|
|
195
|
+
* @returns The total count of edges
|
|
169
196
|
*/
|
|
170
197
|
get totalEdgeCount() {
|
|
171
198
|
return this.edgeCount;
|
|
172
199
|
}
|
|
173
200
|
/**
|
|
174
201
|
* Check if the graph is directed
|
|
202
|
+
* @returns True if the graph is directed, false otherwise
|
|
175
203
|
*/
|
|
176
204
|
get isDirected() {
|
|
177
205
|
return this.config.directed;
|
|
178
206
|
}
|
|
179
207
|
/**
|
|
180
208
|
* Get all nodes in the graph
|
|
209
|
+
* @returns An iterator over all nodes
|
|
181
210
|
*/
|
|
182
211
|
nodes() {
|
|
183
212
|
return this.nodeMap.values();
|
|
184
213
|
}
|
|
185
214
|
/**
|
|
186
215
|
* Get all edges in the graph
|
|
216
|
+
* @yields Each unique edge in the graph
|
|
187
217
|
*/
|
|
188
218
|
*edges() {
|
|
189
219
|
for (const [source, edges] of this.adjacencyList) {
|
|
@@ -198,6 +228,8 @@ export class Graph {
|
|
|
198
228
|
}
|
|
199
229
|
/**
|
|
200
230
|
* Get neighbors of a node (outgoing edges)
|
|
231
|
+
* @param nodeId - The node identifier to get neighbors for
|
|
232
|
+
* @returns An iterator over the neighbor node identifiers
|
|
201
233
|
*/
|
|
202
234
|
neighbors(nodeId) {
|
|
203
235
|
const edges = this.adjacencyList.get(nodeId);
|
|
@@ -205,6 +237,8 @@ export class Graph {
|
|
|
205
237
|
}
|
|
206
238
|
/**
|
|
207
239
|
* Get incoming neighbors of a node (directed graphs only)
|
|
240
|
+
* @param nodeId - The node identifier to get incoming neighbors for
|
|
241
|
+
* @returns An iterator over the incoming neighbor node identifiers
|
|
208
242
|
*/
|
|
209
243
|
inNeighbors(nodeId) {
|
|
210
244
|
if (!this.config.directed) {
|
|
@@ -215,12 +249,16 @@ export class Graph {
|
|
|
215
249
|
}
|
|
216
250
|
/**
|
|
217
251
|
* Get outgoing neighbors of a node
|
|
252
|
+
* @param nodeId - The node identifier to get outgoing neighbors for
|
|
253
|
+
* @returns An iterator over the outgoing neighbor node identifiers
|
|
218
254
|
*/
|
|
219
255
|
outNeighbors(nodeId) {
|
|
220
256
|
return this.neighbors(nodeId);
|
|
221
257
|
}
|
|
222
258
|
/**
|
|
223
259
|
* Get the degree of a node
|
|
260
|
+
* @param nodeId - The node identifier to get the degree for
|
|
261
|
+
* @returns The total degree of the node
|
|
224
262
|
*/
|
|
225
263
|
degree(nodeId) {
|
|
226
264
|
if (this.config.directed) {
|
|
@@ -231,6 +269,8 @@ export class Graph {
|
|
|
231
269
|
}
|
|
232
270
|
/**
|
|
233
271
|
* Get the in-degree of a node
|
|
272
|
+
* @param nodeId - The node identifier to get the in-degree for
|
|
273
|
+
* @returns The number of incoming edges to the node
|
|
234
274
|
*/
|
|
235
275
|
inDegree(nodeId) {
|
|
236
276
|
if (!this.config.directed) {
|
|
@@ -241,6 +281,8 @@ export class Graph {
|
|
|
241
281
|
}
|
|
242
282
|
/**
|
|
243
283
|
* Get the out-degree of a node
|
|
284
|
+
* @param nodeId - The node identifier to get the out-degree for
|
|
285
|
+
* @returns The number of outgoing edges from the node
|
|
244
286
|
*/
|
|
245
287
|
outDegree(nodeId) {
|
|
246
288
|
const edges = this.adjacencyList.get(nodeId);
|
|
@@ -248,6 +290,7 @@ export class Graph {
|
|
|
248
290
|
}
|
|
249
291
|
/**
|
|
250
292
|
* Create a copy of the graph
|
|
293
|
+
* @returns A new Graph instance with the same nodes and edges
|
|
251
294
|
*/
|
|
252
295
|
clone() {
|
|
253
296
|
const cloned = new Graph(this.config);
|
|
@@ -263,6 +306,7 @@ export class Graph {
|
|
|
263
306
|
}
|
|
264
307
|
/**
|
|
265
308
|
* Get graph configuration
|
|
309
|
+
* @returns A copy of the graph configuration object
|
|
266
310
|
*/
|
|
267
311
|
getConfig() {
|
|
268
312
|
return { ...this.config };
|
|
@@ -279,6 +323,7 @@ export class Graph {
|
|
|
279
323
|
/**
|
|
280
324
|
* Get the number of unique edges in the graph
|
|
281
325
|
* For undirected graphs, each edge is counted once
|
|
326
|
+
* @returns The count of unique edges
|
|
282
327
|
*/
|
|
283
328
|
get uniqueEdgeCount() {
|
|
284
329
|
if (this.config.directed) {
|
|
@@ -286,7 +331,6 @@ export class Graph {
|
|
|
286
331
|
}
|
|
287
332
|
// For undirected graphs, we need to count each edge only once
|
|
288
333
|
let count = 0;
|
|
289
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
290
334
|
for (const _edge of this.edges()) {
|
|
291
335
|
count++;
|
|
292
336
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graph.js","sourceRoot":"","sources":["../../../src/core/graph.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,OAAO,KAAK;IAOd,YAAY,SAA+B,EAAE;QACzC,IAAI,CAAC,MAAM,GAAG;YAC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,SAAS;QACL,OAAO,EAAE,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;IAC9B,CAAC;IAED;;OAEG;IACH,KAAK;QACD,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACrB,IAAI,CAAC,aAAa,CAAC,KAAK,EAAE,CAAC;QAC3B,IAAI,CAAC,aAAa,CAAC,KAAK,EAAE,CAAC;QAC3B,IAAI,CAAC,SAAS,GAAG,CAAC,CAAC;IACvB,CAAC;IAED;;;;OAIG;IACH,IAAI,eAAe;QACf,IAAI,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;YACvB,OAAO,IAAI,CAAC,SAAS,CAAC;QAC1B,CAAC;QAED,8DAA8D;QAC9D,IAAI,KAAK,GAAG,CAAC,CAAC;QAEd,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,KAAK,EAAE,EAAE,CAAC;YAC/B,KAAK,EAAE,CAAC;QACZ,CAAC;QACD,OAAO,KAAK,CAAC;IACjB,CAAC;CACJ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/data-structures/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/data-structures/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/data-structures/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/data-structures/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC"}
|
|
@@ -7,30 +7,42 @@
|
|
|
7
7
|
export declare class PriorityQueue<T> {
|
|
8
8
|
private heap;
|
|
9
9
|
private compareFn;
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new PriorityQueue instance.
|
|
12
|
+
* @param compareFn - Custom comparison function for priorities. Returns negative if a has higher priority than b.
|
|
13
|
+
*/
|
|
10
14
|
constructor(compareFn?: (a: number, b: number) => number);
|
|
11
15
|
/**
|
|
12
16
|
* Add an item with the given priority to the queue
|
|
17
|
+
* @param item - The item to add to the queue
|
|
18
|
+
* @param priority - The priority value for the item (lower values have higher priority in min-heap)
|
|
13
19
|
*/
|
|
14
20
|
enqueue(item: T, priority: number): void;
|
|
15
21
|
/**
|
|
16
22
|
* Remove and return the item with the highest priority
|
|
23
|
+
* @returns The item with the highest priority, or undefined if the queue is empty
|
|
17
24
|
*/
|
|
18
25
|
dequeue(): T | undefined;
|
|
19
26
|
/**
|
|
20
27
|
* View the item with the highest priority without removing it
|
|
28
|
+
* @returns The item with the highest priority, or undefined if the queue is empty
|
|
21
29
|
*/
|
|
22
30
|
peek(): T | undefined;
|
|
23
31
|
/**
|
|
24
32
|
* Check if the queue is empty
|
|
33
|
+
* @returns True if the queue has no items, false otherwise
|
|
25
34
|
*/
|
|
26
35
|
isEmpty(): boolean;
|
|
27
36
|
/**
|
|
28
37
|
* Get the number of items in the queue
|
|
38
|
+
* @returns The number of items currently in the queue
|
|
29
39
|
*/
|
|
30
40
|
size(): number;
|
|
31
41
|
/**
|
|
32
42
|
* Update the priority of an item if it exists in the queue
|
|
33
|
-
*
|
|
43
|
+
* @param item - The item whose priority should be updated
|
|
44
|
+
* @param newPriority - The new priority value for the item
|
|
45
|
+
* @returns True if the item was found and updated, false otherwise
|
|
34
46
|
*/
|
|
35
47
|
updatePriority(item: T, newPriority: number): boolean;
|
|
36
48
|
/**
|
|
@@ -39,6 +51,7 @@ export declare class PriorityQueue<T> {
|
|
|
39
51
|
clear(): void;
|
|
40
52
|
/**
|
|
41
53
|
* Convert queue to array (for testing/debugging)
|
|
54
|
+
* @returns An array of all items with their priorities
|
|
42
55
|
*/
|
|
43
56
|
toArray(): {
|
|
44
57
|
item: T;
|
|
@@ -46,14 +59,18 @@ export declare class PriorityQueue<T> {
|
|
|
46
59
|
}[];
|
|
47
60
|
/**
|
|
48
61
|
* Move element up the heap until heap property is satisfied
|
|
62
|
+
* @param index - The index of the element to heapify up
|
|
49
63
|
*/
|
|
50
64
|
private heapifyUp;
|
|
51
65
|
/**
|
|
52
66
|
* Move element down the heap until heap property is satisfied
|
|
67
|
+
* @param index - The index of the element to heapify down
|
|
53
68
|
*/
|
|
54
69
|
private heapifyDown;
|
|
55
70
|
/**
|
|
56
71
|
* Swap two elements in the heap
|
|
72
|
+
* @param i - Index of the first element
|
|
73
|
+
* @param j - Index of the second element
|
|
57
74
|
*/
|
|
58
75
|
private swap;
|
|
59
76
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"priority-queue.d.ts","sourceRoot":"","sources":["../../../src/data-structures/priority-queue.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,qBAAa,aAAa,CAAC,CAAC;IACxB,OAAO,CAAC,IAAI,
|
|
1
|
+
{"version":3,"file":"priority-queue.d.ts","sourceRoot":"","sources":["../../../src/data-structures/priority-queue.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,qBAAa,aAAa,CAAC,CAAC;IACxB,OAAO,CAAC,IAAI,CAAkC;IAC9C,OAAO,CAAC,SAAS,CAAmC;IAEpD;;;OAGG;gBACS,SAAS,CAAC,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,KAAK,MAAM;IAMxD;;;;OAIG;IACH,OAAO,CAAC,IAAI,EAAE,CAAC,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI;IAMxC;;;OAGG;IACH,OAAO,IAAI,CAAC,GAAG,SAAS;IAwBxB;;;OAGG;IACH,IAAI,IAAI,CAAC,GAAG,SAAS;IAKrB;;;OAGG;IACH,OAAO,IAAI,OAAO;IAIlB;;;OAGG;IACH,IAAI,IAAI,MAAM;IAId;;;;;OAKG;IACH,cAAc,CAAC,IAAI,EAAE,CAAC,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO;IAyBrD;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;;OAGG;IACH,OAAO,IAAI;QAAE,IAAI,EAAE,CAAC,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,EAAE;IAI1C;;;OAGG;IACH,OAAO,CAAC,SAAS;IAgBjB;;;OAGG;IACH,OAAO,CAAC,WAAW;IAqCnB;;;;OAIG;IACH,OAAO,CAAC,IAAI;CAQf"}
|
|
@@ -5,6 +5,10 @@
|
|
|
5
5
|
* Uses a min-heap by default but supports custom comparison functions.
|
|
6
6
|
*/
|
|
7
7
|
export class PriorityQueue {
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new PriorityQueue instance.
|
|
10
|
+
* @param compareFn - Custom comparison function for priorities. Returns negative if a has higher priority than b.
|
|
11
|
+
*/
|
|
8
12
|
constructor(compareFn) {
|
|
9
13
|
this.heap = [];
|
|
10
14
|
// Default to min-heap (smaller priority values have higher priority)
|
|
@@ -12,6 +16,8 @@ export class PriorityQueue {
|
|
|
12
16
|
}
|
|
13
17
|
/**
|
|
14
18
|
* Add an item with the given priority to the queue
|
|
19
|
+
* @param item - The item to add to the queue
|
|
20
|
+
* @param priority - The priority value for the item (lower values have higher priority in min-heap)
|
|
15
21
|
*/
|
|
16
22
|
enqueue(item, priority) {
|
|
17
23
|
const element = { item, priority };
|
|
@@ -20,6 +26,7 @@ export class PriorityQueue {
|
|
|
20
26
|
}
|
|
21
27
|
/**
|
|
22
28
|
* Remove and return the item with the highest priority
|
|
29
|
+
* @returns The item with the highest priority, or undefined if the queue is empty
|
|
23
30
|
*/
|
|
24
31
|
dequeue() {
|
|
25
32
|
if (this.heap.length === 0) {
|
|
@@ -41,6 +48,7 @@ export class PriorityQueue {
|
|
|
41
48
|
}
|
|
42
49
|
/**
|
|
43
50
|
* View the item with the highest priority without removing it
|
|
51
|
+
* @returns The item with the highest priority, or undefined if the queue is empty
|
|
44
52
|
*/
|
|
45
53
|
peek() {
|
|
46
54
|
const first = this.heap[0];
|
|
@@ -48,19 +56,23 @@ export class PriorityQueue {
|
|
|
48
56
|
}
|
|
49
57
|
/**
|
|
50
58
|
* Check if the queue is empty
|
|
59
|
+
* @returns True if the queue has no items, false otherwise
|
|
51
60
|
*/
|
|
52
61
|
isEmpty() {
|
|
53
62
|
return this.heap.length === 0;
|
|
54
63
|
}
|
|
55
64
|
/**
|
|
56
65
|
* Get the number of items in the queue
|
|
66
|
+
* @returns The number of items currently in the queue
|
|
57
67
|
*/
|
|
58
68
|
size() {
|
|
59
69
|
return this.heap.length;
|
|
60
70
|
}
|
|
61
71
|
/**
|
|
62
72
|
* Update the priority of an item if it exists in the queue
|
|
63
|
-
*
|
|
73
|
+
* @param item - The item whose priority should be updated
|
|
74
|
+
* @param newPriority - The new priority value for the item
|
|
75
|
+
* @returns True if the item was found and updated, false otherwise
|
|
64
76
|
*/
|
|
65
77
|
updatePriority(item, newPriority) {
|
|
66
78
|
const index = this.heap.findIndex((element) => element.item === item);
|
|
@@ -90,12 +102,14 @@ export class PriorityQueue {
|
|
|
90
102
|
}
|
|
91
103
|
/**
|
|
92
104
|
* Convert queue to array (for testing/debugging)
|
|
105
|
+
* @returns An array of all items with their priorities
|
|
93
106
|
*/
|
|
94
107
|
toArray() {
|
|
95
108
|
return [...this.heap];
|
|
96
109
|
}
|
|
97
110
|
/**
|
|
98
111
|
* Move element up the heap until heap property is satisfied
|
|
112
|
+
* @param index - The index of the element to heapify up
|
|
99
113
|
*/
|
|
100
114
|
heapifyUp(index) {
|
|
101
115
|
if (index === 0) {
|
|
@@ -111,21 +125,26 @@ export class PriorityQueue {
|
|
|
111
125
|
}
|
|
112
126
|
/**
|
|
113
127
|
* Move element down the heap until heap property is satisfied
|
|
128
|
+
* @param index - The index of the element to heapify down
|
|
114
129
|
*/
|
|
115
130
|
heapifyDown(index) {
|
|
116
|
-
const leftChildIndex =
|
|
117
|
-
const rightChildIndex =
|
|
131
|
+
const leftChildIndex = 2 * index + 1;
|
|
132
|
+
const rightChildIndex = 2 * index + 2;
|
|
118
133
|
let targetIndex = index;
|
|
119
134
|
// Find the child with highest priority (lowest value for min-heap)
|
|
120
135
|
const leftChild = this.heap[leftChildIndex];
|
|
121
136
|
const target = this.heap[targetIndex];
|
|
122
|
-
if (leftChildIndex < this.heap.length &&
|
|
137
|
+
if (leftChildIndex < this.heap.length &&
|
|
138
|
+
leftChild &&
|
|
139
|
+
target &&
|
|
123
140
|
this.compareFn(leftChild.priority, target.priority) < 0) {
|
|
124
141
|
targetIndex = leftChildIndex;
|
|
125
142
|
}
|
|
126
143
|
const rightChild = this.heap[rightChildIndex];
|
|
127
144
|
const newTarget = this.heap[targetIndex];
|
|
128
|
-
if (rightChildIndex < this.heap.length &&
|
|
145
|
+
if (rightChildIndex < this.heap.length &&
|
|
146
|
+
rightChild &&
|
|
147
|
+
newTarget &&
|
|
129
148
|
this.compareFn(rightChild.priority, newTarget.priority) < 0) {
|
|
130
149
|
targetIndex = rightChildIndex;
|
|
131
150
|
}
|
|
@@ -137,6 +156,8 @@ export class PriorityQueue {
|
|
|
137
156
|
}
|
|
138
157
|
/**
|
|
139
158
|
* Swap two elements in the heap
|
|
159
|
+
* @param i - Index of the first element
|
|
160
|
+
* @param j - Index of the second element
|
|
140
161
|
*/
|
|
141
162
|
swap(i, j) {
|
|
142
163
|
const temp = this.heap[i];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"priority-queue.js","sourceRoot":"","sources":["../../../src/data-structures/priority-queue.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,OAAO,aAAa;IAItB,YAAY,SAA4C;QACpD,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC;QACf,qEAAqE;QACrE,IAAI,CAAC,SAAS,GAAG,SAAS,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IACpD,CAAC;IAED
|
|
1
|
+
{"version":3,"file":"priority-queue.js","sourceRoot":"","sources":["../../../src/data-structures/priority-queue.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,OAAO,aAAa;IAItB;;;OAGG;IACH,YAAY,SAA4C;QACpD,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC;QACf,qEAAqE;QACrE,IAAI,CAAC,SAAS,GAAG,SAAS,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IACpD,CAAC;IAED;;;;OAIG;IACH,OAAO,CAAC,IAAO,EAAE,QAAgB;QAC7B,MAAM,OAAO,GAAG,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;QACnC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACzC,CAAC;IAED;;;OAGG;IACH,OAAO;QACH,IAAI,IAAI,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACzB,OAAO,SAAS,CAAC;QACrB,CAAC;QAED,IAAI,IAAI,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACzB,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,EAAE,IAAI,CAAC;QACjC,CAAC;QAED,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAC1B,MAAM,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;QAEpC,IAAI,CAAC,IAAI,EAAE,CAAC;YACR,OAAO,SAAS,CAAC;QACr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UnionFind {
|
|
|
9
9
|
private parent;
|
|
10
10
|
private rank;
|
|
11
11
|
private componentCount;
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new UnionFind data structure with the given elements.
|
|
14
|
+
* @param elements - Array of elements to initialize the data structure with
|
|
15
|
+
*/
|
|
12
16
|
constructor(elements: NodeId[]);
|
|
13
17
|
/**
|
|
14
18
|
* Find the root of the set containing the element with path compression
|
|
19
|
+
* @param element - The element to find the root for
|
|
20
|
+
* @returns The root element of the set containing the given element
|
|
15
21
|
*/
|
|
16
22
|
find(element: NodeId): NodeId;
|
|
17
23
|
/**
|
|
18
24
|
* Union two sets using union by rank
|
|
25
|
+
* @param elementA - The first element whose set should be merged
|
|
26
|
+
* @param elementB - The second element whose set should be merged
|
|
19
27
|
*/
|
|
20
28
|
union(elementA: NodeId, elementB: NodeId): void;
|
|
21
29
|
/**
|
|
22
30
|
* Check if two elements are in the same connected component
|
|
31
|
+
* @param elementA - The first element to check
|
|
32
|
+
* @param elementB - The second element to check
|
|
33
|
+
* @returns True if both elements are in the same component, false otherwise
|
|
23
34
|
*/
|
|
24
35
|
connected(elementA: NodeId, elementB: NodeId): boolean;
|
|
25
36
|
/**
|
|
26
37
|
* Get the number of connected components
|
|
38
|
+
* @returns The current count of disjoint components
|
|
27
39
|
*/
|
|
28
40
|
getComponentCount(): number;
|
|
29
41
|
/**
|
|
30
42
|
* Get all elements that belong to the same component as the given element
|
|
43
|
+
* @param element - The element whose component should be retrieved
|
|
44
|
+
* @returns Array of all elements in the same component
|
|
31
45
|
*/
|
|
32
46
|
getComponent(element: NodeId): NodeId[];
|
|
33
47
|
/**
|
|
34
48
|
* Get all connected components as separate arrays
|
|
49
|
+
* @returns Array of arrays, where each inner array contains elements of one component
|
|
35
50
|
*/
|
|
36
51
|
getAllComponents(): NodeId[][];
|
|
37
52
|
/**
|
|
38
53
|
* Get the size of the component containing the given element
|
|
54
|
+
* @param element - The element whose component size should be retrieved
|
|
55
|
+
* @returns The number of elements in the component containing the given element
|
|
39
56
|
*/
|
|
40
57
|
getComponentSize(element: NodeId): number;
|
|
41
58
|
/**
|
|
42
59
|
* Add a new element to the data structure
|
|
60
|
+
* @param element - The element to add as a new singleton set
|
|
43
61
|
*/
|
|
44
62
|
addElement(element: NodeId): void;
|
|
45
63
|
/**
|
|
46
64
|
* Check if an element exists in the data structure
|
|
65
|
+
* @param element - The element to check for
|
|
66
|
+
* @returns True if the element exists, false otherwise
|
|
47
67
|
*/
|
|
48
68
|
hasElement(element: NodeId): boolean;
|
|
49
69
|
/**
|
|
50
70
|
* Get the total number of elements
|
|
71
|
+
* @returns The total count of all elements in the data structure
|
|
51
72
|
*/
|
|
52
73
|
size(): number;
|
|
53
74
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"union-find.d.ts","sourceRoot":"","sources":["../../../src/data-structures/union-find.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"union-find.d.ts","sourceRoot":"","sources":["../../../src/data-structures/union-find.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEhD;;;;;GAKG;AACH,qBAAa,SAAS;IAClB,OAAO,CAAC,MAAM,CAAsB;IACpC,OAAO,CAAC,IAAI,CAAsB;IAClC,OAAO,CAAC,cAAc,CAAS;IAE/B;;;OAGG;gBACS,QAAQ,EAAE,MAAM,EAAE;IAY9B;;;;OAIG;IACH,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM;IAoB7B;;;;OAIG;IACH,KAAK,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI;IA6B/C;;;;;OAKG;IACH,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO;IAQtD;;;OAGG;IACH,iBAAiB,IAAI,MAAM;IAI3B;;;;OAIG;IACH,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE;IAavC;;;OAGG;IACH,gBAAgB,IAAI,MAAM,EAAE,EAAE;IAmB9B;;;;OAIG;IACH,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM;IAIzC;;;OAGG;IACH,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAUjC;;;;OAIG;IACH,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAIpC;;;OAGG;IACH,IAAI,IAAI,MAAM;CAGjB"}
|
|
@@ -5,6 +5,10 @@
|
|
|
5
5
|
* algorithms like connected components and minimum spanning trees.
|
|
6
6
|
*/
|
|
7
7
|
export class UnionFind {
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new UnionFind data structure with the given elements.
|
|
10
|
+
* @param elements - Array of elements to initialize the data structure with
|
|
11
|
+
*/
|
|
8
12
|
constructor(elements) {
|
|
9
13
|
this.parent = new Map();
|
|
10
14
|
this.rank = new Map();
|
|
@@ -17,6 +21,8 @@ export class UnionFind {
|
|
|
17
21
|
}
|
|
18
22
|
/**
|
|
19
23
|
* Find the root of the set containing the element with path compression
|
|
24
|
+
* @param element - The element to find the root for
|
|
25
|
+
* @returns The root element of the set containing the given element
|
|
20
26
|
*/
|
|
21
27
|
find(element) {
|
|
22
28
|
const parent = this.parent.get(element);
|
|
@@ -35,6 +41,8 @@ export class UnionFind {
|
|
|
35
41
|
}
|
|
36
42
|
/**
|
|
37
43
|
* Union two sets using union by rank
|
|
44
|
+
* @param elementA - The first element whose set should be merged
|
|
45
|
+
* @param elementB - The second element whose set should be merged
|
|
38
46
|
*/
|
|
39
47
|
union(elementA, elementB) {
|
|
40
48
|
const rootA = this.find(elementA);
|
|
@@ -64,6 +72,9 @@ export class UnionFind {
|
|
|
64
72
|
}
|
|
65
73
|
/**
|
|
66
74
|
* Check if two elements are in the same connected component
|
|
75
|
+
* @param elementA - The first element to check
|
|
76
|
+
* @param elementB - The second element to check
|
|
77
|
+
* @returns True if both elements are in the same component, false otherwise
|
|
67
78
|
*/
|
|
68
79
|
connected(elementA, elementB) {
|
|
69
80
|
try {
|
|
@@ -75,12 +86,15 @@ export class UnionFind {
|
|
|
75
86
|
}
|
|
76
87
|
/**
|
|
77
88
|
* Get the number of connected components
|
|
89
|
+
* @returns The current count of disjoint components
|
|
78
90
|
*/
|
|
79
91
|
getComponentCount() {
|
|
80
92
|
return this.componentCount;
|
|
81
93
|
}
|
|
82
94
|
/**
|
|
83
95
|
* Get all elements that belong to the same component as the given element
|
|
96
|
+
* @param element - The element whose component should be retrieved
|
|
97
|
+
* @returns Array of all elements in the same component
|
|
84
98
|
*/
|
|
85
99
|
getComponent(element) {
|
|
86
100
|
const root = this.find(element);
|
|
@@ -94,6 +108,7 @@ export class UnionFind {
|
|
|
94
108
|
}
|
|
95
109
|
/**
|
|
96
110
|
* Get all connected components as separate arrays
|
|
111
|
+
* @returns Array of arrays, where each inner array contains elements of one component
|
|
97
112
|
*/
|
|
98
113
|
getAllComponents() {
|
|
99
114
|
const componentMap = new Map();
|
|
@@ -111,12 +126,15 @@ export class UnionFind {
|
|
|
111
126
|
}
|
|
112
127
|
/**
|
|
113
128
|
* Get the size of the component containing the given element
|
|
129
|
+
* @param element - The element whose component size should be retrieved
|
|
130
|
+
* @returns The number of elements in the component containing the given element
|
|
114
131
|
*/
|
|
115
132
|
getComponentSize(element) {
|
|
116
133
|
return this.getComponent(element).length;
|
|
117
134
|
}
|
|
118
135
|
/**
|
|
119
136
|
* Add a new element to the data structure
|
|
137
|
+
* @param element - The element to add as a new singleton set
|
|
120
138
|
*/
|
|
121
139
|
addElement(element) {
|
|
122
140
|
if (this.parent.has(element)) {
|
|
@@ -128,12 +146,15 @@ export class UnionFind {
|
|
|
128
146
|
}
|
|
129
147
|
/**
|
|
130
148
|
* Check if an element exists in the data structure
|
|
149
|
+
* @param element - The element to check for
|
|
150
|
+
* @returns True if the element exists, false otherwise
|
|
131
151
|
*/
|
|
132
152
|
hasElement(element) {
|
|
133
153
|
return this.parent.has(element);
|
|
134
154
|
}
|
|
135
155
|
/**
|
|
136
156
|
* Get the total number of elements
|
|
157
|
+
* @returns The total count of all elements in the data structure
|
|
137
158
|
*/
|
|
138
159
|
size() {
|
|
139
160
|
return this.parent.size;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"union-find.js","sourceRoot":"","sources":["../../../src/data-structures/union-find.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,OAAO,SAAS;IAKlB,YAAY,QAAkB;QAC1B,IAAI,CAAC,MAAM,GAAG,IAAI,GAAG,EAAE,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,IAAI,GAAG,EAAE,CAAC;QACtB,IAAI,CAAC,cAAc,GAAG,QAAQ,CAAC,MAAM,CAAC;QAEtC,wDAAwD;QACxD,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;YAC7B,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YAClC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;QAC9B,CAAC;IACL,CAAC;IAED
|
|
1
|
+
{"version":3,"file":"union-find.js","sourceRoot":"","sources":["../../../src/data-structures/union-find.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,OAAO,SAAS;IAKlB;;;OAGG;IACH,YAAY,QAAkB;QAC1B,IAAI,CAAC,MAAM,GAAG,IAAI,GAAG,EAAE,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,IAAI,GAAG,EAAE,CAAC;QACtB,IAAI,CAAC,cAAc,GAAG,QAAQ,CAAC,MAAM,CAAC;QAEtC,wDAAwD;QACxD,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;YAC7B,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YAClC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC,CAA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