Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2be0410

Browse files
feat(js): add modular solution and documentation for hard challenge 7 - Minimum Cost Maximum Flow
1 parent 33ef7b5 commit 2be0410

File tree

3 files changed

+281
-0
lines changed

3 files changed

+281
-0
lines changed
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
# Minimum Cost Maximum Flow
2+
3+
## English
4+
5+
The minimum cost maximum flow problem is a fundamental problem in network flow theory, combining flow maximization with cost minimization. The Bellman-Ford algorithm is used to find shortest paths in graphs with negative edge weights, enabling efficient cost optimization.
6+
7+
This challenge involves implementing an algorithm that finds the maximum flow with the minimum cost in a network.
8+
9+
### Relevant Code Snippet
10+
11+
```javascript
12+
class MinCostMaxFlow {
13+
constructor(n) {
14+
this.n = n;
15+
this.graph = Array.from({ length: n }, () => []);
16+
}
17+
18+
addEdge(u, v, capacity, cost) {
19+
this.graph[u].push([v, capacity, cost, this.graph[v].length]);
20+
this.graph[v].push([u, 0, -cost, this.graph[u].length - 1]);
21+
}
22+
23+
bellmanFord(source, dist, parent) {
24+
dist.fill(Infinity);
25+
dist[source] = 0;
26+
const inQueue = new Array(this.n).fill(false);
27+
const queue = [source];
28+
inQueue[source] = true;
29+
30+
while (queue.length > 0) {
31+
const u = queue.shift();
32+
inQueue[u] = false;
33+
for (let i = 0; i < this.graph[u].length; i++) {
34+
const [v, capacity, cost, rev] = this.graph[u][i];
35+
if (capacity > 0 && dist[u] + cost < dist[v]) {
36+
dist[v] = dist[u] + cost;
37+
parent[v] = [u, i];
38+
if (!inQueue[v]) {
39+
queue.push(v);
40+
inQueue[v] = true;
41+
}
42+
}
43+
}
44+
}
45+
}
46+
47+
minCostMaxFlow(source, sink) {
48+
let flow = 0;
49+
let cost = 0;
50+
const dist = new Array(this.n);
51+
const parent = new Array(this.n).fill([-1, -1]);
52+
53+
while (true) {
54+
this.bellmanFord(source, dist, parent);
55+
if (dist[sink] === Infinity) {
56+
break;
57+
}
58+
59+
let pathFlow = Infinity;
60+
let v = sink;
61+
while (v !== source) {
62+
const [u, i] = parent[v];
63+
pathFlow = Math.min(pathFlow, this.graph[u][i][1]);
64+
v = u;
65+
}
66+
67+
flow += pathFlow;
68+
cost += pathFlow * dist[sink];
69+
70+
v = sink;
71+
while (v !== source) {
72+
const [u, i] = parent[v];
73+
this.graph[u][i][1] -= pathFlow;
74+
const rev = this.graph[u][i][3];
75+
this.graph[v][rev][1] += pathFlow;
76+
v = u;
77+
}
78+
}
79+
80+
return { flow, cost };
81+
}
82+
}
83+
```
84+
85+
### History
86+
87+
The minimum cost maximum flow problem is a classic combinatorial optimization problem with applications in transportation, network design, and resource allocation.
88+
89+
---
90+
91+
## Español
92+
93+
Flujo Máximo de Costo Mínimo
94+
95+
El problema de flujo máximo de costo mínimo es un problema fundamental en la teoría de flujos en redes, que combina la maximización del flujo con la minimización del costo. El algoritmo de Bellman-Ford se usa para encontrar caminos más cortos en grafos con pesos negativos, permitiendo una optimización eficiente del costo.
96+
97+
Este reto consiste en implementar un algoritmo que encuentre el flujo máximo con el costo mínimo en una red.
98+
99+
### Fragmento de Código Relevante
100+
101+
```javascript
102+
class MinCostMaxFlow {
103+
constructor(n) {
104+
this.n = n;
105+
this.graph = Array.from({ length: n }, () => []);
106+
}
107+
108+
addEdge(u, v, capacity, cost) {
109+
this.graph[u].push([v, capacity, cost, this.graph[v].length]);
110+
this.graph[v].push([u, 0, -cost, this.graph[u].length - 1]);
111+
}
112+
113+
bellmanFord(source, dist, parent) {
114+
dist.fill(Infinity);
115+
dist[source] = 0;
116+
const inQueue = new Array(this.n).fill(false);
117+
const queue = [source];
118+
inQueue[source] = true;
119+
120+
while (queue.length > 0) {
121+
const u = queue.shift();
122+
inQueue[u] = false;
123+
for (let i = 0; i < this.graph[u].length; i++) {
124+
const [v, capacity, cost, rev] = this.graph[u][i];
125+
if (capacity > 0 && dist[u] + cost < dist[v]) {
126+
dist[v] = dist[u] + cost;
127+
parent[v] = [u, i];
128+
if (!inQueue[v]) {
129+
queue.push(v);
130+
inQueue[v] = true;
131+
}
132+
}
133+
}
134+
}
135+
}
136+
137+
minCostMaxFlow(source, sink) {
138+
let flow = 0;
139+
let cost = 0;
140+
const dist = new Array(this.n);
141+
const parent = new Array(this.n).fill([-1, -1]);
142+
143+
while (true) {
144+
this.bellmanFord(source, dist, parent);
145+
if (dist[sink] === Infinity) {
146+
break;
147+
}
148+
149+
let pathFlow = Infinity;
150+
let v = sink;
151+
while (v !== source) {
152+
const [u, i] = parent[v];
153+
pathFlow = Math.min(pathFlow, this.graph[u][i][1]);
154+
v = u;
155+
}
156+
157+
flow += pathFlow;
158+
cost += pathFlow * dist[sink];
159+
160+
v = sink;
161+
while (v !== source) {
162+
const [u, i] = parent[v];
163+
this.graph[u][i][1] -= pathFlow;
164+
const rev = this.graph[u][i][3];
165+
this.graph[v][rev][1] += pathFlow;
166+
v = u;
167+
}
168+
}
169+
170+
return { flow, cost };
171+
}
172+
}
173+
```
174+
175+
### Historia
176+
177+
El problema de flujo máximo de costo mínimo es un problema clásico de optimización combinatoria con aplicaciones en transporte, diseño de redes y asignación de recursos.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// MinCostMaxFlow.js - Minimum Cost Maximum Flow algorithm using Bellman-Ford in JavaScript
2+
3+
class MinCostMaxFlow {
4+
constructor(n) {
5+
this.n = n;
6+
this.graph = Array.from({ length: n }, () => []);
7+
}
8+
9+
addEdge(u, v, capacity, cost) {
10+
this.graph[u].push([v, capacity, cost, this.graph[v].length]);
11+
this.graph[v].push([u, 0, -cost, this.graph[u].length - 1]);
12+
}
13+
14+
bellmanFord(source, dist, parent) {
15+
dist.fill(Infinity);
16+
dist[source] = 0;
17+
const inQueue = new Array(this.n).fill(false);
18+
const queue = [source];
19+
inQueue[source] = true;
20+
21+
while (queue.length > 0) {
22+
const u = queue.shift();
23+
inQueue[u] = false;
24+
for (let i = 0; i < this.graph[u].length; i++) {
25+
const [v, capacity, cost] = this.graph[u][i];
26+
if (capacity > 0 && dist[u] + cost < dist[v]) {
27+
dist[v] = dist[u] + cost;
28+
parent[v] = [u, i];
29+
if (!inQueue[v]) {
30+
queue.push(v);
31+
inQueue[v] = true;
32+
}
33+
}
34+
}
35+
}
36+
}
37+
38+
minCostMaxFlow(source, sink) {
39+
let flow = 0;
40+
let cost = 0;
41+
const dist = new Array(this.n);
42+
const parent = new Array(this.n).fill([-1, -1]);
43+
44+
while (true) {
45+
this.bellmanFord(source, dist, parent);
46+
if (dist[sink] === Infinity) {
47+
break;
48+
}
49+
50+
let pathFlow = Infinity;
51+
let v = sink;
52+
while (v !== source) {
53+
const [u, i] = parent[v];
54+
pathFlow = Math.min(pathFlow, this.graph[u][i][1]);
55+
v = u;
56+
}
57+
58+
flow += pathFlow;
59+
cost += pathFlow * dist[sink];
60+
61+
v = sink;
62+
while (v !== source) {
63+
const [u, i] = parent[v];
64+
this.graph[u][i][1] -= pathFlow;
65+
const rev = this.graph[u][i][3];
66+
this.graph[v][rev][1] += pathFlow;
67+
v = u;
68+
}
69+
}
70+
71+
return { flow, cost };
72+
}
73+
}
74+
75+
export default MinCostMaxFlow;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Challenge:
3+
Combine maximum flow and cost optimization concepts to implement an algorithm that finds the maximum flow with the minimum cost in a network.
4+
5+
This solution follows DRY principles and is implemented in JavaScript.
6+
*/
7+
8+
import MinCostMaxFlow from "./MinCostMaxFlow.js";
9+
10+
function main() {
11+
const n = 4;
12+
const mcmf = new MinCostMaxFlow(n);
13+
14+
// Example edges: addEdge(u, v, capacity, cost)
15+
mcmf.addEdge(0, 1, 3, 1);
16+
mcmf.addEdge(0, 2, 2, 2);
17+
mcmf.addEdge(1, 2, 2, 1);
18+
mcmf.addEdge(1, 3, 2, 3);
19+
mcmf.addEdge(2, 3, 3, 1);
20+
21+
const source = 0;
22+
const sink = 3;
23+
24+
const result = mcmf.minCostMaxFlow(source, sink);
25+
console.log("Maximum flow:", result.flow);
26+
console.log("Minimum cost:", result.cost);
27+
}
28+
29+
main();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /