addEdge fix
This commit is contained in:
parent
d1bd33f862
commit
24859d45ab
|
@ -6,7 +6,7 @@ public class Edge {
|
|||
private boolean isIncluded = false;
|
||||
private boolean isPrinted = false;
|
||||
|
||||
public Edge (int weight){
|
||||
public Edge(int weight) {
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
|
@ -34,4 +34,3 @@ public class Edge {
|
|||
isPrinted = printed;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -10,21 +10,23 @@ public class Prim {
|
|||
|
||||
private List<Vertex> graph;
|
||||
|
||||
public Prim(List<Vertex> graph){
|
||||
public Prim(List<Vertex> graph) {
|
||||
this.graph = graph;
|
||||
}
|
||||
|
||||
public void run(){
|
||||
if (graph.size() > 0){
|
||||
graph.get(0).setVisited(true);
|
||||
public void run() {
|
||||
if (graph.size() > 0) {
|
||||
graph.get(0)
|
||||
.setVisited(true);
|
||||
}
|
||||
while (isDisconnected()){
|
||||
while (isDisconnected()) {
|
||||
Edge nextMinimum = new Edge(Integer.MAX_VALUE);
|
||||
Vertex nextVertex = graph.get(0);
|
||||
for (Vertex vertex : graph){
|
||||
if (vertex.isVisited()){
|
||||
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();
|
||||
}
|
||||
|
@ -35,40 +37,41 @@ public class Prim {
|
|||
}
|
||||
}
|
||||
|
||||
private boolean isDisconnected(){
|
||||
for (Vertex vertex : graph){
|
||||
if (!vertex.isVisited()){
|
||||
private boolean isDisconnected() {
|
||||
for (Vertex vertex : graph) {
|
||||
if (!vertex.isVisited()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public String originalGraphToString(){
|
||||
public String originalGraphToString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Vertex vertex : graph){
|
||||
for (Vertex vertex : graph) {
|
||||
sb.append(vertex.originalToString());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void resetPrintHistory(){
|
||||
for (Vertex vertex : graph){
|
||||
Iterator it = vertex.getEdges().entrySet().iterator();
|
||||
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);
|
||||
((Edge) pair.getValue()).setPrinted(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String minimumSpanningTreeToString(){
|
||||
public String minimumSpanningTreeToString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Vertex vertex : graph){
|
||||
for (Vertex vertex : graph) {
|
||||
sb.append(vertex.includedToString());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ public class Vertex {
|
|||
private Map<Vertex, Edge> edges = new HashMap<>();
|
||||
private boolean isVisited = false;
|
||||
|
||||
public Vertex(String label){
|
||||
public Vertex(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
|
@ -28,7 +28,7 @@ public class Vertex {
|
|||
return edges;
|
||||
}
|
||||
|
||||
public void addEdge(Vertex vertex, Edge edge){
|
||||
public void addEdge(Vertex vertex, Edge edge) {
|
||||
this.edges.put(vertex, edge);
|
||||
}
|
||||
|
||||
|
@ -40,14 +40,15 @@ public class Vertex {
|
|||
isVisited = visited;
|
||||
}
|
||||
|
||||
public Pair<Vertex, Edge> nextMinimum(){
|
||||
public Pair<Vertex, Edge> nextMinimum() {
|
||||
Edge nextMinimum = new Edge(Integer.MAX_VALUE);
|
||||
Vertex nextVertex = this;
|
||||
Iterator it = edges.entrySet().iterator();
|
||||
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 (!((Vertex) pair.getKey()).isVisited()) {
|
||||
if (!((Edge) pair.getValue()).isIncluded()) {
|
||||
if (((Edge) pair.getValue()).getWeight() < nextMinimum.getWeight()) {
|
||||
nextMinimum = (Edge) pair.getValue();
|
||||
nextVertex = (Vertex) pair.getKey();
|
||||
|
@ -58,12 +59,13 @@ public class Vertex {
|
|||
return new Pair<>(nextVertex, nextMinimum);
|
||||
}
|
||||
|
||||
public String originalToString(){
|
||||
public String originalToString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Iterator it = edges.entrySet().iterator();
|
||||
Iterator it = edges.entrySet()
|
||||
.iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry pair = (Map.Entry) it.next();
|
||||
if (!((Edge)pair.getValue()).isPrinted()) {
|
||||
if (!((Edge) pair.getValue()).isPrinted()) {
|
||||
sb.append(getLabel());
|
||||
sb.append(" --- ");
|
||||
sb.append(((Edge) pair.getValue()).getWeight());
|
||||
|
@ -76,14 +78,15 @@ public class Vertex {
|
|||
return sb.toString();
|
||||
}
|
||||
|
||||
public String includedToString(){
|
||||
public String includedToString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (isVisited()) {
|
||||
Iterator it = edges.entrySet().iterator();
|
||||
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()) {
|
||||
if (!((Edge) pair.getValue()).isPrinted()) {
|
||||
sb.append(getLabel());
|
||||
sb.append(" --- ");
|
||||
sb.append(((Edge) pair.getValue()).getWeight());
|
||||
|
@ -98,4 +101,3 @@ public class Vertex {
|
|||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ public class PrimUnitTest {
|
|||
System.out.println(prim.minimumSpanningTreeToString());
|
||||
}
|
||||
|
||||
public static List<Vertex> createGraph(){
|
||||
public static List<Vertex> createGraph() {
|
||||
List<Vertex> graph = new ArrayList<>();
|
||||
Vertex a = new Vertex("A");
|
||||
Vertex b = new Vertex("B");
|
||||
|
@ -52,4 +52,3 @@ public class PrimUnitTest {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue