C++ 图 Kruskal最小生成树算法

14 篇文章 0 订阅
12 篇文章 0 订阅
本文详细介绍了使用C++编程语言实现Kruskal算法来寻找图的最小生成树的过程,重点在于权值排序和避免形成闭环的判断策略。
摘要由CSDN通过智能技术生成

权值排序,依次取出,判断闭环


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cassert>
#include <vector>
#include <ctime>
#include <queue>
#include <iomanip>
#include <stack>
using namespace std;

template<typename Weight>
class Edge {
private:
	int a, b;
	Weight weight;
public:
	Edge(int a, int b, Weight weight) {
		this->a = a;
		this->b = b;
		this->weight = weight;
	}
	Edge() {}
	~Edge() {}
	int v() { return a; }
	int w() { return b; }
	Weight wt() { return weight; }

	int other(int x) {
		assert(x == a || x == b);
		return x == a ? b : a;
	}
	bool operator<(Edge<Weight>& e) {
		return weight < e.wt();
	}
	bool operator<=(Edge<Weight>& e) {
		return weight <= e.wt();
	}
	bool operator>(Edge<Weight>& e) {
		return weight > e.wt();
	}
	bool operator>=(Edge<Weight>& e) {
		return weight >= e.wt();
	}
	bool operator==(Edge<Weight>& e) {
		return weight == e.wt();
	}

	friend ostream& operator<<(ostream &os, const Edge &e) {
		os << e.a << "-" << e.b << ": " << e.weight;
		return os;
	}
};


// 稀疏图 - 邻接表
template<typename Weight>
class SparseGraph {
private:
	int n, m;
	bool directed;
	vector<vector<Edge<Weight> *>> g;
public:
	SparseGraph(int n, bool directed) {
		this->n = n;
		this->m = 0;
		this->directed = directed;
		for (int i = 0; i < n; i++)
			g.push_back(vector<Edge<Weight> *>());
	}
	~SparseGraph() {
		for (int i = 0; i < n; i++)
			for (int j = 0; j < g[i].size(); j++)
				delete g[i][j];
	}
	int V() { return n; }
	int E() { return m; }
	void addEdge(int v, int w, Weight weight) {
		assert(v >= 0 && v < n);
		assert(w >= 0 && w < n);
		g[v].push_back(new Edge<Weight>(v, w, weight));
		if (v != w && !directed)
			g[w].push_back(new Edge<Weight>(w, v, weight));
		m++;
	}
	bool hasEdge(int v, int w) {
		assert(v >= 0 && v < n);
		assert(w >= 0 && w < n);
		for (int i = 0; i < g[v].size(); i++)
			if (g[v][i]->other(v) == w)
				return true;
		return false;
	}
	void show() {
		for (int i = 0; i < n; i++) {
			cout << "vertex " << i << ":\t";
			for (int j = 0; j < g[i].size(); j++)
				cout << "( to:" << g[i][j]->w() << ",wt:" << g[i][j]->wt() << ")\t";
			cout << endl;
		}
	}
	class adjIterator {
	private:
		SparseGraph &G;
		int v;
		int index;
	public:
		adjIterator(SparseGraph &graph, int v) : G(graph) {
			this->v = v;
			this->index = 0;
		}
		Edge<Weight>* begin() {
			index = 0;
			if (G.g[v].size())
				return G.g[v][index];
			return NULL;
		}
		Edge<Weight>* next() {
			index += 1;
			if (index < G.g[v].size())
				return G.g[v][index];
			return NULL;
		}
		bool end() {
			return index >= G.g[v].size();
		}
	};
};


template <typename Graph, typename Weight>
class ReadGraph {

public:
	ReadGraph(Graph &graph, const string &filename) {

		ifstream file(filename);
		string line;
		int V, E;

		assert(file.is_open());
		//cout<<"open "<<filename<<" successfully."<<endl;

		assert(getline(file, line));
		stringstream ss(line);
		ss >> V >> E;
		assert(graph.V() == V);
		//cout<<"V = "<<V<<" , E = "<<E<<" , in "<<filename<<"."<<endl;

		for (int i = 0; i < E; i++) {
			//cout<<"read line "<<i<<endl;
			assert(getline(file, line));
			stringstream ss(line);

			int a, b;
			Weight w;
			ss >> a >> b >> w;
			assert(a >= 0 && a < V);
			assert(b >= 0 && b < V);
			graph.addEdge(a, b, w);
		}

		//cout<<"read "<<filename<<" successfully."<<endl;
	}
};
// 并查集
class UnionFind {

private:
	int* parent;
	int* rank;
	int count;
public:
	UnionFind(int count) {
		parent = new int[count];
		rank = new int[count];
		this->count = count;
		for (int i = 0; i < count; i++) {
			parent[i] = i;
			rank[i] = 1;
		}
	}
	~UnionFind() {
		delete[] parent;
		delete[] rank;
	}
	int size() {
		return count;
	}
	bool isConnected(int p, int q) {
		return find(p) == find(q);
	}
	int find(int p) {
		assert(p >= 0 && p < count);
		while (p != parent[p]) {
			parent[p] = parent[parent[p]];
			p = parent[p];
		}
		return p;
	}
	void unionElements(int p, int q) {
		int pRoot = find(p);
		int qRoot = find(q);
		if (pRoot == qRoot)
			return;
		if (rank[pRoot] < rank[qRoot])
			parent[pRoot] = qRoot;
		else if (rank[qRoot] < rank[pRoot])
			parent[qRoot] = pRoot;
		else { // rank[pRoot] == rank[qRoot]
			parent[pRoot] = qRoot;
			rank[qRoot] ++;
		}
	}
	void show() {
		for (int i = 0; i < count; i++)
			cout << i << " : " << parent[i] << endl;
	}
};

template<typename Item>
class MinHeap {

private:
	Item *data;
	int count;
	int capacity;

	void shiftUp(int k) {
		while (k > 1 && data[k / 2] > data[k]) {
			swap(data[k / 2], data[k]);
			k /= 2;
		}
	}

	void shiftDown(int k) {
		while (2 * k <= count) {
			int j = 2 * k;
			if (j + 1 <= count && data[j + 1] < data[j]) j++;
			if (data[k] <= data[j]) break;
			swap(data[k], data[j]);
			k = j;
		}
	}

public:

	MinHeap(int capacity) {
		data = new Item[capacity + 1];
		count = 0;
		this->capacity = capacity;
	}

	MinHeap(Item arr[], int n) {
		data = new Item[n + 1];
		capacity = n;

		for (int i = 0; i < n; i++)
			data[i + 1] = arr[i];
		count = n;

		for (int i = count / 2; i >= 1; i--)
			shiftDown(i);
	}

	~MinHeap() {
		delete[] data;
	}

	int size() {
		return count;
	}

	bool isEmpty() {
		return count == 0;
	}

	void insert(Item item) {
		assert(count + 1 <= capacity);
		data[count + 1] = item;
		shiftUp(count + 1);
		count++;
	}

	Item extractMin() {
		assert(count > 0);
		Item ret = data[1];
		swap(data[1], data[count]);
		count--;
		shiftDown(1);
		return ret;
	}

	Item getMin() {
		assert(count > 0);
		return data[1];
	}

	void show() {
		cout << "| ";
		for (int i = 1; i <= count; i++)
			cout << data[i]->wt()/*<<","<<data[i]*/ << " | ";
		cout << endl;
	}
};
//Kruskal算法
template <typename Graph, typename Weight>
class KruskalMST {

private:
	vector<Edge<Weight>> mst;
	Weight mstWeight;

public:
	KruskalMST(Graph &graph) {
		MinHeap<Edge<Weight>> pq(graph.E());
		for (int i = 0; i < graph.V(); i++) {
			typename Graph::adjIterator adj(graph, i);
			for (Edge<Weight> *e = adj.begin(); !adj.end(); e = adj.next())
				if (e->v() < e->w())
					pq.insert(*e);
		}
		UnionFind uf = UnionFind(graph.V());
		while (!pq.isEmpty() && mst.size() < graph.V() - 1) {
			Edge<Weight> e = pq.extractMin();
			if (uf.isConnected(e.v(), e.w()))
				continue;
			mst.push_back(e);
			uf.unionElements(e.v(), e.w());
		}
		mstWeight = mst[0].wt();
		for (int i = 1; i < mst.size(); i++)
			mstWeight += mst[i].wt();
	}
	~KruskalMST() {	}
	vector<Edge<Weight>> mstEdges() {
		return mst;
	};
	Weight result() {
		return mstWeight;
	};
};

int main(void)
{
	string filename = "testG1.txt";
	int V = 8;

	SparseGraph<double> g = SparseGraph<double>(V, false);
	ReadGraph<SparseGraph<double>, double> readGraph(g, filename);

	cout << "Test Kruskal MST:" << endl;
	KruskalMST<SparseGraph<double>, double> kruskalMST(g);
	vector<Edge<double>> mst = kruskalMST.mstEdges();
	for (int i = 0; i < mst.size(); i++)
		cout << mst[i] << endl;
	cout << "The MST weight is: " << kruskalMST.result() << endl;

	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值