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