1201-2019-算法-克鲁斯卡尔算法(最小生成树MST-Kruskal算法)

① Kruskal算法先对路径的权值进行排序
② 再在图中加入这个路径(要求不产生回路)。
1201-2019-算法-克鲁斯卡尔算法(最小生成树MST-Kruskal算法)_第1张图片
关键就是不能形成回路。判断是否为回路的准则是:两个顶点例如(C-E)是否有同一个终点。
关键代码:关键代码是end数组的生成,end数组用来记录每个顶点的终点。(end[ ]也是逐步完善的,一步一步填)

package Kruskal;

/**
 * @author pdzz
 * @create 2019-11-30 16:07
 */
public class Kruskal {
    private int edgeNum;//边的个数
    private char[] vertex;//顶点数组
    private int[][] matrix;//邻接矩阵
    private static final int INF = Integer.MAX_VALUE;

    public static void main(String[] args) {
        char[] vertexes = {'A','B','C','D','E','F','G'};
        int[][] matrix = {
                {0,12,INF,INF,INF,16,14},
                {12,0,10,INF,INF,7,INF},
                {INF,10,0,3,5,6,INF},
                {INF,INF,3,0,4,INF,INF},
                {INF,INF,5,4,0,2,8},
                {16,7,6,INF,2,0,9},
                {14,INF,INF,INF,8,9,0},
        };
        Kruskal kruskal = new Kruskal(vertexes, matrix);
        kruskal.kruskal();
    }


    private int getEnd(int[] ends,int i){
        while (ends[i] != 0){
            i = ends[i];
        }
        return i;
    }

    public void kruskal(){
        int index = 0;
        int[] ends = new int[edgeNum];
        Edata[] ret = new Edata[edgeNum];

        Edata[] edges = getEdges();
        sortEdges(edges);
        //System.out.println("边的集合:" + Arrays.toString(edges));
        //遍历边的集合,判断是否形成回路。
        for (int i = 0; i < edgeNum; i++) {
            int p1 = getPosition(edges[i].start);
            int p2 = getPosition(edges[i].end);
            //获取p1顶点的终点
            int m = getEnd(ends,p1);
            //获取p2顶点的最小下标
            int n = getEnd(ends,p2);
            if (m != n){
                ends[m] = n;//加入m,n
                ret[index++] = edges[i];
            }
        }
        int sumWeight = 0;
        System.out.println("最小生成树为:");
        for (int i = 0; i < index; i++) {
            System.out.println(ret[i]);
            sumWeight += ret[i].weight;
        }
        System.out.println("路径总长 = " + sumWeight);
    }


    public Kruskal(char[] vertex,int[][] matrix) {
        //初始化顶点数和边的个数
        int vlen = vertex.length;
        this.vertex = new char[vlen];
        //初始化
        for (int i = 0;i < vlen;i++){
            this.vertex[i] = vertex[i];
        }
        this.matrix = new int[vlen][vlen];

        //传递到kruskal中的全局变量
        for (int i = 0; i < vlen; i++) {
            for (int j = 0; j < vlen; j++) {
                this.matrix[i][j] = matrix[i][j];
            }
        }
        //统计边的条数
        for (int i = 0; i < vlen; i++) {
            for (int j = i + 1; j < vlen; j++) {
                if (this.matrix[i][j] != INF){
                    edgeNum++;
                }
            }

        }
        System.out.println(edgeNum);

        for (int i = 0; i < vlen; i++) {
            for (int j = 0; j < vlen; j++) {
                System.out.printf("%12d", matrix[i][j]);
            }
            System.out.println();
        }



    }
    private void sortEdges(Edata[] edges){

        for (int i = 0; i < edges.length - 1; i++) {
            for (int j = 0; j < edges.length - 1 -i; j++) {
                if (edges[j].weight > edges[j + 1].weight){
                    Edata temp = edges[j];
                    edges[j] = edges[j + 1];
                    edges[j + 1] = temp;
                }
            }
        }
    }

    public int getPosition(char ch){
        for (int i = 0; i < vertex.length; i++) {
            if (vertex[i] == ch){
                return i;
            }
        }
        return -1;
    }


    private Edata[] getEdges(){
        int index = 0;
        Edata[] edata = new Edata[edgeNum];
        for (int i = 0; i < vertex.length; i++) {
            for (int j = i + 1; j < vertex.length; j++) {
                if (matrix[i][j] != INF){
                    edata[index++] = new Edata(vertex[i],vertex[j],matrix[i][j]);
                }
            }
        }
        return edata;
    }






}
class Edata{

    char start;
    char end;
    int weight;

    public Edata(char start, char end, int weight) {
        this.start = start;
        this.end = end;
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "Edata{" +
                "start=" + start +
                ", end=" + end +
                ", weight=" + weight +
                '}';
    }
}

来自尚硅谷韩顺平老师的Java数据结构的视频。

你可能感兴趣的:(算法)