Geeks : Kruskal’s Minimum Spanning Tree Algorithm 最小生成树

寻找图中最小连通的路径,图如下:

算法步骤:

1. Sort all the edges in non-decreasing order of their weight.

2. Pick the smallest edge. Check if it forms a cycle with the spanning tree
formed so far. If cycle is not formed, include this edge. Else, discard it.  

3. Repeat step#2 until there are (V-1) edges in the spanning tree.

关键是第二步难,这里使用Union Find来解决,可以几乎小于O(lgn)的时间效率来判断是否需要判断的顶点和已经选择的顶点成环。

正因为这步,使得原本简单的贪心法,变得不那么简单了。

这样本算法的时间效率达到:max(O(ElogE) , O(ElogV))

原文参考:http://www.geeksforgeeks.org/greedy-algorithms-set-2-kruskals-minimum-spanning-tree-mst/

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

class KruskalsMST
{
	struct Edge
	{
		int src, des, weight;
	};

	static int cmp(const void *a, const void *b)
	{
		Edge *a1 = (Edge *) a, *b1 = (Edge *) b;
		return a1->weight - b1->weight;
	}

	struct Graph
	{
		int V, E;
		Edge *edges;
		Graph(int v, int e) : V(v), E(e)
		{
			edges = new Edge[e];
		}
		virtual ~Graph()
		{
			if (edges) delete [] edges;
		}
	};

	struct SubSet
	{
		int parent, rank;
	};

	int find(SubSet *subs, int i)
	{
		if (subs[i].parent != i)
			subs[i].parent = find(subs, subs[i].parent);
		return subs[i].parent;
	}

	void UnionTwo(SubSet *subs, int x, int y)
	{
		int xroot = find(subs, x);
		int yroot = find(subs, y);
		if (subs[xroot].rank < subs[yroot].rank)
			subs[xroot].parent = yroot;
		else if (subs[xroot].rank > subs[yroot].rank)
			subs[yroot].parent = xroot;
		else
		{
			subs[xroot].rank++;
			subs[yroot].parent = xroot;
		}
	}

	Graph *graph;
	Edge *res;
	SubSet *subs;

	void initSubSet()
	{
		subs = new SubSet[graph->V];
		for (int i = 0; i < graph->V; i++)
		{
			subs[i].parent = i;
			subs[i].rank = 0;
		}
	}

	void mst()
	{
		res = new Edge[graph->V-1];

		qsort(graph->edges, graph->E, sizeof(graph->edges[0]), cmp);

		initSubSet();

		for (int e = 0, i = 0; e < graph->V - 1 && i < graph->E; i++)
		{
			Edge nextEdge = graph->edges[i];
			int x = find(subs, nextEdge.src);
			int y = find(subs, nextEdge.des);
			if (x != y)
			{
				res[e++] = nextEdge;
				UnionTwo(subs, x, y);
			}
		}
	}

	void printResult()
	{
		printf("Following are the edges in the constructed MST\n");
		for (int i = 0; i < graph->V-1; ++i)
			printf("%d -- %d == %d\n", res[i].src, res[i].des, res[i].weight);
	}
public:
	KruskalsMST()
	{
		/* Let us create following weighted graph
		10
		0--------1
		|  \     |
		6|   5\   |15
		|      \ |
		2--------3
		4       */
		int V = 4;  // Number of vertices in graph
		int E = 5;  // Number of edges in graph
		graph = new Graph(V, E);

		// add edge 0-1
		graph->edges[0].src = 0;
		graph->edges[0].des = 1;
		graph->edges[0].weight = 10;

		// add edges 0-2
		graph->edges[1].src = 0;
		graph->edges[1].des = 2;
		graph->edges[1].weight = 6;

		// add edges 0-3
		graph->edges[2].src = 0;
		graph->edges[2].des = 3;
		graph->edges[2].weight = 5;

		// add edges 1-3
		graph->edges[3].src = 1;
		graph->edges[3].des = 3;
		graph->edges[3].weight = 15;

		// add edges 2-3
		graph->edges[4].src = 2;
		graph->edges[4].des = 3;
		graph->edges[4].weight = 4;

		mst();
		printResult();
	}
	~KruskalsMST()
	{
		if (res) delete [] res;
		if (subs) delete [] subs;
		if (graph) delete graph;
	}
};

