Minimum Spanning Tree definition:
Each undirected graph can be split into multiple subgraphs. In these subgraphs, if each vertex of the graph does not form a loop, it is a spanning tree of the graph.
The Minimum Spanning Tree consciousness is the minimum sum of distance between adjacent nodes of the tree.
Application scenarios:
John was elected as the mayor of their town! One of his campaign promises to build the Internet in town and connect to all farms.
John has arranged a high-speed network line for his farm. He wants to share the line with other farms. To minimize consumption, he wanted to build the shortest optical fiber to connect all farms and asked how to implement it.
Algorithm Implementation:
There are two common algorithms for the problem of the minimal spanning tree, namely the Kruskal algorithm and prim algorithm. This article describes the implementation of the Kruskal algorithm.
The calculation process of the Kruskal algorithm is roughly as follows:
1. incrementally sort the edges of an undirected graph by distance and place them in the set.
2. traverse the set to find the shortest edge and add it to the set of the result generation tree.
3. If the result tree shows a loop, discard this edge.
4. Execute Step 2 again until all vertices are traversed.
We can see that the greedy algorithm is used in each traversal process.
Algorithm instance diagram:
Code implementation:
Edge Class uses and encapsulates the information of each edge in an undirected graph
public class Edge implements Comparable<Edge>{private String start;private String end;private int distance;public Edge(String start,String end,int distance){this.start=start;this.end=end;this.distance=distance;}public String getStart() {return start;}public void setStart(String start) {this.start = start;}public String getEnd() {return end;}public void setEnd(String end) {this.end = end;}public int getDistance() {return distance;}public void setDistance(int distance) {this.distance = distance;}@Overridepublic String toString() {return start + "->" + end;}@Overridepublic int compareTo(Edge obj) {int targetDis=obj.getDistance();return distance>targetDis?1:(distance==targetDis?0:-1);}}
The mapbuilder class is used to build a data source (the data source structure is shown in ):
public class MapBuilder {public TreeSet<Edge> build(){TreeSet<Edge> edges=new TreeSet<Edge>();edges.add(new Edge("A","B",1));edges.add(new Edge("A","C",4));edges.add(new Edge("A","F",6));edges.add(new Edge("B","D",8));edges.add(new Edge("B","E",3));edges.add(new Edge("C","F",5));edges.add(new Edge("C","E",9));edges.add(new Edge("D","E",7));edges.add(new Edge("D","F",10));edges.add(new Edge("E","F",2));return edges;}public int getPointNum(){return 6;}}
The Kruskal class is used to calculate the Minimum Spanning Tree.
Public class Kruskal {private set <string> points = new hashset <string> (); Private list <edge> treeedges = new arraylist <edge> (); Public void buildtree () {mapbuilder builder = new mapbuilder (); treeset <edge> edges = builder. build (); int pointnum = builder. getpointnum (); For (edge: edges) {If (iscircle (edge) {continue;} else {// no loop appears. Add this edge to the treeedges set treeedges. add (edge); // if the number of edges is equal to the number of vertices-1, the traversal ends if (treeedges. size () = pointnu M-1) {return ;}}} public void printtreeinfo () {int totaldistance = 0; For (edge: treeedges) {totaldistance + = edge. getdistance (); system. out. println (edge. tostring ();} system. out. println ("Total path length:" + totaldistance);} private Boolean iscircle (edge) {int size = points. size (); If (! Points. Contains (edge. getstart () {size ++;} If (! Points. contains (edge. getend () {size ++;} If (size = treeedges. size () + 1) {return true;} else {points. add (edge. getstart (); points. add (edge. getend (); Return false ;}}}
Main class is used to test the Kruskal algorithm.
public class Main {public static void main(String[] args) {Kruskal test=new Kruskal();test.buildTree();test.printTreeInfo();}}
Print Output:
A-> B
E-> F
B-> E
A-> C
D-> E
Total path length: 17