aaa
This commit is contained in:
parent
8095153e25
commit
58ee757436
@ -0,0 +1,37 @@
|
|||||||
|
package com.baeldung.algorithms.prim;
|
||||||
|
|
||||||
|
public class Edge {
|
||||||
|
|
||||||
|
private int weight;
|
||||||
|
private boolean isIncluded = false;
|
||||||
|
private boolean isPrinted = false;
|
||||||
|
|
||||||
|
public Edge (int weight){
|
||||||
|
this.weight = weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWeight() {
|
||||||
|
return weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWeight(int weight) {
|
||||||
|
this.weight = weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isIncluded() {
|
||||||
|
return isIncluded;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIncluded(boolean included) {
|
||||||
|
isIncluded = included;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isPrinted() {
|
||||||
|
return isPrinted;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrinted(boolean printed) {
|
||||||
|
isPrinted = printed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,74 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,101 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,55 @@
|
|||||||
|
package com.baeldung.algorithms.prim;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class PrimTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAGraph_whenPrimRuns_thenPrintMST() {
|
||||||
|
Prim prim = new Prim(createGraph());
|
||||||
|
System.out.println(prim.originalGraphToString());
|
||||||
|
System.out.println("----------------");
|
||||||
|
prim.run();
|
||||||
|
System.out.println();
|
||||||
|
prim.resetPrintHistory();
|
||||||
|
System.out.println(prim.minimumSpanningTreeToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Vertex> createGraph(){
|
||||||
|
List<Vertex> graph = new ArrayList<>();
|
||||||
|
Vertex a = new Vertex("A");
|
||||||
|
Vertex b = new Vertex("B");
|
||||||
|
Vertex c = new Vertex("C");
|
||||||
|
Vertex d = new Vertex("D");
|
||||||
|
Vertex e = new Vertex("E");
|
||||||
|
Edge ab = new Edge(2);
|
||||||
|
a.addEdge(b, ab);
|
||||||
|
b.addEdge(a, ab);
|
||||||
|
Edge ac = new Edge(3);
|
||||||
|
a.addEdge(c, ac);
|
||||||
|
c.addEdge(a, ac);
|
||||||
|
Edge bc = new Edge(2);
|
||||||
|
b.addEdge(c, bc);
|
||||||
|
c.addEdge(b, bc);
|
||||||
|
Edge be = new Edge(5);
|
||||||
|
b.addEdge(e, be);
|
||||||
|
e.addEdge(b, be);
|
||||||
|
Edge cd = new Edge(1);
|
||||||
|
c.addEdge(d, cd);
|
||||||
|
d.addEdge(c, cd);
|
||||||
|
Edge ce = new Edge(1);
|
||||||
|
c.addEdge(e, ce);
|
||||||
|
e.addEdge(c, ce);
|
||||||
|
graph.add(a);
|
||||||
|
graph.add(b);
|
||||||
|
graph.add(c);
|
||||||
|
graph.add(d);
|
||||||
|
graph.add(e);
|
||||||
|
return graph;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user