基本思想
对于图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</