package com.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
// 求最小生成树----克鲁斯卡尔算法(Kruskal)
public class MiniSpanTreeKruskal {
public static void main(String[] args) {
List<Edge> edges = new ArrayList<Edge>();
Edge edge0 = new Edge(0, 1, 10);
Edge edge1 = new Edge(0, 5, 11);
Edge edge2 = new Edge(1, 8, 12);
Edge edge3 = new Edge(1, 2, 18);
Edge edge4 = new Edge(1, 6, 16);
Edge edge5 = new Edge(2, 8, 8);
Edge edge6 = new Edge(2, 3, 22);
Edge edge7 = new Edge(3, 4, 20);
Edge edge8 = new Edge(3, 7, 16);
Edge edge9 = new Edge(3, 6, 24);
Edge edge10 = new Edge(3, 8, 21);
Edge edge11 = new Edge(4, 7, 7);
Edge edge12 = new Edge(4, 5, 26);
Edge edge13 = new Edge(5, 6, 17);
Edge edge14 = new Edge(6, 7, 19);
edges.add(edge0);
edges.add(edge1);
edges.add(edge2);
edges.add(edge3);
edges.add(edge4);
edges.add(edge5);
edges.add(edge6);
edges.add(edge7);
edges.add(edge8);
edges.add(edge9);
edges.add(edge10);
edges.add(edge11);
edges.add(edge12);
edges.add(edge13);
edges.add(edge14);
Collections.sort(edges);
System.out.println("排序后的边:");
for (Edge edge : edges) {
System.out.println(edge);
}
System.out.println("-------------");
// 开始生成最小树--Kruskal
int edgeSize = edges.size();// 边的条数
int[] parent = new int[edgeSize];// 定义一个数组用来判断边与边是否形成环路
int m, n;
for (int i = 0; i < edgeSize; i++) {
m = find(parent, edges.get(i).begin);
n = find(parent, edges.get(i).end);
if (m != n) {
parent[m] = n;// 将此边的皆为顶点放入下标为起点的parent中
System.out.println(edges.get(i));
}
}
}
/**
* 查找连线顶点的尾部下标
*
* @param parent
* 查找的数组
* @param m
* 查找的下边
* @return 返回的值
*/
static int find(int[] parent, int m) {
return parent[m] > 0 ? parent[m] : m;
}
}
class Edge implements Comparable<Edge> {
public Edge(int begin, int end, int weight) {
this.begin = begin;
this.end = end;
this.weight = weight;
}
int begin;
int end;
int weight;
@Override
public int compareTo(Edge edge) {
return this.weight > edge.weight ? 1 : (this.weight == edge.weight ? 0 : -1);
}
@Override
public String toString() {
return "(" + this.begin + "," + this.end + "," + this.weight + ")";
}
}
输出结果:
排序后的边:
(4,7,7)
(2,8,8)
(0,1,10)
(0,5,11)
(1,8,12)
(1,6,16)
(3,7,16)
(5,6,17)
(1,2,18)
(6,7,19)
(3,4,20)
(3,8,21)
(2,3,22)
(3,6,24)
(4,5,26)
-------------
(4,7,7)
(2,8,8)
(0,1,10)
(0,5,11)
(1,8,12)
(1,6,16)
(3,7,16)
(1,2,18)
(6,7,19)
(3,8,21)
(2,3,22)
(4,5,26)