Prim:
普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树。意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点(英语:Vertex (graph theory)),且其所有边的权值之和亦为最小。该算法于1930年由捷克数学家沃伊捷赫·亚尔尼克(英语:Vojtěch Jarník)发现;并在1957年由美国计算机科学家罗伯特·普里姆(英语:Robert C. Prim)独立发现;1959年,艾兹格·迪科斯彻再次发现了该算法。因此,在某些场合,普里姆算法又被称为DJP算法、亚尔尼克算法或普里姆-亚尔尼克算法。
算法描述
1).输入:一个加权连通图,其中顶点集合为V,边集合为E;
2).初始化:Vnew = {x},其中x为集合V中的任一节点(起始点),Enew = {},为空;
3).重复下列操作,直到Vnew = V:
a.在集合E中选取权值最小的边<u, v>,其中u为集合Vnew中的元素,而v不在Vnew集合当中,并且v∈V(如果存在有多条满足前述条件即具有相同权值的边,则可任意选取其中之一);
b.将v加入集合Vnew中,将<u, v>边加入集合Enew中;
4).输出:使用集合Vnew和Enew来描述所得到的最小生成树。
以上来源与百度百科,Prim算法通俗来说就是在V这个顶点集合中找出一个点加入到V1集合中,再从V中剩下的顶点中找出离集合V1最近的顶点加到集合V1中,这么做有一个问题是不能判断是否形成了回路,在最小生成树中不允许出现回路,此时可以通过维护两个数组,colsest[i]和lowcost[i],分别表示剩余集合中的节点i到v节点最近的节点closest和最短的距离lowcost[i]。
代码:
- #include <iostream>
- using namespace std;
- const int INF = 0x3fffffff;
- const int N = 100;
- bool s[N];
- int closest[N];
- int lowcost[N];
- void Prime(int n, int u0, int c[N][N])
- {
- int i, j;
- s[u0] = true;//访问
- for (i = 1; i <= n; i++)//初始化
- {
- if (i == u0)
- {
- lowcost[i] = 0;
- }
- else
- {
- lowcost[i] = c[u0][i];
- s[i] = false;//没被访问
- closest[i] = u0;
- }
- }
- for (i = 1; i <= n; i++)
- {
- int temp = INF;
- int t = u0;
- for (j = 1; j <= n; j++)
- {
- if ((!s[j]) && lowcost[j] < temp)
- {
- t = j;
- temp = lowcost[j];//当前最小
- }
- }
- s[t] = true;
- for (j = 1; j <= n; j++)
- {
- if ((!s[j]) && c[t][j] < lowcost[j])
- {
- lowcost[j] = c[t][j];
- closest[j] = t;//离j最近的是t
- }
- }
- }
- }
- int main()
- {
- int i, j;
- int n, c[N][N], m, u, v, w, sumcost = 0;;
- int u0;
- cin >> n >> m;
- for (i = 1; i <= n; i++)
- {
- for (j = 1; j <= n; j++)
- {
- c[i][j] = INF;
- }
- }
- for (i = 1; i <= m; i++)
- {
- cin >> u >> v >> w;
- c[u][v] = c[v][u] = w;
- }
- cin >> u0;
- Prime(n, u0, c);
- for (i = 1; i <= n; i++)
- {
- cout << lowcost[i] << " ";
- sumcost += lowcost[i];
- }
- cout << endl << sumcost << endl;
- return 0;
- }
Prim算法是基于顶点集合是最小生成树算法,时间复杂度为O(n^2),辅助空间为O(n),可以通过找集合V到V1最短边用优先队列,边判断用邻接表,可以将时间复杂度降到O(Elogn);
另一个最小生成树算法,Kruskal算法是基于边集的最小生成树算法
1.概览
Kruskal算法是一种用来寻找最小生成树的算法,由Joseph Kruskal在1956年发表。用来解决同样问题的还有Prim算法和Boruvka算法等。三种算法都是贪婪算法的应用。和Boruvka算法不同的地方是,Kruskal算法在图中存在相同权值的边时也有效。
2.算法简单描述
1).记Graph中有v个顶点,e个边
2).新建图Graphnew,Graphnew中拥有原图中相同的e个顶点,但没有边
3).将原图Graph中所有e个边按权值从小到大排序
4).循环:从权值最小的边开始遍历每条边 直至图Graph中所有的节点都在同一个连通分量中(同一集合中算法结束)
通俗来讲,该算法是找权值最小的边,如果权值最小的边的两个顶点不再同一个集合,就将该边的权值累加到最终结果中去,并且将两个顶点合并成一个集合,算法过程就是找最小->判断->合并->找最小->判断->合并。。。。直到所有顶点合并成一个集合算法结束。该算法的时间复杂度为O(n^2),在合并的时候产生的,可以通过用并查集将时间复杂度降到O(Elogn)
代码:
- #include <iostream>
- #include <algorithm>
- using namespace std;
- const int N = 100;
- int n, m;
- int nodes[N];//节点数组
- struct Edge
- {
- int u;
- int v;
- int w;
- }e[N*N];
- bool cmp(Edge x, Edge y)
- {
- return x.w < y.w;
- }
- void init(int n)
- {
- for (int i = 1; i <= n; i++)
- {
- nodes[i] = i;
- }
- }
- int Merge(int a, int b)
- {
- int p = nodes[a];
- int q = nodes[b];
- if (p == q)
- {
- return 0;
- }
- for (int i = 1; i <= n; i++)
- {
- if (nodes[i] == p)
- {
- nodes[i] = q;
- }
- }
- return 1;
- }
- int Kruskal(int n)
- {
- int i,ans = 0;
- for (i = 1;i <= m; i++)
- {
- if (Merge(e[i].u, e[i].v))//可以合并
- {
- ans += e[i].w;
- n--;
- if (n == 1)
- {
- return ans;
- }
- }
- }
- return 0;
- }
- int main()
- {
- int i;
- cin >> n >> m;
- init(n);
- for (i = 1; i <= m; i++)
- {
- cin >> e[i].u >> e[i].v >> e[i].w;
- }
- sort(e, e + m, cmp);
- int ans = Kuskal(n);
- cout << ans << endl;
- return 0;
- }
并查集优化后的代码:
- #include <iostream>
- #include <algorithm>
- using namespace std;
- #define N 100
- int father[N];
- int n, m;//顶点数和边数
- struct Edge{
- int u, v, w;
- }e[N*N];
- void init(int n)
- {
- for (int i = 1; i <= n; i++)
- {
- father[i] = i;
- }
- }
- int Find(int x)
- {
- if (x != father[x])
- {
- father[x] = Find(father[x]);
- }
- return father[x];
- }
- int Merge(int u, int v)
- {
- int p = Find(u);
- int q = Find(v);
- if (p == q)return 0;
- if (p > q)
- {
- father[p] = q;
- }
- else
- {
- father[q] = p;
- }
- }
- bool cmp(Edge x, Edge y)
- {
- return x.w < y.w;
- }
- int Kruskal(int n)
- {
- int i, ans = 0;
- for (i = 1; i <= m; i++)
- {
- if (Merge(e[i].u, e[i].v))
- {
- ans += e[i].w;
- n--;
- if (n == 1)return ans;
- }
- }
- return 0;
- }
- int main()
- {
- int i;
- cin >> n >> m;
- init(n);
- for (i = 1; i <= m; i++)
- {
- cin >> e[i].u >> e[i].v >> e[i].w;
- }
- sort(e, e + m, cmp);
- int ans = Krukal(n);
- cout << ans << endl;
- return 0;
- }