Skip to content

Commit fb0cbcb

Browse files
addEdge fix
1 parent 3a573ab commit fb0cbcb

4 files changed

Lines changed: 40 additions & 37 deletions

File tree

‎algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/prim/Edge.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class Edge {
66
private boolean isIncluded = false;
77
private boolean isPrinted = false;
88

9-
public Edge (int weight){
9+
public Edge(int weight) {
1010
this.weight = weight;
1111
}
1212

@@ -34,4 +34,3 @@ public void setPrinted(boolean printed) {
3434
isPrinted = printed;
3535
}
3636
}
37-

‎algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/prim/Prim.java‎

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,23 @@ public class Prim {
1010

1111
private List<Vertex> graph;
1212

13-
public Prim(List<Vertex> graph){
13+
public Prim(List<Vertex> graph) {
1414
this.graph = graph;
1515
}
1616

17-
public void run(){
18-
if (graph.size() > 0){
19-
graph.get(0).setVisited(true);
17+
public void run() {
18+
if (graph.size() > 0) {
19+
graph.get(0)
20+
.setVisited(true);
2021
}
21-
while (isDisconnected()){
22+
while (isDisconnected()) {
2223
Edge nextMinimum = new Edge(Integer.MAX_VALUE);
2324
Vertex nextVertex = graph.get(0);
24-
for (Vertex vertex : graph){
25-
if (vertex.isVisited()){
25+
for (Vertex vertex : graph) {
26+
if (vertex.isVisited()) {
2627
Pair<Vertex, Edge> candidate = vertex.nextMinimum();
27-
if (candidate.getValue().getWeight() < nextMinimum.getWeight()){
28+
if (candidate.getValue()
29+
.getWeight() < nextMinimum.getWeight()) {
2830
nextMinimum = candidate.getValue();
2931
nextVertex = candidate.getKey();
3032
}
@@ -35,40 +37,41 @@ public void run(){
3537
}
3638
}
3739

38-
private boolean isDisconnected(){
39-
for (Vertex vertex : graph){
40-
if (!vertex.isVisited()){
40+
private boolean isDisconnected() {
41+
for (Vertex vertex : graph) {
42+
if (!vertex.isVisited()) {
4143
return true;
4244
}
4345
}
4446
return false;
4547
}
4648

47-
public String originalGraphToString(){
49+
public String originalGraphToString() {
4850
StringBuilder sb = new StringBuilder();
49-
for (Vertex vertex : graph){
51+
for (Vertex vertex : graph) {
5052
sb.append(vertex.originalToString());
5153
}
5254
return sb.toString();
5355
}
5456

55-
public void resetPrintHistory(){
56-
for (Vertex vertex : graph){
57-
Iterator it = vertex.getEdges().entrySet().iterator();
57+
public void resetPrintHistory() {
58+
for (Vertex vertex : graph) {
59+
Iterator it = vertex.getEdges()
60+
.entrySet()
61+
.iterator();
5862
while (it.hasNext()) {
5963
Map.Entry pair = (Map.Entry) it.next();
60-
((Edge)pair.getValue()).setPrinted(false);
64+
((Edge) pair.getValue()).setPrinted(false);
6165
}
6266
}
6367
}
6468

65-
public String minimumSpanningTreeToString(){
69+
public String minimumSpanningTreeToString() {
6670
StringBuilder sb = new StringBuilder();
67-
for (Vertex vertex : graph){
71+
for (Vertex vertex : graph) {
6872
sb.append(vertex.includedToString());
6973
}
7074
return sb.toString();
7175
}
7276

7377
}
74-

‎algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/prim/Vertex.java‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class Vertex {
1212
private Map<Vertex, Edge> edges = new HashMap<>();
1313
private boolean isVisited = false;
1414

15-
public Vertex(String label){
15+
public Vertex(String label) {
1616
this.label = label;
1717
}
1818

@@ -28,7 +28,7 @@ public Map<Vertex, Edge> getEdges() {
2828
return edges;
2929
}
3030

31-
public void addEdge(Vertex vertex, Edge edge){
31+
public void addEdge(Vertex vertex, Edge edge) {
3232
this.edges.put(vertex, edge);
3333
}
3434

@@ -40,14 +40,15 @@ public void setVisited(boolean visited) {
4040
isVisited = visited;
4141
}
4242

43-
public Pair<Vertex, Edge> nextMinimum(){
43+
public Pair<Vertex, Edge> nextMinimum() {
4444
Edge nextMinimum = new Edge(Integer.MAX_VALUE);
4545
Vertex nextVertex = this;
46-
Iterator it = edges.entrySet().iterator();
46+
Iterator it = edges.entrySet()
47+
.iterator();
4748
while (it.hasNext()) {
4849
Map.Entry pair = (Map.Entry) it.next();
49-
if (!((Vertex)pair.getKey()).isVisited()){
50-
if (!((Edge)pair.getValue()).isIncluded()) {
50+
if (!((Vertex) pair.getKey()).isVisited()) {
51+
if (!((Edge) pair.getValue()).isIncluded()) {
5152
if (((Edge) pair.getValue()).getWeight() < nextMinimum.getWeight()) {
5253
nextMinimum = (Edge) pair.getValue();
5354
nextVertex = (Vertex) pair.getKey();
@@ -58,12 +59,13 @@ public Pair<Vertex, Edge> nextMinimum(){
5859
return new Pair<>(nextVertex, nextMinimum);
5960
}
6061

61-
public String originalToString(){
62+
public String originalToString() {
6263
StringBuilder sb = new StringBuilder();
63-
Iterator it = edges.entrySet().iterator();
64+
Iterator it = edges.entrySet()
65+
.iterator();
6466
while (it.hasNext()) {
6567
Map.Entry pair = (Map.Entry) it.next();
66-
if (!((Edge)pair.getValue()).isPrinted()) {
68+
if (!((Edge) pair.getValue()).isPrinted()) {
6769
sb.append(getLabel());
6870
sb.append(" --- ");
6971
sb.append(((Edge) pair.getValue()).getWeight());
@@ -76,14 +78,15 @@ public String originalToString(){
7678
return sb.toString();
7779
}
7880

79-
public String includedToString(){
81+
public String includedToString() {
8082
StringBuilder sb = new StringBuilder();
8183
if (isVisited()) {
82-
Iterator it = edges.entrySet().iterator();
84+
Iterator it = edges.entrySet()
85+
.iterator();
8386
while (it.hasNext()) {
8487
Map.Entry pair = (Map.Entry) it.next();
8588
if (((Edge) pair.getValue()).isIncluded()) {
86-
if (!((Edge)pair.getValue()).isPrinted()) {
89+
if (!((Edge) pair.getValue()).isPrinted()) {
8790
sb.append(getLabel());
8891
sb.append(" --- ");
8992
sb.append(((Edge) pair.getValue()).getWeight());
@@ -98,4 +101,3 @@ public String includedToString(){
98101
return sb.toString();
99102
}
100103
}
101-

‎algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/prim/PrimUnitTest.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void givenAGraph_whenPrimRuns_thenPrintMST() {
1818
System.out.println(prim.minimumSpanningTreeToString());
1919
}
2020

21-
public static List<Vertex> createGraph(){
21+
public static List<Vertex> createGraph() {
2222
List<Vertex> graph = new ArrayList<>();
2323
Vertex a = new Vertex("A");
2424
Vertex b = new Vertex("B");
@@ -52,4 +52,3 @@ public static List<Vertex> createGraph(){
5252
}
5353

5454
}
55-

0 commit comments

Comments
 (0)