9.1. Flow Network
This section describes how to implement a flow network.
Last updated
Was this helpful?
This section describes how to implement a flow network.
Last updated
Was this helpful?
Was this helpful?
public class MaxFlow {
private Map<Edge, Double> flow_map;
private double maxflow;
public MaxFlow(Graph graph) {
init(graph);
}
public void init(Graph graph) {
flow_map = new HashMap<>();
maxflow = 0;
for (Edge edge : graph.getAllEdges())
flow_map.put(edge, 0d);
}
}public double getMaxFlow() {
return maxflow;
}
public double getResidual(Edge edge) {
return edge.getWeight() - flow_map.get(edge);
}public void updateFlow(List<Edge> path, double flow) {
path.forEach(e -> updateFlow(e, flow));
maxflow += flow;
}
public void updateFlow(Edge edge, double flow) {
Double prev = flow_map.getOrDefault(edge, 0d);
flow_map.put(edge, prev + flow);
}