iterator fix
This commit is contained in:
parent
24859d45ab
commit
514e6de373
@ -16,8 +16,7 @@ public class Prim {
|
||||
|
||||
public void run(){
|
||||
if (graph.size() > 0){
|
||||
graph.get(0)
|
||||
.setVisited(true);
|
||||
graph.get(0).setVisited(true);
|
||||
}
|
||||
while (isDisconnected()){
|
||||
Edge nextMinimum = new Edge(Integer.MAX_VALUE);
|
||||
@ -25,8 +24,7 @@ public class Prim {
|
||||
for (Vertex vertex : graph){
|
||||
if (vertex.isVisited()){
|
||||
Pair<Vertex, Edge> candidate = vertex.nextMinimum();
|
||||
if (candidate.getValue()
|
||||
.getWeight() < nextMinimum.getWeight()) {
|
||||
if (candidate.getValue().getWeight() < nextMinimum.getWeight()){
|
||||
nextMinimum = candidate.getValue();
|
||||
nextVertex = candidate.getKey();
|
||||
}
|
||||
@ -56,12 +54,10 @@ public class Prim {
|
||||
|
||||
public void resetPrintHistory(){
|
||||
for (Vertex vertex : graph){
|
||||
Iterator it = vertex.getEdges()
|
||||
.entrySet()
|
||||
.iterator();
|
||||
Iterator<Map.Entry<Vertex,Edge>> it = vertex.getEdges().entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry pair = (Map.Entry) it.next();
|
||||
((Edge) pair.getValue()).setPrinted(false);
|
||||
Map.Entry<Vertex,Edge> pair = it.next();
|
||||
pair.getValue().setPrinted(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -29,8 +29,14 @@ public class Vertex {
|
||||
}
|
||||
|
||||
public void addEdge(Vertex vertex, Edge edge){
|
||||
if (this.edges.containsKey(vertex)){
|
||||
if (edge.getWeight() < this.edges.get(vertex).getWeight()){
|
||||
this.edges.replace(vertex, edge);
|
||||
}
|
||||
} else {
|
||||
this.edges.put(vertex, edge);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isVisited() {
|
||||
return isVisited;
|
||||
@ -43,15 +49,14 @@ public class Vertex {
|
||||
public Pair<Vertex, Edge> nextMinimum(){
|
||||
Edge nextMinimum = new Edge(Integer.MAX_VALUE);
|
||||
Vertex nextVertex = this;
|
||||
Iterator it = edges.entrySet()
|
||||
.iterator();
|
||||
Iterator<Map.Entry<Vertex,Edge>> 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 (((Edge) pair.getValue()).getWeight() < nextMinimum.getWeight()) {
|
||||
nextMinimum = (Edge) pair.getValue();
|
||||
nextVertex = (Vertex) pair.getKey();
|
||||
Map.Entry<Vertex,Edge> pair = it.next();
|
||||
if (!pair.getKey().isVisited()){
|
||||
if (!pair.getValue().isIncluded()) {
|
||||
if (pair.getValue().getWeight() < nextMinimum.getWeight()) {
|
||||
nextMinimum = pair.getValue();
|
||||
nextVertex = pair.getKey();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -61,18 +66,17 @@ public class Vertex {
|
||||
|
||||
public String originalToString(){
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Iterator it = edges.entrySet()
|
||||
.iterator();
|
||||
Iterator<Map.Entry<Vertex,Edge>> it = edges.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry pair = (Map.Entry) it.next();
|
||||
if (!((Edge) pair.getValue()).isPrinted()) {
|
||||
Map.Entry<Vertex,Edge> pair = it.next();
|
||||
if (!pair.getValue().isPrinted()) {
|
||||
sb.append(getLabel());
|
||||
sb.append(" --- ");
|
||||
sb.append(((Edge) pair.getValue()).getWeight());
|
||||
sb.append(pair.getValue().getWeight());
|
||||
sb.append(" --- ");
|
||||
sb.append(((Vertex) pair.getKey()).getLabel());
|
||||
sb.append(pair.getKey().getLabel());
|
||||
sb.append("\n");
|
||||
((Edge) pair.getValue()).setPrinted(true);
|
||||
pair.getValue().setPrinted(true);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
@ -81,19 +85,18 @@ public class Vertex {
|
||||
public String includedToString(){
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (isVisited()) {
|
||||
Iterator it = edges.entrySet()
|
||||
.iterator();
|
||||
Iterator<Map.Entry<Vertex,Edge>> it = edges.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry pair = (Map.Entry) it.next();
|
||||
if (((Edge) pair.getValue()).isIncluded()) {
|
||||
if (!((Edge) pair.getValue()).isPrinted()) {
|
||||
Map.Entry<Vertex,Edge> pair = it.next();
|
||||
if (pair.getValue().isIncluded()) {
|
||||
if (!pair.getValue().isPrinted()) {
|
||||
sb.append(getLabel());
|
||||
sb.append(" --- ");
|
||||
sb.append(((Edge) pair.getValue()).getWeight());
|
||||
sb.append(pair.getValue().getWeight());
|
||||
sb.append(" --- ");
|
||||
sb.append(((Vertex) pair.getKey()).getLabel());
|
||||
sb.append(pair.getKey().getLabel());
|
||||
sb.append("\n");
|
||||
((Edge) pair.getValue()).setPrinted(true);
|
||||
pair.getValue().setPrinted(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user