diff options
author | Jonas Smedegaard <dr@jones.dk> | 2025-04-29 08:09:28 +0200 |
---|---|---|
committer | Jonas Smedegaard <dr@jones.dk> | 2025-04-29 08:09:28 +0200 |
commit | 3738c1fc8b2fa92b819b1ff948b3b39de60757b7 (patch) | |
tree | c5db7a878770da6c8180cc468184712d585b0d88 /src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java | |
parent | 794be4694f1a1fa36d87543aab840331c6d39745 (diff) |
always use brace in for- and if-construct
Diffstat (limited to 'src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java')
-rw-r--r-- | src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java | 74 |
1 files changed, 50 insertions, 24 deletions
diff --git a/src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java b/src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java index 6b47bd2..5c0dd80 100644 --- a/src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java +++ b/src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java @@ -40,8 +40,9 @@ public class GraphAlgorithms { /// @return boolean public static boolean isPath(final List<Edge> edges) { for (int i = 1; i < edges.size(); i++) { - if (edges.get(i - 1).to() != edges.get(i).from()) + if (edges.get(i - 1).to() != edges.get(i).from()) { return false; + } } return true; @@ -58,8 +59,9 @@ public class GraphAlgorithms { int length = 0; for (int i = 1; i < path.size(); i++) { Integer w = g.getWeight(path.get(i - 1),path.get(i)); - if (w == null) + if (w == null) { return null; + } length += w; } @@ -79,10 +81,12 @@ public class GraphAlgorithms { /// @return int static int cmpEdgeWeight(final Edge e1, final Edge e2) { int w1 = e1.weight(), w2 = e2.weight(); - if (w1 != w2) + if (w1 != w2) { return w1 - w2; - if (e1.from() != e2.from()) + } + if (e1.from() != e2.from()) { return e1.from().name().compareTo(e2.from().name()); + } return e1.to().name().compareTo(e2.to().name()); } @@ -95,11 +99,13 @@ public class GraphAlgorithms { /// @param e2 foo /// @return int static int cmpEdgeFrom(final Edge e1, final Edge e2) { - if (e1.from() != e2.from()) + if (e1.from() != e2.from()) { return e1.from().name().compareTo(e2.from().name()); + } int w1 = e1.weight(), w2 = e2.weight(); - if (w1 != w2) + if (w1 != w2) { return w1 - w2; + } return e1.to().name().compareTo(e2.to().name()); } @@ -112,10 +118,12 @@ public class GraphAlgorithms { /// @param e2 foo /// @return int static int cmpEdgeTo(final Edge e1, final Edge e2) { - if (e1.to() != e2.to()) + if (e1.to() != e2.to()) { return e1.to().name().compareTo(e2.to().name()); - if (e1.from() != e2.from()) + } + if (e1.from() != e2.from()) { return e1.from().name().compareTo(e2.from().name()); + } int w1 = e1.weight(), w2 = e2.weight(); return w1 - w2; @@ -186,13 +194,16 @@ public class GraphAlgorithms { //System.out.println("visited " + w); visited.add(w); Collection<Edge> outedge = g.outEdge(w); - if (outedge == null) + if (outedge == null) { continue; + } for (Edge e: outedge) { - if (visited.contains(e.to())) + if (visited.contains(e.to())) { continue; - if (thisLevel.contains(e.to())) + } + if (thisLevel.contains(e.to())) { continue; + } nextLevel.add(e.to()); } } @@ -222,12 +233,14 @@ public class GraphAlgorithms { /// @param v foo /// @param visited foo private static void visitDepthFirst(final Graph g, final Vertex v, final Set<Vertex> visited) { - if (visited.contains(v)) + if (visited.contains(v)) { return; + } //System.out.println("visited "+v); visited.add(v); - for (Edge e: g.outEdge(v)) + for (Edge e: g.outEdge(v)) { visitDepthFirst(g, e.to(), visited); + } } /// an implementation of Prim's algorithm @@ -247,15 +260,19 @@ public class GraphAlgorithms { while (true) { Edge nearest = null; for (Edge e: edges) { - if (!frontier.contains(e.from())) + if (!frontier.contains(e.from())) { continue; - if (frontier.contains(e.to())) + } + if (frontier.contains(e.to())) { continue; - if (nearest == null || nearest.weight() > e.weight()) + } + if (nearest == null || nearest.weight() > e.weight()) { nearest = e; + } } - if (nearest == null) + if (nearest == null) { break; + } mst.add(nearest); frontier.add(nearest.to()); } @@ -278,8 +295,9 @@ public class GraphAlgorithms { HashSet<Vertex> done = new HashSet<>(); HashMap<Vertex,Edge> prev = new HashMap<>(); HashMap<Vertex,Integer> weight = new HashMap<>(); - for (Vertex w: g.vertices()) + for (Vertex w: g.vertices()) { weight.put(w, maxint); + } // start node is done, distance 0 from start weight.put(start, 0); @@ -294,7 +312,9 @@ public class GraphAlgorithms { for (Vertex w1:done) { for (Edge e :g.outEdge(w1)) { Vertex w2 = e.to(); - if (done.contains(w2)) continue; + if (done.contains(w2)) { + continue; + } if ((weight.get(w1) + e.weight()) < neardist) { nearest = e.to(); neardist = weight.get(w1) + e.weight(); @@ -305,7 +325,9 @@ public class GraphAlgorithms { // System.out.println("find nearest "+done2near); // if no more, then we are done - if (nearest == null) break; + if (nearest == null) { + break; + } // update distance from this node to other nodes for (Edge e1 : g.outEdge(nearest)) { @@ -338,11 +360,13 @@ public class GraphAlgorithms { try { BufferedReader in = new BufferedReader(new FileReader(file)); for (String line = in.readLine(); line != null; line = in.readLine()) { - if (line.length() == 0) + if (line.length() == 0) { continue; + } String[] arr = line.split(","); - if (arr.length != 3) + if (arr.length != 3) { throw new RuntimeException("CSV file format error: " + line); + } g.insertEdge(arr[0].trim(), arr[1].trim(), Integer.parseInt(arr[2].trim())); g.insertEdge(arr[1].trim(), arr[0].trim(), Integer.parseInt(arr[2].trim())); } @@ -358,8 +382,9 @@ public class GraphAlgorithms { public static void printGraph(final Graph g) { for (Vertex v: sortVertex(g.vertices())) { System.out.println(v.toString()); - for (Edge e: sortEdgesTo(g.outEdge(v))) + for (Edge e: sortEdgesTo(g.outEdge(v))) { System.out.println(" " + e.toString()); + } } } @@ -389,8 +414,9 @@ public class GraphAlgorithms { BufferedReader in = new BufferedReader(new FileReader(f)); while (true) { String s = in.readLine(); - if (s == null) + if (s == null) { break; + } list.add(s); } in.close(); |