Repository navigation
Expand file tree
/
Copy pathCapacityScalingSolverAdjacencyList.java
More file actions
151 lines (123 loc) · 4.08 KB
/
Copy pathCapacityScalingSolverAdjacencyList.java
File metadata and controls
151 lines (123 loc) · 4.08 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* Implementation of the Capacity Scaling algorithm using a DFS as a method of finding augmenting
* paths.
*
* <p>Time Complexity: O(E^2log(U)), where E = num edges, U = max capacity
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.graphtheory.networkflow;
import static java.lang.Math.max;
import static java.lang.Math.min;
import java.util.List;
public class CapacityScalingSolverAdjacencyList extends NetworkFlowSolverBase {
private long delta;
/**
* Creates an instance of a flow network solver. Use the {@link #addEdge(int, int, int)} method to
* add edges to the graph.
*
* @param n - The number of nodes in the graph including source and sink nodes.
* @param s - The index of the source node, 0 <= s < n
* @param t - The index of the sink node, 0 <= t < n, t != s
*/
public CapacityScalingSolverAdjacencyList(int n, int s, int t) {
super(n, s, t);
}
/**
* Adds a directed edge (and residual edge) to the flow graph.
*
* @param from - The index of the node the directed edge starts at.
* @param to - The index of the node the directed edge end at.
* @param capacity - The capacity of the edge.
*/
@Override
public void addEdge(int from, int to, long capacity) {
super.addEdge(from, to, capacity);
delta = max(delta, capacity);
}
// Performs the Ford-Fulkerson method applying a depth first search as
// a means of finding an augmenting path.
@Override
public void solve() {
// Start delta at the largest power of 2 <= the largest capacity.
// Equivalent of: delta = (long) pow(2, (int)floor(log(delta)/log(2)))
delta = Long.highestOneBit(delta);
// Repeatedly find augmenting paths from source to sink using only edges
// with a remaining capacity >= delta. Half delta every time we become unable
// to find an augmenting path from source to sink until the graph is saturated.
for (long f = 0; delta > 0; delta /= 2) {
do {
markAllNodesAsUnvisited();
f = dfs(s, INF);
maxFlow += f;
} while (f != 0);
}
// Find min cut.
for (int i = 0; i < n; i++) if (visited(i)) minCut[i] = true;
}
private long dfs(int node, long flow) {
// At sink node, return augmented path flow.
if (node == t) return flow;
List<Edge> edges = graph[node];
visit(node);
for (Edge edge : edges) {
long cap = edge.remainingCapacity();
if (cap >= delta && !visited(edge.to)) {
long bottleNeck = dfs(edge.to, min(flow, cap));
// Augment flow with bottle neck value
if (bottleNeck > 0) {
edge.augment(bottleNeck);
return bottleNeck;
}
}
}
return 0;
}
/* Example */
public static void main(String[] args) {
testSmallFlowGraph();
testExampleFromMySlides();
}
// Testing graph from:
// http://crypto.cs.mcgill.ca/~crepeau/COMP251/KeyNoteSlides/07demo-maxflowCS-C.pdf
private static void testSmallFlowGraph() {
int n = 6;
int s = n - 1;
int t = n - 2;
CapacityScalingSolverAdjacencyList solver;
solver = new CapacityScalingSolverAdjacencyList(n, s, t);
// Source edges
solver.addEdge(s, 0, 10);
solver.addEdge(s, 1, 10);
// Sink edges
solver.addEdge(2, t, 10);
solver.addEdge(3, t, 10);
// Middle edges
solver.addEdge(0, 1, 2);
solver.addEdge(0, 2, 4);
solver.addEdge(0, 3, 8);
solver.addEdge(1, 3, 9);
solver.addEdge(3, 2, 6);
System.out.println(solver.getMaxFlow()); // 19
}
private static void testExampleFromMySlides() {
int n = 6;
int s = n - 1;
int t = n - 2;
CapacityScalingSolverAdjacencyList solver;
solver = new CapacityScalingSolverAdjacencyList(n, s, t);
// Source edges
solver.addEdge(s, 0, 6);
solver.addEdge(s, 1, 14);
// Sink edges
solver.addEdge(2, t, 11);
solver.addEdge(3, t, 12);
// Middle edges
solver.addEdge(0, 1, 1);
solver.addEdge(2, 3, 1);
solver.addEdge(0, 2, 5);
solver.addEdge(1, 2, 7);
solver.addEdge(1, 3, 10);
System.out.println(solver.getMaxFlow()); // 20
}
}