Prim算法(最小生成树)

Prim算法

概念

首先任取一个顶点加入树,之后选择一个与当前树中顶点集合距离最小的顶点,将他加入树。直至图中所有顶点都并入树,得到的就是最小生成树。

在这里插入图片描述

模板
/*  
最小生成树 Prim算法
*/

#include <iostream>
using namespace std;

#define MaxVertexNum 10

struct Graph
{
    int edgenum;
    int vertexnum;
    int edgeList[MaxVertexNum][MaxVertexNum];
    int parent[MaxVertexNum]; // 树的父亲结点
    int dist[MaxVertexNum];   // 到树的距离
};

void BuildGraph(Graph *G)
{
    int start, end;
    cout << "Please enter the number of vertices and edges" << endl;
    cin >> G->vertexnum >> G->edgenum;
    // 图的权重初始化
    for (int i = 1; i <= G->vertexnum; i++)
    {
        G->parent[i] = -1;
        G->dist[i] = INT_MAX;
        for (int j = 1; j <= G->vertexnum; j++)
        {
            G->edgeList[i][j] = 0;
        }
    }
    // 输入权重信息
    for (int i = 1; i <= G->edgenum; i++)
    {
        cout << "Please enter the Start number, end number, weight" << endl;
        cin >> start >> end;
        cin >> G->edgeList[start][end];
        G->edgeList[end][start] = G->edgeList[start][end];
    }
    cout << endl;
}

void Prim(Graph *G)
{
    int firstvertex = 1;
    G->dist[firstvertex] = 0;
    for (int i = 1; i <= G->vertexnum; i++)
    {
        if (G->edgeList[firstvertex][i])
        {
            G->dist[i] = G->edgeList[firstvertex][i];
            G->parent[i] = firstvertex;
        }
    }
    while (true)
    {
        // 寻找不在树里的结点中离树的距离最短的
        int minvertex, mindist = INT_MAX;
        for (int i = 1; i <= G->vertexnum; i++)
        {
            if (G->dist[i] != 0 && G->dist[i] < mindist)
            {
                mindist = G->dist[i];
                minvertex = i;
            }
        }
        // 循环结束条件:都在树中
        if (mindist == INT_MAX)
        {
            break;
        }
        // 将找到的点放入树中
        G->dist[minvertex] = 0;
        cout << "-->" << minvertex;
        // 以minvertex为源点扩散更新dist
        for (int i = 1; i <= G->vertexnum; i++)
        {
            // 寻找源点的邻接点且不在树中
            if (G->dist[i] != 0 && G->edgeList[minvertex][i])
            {
                // 如果距离小于原来的dist,则更新
                if (G->edgeList[minvertex][i] < G->dist[i])
                {
                    G->dist[i] = G->edgeList[minvertex][i];
                    G->parent[i] = minvertex;
                }
            }
        }
    }
    cout << endl;
}

int main()
{
    Graph G;
    BuildGraph(&G);
    Prim(&G);
    cout << "distance" << endl;
    for (int i = 1; i <= G.vertexnum; i++)
    {
        cout << G.dist[i] << " ";
    }
    cout << endl;
    cout << "parent" << endl;
    for (int i = 1; i <= G.vertexnum; i++)
    {
        cout << G.parent[i] << " ";
    }
    system("pause");
    return 0;
}

/*  
6 10
1 2 6
1 4 5
1 3 1
2 3 5
3 4 5
3 5 6
3 6 4
5 6 6
2 5 3
4 6 2
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值