/* @author Dr. Manal Helal for @Module COM2031 Advanced Computer Algorithms - Computer Science Department * Led by Prof. Steve Schneider * @date created: November 2019 * @University of Surrey * Week 8 Lab: Reduce Circulation with Demands Algorithm using Ford Fulkerson algorithm of last week using the adjacency matrices approach * * It is educationally easier to follow the adjacency matrix graph representation * However, and adjacency list approach is adopted in this link: * https://github.com/SleekPanther/circulation-with-demands-network-flow * * Using similar data structures as used in Princeton university for faster algorithms for bigger graphs: * https://algs4.cs.princeton.edu/64maxflow/ * */ import static org.junit.Assert.assertEquals; public class CirculationWithDemands { static class SupplyDemandNode { public int pv; SupplyDemandNode (int pv) { this.pv = pv; } } // input is demand/supply graph, and return true if there is circulation or false if there is not. // the max flow is calculated after adding source and sink and connect // source to all supply vertices with -pv as capacity, and connect all demand vertices to sink with pv as capacity public boolean circulationToMaxFlow (int[][] g, SupplyDemandNode nodes[]) { // if adjacency list is empty return no circulation if ((g == null) || (g.length == 0)) return false; int maxSupplies = 0; int s = 0; int t = g.length + 1; // add source as vertex zero, and add sink as last vertex int[][] adjMatrix = new int[g.length + 2][g.length + 2]; // this way we created one adjacency matrix containing all vertices adding source and sink // we copy the values from the Graph adjacency matrix to the new one copying capacities // taking care of indices to account for the newly added vertices for (int i = 0; i 0) adjMatrix[i][t] = nodes[i-1].pv; } // find max flow fordFulkerson maxFlowFordFulkerson = new fordFulkerson(); // the min cut is printed inside the ford Fulkerson method called in the following line int[][] maxFlow = maxFlowFordFulkerson.fordFulkerson(adjMatrix, 0, t); int flowValue = 0; for (int i=0;i