普里姆算法最小生成树_普里姆的最小生成树

本文介绍了如何使用普里姆算法构造最小生成树,并提供了C++实现。初始时,除起点外所有顶点距离设为无穷大,然后不断选择与已访问顶点相邻的最小距离顶点,直至所有顶点被覆盖。通过示例和代码解释了算法的详细步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

普里姆算法最小生成树

What to Learn?

学什么?

How to construct minimum spanning tree using Prim's Minimum Spanning Tree algorithm and its C++ implementation?

如何使用Prim的最小生成树算法及其C ++实现构造最小生成树?

Minimum Spanning Tree is a tree with all the vertices of the graph and who has a minimum weight.

最小生成树是具有图形的所有顶点且权重最小的树。

In the figure there is a graph with 9 vertexes.

图中有一个具有9个顶点的图形。

Prim's Minimum Spanning Tree

If we construct a minimum spanning tree then it would be like,

如果我们构造一个最小生成树,那么它将是,

Prim's Minimum Spanning Tree 1

Algorithm:

算法:

To implement the Prim's Minimum Spanning Tree algorithm, we have an array of all the vertices with their corresponding distance.

为了实现Prim的最小生成树算法 ,我们有一个所有顶点及其对应距离的数组。

  1. Initially, all the vertices have a distance infinity except the starting vertex which has distance zero.

    最初,除起始顶点的距离为零外,所有顶点的距离都为无穷大。

  2. We check the all the unvisited reachable vertices from the starting vertex and update all the distance with weighted edge distance from that vertex.

    我们从起始顶点检查所有未访问的可达顶点,并使用从该顶点开始的加权边距更新所有距离。

  3. Choose the minimum unvisited distance and choose the corresponding vertex.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值