跳到主要内容

资源 · 指南或文章

Prim's Algorithm

描述 Prim 贪心最小生成树算法:从任意顶点出发,不断接上离开当前树的最便宜的边。

审核日期

打开原站: Prim's Algorithm (在新标签页中打开原网站)

练习这个主题打开练习面试官,并带上一道关于 图 的题目。

添加到路线图

把这个资源放进你正在跟进的路线图。它会作为你自己的补充资源显示;路线图的步骤不会改变,也不计入你已记录的步骤。

来源内容语言:英语

In computer science, Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a weighted undirected graph. The algorithm operates by building this tree one vertex at a time, from an arbitrary starting vertex, at each step adding the cheapest possible connection from the tree to another vertex. Prim's algorithm starting at vertex A. In the third step, edges BD and AB both have weight 2, so BD is chosen arbitrarily. After that step, AB is no longer a candidate for addition to the tree because it links two nodes that are already in the tree.

PrepKit 概览

本概览由 PrepKit 编辑根据源代码仓库撰写并审核,并非资源本身的文字。

适合人群
来源未说明适用级别
形式
参考资料

涵盖内容

  • 从任意起点开始逐个顶点扩展
  • 一段图注解释边 BD 与 AB 权重相同的情况,以及为什么之后 AB 会被排除
  • 最小生成树和最小生成森林的定义,并举例说明两棵同样最小的树

使用建议

这是 PrepKit 的学习建议,并非来源的说法。

用最小堆实现 Prim,在测试图上检查总权重,然后说明在稠密图上你何时会优先选择它。

范围与局限

没有讨论数据结构的选择和复杂度,除一段图注外也没有文字讲解;最小生成树部分只是一般性背景。

获取方式与形式

GitHub 上采用 MIT 许可的 trekhleb/javascript-algorithms 仓库中的免费英文 README。

PrepKit 于 2026年10月2日 审核。 资源来源提交: 85293e3e2b88

本概览的依据
  • src/algorithms/graph/prim/README.md

收录此条目的页面

订阅 PrepKit 通讯

可选。使用 PrepKit 并不需要订阅。

我们如何处理你的邮箱