昨天写了最小生成树的Prim算法,今天也就到了Kruskal算法了。一个对顶点进行操作,一个对边进行操作。当边稀疏的时候,Kruskal算法还是很牛的。时间复杂度:O(Elog2E)。(感觉更像是一个并查集的使用。http://zh.wikipedia.org/wiki/%E5%B9%B6%E6%9F%A5%E9%9B%86)
关于科普,还是复制Wiki上面的了,哈哈。
关于Kruskal算法的步骤:
步骤
- 新建图G,G中拥有原图中相同的节点,但没有边
- 将原图中所有的边按权值从小到大排序
- 从权值最小的边开始,如果这条边连接的两个节点于图G中不在同一个连通分量中,则添加这条边到图G中
- 重复3,直至图G中所有的节点都在同一个连通分量中
关于Kruskal算法的证明:
证明
- 这样的步骤保证了选取的每条边都是桥,因此图G构成一个树。
- 为什么这一定是最小生成树呢?关键还是步骤3中对边的选取。算法中总共选取了n-1条边,每条边在选取的当时,都是连接两个不同的连通分量的权值最小的边
- 要证明这条边一定属于最小生成树,可以用反证法:如果这条边不在最小生成树中,它连接的两个连通分量最终还是要连起来的,通过其他的连法,那么另一种连法与这条边一定构成了环,而环中一定有一条权值大于这条边的边,用这条边将其替换掉,图仍旧保持连通,但总权值减小了。也就是说,如果不选取这条边,最后构成的生成树的总权值一定不会是最小的。
关于Kruskal算法的伪代码:
算法
KRUSKAL-FUNCTION(G, w)1 F := 空集合
2 for each 圖 G 中的頂點 v
3 do 將 v 加入森林 F
4 所有的邊(u, v) ∈ E依權重 w 遞增排序
5 for each 邊(u, v) ∈ E
6 do if u 和 v 不在同一棵子樹
7 then F := F ∪ {(u, v)}
8 將 u 和 v 所在的子樹合併
代码如下:
#include <iostream>
#include <queue>
#define MAX_VERTEX 10
using namespace std;
struct Edge
{
int source;
int destination;
int cost;
};
bool operator < (const Edge &a, const Edge &b)
{
return a.cost > b.cost;
}
class Graph
{
public:
int allCost;
int vertexNum;
int edgeNum;
void kruskal();
private:
int vertexRoot[MAX_VERTEX]; //Save the root of the tree the node belong to.
//Sometimes it just save node's father.
priority_queue<Edge> kruskalQueue; //Save all edges.
void getData();
void initialize();
void rootReset(); //Reset all root of each vertex.
void refreshRoot(int node, int root); //Update the root of the node and its ancestors.
int findRoot(int node); //Find the root of the tree the node belong to.
void unionTree(int nodeA, int nodeB); //Set nodeB as nodeA's father.
};
void Graph::rootReset()
{
for (int i = 0; i < vertexNum; i++)
{
vertexRoot[i] = i;
}
}
void Graph::initialize()
{
allCost = 0;
vertexNum = 0;
edgeNum = 0;
while (!kruskalQueue.empty())
{
kruskalQueue.pop();
}
getData();
rootReset();
}
void Graph::getData()
{
cout << "Please input the number of vertex(no more than " << MAX_VERTEX << "): ";
cin >> vertexNum;
cout << "Please input the number of edges: ";
cin >> edgeNum;
Edge inputEdge;
cout << "Please input the edges.(Source, Destination, Cost)" << endl;
for (int i = 0; i < edgeNum; i++)
{
cin >> inputEdge.source >> inputEdge.destination >> inputEdge.cost;
kruskalQueue.push(inputEdge);
swap(inputEdge.source, inputEdge.destination);
kruskalQueue.push(inputEdge);
}
}
void Graph::refreshRoot(int node, int root)
{
int father;
while (root != vertexRoot[node])
{
father = vertexRoot[node];
vertexRoot[node] = root;
node = father;
}
}
int Graph::findRoot(int node)
{
int rootNode = node;
while (vertexRoot[rootNode] != rootNode)
{
rootNode = vertexRoot[rootNode];
}
refreshRoot(node, rootNode);
return rootNode;
}
void Graph::unionTree(int nodeA, int nodeB)
{
vertexRoot[vertexRoot[nodeA]] = vertexRoot[nodeB];
}
void Graph::kruskal()
{
initialize();
int gotEdgeNum = 0;
int edgeNeeded = vertexNum - 1;
Edge currentEdge;
while (!kruskalQueue.empty() && gotEdgeNum < edgeNeeded)
{
currentEdge = kruskalQueue.top();
kruskalQueue.pop();
if (findRoot(currentEdge.source) != findRoot(currentEdge.destination))
{
unionTree(currentEdge.source, currentEdge.destination);
allCost += currentEdge.cost;
gotEdgeNum++;
}
}
}
int main()
{
Graph test;
test.kruskal();
cout << "The cost is: " << test.allCost << endl;
return 0;
}