|
| 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. |
0 commit comments