Algorithmic Solutions > LEDA > LEDA Guide > Graphs and Related Data Types > Node Partitions > Example
Example Node PartitionsThe following program implements Kruskals algorithm for computing a minimum spanning tree of a weighted graph. The The computation of the minimum spanning tree in
#include <LEDA/graph/graph.h>
#include <LEDA/graph/node_partition.h>
#include <LEDA/core/random_source.h>
#include <LEDA/core/p_queue.h>
using namespace leda;
list<edge> MST(const graph& G,const edge_array<int>& cost)
{
list<edge> T; //the minimum spanning tree
p_queue<int,edge> PQ;
edge e;
forall_edges(e,G) PQ.insert(cost[e],e);
//sort edges according to cost
node_partition P(G);
while (!PQ.empty()) {
e=PQ.inf(PQ.find_min()); //get edge with minimum cost
PQ.del_min();
node u=G.source(e);
node v=G.target(e);
if (!P.same_block(u,v)) {
T.append(e);
P.union_blocks(u,v);
}
}
return T;
}
int main()
{
graph G;
complete_graph(G,5);
random_source S;
edge_array<int> cost(G);
edge e;forall_edges(e,G) S>>cost[e];
list<edge> tree=MST(G,cost);
forall(e,tree) {
G.print_edge(e);
std::cout << std::endl;
}
return 0;
}
|
See also:Manual Entries: |