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