-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
283 lines (230 loc) · 11 KB
/
Main.java
File metadata and controls
283 lines (230 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import java.util.*;
class Main {
static class Edge {
int from, to, capacity;
public Edge(int from, int to, int capacity) {
this.from = from;
this.to = to;
this.capacity = capacity;
}
}
public static void main(String[] args) {
float start = System.nanoTime();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] scores = new int[n+2];
// Read scores for each node
for (int i = 0; i < n; i++) {
scores[i] = sc.nextInt();
}
// Add source and sink scores to the graph
scores[n]=0;
scores[n+1]=0;
// Build graph G = (V, E)
@SuppressWarnings("unchecked")
HashMap<Integer, List<Edge>> graph = new HashMap<>();
for (int i = 0; i < n; i++) {
graph.put(i, new ArrayList<Edge>());
}
// Define a source and sink to the graph
int source = 0;
int sink = n-1;
for (int i = 0; i < m; i++) {
int u = sc.nextInt() - 1;
int v = sc.nextInt() - 1;
int capacity = sc.nextInt();
addEdge(graph, u, v, capacity);
}
// Run Ford-Fulkerson algorithm to find max flow from source to sink. Calculate the residual graph.
HashMap<Integer, List<Edge>> rGraph = fordFulkerson(graph, source, sink);
// Find all nodes reachable from the source
List<Integer> reachableNodesFromSource = findReachableNodesFromSource(rGraph, source);
// Find all nodes reachable from the sink
List<Integer> reachableNodesToSink = findReachableNodesToSink(rGraph, sink);
// Define a source and sink to the project selection graph
int projectSource = n;
int projectSink = n+1;
// create the project selection graph
HashMap<Integer, List<Edge>> projectSelectionGraph = createProjectSelectionGraph(reachableNodesFromSource, reachableNodesToSink, projectSource, projectSink, scores, rGraph);
// Reduce to max flow problem. Calculate the maxscore from project selection. Run the FF algorithm on the project selection graph to find the max flow from the source to the sink. Calculate the residual graph.
HashMap<Integer, List<Edge>> rProjectSelectionGraph = fordFulkerson(projectSelectionGraph, projectSource, projectSink);
// Find all nodes reachable from the source
List<Integer> reachableNodesFromSourceInProjectSelection = findReachableNodesFromSource(rProjectSelectionGraph, projectSource);
// Sum up the scores of all nodes reachable from the source in the project selection graph
int maxScoreFromProjectSelection = 0;
for (int i = 0; i < reachableNodesFromSourceInProjectSelection.size(); i++) {
maxScoreFromProjectSelection += scores[reachableNodesFromSourceInProjectSelection.get(i)];
}
// calculate the max total score
int maxScore = 0;
for (int i = 0; i < n; i++) {
if (reachableNodesFromSource.contains(i) && !reachableNodesToSink.contains(i)) {
maxScore += scores[i];
}
}
//print the max score from project selection
System.out.println(maxScoreFromProjectSelection+maxScore);
float end = System.nanoTime();
float time = end - start;
float timeToSec = time/1000000000;
//print the time
// System.out.println(timeToSec);
sc.close();
}
private static void addEdge(HashMap<Integer, List<Edge>> graph, int from, int to, int capacity) {
Edge forwardEdge = new Edge(from, to, capacity);
graph.get(from).add(forwardEdge);
}
// Returns the maximum flow from s to t in the given
// graph using the Ford-Fulkerson algorithm. Created a residual graph rGraph.
private static HashMap<Integer, List<Edge>> fordFulkerson(HashMap<Integer, List<Edge>> graph, int source, int sink) {
// Create a residual graph and fill the residual
// graph with given capacities in the original graph
// as residual capacities in residual graph
// Construct residual graph as a HashMap
HashMap<Integer, List<Edge>> rGraph = new HashMap<>();
for (int u : graph.keySet()) {
rGraph.put(u, new ArrayList<>());
for (Edge e : graph.get(u)) {
Edge forwardEdge = new Edge(u, e.to, e.capacity);
rGraph.get(u).add(forwardEdge);
}
}
// This array is filled by BFS to store path
int[] parent = new int[graph.size()];
int max_flow = 0; // There is no flow initially. We can keep track of the flow.
// Augment the flow while there is an s-t path
while (bfs(rGraph, source, sink, parent)) {
// Find minimum residual capacity of the edges
// along the path filled by BFS. Or we can say
// find the maximum flow through the path found.
int pathFlow = Integer.MAX_VALUE;
for (int v = sink; v != source; v = parent[v]) {
int u = parent[v];
pathFlow = Math.min(pathFlow, findEdge(rGraph, u, v).capacity);
}
// Update residual capacities of the edges and
// reverse edges along the path
for (int v = sink; v != source; v = parent[v]) {
int u = parent[v];
findEdge(rGraph, u, v).capacity -= pathFlow;
if (findEdge(rGraph, u, v).capacity == 0) {
rGraph.get(u).remove(findEdge(rGraph, u, v));
}
Edge reverseEdge = findEdge(rGraph, v, u);
if (reverseEdge == null) {
// Add reverse edge if it doesn't exist
reverseEdge = new Edge(v, u, pathFlow);
rGraph.get(v).add(reverseEdge);
}
else {
// Update reverse edge if it exists
reverseEdge.capacity += pathFlow;
}
}
// Add path flow to overall flow
max_flow += pathFlow;
}
// Return the overall residual graph
return rGraph;
}
// Helper method to find an edge between nodes u and v in the graph
private static Edge findEdge(HashMap<Integer, List<Edge>> graph, int u, int v) {
for (Edge edge : graph.get(u)) {
if (edge.to == v) {
return edge;
}
}
return null; // This should not happen if the edge exists
}
// Returns true if there is a path from source 's' to
// sink 't' in residual graph. Also fills parent[] to
// store the path
private static boolean bfs(HashMap<Integer, List<Edge>> rGraph, int s, int t, int[] parent) {
int n = rGraph.size();
boolean[] visited = new boolean[n];
// Create a queue, enqueue source vertex
// and mark source vertex as visited
Queue<Integer> queue = new LinkedList<>();
queue.add(s);
visited[s] = true;
parent[s] = -1;
// BFS loop
while (!queue.isEmpty()) {
int u = queue.poll();
for (Edge edge : rGraph.get(u)) {
int v = edge.to;
if (!visited[v] && edge.capacity > 0) {
// Mark the neighbor node as visited
// and enqueue it
queue.add(v);
parent[v] = u;
visited[v] = true;
}
}
}
return (visited[t] == true);
}
// Returns all nodes reachable from the source
private static List<Integer> findReachableNodesFromSource(HashMap<Integer, List<Edge>> rGraph, int source) {
int n = rGraph.size();
int[] parent = new int[n];
List<Integer> reachableNodesFromSource = new ArrayList<>();
// Loop over all nodes in the graph and find the reachable nodes from the source
// using the bfs over the residual graph rGraph
for (int i = 0; i < n; i++) {
if (bfs(rGraph, source, i, parent)) {
reachableNodesFromSource.add(i);
}
}
return reachableNodesFromSource;
}
// Returns the nodes that can reach the sink
private static List<Integer> findReachableNodesToSink(HashMap<Integer, List<Edge>> rGraph, int sink) {
int n = rGraph.size();
int[] parent = new int[n];
List<Integer> reachableNodesFromSink = new ArrayList<>();
// Loop over all nodes in the graph and find the reachable nodes from the source
// using the bfs over the residual graph rGraph
for (int i = 0; i < n; i++) {
if (bfs(rGraph, i, sink, parent)) {
reachableNodesFromSink.add(i);
}
}
return reachableNodesFromSink;
}
//First, create the project selection graph
private static HashMap<Integer, List<Edge>> createProjectSelectionGraph(List<Integer> reachableNodesFromSource, List<Integer> reachableNodesToSink, int projectSource, int projectSink, int[] scores, HashMap<Integer, List<Edge>> rGraph) {
HashMap<Integer, List<Edge>> projectSelectionGraph = new HashMap<>();
for (Integer key : rGraph.keySet()) {
projectSelectionGraph.put(key, new ArrayList<Edge>());
}
projectSelectionGraph.put(projectSource, new ArrayList<Edge>());
projectSelectionGraph.put(projectSink, new ArrayList<Edge>()); // add new source and sink
// add prequisite edges: if there is an edge from u to v in the residual graph, add an edge from u to v in the project selection graph
for (Integer fromNode : rGraph.keySet()) {
for (Edge edge : rGraph.get(fromNode)) {
int toNode = edge.to;
if (!reachableNodesFromSource.contains(fromNode) && !reachableNodesToSink.contains(toNode) && !reachableNodesToSink.contains(fromNode) && !reachableNodesFromSource.contains(toNode)) {
if (edge.capacity > 0) {
projectSelectionGraph.get(fromNode).add(new Edge(fromNode, toNode, Integer.MAX_VALUE));
}
}
}
}
// add positive edges from source and negative edges to sink
for (Integer fromNode : rGraph.keySet()) {
if (!reachableNodesFromSource.contains(fromNode) && !reachableNodesToSink.contains(fromNode)) {
if (scores[fromNode] > 0) {
projectSelectionGraph.get(projectSource).add(new Edge(projectSource, fromNode, scores[fromNode]));
}
if (scores[fromNode] < 0) {
projectSelectionGraph.get(fromNode).add(new Edge(fromNode, projectSink, -scores[fromNode]));
}
}
}
// change to only passing in the size of the graph
return projectSelectionGraph;
}
}