MST — Kruskal's algorithm
算法简介
Kruskal算法可用来求解MST(最小生成树)问题,还可以作为迷宫生成算法等。
算法分析
其实算法不难理解,算法先要将 $ G(V, E) $ 的集合 $ E $ 按权重 $ \Omega $ 由小到大排序,然后还利用了不相交集中的`find()`(这里使用的是带路径压缩功能的) 和`union()`(这里函数名使用`marge()`) 函数,`find()`用于判断是否连通,如果连通则不能构成MST,反之则加入到MST的集合中,并调用`union()`函数将顶点连通。
时间复杂度 $ O(ElgV) $
空间复杂度 $ O(V + E) $
算法实现
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
const int N = 10010;
int p[N];
vector<pair<int, pair<int, int>>> graph;
void init(int V, int E)
{
for (int i = 1; i <= V; i++)
p[i] = i;
for (int i = 0; i < E; i++)
{
int w, s, e; // w:权重
cin >> w >> s >> e;
graph.push_back(pair<int, pair<int ,int>>(w, pair<int, int>(s, e)));
}
sort(graph.begin(), graph.end());
for (auto e : graph)
cout << e.first << e.second.first << e.second.second << endl;
}
int find(int x)
{
if (x != p[x]) p[x] = find(p[x]);
return p[x];
}
void marge(int x, int y)
{
int r = find(x), t = find(y);
if (r != t) p[r] = t;
}
vector<pair<int, int>> kruskal(int V, int E)
{
vector<pair<int, int>> msts;
init(V, E);
for (vector<pair<int, pair<int, int>>>::iterator i = graph.begin(); i != graph.end(); i++)
{
if (find(i->second.first) != find(i->second.second))
{
msts.push_back(i->second);
marge(i->second.first, i->second.second);
}
}
return msts;
}
int main(int argc, char **argv)
{
int V, E;
cin >> V >> E;
vector<pair<int, int>> es = kruskal(V, E);
for (auto e : es)
cout << e.first << " " << e.second << endl;
return 0;
}
参考:
1.[Kruskal's algorithm - wikipedia](https://en.wikipedia.org/wiki/Kruskal%27s_algorithm)
2.[Maze generation algorithm - wikipedia](https://en.wikipedia.org/wiki/Maze_generation_algorithm)
MST — Kruskal's algorithm的更多相关文章
- MST(Kruskal’s Minimum Spanning Tree Algorithm)
You may refer to the main idea of MST in graph theory. http://en.wikipedia.org/wiki/Minimum_spanning ...
- Prim's Algorithm & Kruskal's algorithm
1. Problem These two algorithm are all used to find a minimum spanning tree for a weighted undirecte ...
- POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27141 Accepted: 9712 D ...
- NOI.AC #31 MST —— Kruskal+点集DP
题目:http://noi.ac/problem/31 好题啊! 题意很明白,对于有关最小生成树(MST)的题,一般是要模拟 Kruskal 过程了: 模拟 Kruskal,也就是把给出的 n-1 条 ...
- POJ 1679 The Unique MST --Kruskal应用
这题可以用次小生成树解,这里用Kruskal算法来做.每条边除维护u,v,w外,还维护: used:表示这条边是否加过 eq:表示有没有与这条边相等的边 del:删除标记,以便删边之用 如果对于一个最 ...
- 最小生成树-克鲁斯卡尔算法(kruskal's algorithm)实现
算法描述 克鲁斯卡尔算法是一种贪心算法,因为它每一步都挑选当前最轻的边而并不知道全局路径的情况. 算法最关键的一个步骤是要判断要加入mst的顶点是否会形成回路,我们可以利用并查集的技术来做. 并查集的 ...
- 最小生成树 (Minimum Spanning Tree,MST) --- Kruskal算法
本文链接:http://www.cnblogs.com/Ash-ly/p/5409265.html 引导问题: 假设要在N个城市之间建立通信联络网,则连通N个城市只需要N - 1条线路.这时,自然会考 ...
- 贪心算法(Greedy Algorithm)之最小生成树 克鲁斯卡尔算法(Kruskal's algorithm)
克鲁斯卡尔算法(Kruskal's algorithm)是两个经典的最小生成树算法的较为简单理解的一个.这里面充分体现了贪心算法的精髓.大致的流程能够用一个图来表示.这里的图的选择借用了Wikiped ...
- 贪心算法(Greedy Algorithm)最小生成树 克鲁斯卡尔算法(Kruskal's algorithm)
克鲁斯卡尔算法(Kruskal's algorithm)它既是古典最低的一个简单的了解生成树算法. 这充分反映了这一点贪心算法的精髓.该方法可以通常的图被表示.图选择这里借用Wikipedia在.非常 ...
随机推荐
- python3练习100题——027
又是一道迭代的题,没做好. 看了答案才试着写出来. 我一定要加油啊,为了尽快摆脱现在讨厌的生活! 原题链接:http://www.runoob.com/python/python-exercise-e ...
- 【牛客小白月赛21】NC201605 Bits
[牛客小白月赛21]NC201605 Bits 题目链接 题目描述 Nancy喜欢做游戏! 汉诺塔是一个神奇的游戏,神奇在哪里呢? 给出3根柱子,最开始时n个盘子按照大小被置于最左的柱子. 如果盘子数 ...
- JS高级---总结apply和call方法的使用
apply和call的使用方法 apply的使用语法 函数名字.apply(对象,[参数1,参数2,...]); 方法名字.apply(对象,[参数1,参数2,...]); call的使用语法 ...
- spring(二):体系结构&核心模块
Spring框架 帮助管理对象及其依赖关系 提供如通用日志记录.性能统计.安全控制.异常处理等面向切面的能力 帮助管理数据库事务,提供了一套简单的JDBC访问实现,提供与第三方数据访问框架集成(如Hi ...
- 美化git commit历史
为什么要美化commit历史? 答:假如一个分支的多次意义相近的 commit,会把整个提交历史搞得很混乱, 此时可以将几个commit 合并为一个 commit,以美化整个 commit 历史. 怎 ...
- 路飞-后台xadmin配置
xadmin后台管理 安装:luffy虚拟环境下 # >: pip install https://codeload.github.com/sshwsfc/xadmin/zip/django2 ...
- mysql学习笔记(1)
以下笔记并不系统,只是针对遇到的问题和特别的点记录一下: 数据类型: 1.mysql小数存储数据类型 有float double decimal ,前两个不属于精确类型,不推荐使用,一般生产库亦不会使 ...
- HDU多校第三场 Hdu6606 Distribution of books 线段树优化DP
Hdu6606 Distribution of books 题意 把一段连续的数字分成k段,不能有空段且段和段之间不能有间隔,但是可以舍去一部分后缀数字,求\(min(max((\sum ai ))\ ...
- LED Decorative Light Supplier - Decorative Use Of LED Light Strips
Led strip refers to the led assembly in the ribbon of the FPC (flexible circuit board) or PCB hard b ...
- 每天进步一点点------Allegro 原理图到PCB网表导入