addEdge fix

This commit is contained in:
pazis 2019-11-24 18:40:13 +00:00
parent d1bd33f862
commit 24859d45ab
4 changed files with 40 additions and 37 deletions

View File

@ -34,4 +34,3 @@ public class Edge {
isPrinted = printed; isPrinted = printed;
} }
} }

View File

@ -16,7 +16,8 @@ public class Prim {
public void run() { public void run() {
if (graph.size() > 0) { if (graph.size() > 0) {
graph.get(0).setVisited(true); graph.get(0)
.setVisited(true);
} }
while (isDisconnected()) { while (isDisconnected()) {
Edge nextMinimum = new Edge(Integer.MAX_VALUE); Edge nextMinimum = new Edge(Integer.MAX_VALUE);
@ -24,7 +25,8 @@ public class Prim {
for (Vertex vertex : graph) { for (Vertex vertex : graph) {
if (vertex.isVisited()) { if (vertex.isVisited()) {
Pair<Vertex, Edge> candidate = vertex.nextMinimum(); Pair<Vertex, Edge> candidate = vertex.nextMinimum();
if (candidate.getValue().getWeight() < nextMinimum.getWeight()){ if (candidate.getValue()
.getWeight() < nextMinimum.getWeight()) {
nextMinimum = candidate.getValue(); nextMinimum = candidate.getValue();
nextVertex = candidate.getKey(); nextVertex = candidate.getKey();
} }
@ -54,7 +56,9 @@ public class Prim {
public void resetPrintHistory() { public void resetPrintHistory() {
for (Vertex vertex : graph) { for (Vertex vertex : graph) {
Iterator it = vertex.getEdges().entrySet().iterator(); Iterator it = vertex.getEdges()
.entrySet()
.iterator();
while (it.hasNext()) { while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next(); Map.Entry pair = (Map.Entry) it.next();
((Edge) pair.getValue()).setPrinted(false); ((Edge) pair.getValue()).setPrinted(false);
@ -71,4 +75,3 @@ public class Prim {
} }
} }

View File

@ -43,7 +43,8 @@ public class Vertex {
public Pair<Vertex, Edge> nextMinimum() { public Pair<Vertex, Edge> nextMinimum() {
Edge nextMinimum = new Edge(Integer.MAX_VALUE); Edge nextMinimum = new Edge(Integer.MAX_VALUE);
Vertex nextVertex = this; Vertex nextVertex = this;
Iterator it = edges.entrySet().iterator(); Iterator it = edges.entrySet()
.iterator();
while (it.hasNext()) { while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next(); Map.Entry pair = (Map.Entry) it.next();
if (!((Vertex) pair.getKey()).isVisited()) { if (!((Vertex) pair.getKey()).isVisited()) {
@ -60,7 +61,8 @@ public class Vertex {
public String originalToString() { public String originalToString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
Iterator it = edges.entrySet().iterator(); Iterator it = edges.entrySet()
.iterator();
while (it.hasNext()) { while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next(); Map.Entry pair = (Map.Entry) it.next();
if (!((Edge) pair.getValue()).isPrinted()) { if (!((Edge) pair.getValue()).isPrinted()) {
@ -79,7 +81,8 @@ public class Vertex {
public String includedToString() { public String includedToString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
if (isVisited()) { if (isVisited()) {
Iterator it = edges.entrySet().iterator(); Iterator it = edges.entrySet()
.iterator();
while (it.hasNext()) { while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next(); Map.Entry pair = (Map.Entry) it.next();
if (((Edge) pair.getValue()).isIncluded()) { if (((Edge) pair.getValue()).isIncluded()) {
@ -98,4 +101,3 @@ public class Vertex {
return sb.toString(); return sb.toString();
} }
} }

View File

@ -52,4 +52,3 @@ public class PrimUnitTest {
} }
} }