Skip to content

Commit 6fed73d

Browse files
Implement the furhter changes to make the algorithm more refined
1 parent a358ea6 commit 6fed73d

File tree

4 files changed

+178
-160
lines changed

4 files changed

+178
-160
lines changed

graph/edmondkarp.ts

-76
This file was deleted.

graph/edmonds_karp.ts

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* @function edmondsKarp
3+
* @description Compute the maximum flow from a source node to a sink node using the Edmonds-Karp algorithm.
4+
* @Complexity_Analysis
5+
* Time complexity: O(V * E^2) where V is the number of vertices and E is the number of edges.
6+
* Space Complexity: O(E) due to residual graph representation.
7+
* @param {[number, number][][]} graph - The graph in adjacency list form.
8+
* @param {number} source - The source node.
9+
* @param {number} sink - The sink node.
10+
* @return {number} - The maximum flow from the source node to the sink node.
11+
* @see https://en.wikipedia.org/wiki/Edmonds%E2%80%93Karp_algorithm
12+
*/
13+
14+
export default function edmondsKarp(
15+
graph: [number, number][][],
16+
source: number,
17+
sink: number
18+
): number {
19+
const n = graph.length
20+
21+
// Initialize residual graph
22+
const residualGraph: [number, number][][] = Array.from(
23+
{ length: n },
24+
() => []
25+
)
26+
27+
// Build residual graph from the original graph
28+
for (let u = 0; u < n; u++) {
29+
for (const [v, cap] of graph[u]) {
30+
if (cap > 0) {
31+
residualGraph[u].push([v, cap]) // Forward edge
32+
residualGraph[v].push([u, 0]) // Reverse edge with 0 capacity
33+
}
34+
}
35+
}
36+
37+
const findAugmentingPath = (parent: (number | null)[]): number => {
38+
const visited = Array(n).fill(false)
39+
const queue: number[] = []
40+
queue.push(source)
41+
visited[source] = true
42+
parent[source] = null
43+
44+
while (queue.length > 0) {
45+
const u = queue.shift()!
46+
for (const [v, cap] of residualGraph[u]) {
47+
if (!visited[v] && cap > 0) {
48+
parent[v] = u
49+
visited[v] = true
50+
if (v === sink) {
51+
// Return the bottleneck capacity along the path
52+
let pathFlow = Infinity
53+
let current = v
54+
while (parent[current] !== null) {
55+
const prev = parent[current]!
56+
const edgeCap = residualGraph[prev].find(
57+
([node]) => node === current
58+
)![1]
59+
pathFlow = Math.min(pathFlow, edgeCap)
60+
current = prev
61+
}
62+
return pathFlow
63+
}
64+
queue.push(v)
65+
}
66+
}
67+
}
68+
return 0
69+
}
70+
71+
let maxFlow = 0
72+
const parent = Array(n).fill(null)
73+
74+
while (true) {
75+
const pathFlow = findAugmentingPath(parent)
76+
if (pathFlow === 0) break // No augmenting path found
77+
78+
// Update the capacities and reverse capacities in the residual graph
79+
let v = sink
80+
while (parent[v] !== null) {
81+
const u = parent[v]!
82+
// Update capacity of the forward edge
83+
const forwardEdge = residualGraph[u].find(([node]) => node === v)!
84+
forwardEdge[1] -= pathFlow
85+
// Update capacity of the reverse edge
86+
const reverseEdge = residualGraph[v].find(([node]) => node === u)!
87+
reverseEdge[1] += pathFlow
88+
89+
v = u
90+
}
91+
92+
maxFlow += pathFlow
93+
}
94+
95+
return maxFlow
96+
}

graph/test/edmondkarp_test.ts

-84
This file was deleted.

graph/test/edmonds_karp.test.ts

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import edmondsKarp from '../edmonds_karp'
2+
3+
describe('Edmonds-Karp Algorithm', () => {
4+
it('should find the maximum flow in a simple graph', () => {
5+
const graph: [number, number][][] = [
6+
[
7+
[1, 3],
8+
[2, 2]
9+
], // Node 0: Edges to node 1 (capacity 3), and node 2 (capacity 2)
10+
[[3, 2]], // Node 1: Edge to node 3 (capacity 2)
11+
[[3, 3]], // Node 2: Edge to node 3 (capacity 3)
12+
[] // Node 3: No outgoing edges
13+
]
14+
const source = 0
15+
const sink = 3
16+
const maxFlow = edmondsKarp(graph, source, sink)
17+
expect(maxFlow).toBe(4)
18+
})
19+
20+
it('should find the maximum flow in a more complex graph', () => {
21+
const graph: [number, number][][] = [
22+
[
23+
[1, 10],
24+
[2, 10]
25+
], // Node 0: Edges to node 1 and node 2 (both capacity 10)
26+
[
27+
[3, 4],
28+
[4, 8]
29+
], // Node 1: Edges to node 3 (capacity 4), and node 4 (capacity 8)
30+
[[4, 9]], // Node 2: Edge to node 4 (capacity 9)
31+
[[5, 10]], // Node 3: Edge to node 5 (capacity 10)
32+
[[5, 10]], // Node 4: Edge to node 5 (capacity 10)
33+
[] // Node 5: No outgoing edges (sink)
34+
]
35+
const source = 0
36+
const sink = 5
37+
const maxFlow = edmondsKarp(graph, source, sink)
38+
expect(maxFlow).toBe(14)
39+
})
40+
41+
it('should return 0 when there is no path from source to sink', () => {
42+
const graph: [number, number][][] = [
43+
[], // Node 0: No outgoing edges
44+
[], // Node 1: No outgoing edges
45+
[] // Node 2: No outgoing edges (sink)
46+
]
47+
const source = 0
48+
const sink = 2
49+
const maxFlow = edmondsKarp(graph, source, sink)
50+
expect(maxFlow).toBe(0)
51+
})
52+
53+
it('should handle graphs with no edges', () => {
54+
const graph: [number, number][][] = [
55+
[], // Node 0: No outgoing edges
56+
[], // Node 1: No outgoing edges
57+
[] // Node 2: No outgoing edges
58+
]
59+
const source = 0
60+
const sink = 2
61+
const maxFlow = edmondsKarp(graph, source, sink)
62+
expect(maxFlow).toBe(0)
63+
})
64+
65+
it('should handle graphs with self-loops', () => {
66+
const graph: [number, number][][] = [
67+
[
68+
[0, 10],
69+
[1, 10]
70+
], // Node 0: Self-loop with capacity 10, and edge to node 1 (capacity 10)
71+
[
72+
[1, 10],
73+
[2, 10]
74+
], // Node 1: Self-loop and edge to node 2
75+
[] // Node 2: No outgoing edges (sink)
76+
]
77+
const source = 0
78+
const sink = 2
79+
const maxFlow = edmondsKarp(graph, source, sink)
80+
expect(maxFlow).toBe(10)
81+
})
82+
})

0 commit comments

Comments
 (0)