Skip to content

Commit 2aa4555

Browse files
committedOct 11, 2024
Implement the optimal stack queue implementation in the edmond karp algorithm
2 parents 76dd412 + 4b5f2b5 commit 2aa4555

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed
 

‎graph/edmonds_karp.ts

+36-36
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { StackQueue } from "../data_structures/queue/stack_queue";
1+
import { StackQueue } from '../data_structures/queue/stack_queue'
22

33
/**
44
* @function edmondsKarp
@@ -17,81 +17,81 @@ export default function edmondsKarp(
1717
source: number,
1818
sink: number
1919
): number {
20-
const n = graph.length;
20+
const n = graph.length
2121

2222
// Initialize residual graph
2323
const residualGraph: [number, number][][] = Array.from(
2424
{ length: n },
2525
() => []
26-
);
26+
)
2727

2828
// Build residual graph from the original graph
2929
for (let u = 0; u < n; u++) {
3030
for (const [v, cap] of graph[u]) {
3131
if (cap > 0) {
32-
residualGraph[u].push([v, cap]); // Forward edge
33-
residualGraph[v].push([u, 0]); // Reverse edge with 0 capacity
32+
residualGraph[u].push([v, cap]) // Forward edge
33+
residualGraph[v].push([u, 0]) // Reverse edge with 0 capacity
3434
}
3535
}
3636
}
3737

3838
const findAugmentingPath = (parent: (number | null)[]): number => {
39-
const visited = Array(n).fill(false);
40-
const queue = new StackQueue<number>();
41-
queue.enqueue(source);
42-
visited[source] = true;
43-
parent[source] = null;
39+
const visited = Array(n).fill(false)
40+
const queue = new StackQueue<number>()
41+
queue.enqueue(source)
42+
visited[source] = true
43+
parent[source] = null
4444

4545
while (queue.length() > 0) {
46-
const u = queue.dequeue();
46+
const u = queue.dequeue()
4747
for (const [v, cap] of residualGraph[u]) {
4848
if (!visited[v] && cap > 0) {
49-
parent[v] = u;
50-
visited[v] = true;
49+
parent[v] = u
50+
visited[v] = true
5151
if (v === sink) {
5252
// Return the bottleneck capacity along the path
53-
let pathFlow = Infinity;
54-
let current = v;
53+
let pathFlow = Infinity
54+
let current = v
5555
while (parent[current] !== null) {
56-
const prev = parent[current]!;
56+
const prev = parent[current]!
5757
const edgeCap = residualGraph[prev].find(
5858
([node]) => node === current
59-
)![1];
60-
pathFlow = Math.min(pathFlow, edgeCap);
61-
current = prev;
59+
)![1]
60+
pathFlow = Math.min(pathFlow, edgeCap)
61+
current = prev
6262
}
63-
return pathFlow;
63+
return pathFlow
6464
}
65-
queue.enqueue(v);
65+
queue.enqueue(v)
6666
}
6767
}
6868
}
69-
return 0;
70-
};
69+
return 0
70+
}
7171

72-
let maxFlow = 0;
73-
const parent = Array(n).fill(null);
72+
let maxFlow = 0
73+
const parent = Array(n).fill(null)
7474

7575
while (true) {
76-
const pathFlow = findAugmentingPath(parent);
77-
if (pathFlow === 0) break; // No augmenting path found
76+
const pathFlow = findAugmentingPath(parent)
77+
if (pathFlow === 0) break // No augmenting path found
7878

7979
// Update the capacities and reverse capacities in the residual graph
80-
let v = sink;
80+
let v = sink
8181
while (parent[v] !== null) {
82-
const u = parent[v]!;
82+
const u = parent[v]!
8383
// Update capacity of the forward edge
84-
const forwardEdge = residualGraph[u].find(([node]) => node === v)!;
85-
forwardEdge[1] -= pathFlow;
84+
const forwardEdge = residualGraph[u].find(([node]) => node === v)!
85+
forwardEdge[1] -= pathFlow
8686
// Update capacity of the reverse edge
87-
const reverseEdge = residualGraph[v].find(([node]) => node === u)!;
88-
reverseEdge[1] += pathFlow;
87+
const reverseEdge = residualGraph[v].find(([node]) => node === u)!
88+
reverseEdge[1] += pathFlow
8989

90-
v = u;
90+
v = u
9191
}
9292

93-
maxFlow += pathFlow;
93+
maxFlow += pathFlow
9494
}
9595

96-
return maxFlow;
96+
return maxFlow
9797
}

0 commit comments

Comments
 (0)