Geeks : Kruskal’s Minimum Spanning Tree Algorithm 最小生成树

时间: 2024-10-29 19:05:46

Geeks : Kruskal’s Minimum Spanning Tree Algorithm 最小生成树的相关文章

【HDU 4408】Minimum Spanning Tree(最小生成树计数)

Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Kruskal algorithm of minimum spanning tree, XXX finds that there might be multiple solutions. Given an undirected weighted graph with n (1<=n<=100) vertex

UVALive 3662 Another Minimum Spanning Tree 曼哈顿最小生成树

题目链接:点击打开链接 题意: 给定二维平面的n个点坐标,问曼哈顿MST 的值. 模版题 #include <stdio.h> #include <iostream> #include <algorithm> #include <sstream> #include <stdlib.h> #include <string.h> #include <limits.h> #include <vector> #incl

HDU 4408 Minimum Spanning Tree 最小生成树计数

Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Problem Description] XXX is very interested in algorithm. After learning the Prim algorithm and Kruskal algorithm of minimum spanning tree, XXX

【算法】关于图论中的最小生成树(Minimum Spanning Tree)详解

喜欢的话可以扫码关注我们的公众号哦,更多精彩尽在微信公众号[程序猿声] 本节纲要 什么是图(network) 什么是最小生成树 (minimum spanning tree) 最小生成树的算法 什么是图(network)? 这里的图当然不是我们日常说的图片或者地图.通常情况下,我们把图看成是一种由"顶点"和"边"组成的抽象网络.在各个"顶点"间可以由"边"连接起来,使两个顶点间相互关联起来.图的结构可以描述多种复杂的数据对象,

HDOJ 题目4408 Minimum Spanning Tree(Kruskal+Matrix_Tree)

Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1408    Accepted Submission(s): 450 Problem Description XXX is very interested in algorithm. After learning the Prim algori

CF609E. Minimum spanning tree for each edge

题解:随便构造一颗最小生成树 然后对于其他不在树上的边  考虑到 删除这条链上的最大值在把这条边加上去 能得到这条边所在的最小生成树 可以LCT维护 但是明显这个题是静态的树就没必要LCT 当然我觉得最优的是树剖以后ST nlogn的的复杂度 也可以树剖+线段树nlog^2的复杂度 #include <bits/stdc++.h> const int MAXN=2e5+10; #define ll long long using namespace std; ll read(){ ll x=0

[思维]Minimum Spanning Tree

题目描述 In the mathematical discipline of graph theory, the line graph of a simple undirected weighted graph G is another simple undirected weighted graph L(G) that represents the adjacency between every two edges in G. Precisely speaking, for an undire

Minimum spanning tree for each edge

Connected undirected weighted graph without self-loops and multiple edges is given. Graph contains n vertices and m edges. For each edge (u,?v) find the minimal possible weight of the spanning tree that contains the edge (u,?v). The weight of the spa

最小生成树 (Minimum Spanning Tree,MST) ---Kruskal算法

引导问题: 假设要在N个城市之间建立通信联络网,则连通N个城市只需要N - 1条线路.这时,自然会考虑这样一个问题,如何在最省经费的前提下建立这个通信网. 基于问题所建立的定义: 可以用联通网来表示N个城市以及N个城市之间可能设置的连通线路,其中网的顶点表示城市,边表示两城市之间的线路,赋予边的权值表示相应的代价.对于N个顶点的连通网可以建立许多不同的生成树,每一棵生成树都可以是一个通信网.现在,要选择这样一颗生成树,也就是使总的耗费最少,这个问题就是构造连通网的的最小代价生成树的问题,即最小生