diff --git a/Gvisual/src/gvisual/NetworkFlowAnalyzer.java b/Gvisual/src/gvisual/NetworkFlowAnalyzer.java index f692d2e..031c3b6 100644 --- a/Gvisual/src/gvisual/NetworkFlowAnalyzer.java +++ b/Gvisual/src/gvisual/NetworkFlowAnalyzer.java @@ -41,15 +41,15 @@ public class NetworkFlowAnalyzer { private final Graph graph; // Residual capacities: directedKey -> remaining capacity - private Map, Double> residualCapacity; + private Map residualCapacity; // Flow values: directedKey -> flow - private Map, Double> flow; + private Map flow; // Adjacency list for residual graph private Map> residualAdj; // Original capacities - private Map, Double> capacity; + private Map capacity; // Edge lookup: directedKey -> original Edge (null for reverse arcs) - private Map, Edge> edgeLookup; + private Map edgeLookup; private String source; private String sink; @@ -100,7 +100,7 @@ public double compute(String source, String sink) { // Edmonds–Karp: BFS for shortest augmenting path while (true) { Map parent = new LinkedHashMap(); - Map> parentArcKey = new LinkedHashMap>(); + Map parentArcKey = new LinkedHashMap(); double pathFlow = bfsAugmentingPath(parent, parentArcKey); if (pathFlow <= 0) break; @@ -109,8 +109,8 @@ public double compute(String source, String sink) { String v = sink; while (!v.equals(source)) { String u = parent.get(v); - List fwd = directedKey(u, v); - List rev = directedKey(v, u); + String fwd = directedKey(u, v); + String rev = directedKey(v, u); residualCapacity.put(fwd, residualCapacity.get(fwd) - pathFlow); @@ -173,8 +173,8 @@ public Map getEdgeFlows() { ensureComputed(); Map result = new LinkedHashMap(); for (Edge e : graph.getEdges()) { - List fwd = directedKey(e.getVertex1(), e.getVertex2()); - List rev = directedKey(e.getVertex2(), e.getVertex1()); + String fwd = directedKey(e.getVertex1(), e.getVertex2()); + String rev = directedKey(e.getVertex2(), e.getVertex1()); double fwdFlow = flow.getOrDefault(fwd, 0.0); double revFlow = flow.getOrDefault(rev, 0.0); @@ -346,7 +346,7 @@ public List decomposeFlowPaths() { ensureComputed(); // Work on a copy of flows - Map, Double> flowCopy = new HashMap, Double>(flow); + Map flowCopy = new HashMap(flow); List paths = new ArrayList(); while (true) { @@ -362,7 +362,7 @@ public List decomposeFlowPaths() { Set neighbors = residualAdj.get(u); if (neighbors == null) continue; for (String v : neighbors) { - List key = directedKey(u, v); + String key = directedKey(u, v); if (!parent.containsKey(v) && flowCopy.getOrDefault(key, 0.0) > 1e-9) { parent.put(v, u); @@ -394,7 +394,7 @@ public List decomposeFlowPaths() { // Subtract flow for (int i = 0; i < pathVertices.size() - 1; i++) { - List key = directedKey(pathVertices.get(i), pathVertices.get(i + 1)); + String key = directedKey(pathVertices.get(i), pathVertices.get(i + 1)); flowCopy.put(key, flowCopy.getOrDefault(key, 0.0) - pathFlow); } @@ -536,8 +536,8 @@ public String getSummary() { for (Edge e : graph.getEdges()) { String v1 = e.getVertex1(); String v2 = e.getVertex2(); - List fwdKey = directedKey(v1, v2); - List revKey = directedKey(v2, v1); + String fwdKey = directedKey(v1, v2); + String revKey = directedKey(v2, v1); double fwdFlow = flow.getOrDefault(fwdKey, 0.0); double revFlow = flow.getOrDefault(revKey, 0.0); @@ -561,11 +561,11 @@ public String getSummary() { // ── Internal helpers ─────────────────────────────────────────── private void buildResidualGraph() { - residualCapacity = new HashMap, Double>(); - flow = new HashMap, Double>(); + residualCapacity = new HashMap(); + flow = new HashMap(); residualAdj = new HashMap>(); - capacity = new HashMap, Double>(); - edgeLookup = new HashMap, Edge>(); + capacity = new HashMap(); + edgeLookup = new HashMap(); // Initialise adjacency sets for all vertices for (String v : graph.getVertices()) { @@ -577,8 +577,8 @@ private void buildResidualGraph() { String v2 = e.getVertex2(); double cap = getEdgeCapacity(e); - List fwd = directedKey(v1, v2); - List rev = directedKey(v2, v1); + String fwd = directedKey(v1, v2); + String rev = directedKey(v2, v1); // Each undirected Edge → two directed arcs residualCapacity.put(fwd, @@ -598,7 +598,7 @@ private void buildResidualGraph() { } private double bfsAugmentingPath(Map parent, - Map> parentArcKey) { + Map parentArcKey) { Queue queue = new LinkedList(); queue.add(source); parent.put(source, null); @@ -609,7 +609,7 @@ private double bfsAugmentingPath(Map parent, if (neighbors == null) continue; for (String v : neighbors) { - List key = directedKey(u, v); + String key = directedKey(u, v); if (!parent.containsKey(v) && residualCapacity.getOrDefault(key, 0.0) > 1e-9) { parent.put(v, u); @@ -620,7 +620,7 @@ private double bfsAugmentingPath(Map parent, String t = sink; while (!t.equals(source)) { String p = parent.get(t); - List k = directedKey(p, t); + String k = directedKey(p, t); pathFlow = Math.min(pathFlow, residualCapacity.getOrDefault(k, 0.0)); t = p; @@ -639,18 +639,18 @@ private double getEdgeCapacity(Edge e) { return w > 0 ? w : 1.0; } - private List directedKey(String from, String to) { - return Arrays.asList(from, to); + private String directedKey(String from, String to) { + return from + "->" + to; } /** * Formats a directed key as a human-readable string for display purposes. * - * @param key the directed key (two-element list) - * @return formatted string "from->to" + * @param key the directed key string + * @return the key as-is (already in "from->to" format) */ - private String formatKey(List key) { - return key.get(0) + "->" + key.get(1); + private String formatKey(String key) { + return key; } private void validateVertex(String vertex, String name) {