Skip to content

Java implementation of the algorithm for computing maximum flows

License

Notifications You must be signed in to change notification settings

denissudak/max-flow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Network max flow

Java implementation of the push-relabel maximum flow algorithm for computing maximum flows in a flow network. It is considered to be one of the most efficient maximum flow algorithms.


Flow network is a directed graph where every edge has a capacity. It has two special nodes: a source node and a sink node. Stuff is coming out of the source and goes into the sink node. There are also no edges that coming out and into the same node. The graph does not contain parallel edges – two or more arcs with the same tail and head nodes.

Network 1

Given a flow network this algorithm will determine how much stuff is possible to send from a source to a sink without exceeding the capacity of any arc. For example for the graph above the maximum flow would be 4. For the network below it would be 9

Network 2

By the time the algorithm terminates the original network gets transformed into residual graph. For the second example the residual graph would look like this:

Network 2 residual graph

Comparison to the original network can be used to recover the actual flow. Here's how the flow would look like for our example:

Network 2 max flow

Quick start

<dependency>
    <groupId>org.open-structures</groupId>
    <artifactId>max-flow</artifactId>
    <version>1.2.0</version>
</dependency>

The first step is to create a flow network for which we'd like to find max flow.

FlowNetwork flowNetwork = new FlowNetwork(source, sink);
flowNetwork.setArcCapacity(200, source, nodeA);
flowNetwork.setArcCapacity(1, source, nodeB);
flowNetwork.setArcCapacity(10, nodeA, nodeD);
flowNetwork.setArcCapacity(2, nodeA, nodeC);
flowNetwork.setArcCapacity(10, nodeB, sink);
flowNetwork.setArcCapacity(6, nodeC, nodeB);
flowNetwork.setArcCapacity(5, nodeD, nodeC);
flowNetwork.setArcCapacity(2, nodeD, sink);

Next pass it to the algorithm and calculate the max flow amount:

PushRelabelMaxFlow flow = new PushRelabelMaxFlow(flowNetwork);
flow.preflowPush();
flow.getFlowAmount() // would return 9 

Remember that the flowNetwork arc capacities will be modified as the algorithm does its thing and you end-up with the residual graph.

About

Java implementation of the algorithm for computing maximum flows

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages