diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/prim/Edge.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/prim/Edge.java index 3720536df7..52ec4ef534 100644 --- a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/prim/Edge.java +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/prim/Edge.java @@ -6,7 +6,7 @@ public class Edge { private boolean isIncluded = false; private boolean isPrinted = false; - public Edge (int weight){ + public Edge(int weight) { this.weight = weight; } @@ -34,4 +34,3 @@ public class Edge { isPrinted = printed; } } - diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/prim/Prim.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/prim/Prim.java index 944ad4e66c..ec97e34341 100644 --- a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/prim/Prim.java +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/prim/Prim.java @@ -10,21 +10,23 @@ public class Prim { private List graph; - public Prim(List graph){ + public Prim(List graph) { this.graph = graph; } - public void run(){ - if (graph.size() > 0){ - graph.get(0).setVisited(true); + public void run() { + if (graph.size() > 0) { + graph.get(0) + .setVisited(true); } - while (isDisconnected()){ + while (isDisconnected()) { Edge nextMinimum = new Edge(Integer.MAX_VALUE); Vertex nextVertex = graph.get(0); - for (Vertex vertex : graph){ - if (vertex.isVisited()){ + for (Vertex vertex : graph) { + if (vertex.isVisited()) { Pair candidate = vertex.nextMinimum(); - if (candidate.getValue().getWeight() < nextMinimum.getWeight()){ + if (candidate.getValue() + .getWeight() < nextMinimum.getWeight()) { nextMinimum = candidate.getValue(); nextVertex = candidate.getKey(); } @@ -35,40 +37,41 @@ public class Prim { } } - private boolean isDisconnected(){ - for (Vertex vertex : graph){ - if (!vertex.isVisited()){ + private boolean isDisconnected() { + for (Vertex vertex : graph) { + if (!vertex.isVisited()) { return true; } } return false; } - public String originalGraphToString(){ + public String originalGraphToString() { StringBuilder sb = new StringBuilder(); - for (Vertex vertex : graph){ + for (Vertex vertex : graph) { sb.append(vertex.originalToString()); } return sb.toString(); } - public void resetPrintHistory(){ - for (Vertex vertex : graph){ - Iterator it = vertex.getEdges().entrySet().iterator(); + public void resetPrintHistory() { + for (Vertex vertex : graph) { + Iterator it = vertex.getEdges() + .entrySet() + .iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); - ((Edge)pair.getValue()).setPrinted(false); + ((Edge) pair.getValue()).setPrinted(false); } } } - public String minimumSpanningTreeToString(){ + public String minimumSpanningTreeToString() { StringBuilder sb = new StringBuilder(); - for (Vertex vertex : graph){ + for (Vertex vertex : graph) { sb.append(vertex.includedToString()); } return sb.toString(); } } - diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/prim/Vertex.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/prim/Vertex.java index 0bb2454a1f..e2b7d90638 100644 --- a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/prim/Vertex.java +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/prim/Vertex.java @@ -12,7 +12,7 @@ public class Vertex { private Map edges = new HashMap<>(); private boolean isVisited = false; - public Vertex(String label){ + public Vertex(String label) { this.label = label; } @@ -28,7 +28,7 @@ public class Vertex { return edges; } - public void addEdge(Vertex vertex, Edge edge){ + public void addEdge(Vertex vertex, Edge edge) { this.edges.put(vertex, edge); } @@ -40,14 +40,15 @@ public class Vertex { isVisited = visited; } - public Pair nextMinimum(){ + public Pair nextMinimum() { Edge nextMinimum = new Edge(Integer.MAX_VALUE); Vertex nextVertex = this; - Iterator it = edges.entrySet().iterator(); + Iterator it = edges.entrySet() + .iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); - if (!((Vertex)pair.getKey()).isVisited()){ - if (!((Edge)pair.getValue()).isIncluded()) { + if (!((Vertex) pair.getKey()).isVisited()) { + if (!((Edge) pair.getValue()).isIncluded()) { if (((Edge) pair.getValue()).getWeight() < nextMinimum.getWeight()) { nextMinimum = (Edge) pair.getValue(); nextVertex = (Vertex) pair.getKey(); @@ -58,12 +59,13 @@ public class Vertex { return new Pair<>(nextVertex, nextMinimum); } - public String originalToString(){ + public String originalToString() { StringBuilder sb = new StringBuilder(); - Iterator it = edges.entrySet().iterator(); + Iterator it = edges.entrySet() + .iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); - if (!((Edge)pair.getValue()).isPrinted()) { + if (!((Edge) pair.getValue()).isPrinted()) { sb.append(getLabel()); sb.append(" --- "); sb.append(((Edge) pair.getValue()).getWeight()); @@ -76,14 +78,15 @@ public class Vertex { return sb.toString(); } - public String includedToString(){ + public String includedToString() { StringBuilder sb = new StringBuilder(); if (isVisited()) { - Iterator it = edges.entrySet().iterator(); + Iterator it = edges.entrySet() + .iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); if (((Edge) pair.getValue()).isIncluded()) { - if (!((Edge)pair.getValue()).isPrinted()) { + if (!((Edge) pair.getValue()).isPrinted()) { sb.append(getLabel()); sb.append(" --- "); sb.append(((Edge) pair.getValue()).getWeight()); @@ -98,4 +101,3 @@ public class Vertex { return sb.toString(); } } - diff --git a/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/prim/PrimUnitTest.java b/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/prim/PrimUnitTest.java index 548fe2c877..41e53fc9f2 100644 --- a/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/prim/PrimUnitTest.java +++ b/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/prim/PrimUnitTest.java @@ -18,7 +18,7 @@ public class PrimUnitTest { System.out.println(prim.minimumSpanningTreeToString()); } - public static List createGraph(){ + public static List createGraph() { List graph = new ArrayList<>(); Vertex a = new Vertex("A"); Vertex b = new Vertex("B"); @@ -52,4 +52,3 @@ public class PrimUnitTest { } } -