/* @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 9 Lab: Project Selection Problem reduced to Ford Fulkerson algorithm of last week */ import static org.junit.Assert.assertEquals; import java.util.ArrayList; import java.util.List; public class ProjectSelection { static class Project { public int pv; Project (int pv) { this.pv = pv; } } // This one traces the rGraph for all reachable vertices remaining in the s cut int Selection(int[][] rGraph, int[][] graph, int s, int t, int cut_cap, Project[] nodes) { List selProjects = new ArrayList(); // Using the residual graph passed from the Ford Fulkerson algorithm, find vertices reachable from s using DFS boolean[] isVisited = new boolean[graph.length]; fordFulkerson.dfs(rGraph, s, isVisited); // start from s, trace the residual graph to all remaining reachable vertecies , // skipping the source, and decrementing the Project index to cancel the addition of the source //System.out.println (" The previous Min-Cut printing is showing the disconnected edges using indices after addition of source and sink"); System.out.println (" The following are edges from reachable verteces from source after removing the added vertices (selected projects/blocks):"); for (int i = 1; i < graph.length-1; i++) { for (int j = 1; j < graph.length-1; j++) { if (rGraph[i][j] > 0 && isVisited[i] && isVisited[j]) { System.out.println((i-1) + " - " + (j-1)); if(!selProjects.contains(i-1)) selProjects.add(i-1); if(!selProjects.contains(j-1)) selProjects.add(j-1); } } } // Set selProjects contains int netProfit = 0; if (selProjects.size() > 0) { System.out.print("Selected Projects/Blocks are: ( " + String.valueOf(selProjects.get(0))); netProfit += nodes[selProjects.get(0)].pv; for (int i = 1; i 0) adjMatrix[i+1][j+1] = Integer.MAX_VALUE; } } // define new edges between source '0' and all net profit project vertices for (int i = 1; i<=g.length;i++) { if (nodes[i-1].pv > 0) adjMatrix[s][i] = nodes[i-1].pv; } // define new edges between all net cost vertices in second set and the sink and inverting the sign of the cost to make it positive for (int i = 1; i<=g.length;i++) { if (nodes[i-1].pv < 0) adjMatrix[i][t] = -1 * 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 netCost = 0; for (int i=0;i