@graphty/algorithms 1.0.1
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/LICENSE +21 -0
- package/README.md +193 -0
- package/dist/src/algorithms/centrality/betweenness.d.ts +34 -0
- package/dist/src/algorithms/centrality/betweenness.d.ts.map +1 -0
- package/dist/src/algorithms/centrality/betweenness.js +223 -0
- package/dist/src/algorithms/centrality/betweenness.js.map +1 -0
- package/dist/src/algorithms/centrality/closeness.d.ts +43 -0
- package/dist/src/algorithms/centrality/closeness.d.ts.map +1 -0
- package/dist/src/algorithms/centrality/closeness.js +202 -0
- package/dist/src/algorithms/centrality/closeness.js.map +1 -0
- package/dist/src/algorithms/centrality/degree.d.ts +17 -0
- package/dist/src/algorithms/centrality/degree.d.ts.map +1 -0
- package/dist/src/algorithms/centrality/degree.js +86 -0
- package/dist/src/algorithms/centrality/degree.js.map +1 -0
- package/dist/src/algorithms/centrality/index.d.ts +11 -0
- package/dist/src/algorithms/centrality/index.d.ts.map +1 -0
- package/dist/src/algorithms/centrality/index.js +8 -0
- package/dist/src/algorithms/centrality/index.js.map +1 -0
- package/dist/src/algorithms/centrality/pagerank.d.ts +74 -0
- package/dist/src/algorithms/centrality/pagerank.d.ts.map +1 -0
- package/dist/src/algorithms/centrality/pagerank.js +191 -0
- package/dist/src/algorithms/centrality/pagerank.js.map +1 -0
- package/dist/src/algorithms/components/connected.d.ts +58 -0
- package/dist/src/algorithms/components/connected.d.ts.map +1 -0
- package/dist/src/algorithms/components/connected.js +234 -0
- package/dist/src/algorithms/components/connected.js.map +1 -0
- package/dist/src/algorithms/components/index.d.ts +5 -0
- package/dist/src/algorithms/components/index.d.ts.map +1 -0
- package/dist/src/algorithms/components/index.js +5 -0
- package/dist/src/algorithms/components/index.js.map +1 -0
- package/dist/src/algorithms/index.d.ts +8 -0
- package/dist/src/algorithms/index.d.ts.map +1 -0
- package/dist/src/algorithms/index.js +12 -0
- package/dist/src/algorithms/index.js.map +1 -0
- package/dist/src/algorithms/shortest-path/bellman-ford.d.ts +52 -0
- package/dist/src/algorithms/shortest-path/bellman-ford.d.ts.map +1 -0
- package/dist/src/algorithms/shortest-path/bellman-ford.js +131 -0
- package/dist/src/algorithms/shortest-path/bellman-ford.js.map +1 -0
- package/dist/src/algorithms/shortest-path/dijkstra.d.ts +26 -0
- package/dist/src/algorithms/shortest-path/dijkstra.d.ts.map +1 -0
- package/dist/src/algorithms/shortest-path/dijkstra.js +199 -0
- package/dist/src/algorithms/shortest-path/dijkstra.js.map +1 -0
- package/dist/src/algorithms/shortest-path/index.d.ts +7 -0
- package/dist/src/algorithms/shortest-path/index.d.ts.map +1 -0
- package/dist/src/algorithms/shortest-path/index.js +6 -0
- package/dist/src/algorithms/shortest-path/index.js.map +1 -0
- package/dist/src/algorithms/traversal/bfs.d.ts +25 -0
- package/dist/src/algorithms/traversal/bfs.d.ts.map +1 -0
- package/dist/src/algorithms/traversal/bfs.js +189 -0
- package/dist/src/algorithms/traversal/bfs.js.map +1 -0
- package/dist/src/algorithms/traversal/dfs.d.ts +32 -0
- package/dist/src/algorithms/traversal/dfs.d.ts.map +1 -0
- package/dist/src/algorithms/traversal/dfs.js +259 -0
- package/dist/src/algorithms/traversal/dfs.js.map +1 -0
- package/dist/src/algorithms/traversal/index.d.ts +7 -0
- package/dist/src/algorithms/traversal/index.d.ts.map +1 -0
- package/dist/src/algorithms/traversal/index.js +6 -0
- package/dist/src/algorithms/traversal/index.js.map +1 -0
- package/dist/src/core/graph.d.ts +100 -0
- package/dist/src/core/graph.d.ts.map +1 -0
- package/dist/src/core/graph.js +276 -0
- package/dist/src/core/graph.js.map +1 -0
- package/dist/src/data-structures/index.d.ts +6 -0
- package/dist/src/data-structures/index.d.ts.map +1 -0
- package/dist/src/data-structures/index.js +6 -0
- package/dist/src/data-structures/index.js.map +1 -0
- package/dist/src/data-structures/priority-queue.d.ts +60 -0
- package/dist/src/data-structures/priority-queue.d.ts.map +1 -0
- package/dist/src/data-structures/priority-queue.js +152 -0
- package/dist/src/data-structures/priority-queue.js.map +1 -0
- package/dist/src/data-structures/union-find.d.ts +54 -0
- package/dist/src/data-structures/union-find.d.ts.map +1 -0
- package/dist/src/data-structures/union-find.js +145 -0
- package/dist/src/data-structures/union-find.js.map +1 -0
- package/dist/src/index.d.ts +11 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +13 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/types/index.d.ts +74 -0
- package/dist/src/types/index.d.ts.map +1 -0
- package/dist/src/types/index.js +5 -0
- package/dist/src/types/index.js.map +1 -0
- package/dist/test/browser/basic.test.d.ts +2 -0
- package/dist/test/browser/basic.test.d.ts.map +1 -0
- package/dist/test/browser/basic.test.js +70 -0
- package/dist/test/browser/basic.test.js.map +1 -0
- package/dist/test/unit/bellman-ford.test.d.ts +2 -0
- package/dist/test/unit/bellman-ford.test.d.ts.map +1 -0
- package/dist/test/unit/bellman-ford.test.js +231 -0
- package/dist/test/unit/bellman-ford.test.js.map +1 -0
- package/dist/test/unit/betweenness-centrality.test.d.ts +2 -0
- package/dist/test/unit/betweenness-centrality.test.d.ts.map +1 -0
- package/dist/test/unit/betweenness-centrality.test.js +311 -0
- package/dist/test/unit/betweenness-centrality.test.js.map +1 -0
- package/dist/test/unit/bfs.test.d.ts +2 -0
- package/dist/test/unit/bfs.test.d.ts.map +1 -0
- package/dist/test/unit/bfs.test.js +277 -0
- package/dist/test/unit/bfs.test.js.map +1 -0
- package/dist/test/unit/closeness.test.d.ts +2 -0
- package/dist/test/unit/closeness.test.d.ts.map +1 -0
- package/dist/test/unit/closeness.test.js +266 -0
- package/dist/test/unit/closeness.test.js.map +1 -0
- package/dist/test/unit/connected-components.test.d.ts +2 -0
- package/dist/test/unit/connected-components.test.d.ts.map +1 -0
- package/dist/test/unit/connected-components.test.js +355 -0
- package/dist/test/unit/connected-components.test.js.map +1 -0
- package/dist/test/unit/degree-centrality.test.d.ts +2 -0
- package/dist/test/unit/degree-centrality.test.d.ts.map +1 -0
- package/dist/test/unit/degree-centrality.test.js +260 -0
- package/dist/test/unit/degree-centrality.test.js.map +1 -0
- package/dist/test/unit/dfs.test.d.ts +2 -0
- package/dist/test/unit/dfs.test.d.ts.map +1 -0
- package/dist/test/unit/dfs.test.js +350 -0
- package/dist/test/unit/dfs.test.js.map +1 -0
- package/dist/test/unit/dijkstra.test.d.ts +2 -0
- package/dist/test/unit/dijkstra.test.d.ts.map +1 -0
- package/dist/test/unit/dijkstra.test.js +311 -0
- package/dist/test/unit/dijkstra.test.js.map +1 -0
- package/dist/test/unit/graph.test.d.ts +2 -0
- package/dist/test/unit/graph.test.d.ts.map +1 -0
- package/dist/test/unit/graph.test.js +175 -0
- package/dist/test/unit/graph.test.js.map +1 -0
- package/dist/test/unit/pagerank.test.d.ts +2 -0
- package/dist/test/unit/pagerank.test.d.ts.map +1 -0
- package/dist/test/unit/pagerank.test.js +281 -0
- package/dist/test/unit/pagerank.test.js.map +1 -0
- package/dist/test/unit/priority-queue.test.d.ts +2 -0
- package/dist/test/unit/priority-queue.test.d.ts.map +1 -0
- package/dist/test/unit/priority-queue.test.js +189 -0
- package/dist/test/unit/priority-queue.test.js.map +1 -0
- package/dist/test/unit/union-find.test.d.ts +2 -0
- package/dist/test/unit/union-find.test.d.ts.map +1 -0
- package/dist/test/unit/union-find.test.js +245 -0
- package/dist/test/unit/union-find.test.js.map +1 -0
- package/package.json +100 -0
- package/src/algorithms/centrality/betweenness.ts +303 -0
- package/src/algorithms/centrality/closeness.ts +296 -0
- package/src/algorithms/centrality/degree.ts +105 -0
- package/src/algorithms/centrality/index.ts +11 -0
- package/src/algorithms/centrality/pagerank.ts +302 -0
- package/src/algorithms/components/connected.ts +298 -0
- package/src/algorithms/components/index.ts +17 -0
- package/src/algorithms/index.ts +15 -0
- package/src/algorithms/shortest-path/bellman-ford.ts +214 -0
- package/src/algorithms/shortest-path/dijkstra.ts +256 -0
- package/src/algorithms/shortest-path/index.ts +7 -0
- package/src/algorithms/traversal/bfs.ts +236 -0
- package/src/algorithms/traversal/dfs.ts +367 -0
- package/src/algorithms/traversal/index.ts +7 -0
- package/src/core/graph.ts +326 -0
- package/src/data-structures/index.ts +6 -0
- package/src/data-structures/priority-queue.ts +180 -0
- package/src/data-structures/union-find.ts +170 -0
- package/src/index.ts +35 -0
- package/src/types/index.ts +95 -0
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { PriorityQueue } from "../../data-structures/priority-queue.js";
|
|
2
|
+
/**
|
|
3
|
+
* Dijkstra's algorithm implementation for single-source shortest paths
|
|
4
|
+
*
|
|
5
|
+
* Finds shortest paths from a source node to all other nodes in a weighted graph
|
|
6
|
+
* with non-negative edge weights using a priority queue for efficiency.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Find shortest paths from source to all reachable nodes using Dijkstra's algorithm
|
|
10
|
+
*/
|
|
11
|
+
export function dijkstra(graph, source, options = {}) {
|
|
12
|
+
if (!graph.hasNode(source)) {
|
|
13
|
+
throw new Error(`Source node ${String(source)} not found in graph`);
|
|
14
|
+
}
|
|
15
|
+
const distances = new Map();
|
|
16
|
+
const previous = new Map();
|
|
17
|
+
const visited = new Set();
|
|
18
|
+
const pq = new PriorityQueue();
|
|
19
|
+
// Initialize distances
|
|
20
|
+
for (const node of graph.nodes()) {
|
|
21
|
+
const distance = node.id === source ? 0 : Infinity;
|
|
22
|
+
distances.set(node.id, distance);
|
|
23
|
+
previous.set(node.id, null);
|
|
24
|
+
pq.enqueue(node.id, distance);
|
|
25
|
+
}
|
|
26
|
+
while (!pq.isEmpty()) {
|
|
27
|
+
const currentNode = pq.dequeue();
|
|
28
|
+
if (currentNode === undefined) {
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
const currentDistance = distances.get(currentNode);
|
|
32
|
+
if (currentDistance === undefined) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
// Skip if already visited (can happen with priority queue updates)
|
|
36
|
+
if (visited.has(currentNode)) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
visited.add(currentNode);
|
|
40
|
+
// Early termination if target reached
|
|
41
|
+
if (options.target && currentNode === options.target) {
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
// Skip if this node is unreachable
|
|
45
|
+
if (currentDistance === Infinity) {
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
// Check all neighbors
|
|
49
|
+
for (const neighbor of graph.neighbors(currentNode)) {
|
|
50
|
+
if (visited.has(neighbor)) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
const edge = graph.getEdge(currentNode, neighbor);
|
|
54
|
+
if (!edge) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const edgeWeight = edge.weight ?? 1;
|
|
58
|
+
if (edgeWeight < 0) {
|
|
59
|
+
throw new Error("Dijkstra's algorithm does not support negative edge weights");
|
|
60
|
+
}
|
|
61
|
+
const tentativeDistance = currentDistance + edgeWeight;
|
|
62
|
+
const neighborDistance = distances.get(neighbor);
|
|
63
|
+
if (neighborDistance === undefined) {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
// Found a shorter path
|
|
67
|
+
if (tentativeDistance < neighborDistance) {
|
|
68
|
+
distances.set(neighbor, tentativeDistance);
|
|
69
|
+
previous.set(neighbor, currentNode);
|
|
70
|
+
// Add to priority queue with new distance
|
|
71
|
+
pq.enqueue(neighbor, tentativeDistance);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// Build results
|
|
76
|
+
const results = new Map();
|
|
77
|
+
for (const [nodeId, distance] of distances) {
|
|
78
|
+
if (distance < Infinity) {
|
|
79
|
+
const path = reconstructPath(nodeId, previous);
|
|
80
|
+
results.set(nodeId, {
|
|
81
|
+
distance,
|
|
82
|
+
path,
|
|
83
|
+
predecessor: new Map(previous),
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return results;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Find shortest path between two specific nodes using Dijkstra's algorithm
|
|
91
|
+
*/
|
|
92
|
+
export function dijkstraPath(graph, source, target) {
|
|
93
|
+
if (!graph.hasNode(source)) {
|
|
94
|
+
throw new Error(`Source node ${String(source)} not found in graph`);
|
|
95
|
+
}
|
|
96
|
+
if (!graph.hasNode(target)) {
|
|
97
|
+
throw new Error(`Target node ${String(target)} not found in graph`);
|
|
98
|
+
}
|
|
99
|
+
// Special case: source equals target
|
|
100
|
+
if (source === target) {
|
|
101
|
+
return {
|
|
102
|
+
distance: 0,
|
|
103
|
+
path: [source],
|
|
104
|
+
predecessor: new Map([[source, null]]),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
const results = dijkstra(graph, source, { target });
|
|
108
|
+
return results.get(target) ?? null;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Single-source shortest paths with early termination optimization
|
|
112
|
+
*/
|
|
113
|
+
export function singleSourceShortestPath(graph, source, cutoff) {
|
|
114
|
+
if (!graph.hasNode(source)) {
|
|
115
|
+
throw new Error(`Source node ${String(source)} not found in graph`);
|
|
116
|
+
}
|
|
117
|
+
const distances = new Map();
|
|
118
|
+
const visited = new Set();
|
|
119
|
+
const pq = new PriorityQueue();
|
|
120
|
+
// Initialize distances
|
|
121
|
+
for (const node of graph.nodes()) {
|
|
122
|
+
const distance = node.id === source ? 0 : Infinity;
|
|
123
|
+
distances.set(node.id, distance);
|
|
124
|
+
pq.enqueue(node.id, distance);
|
|
125
|
+
}
|
|
126
|
+
while (!pq.isEmpty()) {
|
|
127
|
+
const currentNode = pq.dequeue();
|
|
128
|
+
if (currentNode === undefined) {
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
const currentDistance = distances.get(currentNode);
|
|
132
|
+
if (currentDistance === undefined) {
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
// Skip if already visited
|
|
136
|
+
if (visited.has(currentNode)) {
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
visited.add(currentNode);
|
|
140
|
+
// Skip if unreachable or beyond cutoff
|
|
141
|
+
if (currentDistance === Infinity || (cutoff !== undefined && currentDistance > cutoff)) {
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
// Check all neighbors
|
|
145
|
+
for (const neighbor of graph.neighbors(currentNode)) {
|
|
146
|
+
if (visited.has(neighbor)) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
const edge = graph.getEdge(currentNode, neighbor);
|
|
150
|
+
if (!edge) {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
const edgeWeight = edge.weight ?? 1;
|
|
154
|
+
const tentativeDistance = currentDistance + edgeWeight;
|
|
155
|
+
const neighborDistance = distances.get(neighbor);
|
|
156
|
+
if (neighborDistance === undefined) {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
// Found a shorter path
|
|
160
|
+
if (tentativeDistance < neighborDistance) {
|
|
161
|
+
distances.set(neighbor, tentativeDistance);
|
|
162
|
+
pq.enqueue(neighbor, tentativeDistance);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// Filter out unreachable nodes and apply cutoff
|
|
167
|
+
const result = new Map();
|
|
168
|
+
for (const [nodeId, distance] of distances) {
|
|
169
|
+
if (distance < Infinity && (cutoff === undefined || distance <= cutoff)) {
|
|
170
|
+
result.set(nodeId, distance);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return result;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* All-pairs shortest paths using repeated Dijkstra
|
|
177
|
+
* Note: For dense graphs, consider Floyd-Warshall algorithm instead
|
|
178
|
+
*/
|
|
179
|
+
export function allPairsShortestPath(graph) {
|
|
180
|
+
const results = new Map();
|
|
181
|
+
for (const node of graph.nodes()) {
|
|
182
|
+
const distances = singleSourceShortestPath(graph, node.id);
|
|
183
|
+
results.set(node.id, distances);
|
|
184
|
+
}
|
|
185
|
+
return results;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Reconstruct path from predecessor map
|
|
189
|
+
*/
|
|
190
|
+
function reconstructPath(target, previous) {
|
|
191
|
+
const path = [];
|
|
192
|
+
let current = target;
|
|
193
|
+
while (current !== null) {
|
|
194
|
+
path.unshift(current);
|
|
195
|
+
current = previous.get(current) ?? null;
|
|
196
|
+
}
|
|
197
|
+
return path;
|
|
198
|
+
}
|
|
199
|
+
//# sourceMappingURL=dijkstra.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dijkstra.js","sourceRoot":"","sources":["../../../../src/algorithms/shortest-path/dijkstra.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,aAAa,EAAC,MAAM,yCAAyC,CAAC;AAGtE;;;;;GAKG;AAEH;;GAEG;AACH,MAAM,UAAU,QAAQ,CACpB,KAAY,EACZ,MAAc,EACd,UAA2B,EAAE;IAE7B,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QACzB,MAAM,IAAI,KAAK,CAAC,eAAe,MAAM,CAAC,MAAM,CAAC,qBAAqB,CAAC,CAAC;IACxE,CAAC;IAED,MAAM,SAAS,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC5C,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAyB,CAAC;IAClD,MAAM,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IAClC,MAAM,EAAE,GAAG,IAAI,aAAa,EAAU,CAAC;IAEvC,uBAAuB;IACvB,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,KAAK,EAAE,EAAE,CAAC;QAC/B,MAAM,QAAQ,GAAG,IAAI,CAAC,EAAE,KAAK,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;QACnD,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,EAAE,QAAQ,CAAC,CAA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C;IAED,OAAO,IAAI,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shortest path algorithms
|
|
3
|
+
*/
|
|
4
|
+
export type { BellmanFordOptions, BellmanFordResult } from "./bellman-ford.js";
|
|
5
|
+
export { bellmanFord, bellmanFordPath, hasNegativeCycle } from "./bellman-ford.js";
|
|
6
|
+
export { allPairsShortestPath, dijkstra, dijkstraPath, singleSourceShortestPath } from "./dijkstra.js";
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/algorithms/shortest-path/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,YAAY,EAAC,kBAAkB,EAAE,iBAAiB,EAAC,MAAM,mBAAmB,CAAC;AAC7E,OAAO,EAAC,WAAW,EAAE,eAAe,EAAE,gBAAgB,EAAC,MAAM,mBAAmB,CAAC;AACjF,OAAO,EAAC,oBAAoB,EAAE,QAAQ,EAAE,YAAY,EAAE,wBAAwB,EAAC,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/algorithms/shortest-path/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAC,WAAW,EAAE,eAAe,EAAE,gBAAgB,EAAC,MAAM,mBAAmB,CAAC;AACjF,OAAO,EAAC,oBAAoB,EAAE,QAAQ,EAAE,YAAY,EAAE,wBAAwB,EAAC,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Graph } from "../../core/graph.js";
|
|
2
|
+
import type { NodeId, ShortestPathResult, TraversalOptions, TraversalResult } from "../../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Breadth-First Search (BFS) implementation
|
|
5
|
+
*
|
|
6
|
+
* Explores graph level by level from a starting node. Guarantees shortest path
|
|
7
|
+
* in unweighted graphs and can be used for various graph analysis tasks.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Perform breadth-first search starting from a given node
|
|
11
|
+
*/
|
|
12
|
+
export declare function breadthFirstSearch(graph: Graph, startNode: NodeId, options?: TraversalOptions): TraversalResult;
|
|
13
|
+
/**
|
|
14
|
+
* Find shortest path between two nodes in an unweighted graph using BFS
|
|
15
|
+
*/
|
|
16
|
+
export declare function shortestPathBFS(graph: Graph, source: NodeId, target: NodeId): ShortestPathResult | null;
|
|
17
|
+
/**
|
|
18
|
+
* Find shortest paths from source to all reachable nodes using BFS
|
|
19
|
+
*/
|
|
20
|
+
export declare function singleSourceShortestPathBFS(graph: Graph, source: NodeId): Map<NodeId, ShortestPathResult>;
|
|
21
|
+
/**
|
|
22
|
+
* Check if the graph is bipartite using BFS coloring
|
|
23
|
+
*/
|
|
24
|
+
export declare function isBipartite(graph: Graph): boolean;
|
|
25
|
+
//# sourceMappingURL=bfs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bfs.d.ts","sourceRoot":"","sources":["../../../../src/algorithms/traversal/bfs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,KAAK,EAAC,MAAM,qBAAqB,CAAC;AAC/C,OAAO,KAAK,EAAC,MAAM,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,eAAe,EAAC,MAAM,sBAAsB,CAAC;AAExG;;;;;GAKG;AAEH;;GAEG;AACH,wBAAgB,kBAAkB,CAC9B,KAAK,EAAE,KAAK,EACZ,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE,gBAAqB,GAC/B,eAAe,CA4CjB;AAED;;GAEG;AACH,wBAAgB,eAAe,CAC3B,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,GACf,kBAAkB,GAAG,IAAI,CAuD3B;AAED;;GAEG;AACH,wBAAgB,2BAA2B,CACvC,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE,MAAM,GACf,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAwCjC;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CA0CjD"}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Breadth-First Search (BFS) implementation
|
|
3
|
+
*
|
|
4
|
+
* Explores graph level by level from a starting node. Guarantees shortest path
|
|
5
|
+
* in unweighted graphs and can be used for various graph analysis tasks.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Perform breadth-first search starting from a given node
|
|
9
|
+
*/
|
|
10
|
+
export function breadthFirstSearch(graph, startNode, options = {}) {
|
|
11
|
+
if (!graph.hasNode(startNode)) {
|
|
12
|
+
throw new Error(`Start node ${String(startNode)} not found in graph`);
|
|
13
|
+
}
|
|
14
|
+
const visited = new Set();
|
|
15
|
+
const queue = [];
|
|
16
|
+
const order = [];
|
|
17
|
+
const tree = new Map();
|
|
18
|
+
// Initialize with start node
|
|
19
|
+
queue.push({ node: startNode, level: 0 });
|
|
20
|
+
visited.add(startNode);
|
|
21
|
+
tree.set(startNode, null);
|
|
22
|
+
while (queue.length > 0) {
|
|
23
|
+
const current = queue.shift();
|
|
24
|
+
if (!current) {
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
order.push(current.node);
|
|
28
|
+
// Call visitor callback if provided
|
|
29
|
+
if (options.visitCallback) {
|
|
30
|
+
options.visitCallback(current.node, current.level);
|
|
31
|
+
}
|
|
32
|
+
// Early termination if target found
|
|
33
|
+
if (options.targetNode && current.node === options.targetNode) {
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
// Explore neighbors
|
|
37
|
+
for (const neighbor of graph.neighbors(current.node)) {
|
|
38
|
+
if (!visited.has(neighbor)) {
|
|
39
|
+
visited.add(neighbor);
|
|
40
|
+
tree.set(neighbor, current.node);
|
|
41
|
+
queue.push({ node: neighbor, level: current.level + 1 });
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return { visited, order, tree };
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Find shortest path between two nodes in an unweighted graph using BFS
|
|
49
|
+
*/
|
|
50
|
+
export function shortestPathBFS(graph, source, target) {
|
|
51
|
+
if (!graph.hasNode(source)) {
|
|
52
|
+
throw new Error(`Source node ${String(source)} not found in graph`);
|
|
53
|
+
}
|
|
54
|
+
if (!graph.hasNode(target)) {
|
|
55
|
+
throw new Error(`Target node ${String(target)} not found in graph`);
|
|
56
|
+
}
|
|
57
|
+
// Special case: source equals target
|
|
58
|
+
if (source === target) {
|
|
59
|
+
return {
|
|
60
|
+
distance: 0,
|
|
61
|
+
path: [source],
|
|
62
|
+
predecessor: new Map([[source, null]]),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
const visited = new Set();
|
|
66
|
+
const queue = [];
|
|
67
|
+
const predecessor = new Map();
|
|
68
|
+
// Initialize BFS
|
|
69
|
+
queue.push({ node: source, distance: 0 });
|
|
70
|
+
visited.add(source);
|
|
71
|
+
predecessor.set(source, null);
|
|
72
|
+
while (queue.length > 0) {
|
|
73
|
+
const current = queue.shift();
|
|
74
|
+
if (!current) {
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
// Target found
|
|
78
|
+
if (current.node === target) {
|
|
79
|
+
const path = reconstructPath(target, predecessor);
|
|
80
|
+
return {
|
|
81
|
+
distance: current.distance,
|
|
82
|
+
path,
|
|
83
|
+
predecessor,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
// Explore neighbors
|
|
87
|
+
for (const neighbor of graph.neighbors(current.node)) {
|
|
88
|
+
if (!visited.has(neighbor)) {
|
|
89
|
+
visited.add(neighbor);
|
|
90
|
+
predecessor.set(neighbor, current.node);
|
|
91
|
+
queue.push({ node: neighbor, distance: current.distance + 1 });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// No path found
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Find shortest paths from source to all reachable nodes using BFS
|
|
100
|
+
*/
|
|
101
|
+
export function singleSourceShortestPathBFS(graph, source) {
|
|
102
|
+
if (!graph.hasNode(source)) {
|
|
103
|
+
throw new Error(`Source node ${String(source)} not found in graph`);
|
|
104
|
+
}
|
|
105
|
+
const results = new Map();
|
|
106
|
+
const visited = new Set();
|
|
107
|
+
const queue = [];
|
|
108
|
+
const predecessor = new Map();
|
|
109
|
+
// Initialize BFS
|
|
110
|
+
queue.push({ node: source, distance: 0 });
|
|
111
|
+
visited.add(source);
|
|
112
|
+
predecessor.set(source, null);
|
|
113
|
+
while (queue.length > 0) {
|
|
114
|
+
const current = queue.shift();
|
|
115
|
+
if (!current) {
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
// Store result for current node
|
|
119
|
+
const path = reconstructPath(current.node, predecessor);
|
|
120
|
+
results.set(current.node, {
|
|
121
|
+
distance: current.distance,
|
|
122
|
+
path,
|
|
123
|
+
predecessor: new Map(predecessor),
|
|
124
|
+
});
|
|
125
|
+
// Explore neighbors
|
|
126
|
+
for (const neighbor of graph.neighbors(current.node)) {
|
|
127
|
+
if (!visited.has(neighbor)) {
|
|
128
|
+
visited.add(neighbor);
|
|
129
|
+
predecessor.set(neighbor, current.node);
|
|
130
|
+
queue.push({ node: neighbor, distance: current.distance + 1 });
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return results;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Check if the graph is bipartite using BFS coloring
|
|
138
|
+
*/
|
|
139
|
+
export function isBipartite(graph) {
|
|
140
|
+
if (graph.isDirected) {
|
|
141
|
+
throw new Error("Bipartite test requires an undirected graph");
|
|
142
|
+
}
|
|
143
|
+
const color = new Map();
|
|
144
|
+
const visited = new Set();
|
|
145
|
+
// Check each connected component
|
|
146
|
+
for (const node of graph.nodes()) {
|
|
147
|
+
if (!visited.has(node.id)) {
|
|
148
|
+
const queue = [node.id];
|
|
149
|
+
color.set(node.id, 0);
|
|
150
|
+
visited.add(node.id);
|
|
151
|
+
while (queue.length > 0) {
|
|
152
|
+
const current = queue.shift();
|
|
153
|
+
if (!current) {
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
const currentColor = color.get(current);
|
|
157
|
+
if (currentColor === undefined) {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
for (const neighbor of graph.neighbors(current)) {
|
|
161
|
+
if (!visited.has(neighbor)) {
|
|
162
|
+
// Color with opposite color
|
|
163
|
+
color.set(neighbor, currentColor === 0 ? 1 : 0);
|
|
164
|
+
visited.add(neighbor);
|
|
165
|
+
queue.push(neighbor);
|
|
166
|
+
}
|
|
167
|
+
else if (color.get(neighbor) === currentColor) {
|
|
168
|
+
// Same color as current node - not bipartite
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Reconstruct path from predecessor map
|
|
179
|
+
*/
|
|
180
|
+
function reconstructPath(target, predecessor) {
|
|
181
|
+
const path = [];
|
|
182
|
+
let current = target;
|
|
183
|
+
while (current !== null) {
|
|
184
|
+
path.unshift(current);
|
|
185
|
+
current = predecessor.get(current) ?? null;
|
|
186
|
+
}
|
|
187
|
+
return path;
|
|
188
|
+
}
|
|
189
|
+
//# sourceMappingURL=bfs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bfs.js","sourceRoot":"","sources":["../../../../src/algorithms/traversal/bfs.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AAEH;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAC9B,KAAY,EACZ,SAAiB,EACjB,UAA4B,EAAE;IAE9B,IAAI,CAAC,KAAK,C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,CAAC,CAAC,CAAC,CAAC;wBAChD,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;wBACtB,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;oBACzB,CAAC;yBAAM,IAAI,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,KAAK,YAAY,EAAE,CAAC;wBAC9C,6CAA6C;wBAC7C,OAAO,KAAK,CAAC;oBACjB,CAAC;gBACL,CAAC;YACL,CAAC;QACL,CAAC;IACL,CAAC;IAED,OAAO,IAAI,CAAC;AAChB,CAAC;AAED;;GAEG;AACH,SAAS,eAAe,CAAC,MAAc,EAAE,WAAuC;IAC5E,MAAM,IAAI,GAAa,EAAE,CAAC;IAC1B,IAAI,OAAO,GAAkB,MAAM,CAAC;IAEpC,OAAO,OAAO,KAAK,IAAI,EAAE,CAAC;QACtB,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QACtB,OAAO,GAAG,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC;IAC/C,CAAC;IAED,OAAO,IAAI,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Graph } from "../../core/graph.js";
|
|
2
|
+
import type { NodeId, TraversalOptions, TraversalResult } from "../../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Depth-First Search (DFS) implementation
|
|
5
|
+
*
|
|
6
|
+
* Explores graph by going as deep as possible before backtracking.
|
|
7
|
+
* Useful for cycle detection, topological sorting, and connectivity analysis.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* DFS traversal options
|
|
11
|
+
*/
|
|
12
|
+
export interface DFSOptions extends TraversalOptions {
|
|
13
|
+
recursive?: boolean;
|
|
14
|
+
preOrder?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Perform depth-first search starting from a given node
|
|
18
|
+
*/
|
|
19
|
+
export declare function depthFirstSearch(graph: Graph, startNode: NodeId, options?: DFSOptions): TraversalResult;
|
|
20
|
+
/**
|
|
21
|
+
* Detect cycles in a graph using DFS
|
|
22
|
+
*/
|
|
23
|
+
export declare function hasCycleDFS(graph: Graph): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Topological sorting using DFS (for directed acyclic graphs)
|
|
26
|
+
*/
|
|
27
|
+
export declare function topologicalSort(graph: Graph): NodeId[] | null;
|
|
28
|
+
/**
|
|
29
|
+
* Find strongly connected components using DFS (for directed graphs)
|
|
30
|
+
*/
|
|
31
|
+
export declare function findStronglyConnectedComponents(graph: Graph): NodeId[][];
|
|
32
|
+
//# sourceMappingURL=dfs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dfs.d.ts","sourceRoot":"","sources":["../../../../src/algorithms/traversal/dfs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,qBAAqB,CAAC;AAC1C,OAAO,KAAK,EAAC,MAAM,EAAE,gBAAgB,EAAE,eAAe,EAAC,MAAM,sBAAsB,CAAC;AAEpF;;;;;GAKG;AAEH;;GAEG;AACH,MAAM,WAAW,UAAW,SAAQ,gBAAgB;IAChD,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACtB;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAC5B,KAAK,EAAE,KAAK,EACZ,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE,UAAe,GACzB,eAAe,CAgBjB;AAwGD;;GAEG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAoBjD;AAwDD;;GAEG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,EAAE,GAAG,IAAI,CAsB7D;AAwBD;;GAEG;AACH,wBAAgB,+BAA+B,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,EAAE,EAAE,CAgCxE"}
|