普利姆算法

package com.company;

public class Main {

    public static void main(String[] args) {
    // write your code here
        int[][] adjustMatrix = {
                {0,7,-1,5,-1,-1,-1},
                {-1,0,8,9,7,-1,-1},
                {-1,-1,0,-1,5,-1,-1},
                {-1,-1,-1,0,15,6,-1},
                {-1,-1,-1,-1,0,8,9},
                {-1,-1,-1,-1,-1,0,11},
                {-1,-1,-1,-1,-1,-1,0}
        };
        Prim.prim0(adjustMatrix);
    }
}

package com.company;

public class Prim {
    /**
     * 适用于无向连通图,用于生成最小生成树。
     * 主要思想是把图中所有顶点分成两个集合,
     * 一个已经在最小生成树中的顶点,另一个是
     * 不是。它从与已经在最小生成树中的顶点相
     * 邻的没有在最小生成树中的顶点权值最小的
     * 边所对应的没有在最小生成树中的顶点加入
     * 到最小生成树集合中。
     * 与克鲁斯卡尔算法不同,它不需要检查有没
     * 有洞。
     * @param sourceArray
     */
    static public void prim0(int[][] sourceArray) {
        int arrayLength = sourceArray.length;
        int[] targetVertexSet = new int[arrayLength];
        //-1代表该顶点还没有被加入到最小生成树中。
        for (int counter = 0;counter < arrayLength;counter++)
            targetVertexSet[counter] = -1;
        SingleLinkerNode headPointer = new SingleLinkerNode(-1,-1,-1);
        for (int counter = 0;counter < arrayLength;counter++) {
            for (int counter0 = counter + 1;counter0 < arrayLength;counter0++) {
                if (sourceArray[counter][counter0] > 0) {
                    SingleLinkerNode currentPointer = headPointer;
                    while (currentPointer.nextPointer != null
                            && currentPointer.nextPointer.getWeight() < sourceArray[counter][counter0])
                        currentPointer = currentPointer.nextPointer;
                    SingleLinkerNode newNode = new SingleLinkerNode(counter,counter0,sourceArray[counter][counter0]);
                    newNode.nextPointer = currentPointer.nextPointer;
                    currentPointer.nextPointer = newNode;
                }
            }
        }
        int targetVertexCounter = 0;
        SingleLinkerNode resultEdgesPointer = null;
        while (targetVertexCounter < arrayLength) {
            SingleLinkerNode cursorPointer = headPointer;
            while (cursorPointer.nextPointer != null) {
                if (targetVertexCounter == 0)break;
                if (targetVertexSet[cursorPointer.nextPointer.getSourceVertex()] == -1 &&
                        targetVertexSet[cursorPointer.nextPointer.getTargetVertex()] != -1 ||
                        targetVertexSet[cursorPointer.nextPointer.getSourceVertex()] != -1 &&
                        targetVertexSet[cursorPointer.nextPointer.getTargetVertex()] == -1)
                    break;
                cursorPointer = cursorPointer.nextPointer;
            }
            SingleLinkerNode targetNode = cursorPointer.nextPointer;
            if (targetNode == null) {
                System.out.println("此图不是连通图");
                return;
            }
            if (targetVertexSet[targetNode.getSourceVertex()] == -1) {
                targetVertexSet[targetNode.getSourceVertex()] = targetNode.getSourceVertex();
                targetVertexCounter++;
            }
            if (targetVertexSet[targetNode.getTargetVertex()] == -1) {
                targetVertexSet[targetNode.getTargetVertex()] = targetNode.getTargetVertex();
                targetVertexCounter++;
            }
            cursorPointer.nextPointer = targetNode.nextPointer;
            targetNode.nextPointer = null;
            if (resultEdgesPointer == null)resultEdgesPointer = targetNode;
            else {
                targetNode.nextPointer = resultEdgesPointer.nextPointer;
                resultEdgesPointer.nextPointer = targetNode;
            }
        }
        while (resultEdgesPointer != null) {
            System.out.println("(" + resultEdgesPointer.getSourceVertex() +
                    "," + resultEdgesPointer.getTargetVertex() +
                    ")-->" + resultEdgesPointer.getWeight());
            resultEdgesPointer = resultEdgesPointer.nextPointer;
        }
    }
}

package com.company;

public class SingleLinkerNode {
    private int sourceVertex;
    private int targetVertex;
    private int weight;
    public SingleLinkerNode nextPointer;

    public SingleLinkerNode(int sourceVertex, int targetVertex, int weight) {
        this.sourceVertex = sourceVertex;
        this.targetVertex = targetVertex;
        this.weight = weight;
    }

    public int getSourceVertex() {
        return sourceVertex;
    }

    public int getTargetVertex() {
        return targetVertex;
    }

    public int getWeight() {
        return weight;
    }
}

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