最小生成树-普利姆算法(Prim)

基本思想

对于图G而言,V是所有顶点的集合;现在,设置两个新的集合U和T,其中U用于存放G的最小生成树中的顶点,T存放G的最小生成树中的边。 从所有uЄU,vЄ(V-U) (V-U表示出去U的所有顶点)的边中选取权值最小的边(u, v),将顶点v加入集合U中,将边(u, v)加入集合T中,如此不断重复,直到U=V为止,最小生成树构造完毕,这时集合T中包含了最小生成树中的所有边。

public class Edge
    {
    
    
        public int start;
        public int end;
        public int weight;
        public Edge(int start, int end, int weight)
        {
    
    
            this.start = start;
            this.end = end;
            this.weight = weight;
        }
    }
public class Vertex 
    {
    
    
        public object Data;
        public Vertex(object data)
        {
    
    
            this.Data = data;
        }
    }    
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Prim
{
    
    
    public class Graph
    {
    
    
        //INFINITY=无穷大的数字 = 无穷远的点
        public const int INFINITY = 65535;
        public int VertexCount;
        public int[,] matrix;
        /// <summary>
        /// 生成领接矩阵
        /// </summary>
        /// <returns></returns>
        public static Graph CreateGraph(Vertex[] vertexs,Edge[] edges)
        {
    
    
            Graph graphPrim = new Graph();
            graphPrim.VertexCount = vertexs.Length;
            graphPrim.matrix = new int[vertexs.Length, vertexs.Length];
            //初始化 默认都是无穷大
            for (int i = 0; i < vertexs.Length; i++)
            {
    
    
                for (int j = 0; j < vertexs.Length; j++)
                {
    
    
                    if (i != j)
                    {
    
    
                        graphPrim.matrix[i, j] = INFINITY;
                    }
                }
            }
            //初始化边真正存在的权值
            for (int i = 0; i < edges.Length; i++)
            {
    
    
                int start = edges[i].start;
                int end = edges[i].end;
                graphPrim.matrix[start, end] = edges[i].weight;
                graphPrim.matrix[end, start] = edges[i].weight;
            }
            return graphPrim;
        }
        public static void Prim(Graph graph)
        {
    
    
            //顶点的下表索引
            int[] adjvex = new int[graph.VertexCount];
            //顶点的边的权值
            int[] lowCost = new int[graph.VertexCount];

            //初始化数据:lowCast装入v0的对应的领接矩阵的第一行数据
            //          :adjvex全部初始化为0
            for (int i = 0; i < graph.VertexCount; i++)
            {
    
    
                lowCost[i] = graph.matrix[0, i];
                adjvex[i] = 0;
            }
            //计算权值最小的边
            for (int i = 0; i < graph.VertexCount-1; i++)
            {
    
    
                int min = INFINITY;
                int j = 0;
                int k = 0;
                //计算当前顶点U集合到顶点V-U集合的最短距离
                while (j < graph.VertexCount)
                {
    
    
                    if (lowCost[j] != 0 && lowCost[j] < min)
                    {
    
    
                        min = lowCost[j];
                        k = j;
                    }
                    j++;
                }

                Debug.LogError("(" + adjvex[k] + "," + k + ") ");
                lowCost[k] = 0;//表示此顶点已经完成任务
                //动态重新规划 
                //若下表为k顶点到各个边的权值 <  lowCost数组中的各个值需要更新lowCast数组中的权值
                for (int n = 0; n < graph.VertexCount; n++)
                {
    
    
                    if (lowCost[n] != 0 && graph.matrix[k, n] < lowCost[n])
                    {
    
    
                        lowCost[n] = graph.matrix[k, n];
                        adjvex[n] = k;
                    }
                }
            }
        }
    }
}

测试

using Prim;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestPrim : MonoBehaviour
{
    
    
    //    {0,10,65535,65535,65535,11,65535,65535,65535 },
    //    {10,0,18,65535,65535,65535,16,65535,12},
    //    {65535,65535,0,22,65535,65535,65535,65535,8 },
    //    {65535,65535,22,0,20,65535,65535,16,21},
    //    {65535,65535,65535,20,0,26,65535,7,65535 },
    //    {11,65535,65535,65535,26,0,17,65535,65535},
    //    {65535,16,65535,65535,65535,17,0,19,65535 },
    //    {65535,65535,65535,16,7,65535,19,0,65535 },
    //    {65535,12,8,21,65535,65535,65535,65535,0},
    Graph graph;
    void Start()
    {
    
    
        Vertex[] vertexs = GenerateVertex(9);
        Edge[] edges = GenerateEdges();
        graph = Graph.CreateGraph(vertexs,edges);
        Graph.Prim(graph);
    }
    private Vertex[] GenerateVertex(int len)
    {
    
    
        Vertex[] vertexs = new Vertex[len];
        for (int i = 0; i < len; i++)
        {
    
    
            vertexs[i] = new Vertex(i);
        }
        return vertexs;
    }
    private Edge[] GenerateEdges()
    {
    
    

        Edge edge0 = new Edge(0, 1, 10);
        Edge edge1 = new Edge(0, 5, 11);

        Edge edge2 = new Edge(1, 2, 18);
        Edge edge3 = new Edge(1, 6, 16);
        Edge edge4 = new Edge(1, 8, 12);

        Edge edge5 = new Edge(2, 3, 22);
        Edge edge6 = new Edge(2, 8, 8);

        Edge edge7 = new Edge(3, 4, 20);
        Edge edge8 = new Edge(3, 7, 16);
        Edge edge9 = new Edge(3, 8, 21);

        Edge edge10 = new Edge(4, 5, 26);
        Edge edge11 = new Edge(4, 7, 7);

        Edge edge12 = new Edge(5, 6, 17);
        Edge edge13 = new Edge(6, 7, 19);
        Edge edge14 = new Edge(6, 7, 19);
        Edge[] edges = {
    
     edge0, edge1, edge2, edge3, edge4, edge5, edge6, edge7, edge8, edge9, edge10, edge11, edge12, edge13, edge14 };
        return edges;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_39691716/article/details/122152050
今日推荐