HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Ford-Fulkerson algorithm

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
algorithmfulkersonford

Problem

Solved the ford-fulkerson algorithm, which is too vast to explain it comprehensively here. Check Wikipedia for Ford-Fulkerson and Princeton lecture on Ford-Fulkerson.

Looking for code review, optimizations and best practices. Also I request you avoid mentioning renaming GraphFordFuklerson and unit tests not done in separate files as I am aware it's not good practices, but deliberately done for personal convenience.

``
/**
* A duplex edge is two edges(forward and reverse edge) combined into one.
* In ford-fulkerson algo, we have a concept of forward edge and a reverse edge.
*
* The forward edge is when the "from" node equals "from" node set by constructor
* and
* "to" node equals "to" node set by constructor.
*
* Duplex emulates both these edges by returning different values based on "to" and "from"
* ie, it behaves as both "forward edge" and "backward egde" based on its input parameters.
*
* @param
*/
final class DuplexEdge {
private final T from;
private final T to;
private final double capacity;
private double consumedCapacity;

public DuplexEdge (T from, T to, double capacity, double consumedCapacity) {
if (from == null || to == null) {
throw new NullPointerException("Neither from nor to should be null.");
}

this.from = from;
this.to = to;
this.capacity = capacity;
this.consumedCapacity = consumedCapacity;
}

/**
* Returns the remaining capacity of that pipe/edge/channel.
* From
from and to` a determination is made if its a forward edge of backward edge.
* Depending on edge type the capacity is returned.
*
* @param from the from/source node
* @param to the to node.
* @return the remaining capacity on determing if its a forward or reverse edge.
*/
public double getCapacity(T from, T to) {
if (this.from.equals(from) && this.to.equals(to)) {
return capacity - cons

Solution

Some small remarks:

  • I would change the NullPointerException to IllegalArgumentException when you check for null in the arguments of the method.



  • I would add an addNodes(T ...) convenience method.



  • addNode(T node) javadoc: "returns false if the node is not added". It only does so when the node is already in your Graph. Better to add that to the docs too.



  • What is the use of the synchronized block in the getPath method?

Context

StackExchange Code Review Q#45729, answer score: 2

Revisions (0)

No revisions yet.