最小生成树是找出图中包括所有结点的联通子图,使得其所有的边的权重之和最小。
Kruskal 算法提供一种在 O(ElogV) 运行时间确定最小生成树的方案。其选择的贪心策略就是,每次都选择权重最小的但未形成环路的边加入到生成树中。其算法结构如下:
- 将所有的边按照权重非递减排序;
- 选择最小权重的边,判断是否其在当前的生成树中形成了一个环路。如果环路没有形成,则将该边加入树中,否则放弃。
- 重复步骤 2,直到有 V – 1 条边在生成树中(所有的结点都加入了树中)。
最小生成树是找出图中包括所有结点的联通子图,使得其所有的边的权重之和最小。
Kruskal 算法提供一种在 O(ElogV) 运行时间确定最小生成树的方案。其选择的贪心策略就是,每次都选择权重最小的但未形成环路的边加入到生成树中。其算法结构如下: