kruskal的最小生成樹

     給定一個無向圖,它的生成樹是能夠連接圖中所有定點的的子圖。一個圖可以有很多個生成樹,但是一個無向,帶全圖的最小生成樹是生成樹的所有邊的權值之和小於任何一個生成樹的權值之和。

     一棵最小生成樹的邊是多少呢,設定這個圖一共有V條邊,那麼最小生成樹應該有V-1條邊。

     應用kruskal來解決最小生成樹的問題的步驟爲:

1,將圖中的所有的邊按照權值的大小進行非降序排序。

2,保留圖中的所有頂點,對於圖中的沒一條邊,按照排序後的順序進行處理,如果新加入一條邊,使得已經存在的邊在加上這條邊之後形成一個環,那麼就拋棄這條邊,否則保留這條邊。判斷是否存在環應該應用上一篇的並查集的方法來解決。

3,當已經保留或者加入了V-1條邊之後,就停止進行邊的操作。因爲到現在爲止在不形成環的條件下,已經生成了一棵最小生成樹。

下面給出一個事例:

The graph contains 9 vertices and 14 edges. So, the minimum spanning tree formed will be having (9 – 1) = 8 edges.

After sorting:
Weight   Src    Dest
1         7      6
2         8      2
2         6      5
4         0      1
4         2      5
6         8      6
7         2      3
7         7      8
8         0      7
8         1      2
9         3      4
10        5      4
11        1      7
14        3      5

Now pick all edges one by one from sorted list of edges
1. Pick edge 7-6: No cycle is formed, include it.

2. Pick edge 8-2: No cycle is formed, include it.

3. Pick edge 6-5: No cycle is formed, include it.

4. Pick edge 0-1: No cycle is formed, include it.

5. Pick edge 2-5: No cycle is formed, include it.

6. Pick edge 8-6: Since including this edge results in cycle, discard it.

7. Pick edge 2-3: No cycle is formed, include it.

8. Pick edge 7-8: Since including this edge results in cycle, discard it.

9. Pick edge 0-7: No cycle is formed, include it.

10. Pick edge 1-2: Since including this edge results in cycle, discard it.

11. Pick edge 3-4: No cycle is formed, include it.

附上代碼:

#include<iostream>
#include<string>
using namespace std;

typedef struct edge_s {
	int src;
	int dst;
	int weight;
}edge_t;

class Graph {
private:
	int vex_num;
	int arc_num;
	edge_t *arcs;
	int *parent;
	int *rank;
	static int edgeCmp(const void *a, const void *b) {
		edge_t *ea = (edge_t*)a;
		edge_t *eb = (edge_t*)b;
		return ea->weight > eb->weight;	
	}
public:
	Graph(int _vex_num, int _arc_num) {
		vex_num = _vex_num;
		arc_num = _arc_num;
		arcs = new edge_t[arc_num];
		parent = new int[vex_num];
		rank = new int[vex_num];
		for (int i = 0; i < vex_num; i++) {
			parent[i] = i;
			rank[i] = i;
		}
	}
	~Graph() {
		delete []arcs;
		delete []parent;
		delete []rank;
	}
	int getArcNum();
	int getVexNum();
	int getArcSrc(int index);
	int getArcDst(int index);
	edge_t getEdge(int index);
	int find(int x);
	void _union(int x, int y);
	void sort();
	void setEdge(int index, int src, int dst, int weight);
};

int Graph::getArcNum() {
	return arc_num;
} 

int Graph::getVexNum() {
	return vex_num;
}

void Graph::setEdge(int index, int src, int dst, int weight) {
	arcs[index].src = src;
	arcs[index].dst = dst;
	arcs[index].weight = weight;
}

int Graph::getArcSrc(int index) {
	return arcs[index].src;
}

int Graph::getArcDst(int index) {
	return arcs[index].dst;
}

edge_t Graph::getEdge(int index) {
	return arcs[index];
}

int Graph::find(int x) {
	int r = x;
	while (parent[r] != r)
		r = parent[r];
	int i = x, j;
	while (i != r) {
		j = parent[i];
		parent[i] = r;
		i = j;
	}
	return r;
}

void Graph::_union(int x, int y) {
	int fx = find(x);
	int fy = find(y);
	if (fx == fy)
		return;
	if (rank[fx] > rank[fy]) {
		parent[fy] = fx;
	} else {
		if (rank[fx] == rank[fy]) 
			rank[fy]++;
		parent[fx] = fy;
	}
}

void Graph::sort() {
	qsort(arcs, arc_num, sizeof(edge_t), edgeCmp);
}

void kruskal(Graph &g) {
	int min_arcs = g.getVexNum() - 1;
	edge_t *result = new edge_t[min_arcs];
	g.sort();
	int i = 0;
	int e = 0;
	while (e < min_arcs) {
		edge_t next = g.getEdge(i++);
		int fx = g.find(next.src);
		int fy = g.find(next.dst);
		if (fx != fy) {
			result[e++] = next;
			g._union(fx, fy);
		}
	}
	for (i = 0; i < min_arcs; i++)
		cout <<"from:" << result[i].src << " to:" << result[i].dst << " weight:" << result[i].weight << endl;
	delete []result;
}

int main(int argc, char *argv[]) {
	Graph g = Graph(4, 5);
	g.setEdge(0, 0, 1, 10);
	g.setEdge(1, 0, 2, 6);
	g.setEdge(2, 0, 3, 5);
	g.setEdge(3, 1, 3, 15);
	g.setEdge(4, 2, 3, 4);
	kruskal(g);
	cin.get();
	return 0;
}



發佈了125 篇原創文章 · 獲贊 115 · 訪問量 56萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章