-
Notifications
You must be signed in to change notification settings - Fork 0
/
ford-fulkerson.js
70 lines (58 loc) · 1.86 KB
/
ford-fulkerson.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Graph {
constructor(vertices) {
this.vertices = vertices;
this.adjacencyMatrix = Array.from(Array(vertices), () => new Array(vertices).fill(0));
}
addEdge(u, v, capacity) {
this.adjacencyMatrix[u][v] = capacity;
}
bfs(source, target, parent) {
let queue = [];
let visited = new Array(this.vertices).fill(false);
queue.push(source);
parent[source] = -1;
visited[source] = true;
while (queue.length > 0) {
let u = queue.shift();
for (let v = 0; v < this.vertices; v++) {
if (!visited[v] && this.adjacencyMatrix[u][v] > 0) {
queue.push(v);
parent[v] = u;
visited[v] = true;
}
}
}
return visited[target];
}
fordFulkerson(source, target) {
let maxFlow = 0;
let parent = new Array(this.vertices);
while (this.bfs(source, target, parent)) {
let pathFlow = Infinity;
for (let v = target; v !== source; v = parent[v]) {
let u = parent[v];
pathFlow = Math.min(pathFlow, this.adjacencyMatrix[u][v]);
}
for (let v = target; v !== source; v = parent[v]) {
let u = parent[v];
this.adjacencyMatrix[u][v] -= pathFlow;
this.adjacencyMatrix[v][u] += pathFlow;
}
maxFlow += pathFlow;
}
return maxFlow;
}
}
const graph = new Graph(6);
graph.addEdge(0, 1, 16);
graph.addEdge(0, 2, 13);
graph.addEdge(1, 2, 10);
graph.addEdge(1, 3, 12);
graph.addEdge(2, 1, 4);
graph.addEdge(2, 4, 14);
graph.addEdge(3, 2, 9);
graph.addEdge(3, 5, 20);
graph.addEdge(4, 3, 7);
graph.addEdge(4, 5, 4);
const maxFlow = graph.fordFulkerson(0, 5);
console.log("Maximum Flow:", maxFlow);