Skip to content

Commit c2d0714

Browse files
committed
✨ expose graph.floydwarshall
1 parent 5e6751a commit c2d0714

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

src/graph/floyd-warshal.bench.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { buildDMUsingFloydWarshal } from "./floyd-warshal";
2+
3+
function generateSampleData(
4+
N: number,
5+
edgeProb: number,
6+
): [number, number, number][] {
7+
const edges: [number, number, number][] = [];
8+
9+
// For simplicity, we will just generate a random set of edges.
10+
// There is no guarantee every node will be connected.
11+
for (let i = 0; i < N; i++) {
12+
for (let j = 0; j < N; j++) {
13+
if (Math.random() < edgeProb && i !== j) {
14+
// 5% probability of an edge between any two nodes.
15+
const distance = Math.floor(Math.random() * 10) + 1;
16+
if (distance !== 0) {
17+
edges.push([i, j, distance]);
18+
}
19+
}
20+
}
21+
}
22+
23+
return edges;
24+
}
25+
26+
const N = 300;
27+
const EDGE_PROB = 0.05;
28+
const ITERS = 100;
29+
const edges = generateSampleData(N, EDGE_PROB);
30+
31+
let result: any;
32+
// warmup
33+
for (let i = 0; i < 100; i++) {
34+
result = buildDMUsingFloydWarshal(N, edges);
35+
}
36+
const start = Date.now();
37+
for (let i = 0; i < ITERS; i++) {
38+
result = buildDMUsingFloydWarshal(N, edges);
39+
}
40+
const end = Date.now();
41+
console.log(
42+
"Benchmarking buildDMUsingFloydWarshal",
43+
(end - start) / ITERS,
44+
"ms",
45+
);
46+
console.log(result.length);

src/graph/floyd-warshal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function buildDAGTransitiveClosure<T>(
2626
export function buildDMUsingFloydWarshal(
2727
N: number,
2828
edges: [number, number, number][],
29-
) {
29+
): number[][] {
3030
const dm: number[][] = [];
3131
for (let i = 0; i < N; i++) {
3232
const row: number[] = [];

src/graph/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
export { buildDAGTransitiveClosure } from "./floyd-warshal";
1+
export {
2+
buildDAGTransitiveClosure,
3+
buildDMUsingFloydWarshal,
4+
buildClosureFromDM,
5+
} from "./floyd-warshal";
26
export { Edge, TransitiveClosure } from "./interfaces";
37
export { isThereCycleInDG } from "./detect-cycle";
48
export { topoSort } from "./topo-sort";

0 commit comments

Comments
 (0)