Skip to content

Commit fc349fe

Browse files
committed
feat(js): add modular solution and documentation for hard challenge 2 - Maximum Flow (Edmonds-Karp Algorithm)
1 parent 5198c47 commit fc349fe

File tree

3 files changed

+211
-0
lines changed

3 files changed

+211
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Maximum Flow - Edmonds-Karp Algorithm
2+
3+
## English
4+
5+
The Maximum Flow problem involves finding the greatest possible flow in a flow network from a source node to a sink node without violating capacity constraints on the edges.
6+
7+
This challenge requires implementing the Edmonds-Karp algorithm, which is an implementation of the Ford-Fulkerson method using BFS to find augmenting paths. The solution is implemented in JavaScript using Object-Oriented Programming (OOP) principles and follows DRY (Don't Repeat Yourself) best practices.
8+
9+
### Relevant Code Snippet
10+
11+
```javascript
12+
class MaximumFlow {
13+
constructor(graph) {
14+
this.graph = graph;
15+
this.n = graph.length;
16+
}
17+
18+
bfs(source, sink, parent) {
19+
const visited = new Array(this.n).fill(false);
20+
const queue = [];
21+
queue.push(source);
22+
visited[source] = true;
23+
parent[source] = -1;
24+
25+
while (queue.length > 0) {
26+
const u = queue.shift();
27+
28+
for (let v = 0; v < this.n; v++) {
29+
if (!visited[v] && this.graph[u][v] > 0) {
30+
queue.push(v);
31+
parent[v] = u;
32+
visited[v] = true;
33+
}
34+
}
35+
}
36+
return visited[sink];
37+
}
38+
39+
edmondsKarp(source, sink) {
40+
const parent = new Array(this.n);
41+
let maxFlow = 0;
42+
43+
while (this.bfs(source, sink, parent)) {
44+
let pathFlow = Infinity;
45+
for (let v = sink; v !== source; v = parent[v]) {
46+
const u = parent[v];
47+
pathFlow = Math.min(pathFlow, this.graph[u][v]);
48+
}
49+
50+
for (let v = sink; v !== source; v = parent[v]) {
51+
const u = parent[v];
52+
this.graph[u][v] -= pathFlow;
53+
this.graph[v][u] += pathFlow;
54+
}
55+
56+
maxFlow += pathFlow;
57+
}
58+
return maxFlow;
59+
}
60+
}
61+
```
62+
63+
### History
64+
65+
The Maximum Flow problem and the Edmonds-Karp algorithm have been fundamental in network flow theory since the 1950s. The Edmonds-Karp algorithm improves the Ford-Fulkerson method by using BFS to find shortest augmenting paths, ensuring polynomial time complexity.
66+
67+
---
68+
69+
## Español
70+
71+
El problema de Flujo Máximo consiste en encontrar el flujo máximo posible en una red de flujo desde un nodo fuente hasta un nodo sumidero sin violar las capacidades de las aristas.
72+
73+
Este reto requiere implementar el algoritmo de Edmonds-Karp, que es una implementación del método de Ford-Fulkerson usando BFS para encontrar caminos aumentantes. La solución está implementada en JavaScript usando principios de Programación Orientada a Objetos (POO) y siguiendo las mejores prácticas de DRY (No te repitas).
74+
75+
### Fragmento de Código Relevante
76+
77+
```javascript
78+
class MaximumFlow {
79+
constructor(graph) {
80+
this.graph = graph;
81+
this.n = graph.length;
82+
}
83+
84+
bfs(source, sink, parent) {
85+
const visited = new Array(this.n).fill(false);
86+
const queue = [];
87+
queue.push(source);
88+
visited[source] = true;
89+
parent[source] = -1;
90+
91+
while (queue.length > 0) {
92+
const u = queue.shift();
93+
94+
for (let v = 0; v < this.n; v++) {
95+
if (!visited[v] && this.graph[u][v] > 0) {
96+
queue.push(v);
97+
parent[v] = u;
98+
visited[v] = true;
99+
}
100+
}
101+
}
102+
return visited[sink];
103+
}
104+
105+
edmondsKarp(source, sink) {
106+
const parent = new Array(this.n);
107+
let maxFlow = 0;
108+
109+
while (this.bfs(source, sink, parent)) {
110+
let pathFlow = Infinity;
111+
for (let v = sink; v !== source; v = parent[v]) {
112+
const u = parent[v];
113+
pathFlow = Math.min(pathFlow, this.graph[u][v]);
114+
}
115+
116+
for (let v = sink; v !== source; v = parent[v]) {
117+
const u = parent[v];
118+
this.graph[u][v] -= pathFlow;
119+
this.graph[v][u] += pathFlow;
120+
}
121+
122+
maxFlow += pathFlow;
123+
}
124+
return maxFlow;
125+
}
126+
}
127+
```
128+
129+
### Historia
130+
131+
El problema de Flujo Máximo y el algoritmo de Edmonds-Karp han sido fundamentales en la teoría de flujos de red desde los años 50. El algoritmo de Edmonds-Karp mejora el método de Ford-Fulkerson usando BFS para encontrar los caminos aumentantes más cortos, asegurando una complejidad polinómica.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// MaximumFlow.js - Edmonds-Karp algorithm implementation using OOP and DRY principles
2+
3+
class MaximumFlow {
4+
constructor(graph) {
5+
this.graph = graph;
6+
this.n = graph.length;
7+
}
8+
9+
bfs(source, sink, parent) {
10+
const visited = new Array(this.n).fill(false);
11+
const queue = [];
12+
queue.push(source);
13+
visited[source] = true;
14+
parent[source] = -1;
15+
16+
while (queue.length > 0) {
17+
const u = queue.shift();
18+
19+
for (let v = 0; v < this.n; v++) {
20+
if (!visited[v] && this.graph[u][v] > 0) {
21+
queue.push(v);
22+
parent[v] = u;
23+
visited[v] = true;
24+
}
25+
}
26+
}
27+
return visited[sink];
28+
}
29+
30+
edmondsKarp(source, sink) {
31+
const parent = new Array(this.n);
32+
let maxFlow = 0;
33+
34+
while (this.bfs(source, sink, parent)) {
35+
let pathFlow = Infinity;
36+
for (let v = sink; v !== source; v = parent[v]) {
37+
const u = parent[v];
38+
pathFlow = Math.min(pathFlow, this.graph[u][v]);
39+
}
40+
41+
for (let v = sink; v !== source; v = parent[v]) {
42+
const u = parent[v];
43+
this.graph[u][v] -= pathFlow;
44+
this.graph[v][u] += pathFlow;
45+
}
46+
47+
maxFlow += pathFlow;
48+
}
49+
return maxFlow;
50+
}
51+
}
52+
53+
export default MaximumFlow;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Challenge:
3+
Implement the Edmonds-Karp algorithm to find the maximum flow in a flow network.
4+
Given a directed graph where each edge has a capacity, find the maximum flow from a source node to a sink node.
5+
6+
This solution uses Object-Oriented Programming (OOP) principles and follows DRY (Don't Repeat Yourself) best practices.
7+
*/
8+
9+
import MaximumFlow from "./MaximumFlow.js";
10+
11+
function main() {
12+
// Example graph represented as an adjacency matrix of capacities
13+
const graph = [
14+
[0, 16, 13, 0, 0, 0],
15+
[0, 0, 10, 12, 0, 0],
16+
[0, 4, 0, 0, 14, 0],
17+
[0, 0, 9, 0, 0, 20],
18+
[0, 0, 0, 7, 0, 4],
19+
[0, 0, 0, 0, 0, 0],
20+
];
21+
22+
const maxFlowSolver = new MaximumFlow(graph);
23+
const maxFlow = maxFlowSolver.edmondsKarp(0, 5);
24+
console.log("The maximum possible flow is:", maxFlow);
25+
}
26+
27+
main();

0 commit comments

Comments
 (0)