iterator fix
This commit is contained in:
parent
24859d45ab
commit
514e6de373
@ -1,77 +1,73 @@
|
||||
package com.baeldung.algorithms.prim;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.math3.util.Pair;
|
||||
|
||||
public class Prim {
|
||||
|
||||
private List<Vertex> graph;
|
||||
|
||||
public Prim(List<Vertex> graph) {
|
||||
this.graph = graph;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
if (graph.size() > 0) {
|
||||
graph.get(0)
|
||||
.setVisited(true);
|
||||
}
|
||||
while (isDisconnected()) {
|
||||
Edge nextMinimum = new Edge(Integer.MAX_VALUE);
|
||||
Vertex nextVertex = graph.get(0);
|
||||
for (Vertex vertex : graph) {
|
||||
if (vertex.isVisited()) {
|
||||
Pair<Vertex, Edge> candidate = vertex.nextMinimum();
|
||||
if (candidate.getValue()
|
||||
.getWeight() < nextMinimum.getWeight()) {
|
||||
nextMinimum = candidate.getValue();
|
||||
nextVertex = candidate.getKey();
|
||||
}
|
||||
}
|
||||
}
|
||||
nextMinimum.setIncluded(true);
|
||||
nextVertex.setVisited(true);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isDisconnected() {
|
||||
for (Vertex vertex : graph) {
|
||||
if (!vertex.isVisited()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public String originalGraphToString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Vertex vertex : graph) {
|
||||
sb.append(vertex.originalToString());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String minimumSpanningTreeToString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Vertex vertex : graph) {
|
||||
sb.append(vertex.includedToString());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.algorithms.prim;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.math3.util.Pair;
|
||||
|
||||
public class Prim {
|
||||
|
||||
private List<Vertex> graph;
|
||||
|
||||
public Prim(List<Vertex> graph){
|
||||
this.graph = graph;
|
||||
}
|
||||
|
||||
public void run(){
|
||||
if (graph.size() > 0){
|
||||
graph.get(0).setVisited(true);
|
||||
}
|
||||
while (isDisconnected()){
|
||||
Edge nextMinimum = new Edge(Integer.MAX_VALUE);
|
||||
Vertex nextVertex = graph.get(0);
|
||||
for (Vertex vertex : graph){
|
||||
if (vertex.isVisited()){
|
||||
Pair<Vertex, Edge> candidate = vertex.nextMinimum();
|
||||
if (candidate.getValue().getWeight() < nextMinimum.getWeight()){
|
||||
nextMinimum = candidate.getValue();
|
||||
nextVertex = candidate.getKey();
|
||||
}
|
||||
}
|
||||
}
|
||||
nextMinimum.setIncluded(true);
|
||||
nextVertex.setVisited(true);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isDisconnected(){
|
||||
for (Vertex vertex : graph){
|
||||
if (!vertex.isVisited()){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public String originalGraphToString(){
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Vertex vertex : graph){
|
||||
sb.append(vertex.originalToString());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void resetPrintHistory(){
|
||||
for (Vertex vertex : graph){
|
||||
Iterator<Map.Entry<Vertex,Edge>> it = vertex.getEdges().entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry<Vertex,Edge> pair = it.next();
|
||||
pair.getValue().setPrinted(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String minimumSpanningTreeToString(){
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Vertex vertex : graph){
|
||||
sb.append(vertex.includedToString());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,103 +1,106 @@
|
||||
package com.baeldung.algorithms.prim;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.math3.util.Pair;
|
||||
|
||||
public class Vertex {
|
||||
|
||||
private String label = null;
|
||||
private Map<Vertex, Edge> edges = new HashMap<>();
|
||||
private boolean isVisited = false;
|
||||
|
||||
public Vertex(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
|
||||
public void setLabel(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
public Map<Vertex, Edge> getEdges() {
|
||||
return edges;
|
||||
}
|
||||
|
||||
public void addEdge(Vertex vertex, Edge edge) {
|
||||
this.edges.put(vertex, edge);
|
||||
}
|
||||
|
||||
public boolean isVisited() {
|
||||
return isVisited;
|
||||
}
|
||||
|
||||
public void setVisited(boolean visited) {
|
||||
isVisited = visited;
|
||||
}
|
||||
|
||||
public Pair<Vertex, Edge> nextMinimum() {
|
||||
Edge nextMinimum = new Edge(Integer.MAX_VALUE);
|
||||
Vertex nextVertex = this;
|
||||
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 (((Edge) pair.getValue()).getWeight() < nextMinimum.getWeight()) {
|
||||
nextMinimum = (Edge) pair.getValue();
|
||||
nextVertex = (Vertex) pair.getKey();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return new Pair<>(nextVertex, nextMinimum);
|
||||
}
|
||||
|
||||
public String originalToString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Iterator it = edges.entrySet()
|
||||
.iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry pair = (Map.Entry) it.next();
|
||||
if (!((Edge) pair.getValue()).isPrinted()) {
|
||||
sb.append(getLabel());
|
||||
sb.append(" --- ");
|
||||
sb.append(((Edge) pair.getValue()).getWeight());
|
||||
sb.append(" --- ");
|
||||
sb.append(((Vertex) pair.getKey()).getLabel());
|
||||
sb.append("\n");
|
||||
((Edge) pair.getValue()).setPrinted(true);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public String includedToString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (isVisited()) {
|
||||
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()) {
|
||||
sb.append(getLabel());
|
||||
sb.append(" --- ");
|
||||
sb.append(((Edge) pair.getValue()).getWeight());
|
||||
sb.append(" --- ");
|
||||
sb.append(((Vertex) pair.getKey()).getLabel());
|
||||
sb.append("\n");
|
||||
((Edge) pair.getValue()).setPrinted(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
package com.baeldung.algorithms.prim;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.math3.util.Pair;
|
||||
|
||||
public class Vertex {
|
||||
|
||||
private String label = null;
|
||||
private Map<Vertex, Edge> edges = new HashMap<>();
|
||||
private boolean isVisited = false;
|
||||
|
||||
public Vertex(String label){
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
|
||||
public void setLabel(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
public Map<Vertex, Edge> getEdges() {
|
||||
return edges;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public void setVisited(boolean visited) {
|
||||
isVisited = visited;
|
||||
}
|
||||
|
||||
public Pair<Vertex, Edge> nextMinimum(){
|
||||
Edge nextMinimum = new Edge(Integer.MAX_VALUE);
|
||||
Vertex nextVertex = this;
|
||||
Iterator<Map.Entry<Vertex,Edge>> it = edges.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return new Pair<>(nextVertex, nextMinimum);
|
||||
}
|
||||
|
||||
public String originalToString(){
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Iterator<Map.Entry<Vertex,Edge>> it = edges.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry<Vertex,Edge> pair = it.next();
|
||||
if (!pair.getValue().isPrinted()) {
|
||||
sb.append(getLabel());
|
||||
sb.append(" --- ");
|
||||
sb.append(pair.getValue().getWeight());
|
||||
sb.append(" --- ");
|
||||
sb.append(pair.getKey().getLabel());
|
||||
sb.append("\n");
|
||||
pair.getValue().setPrinted(true);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public String includedToString(){
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (isVisited()) {
|
||||
Iterator<Map.Entry<Vertex,Edge>> it = edges.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry<Vertex,Edge> pair = it.next();
|
||||
if (pair.getValue().isIncluded()) {
|
||||
if (!pair.getValue().isPrinted()) {
|
||||
sb.append(getLabel());
|
||||
sb.append(" --- ");
|
||||
sb.append(pair.getValue().getWeight());
|
||||
sb.append(" --- ");
|
||||
sb.append(pair.getKey().getLabel());
|
||||
sb.append("\n");
|
||||
pair.getValue().setPrinted(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user