Prim's algorithm (java)

1. Introduction to Prim Algorithm

  Prim's algorithm is an algorithm for constructing a minimum spanning tree. The time complexity of Prim's algorithm is O ( ∣ V ∣ 2 ) O(|V|^2)O(V2 ), does not depend onEEE , so it is suitable for solving the minimum spanning tree of graphs with dense edges.

2. Principle of Prim Algorithm

  (1) Initially, a vertex is randomly selected from the graph and added to the minimum spanning tree MinTree vertex set.
  (2) Select a vertex that is closest to the vertex set in the current MinTree, and add the vertex and the corresponding edge to the MinTree, and the number of vertices and the number of edges in the MinTree will increase by 1 after each operation.
  (3) Repeat step (2) until all vertices are added to MinTree to obtain a minimum spanning tree. At this time, there are n − 1 n-1 in MinTreen1 side.

3. Diagram of Prim Algorithm

insert image description here
  The process of constructing the minimum spanning tree of graph (a) is as follows:
  (1) From the vertex V 1 V_{1}V1Start, from V 1 V_{1}V1The nearest three vertices are V 2 , V 3 , V 4 V_{2}, V_{3}, V_{4}V2V3V4, select the vertex V 3 V_{3} with the smallest weightV3, join the vertex set, and make the edges < V 1 , V 3 V_{1}, V_{3}V1V3>Join the minimum spanning tree. (As shown in Figure b)
  (2) At this time, the vertex set is ( V 1 , V 3 V_{1}, V_{3}V1V3), the nearest vertices in the vertex set are V 2 , V 4 , V 5 , V 6 V_{2}, V_{4}, V_{5}, V_{6}V2V4V5V6, select the vertex V 6 V_{6} with the smallest weightV6, join the vertex set, and make the edges < V 3 , V 6 V_{3}, V_{6}V3V6>Join the minimum spanning tree. (As shown in Figure c)
  (3) At this time, the vertex set is ( V 1 , V 3 , V 6 V_{1}, V_{3}, V_{6}V1V3V6), the nearest vertices in the vertex set are V 2 , V 4 , V 5 V_{2}, V_{4}, V_{5}V2V4V5, select the vertex V 4 V_{4} with the smallest weightV4, join the vertex set, and make edges < V 6 , V 4 V_{6}, V_{4}V6V4>Join the minimum spanning tree. (As shown in Figure d)
  (3) At this time, the vertex set is ( V 1 , V 3 , V 6 , V 4 V_{1}, V_{3}, V_{6}, V_{4}V1V3V6V4), the nearest vertices in the vertex set are V 2 , V 5 V_{2}, V_{5}V2V5, select the vertex V 2 V_{2} with the smallest weightV2, join the vertex set, and make the edge < V 3 , V 2 V_{3}, V_{2}V3V2>Join the minimum spanning tree. (As shown in Figure e)
  (4) At this time, the vertex set is ( V 1 , V 3 , V 6 , V 4 , V 2 V_{1}, V_{3}, V_{6}, V_{4}, V_{2}V1V3V6V4V2), the nearest vertex in the vertex set is V 5 V_{5}V5, select the vertex V 5 V_{5} with the smallest weightV5, join the vertex set, and make the edges < V 2 , V 5 V_{2}, V_{5}V2V5>Join the minimum spanning tree. (As shown in Figure f)
  (5) At this time, the vertex set is ( V 1 , V 3 , V 6 , V 4 , V 2 , V 5 V_{1}, V_{3}, V_{6}, V_{ 4}, V_{2}, V_{5}V1V3V6V4V2V5), all vertices have been added to the set. The edges of the minimum spanning tree are < V 1 , V 3 V_{1}, V_{3}V1V3>、< V 3 , V 6 V_{3},V_{6} V3V6>、< V 6 , V 4 V_{6},V_{4} V6V4>、< V 3 , V 2 V_{3},V_{2} V3V2>、< V 2 , V 5 V_{2},V_{5} V2V5>。

4. Implementation of Prim algorithm code

package com.haiyang.algorithm.prim;

/**
 * @author haiYang
 * @create 2022-02-01 16:57
 */
public class PrimAlgorithm {
    
    
    public static void main(String[] args) {
    
    
        
        char[] data = new char[]{
    
    'A', 'B', 'C', 'D', 'E', 'F', 'G'};
        int vertexs = data.length;
        //maxValue表示两个顶点之前没有边
        int maxValue = Integer.MAX_VALUE;
        //邻接矩阵的关系使用二维数组表示,maxValue表示两个点不联通
        int[][] weight = new int[][]{
    
    
                {
    
    maxValue, 5, 7, maxValue, maxValue, maxValue, 2},
                {
    
    5, maxValue, maxValue, 9, maxValue, maxValue, 3},
                {
    
    7, maxValue, maxValue, maxValue, 8, maxValue, maxValue},
                {
    
    maxValue, 9, maxValue, maxValue, maxValue, 4, maxValue},
                {
    
    maxValue, maxValue, 8, maxValue, maxValue, 5, 4},
                {
    
    maxValue, maxValue, maxValue, 4, 5, maxValue, 6},
                {
    
    2, 3, maxValue, maxValue, 4, 6, maxValue}};
        Graph graph = new Graph(vertexs, data, weight);
        prim(graph, 0);


    }

    /**
     * @param graph 图
     * @param v     开始顶点
     */
    public static void prim(Graph graph, int v) {
    
    
        //标记已访问顶点
        int[] visited = new int[graph.vertexs];
        //将开始顶点标记已访问
        visited[v] = 1;
        //h1、h2标记最小的权值weight位置
        int h1 = -1;
        int h2 = -1;
        //记录最小权值
        int minWeight = Integer.MAX_VALUE;
        //除开始顶点,将其他graph.verrtexs-1个顶点进行选取
        for (int k = 1; k < graph.vertexs; k++) {
    
    

            //遍历图的所有情况,找到此轮的最小权值连接的顶点
            for (int i = 0; i < graph.vertexs; i++) {
    
    
                for (int j = 0; j < graph.vertexs; j++) {
    
    
                    //选取的顶点要求:i是已经选取的的顶点,j是为选取的顶点,找到满足情况的最小权值,记录位置
                    if (visited[i] == 1 && visited[j] == 0 && graph.weight[i][j] < minWeight) {
    
    
                        minWeight = graph.weight[i][j];
                        h1 = i;
                        h2 = j;
                    }
                }

            }
            //将此轮选取的最小权值连接的顶点信息输出
            System.out.println("边<" + graph.data[h1] + "," + graph.data[h2] + "> 权值=" + minWeight);
            //将此轮选取的顶点标记为已访问
            visited[h2] = 1;
            //重新初始化minWeight值
            minWeight = Integer.MAX_VALUE;

        }


    }
}

/**
 * 图类
 */
class Graph {
    
    
    int vertexs; //表示顶点个数
    char[] data;//存放顶点数据
    int[][] weight; //使用邻接矩阵存放边

    public Graph(int vertexs, char[] data, int[][] weight) {
    
    
        this.vertexs = vertexs;
        this.data = data;
        this.weight = weight;
    }
}

Guess you like

Origin blog.csdn.net/weixin_43698993/article/details/122770231