//assume that G has at least one vertex.
TV={0};//start with 0 and no edges.
for (T=o;T contain fewer than n-1 edges;add(u,v) to T){
let (u,V) be a least-cost edge such that U anaclitic to TV and v not anaclitic to TV;
if(there is no such edge)break;
add v to TV;
}
if (T contains fewer than n-1 edges) cout<<"no spanning tree"<<endl;
1 function Kruskal(G)
2 for each vertex v in G do
3 Define an elementary cluster C(v) ← {v}.
4 Initialize a priority queue Q to contain all edges in G, using the weights as keys.
5 Define a tree T ← Ø //T will ultimately contain the edges of the MST
6 // n is total number of vertices
7 while T has fewer than n-1 edges do
8 // edge u,v is the minimum weighted route from/to v
9 (u,v) ← Q.removeMin()
10 // prevent cycles in T. add u,v only if T does not already contain an edge consisting of u and v.
// Note that the cluster contains more than one vertex only if an edge containing a pair of
// the vertices has been added to the tree.
12 Let C(v) be the cluster containing v, and let C(u) be the cluster containing u.
13 if C(v) ≠ C(u) then
14 Add edge (v,u) to T.
15 Merge C(v) and C(u) into one cluster, that is, union C(v) and C(u).
16 return tree T
void kruskal()
{
int i, m, V, E;
struct edge
{ char v1, v2; int w; };
struct edge e[maxE];
PQ pq(maxE); EQ eq(maxE);
cin >> V >> E;
for (i = 1; i <= E; i++)
cin >> e[i].v1 >> e[i].v2 >> e[i].w;
for (i = 1; i <= E; i++)
pq.insert(i, e[i].w);
for (i = 0; i < V-1; )
{
if (pq.empty()) break; else m = pq.remove();
if (eq.find(index(e[m].v1),index(e[m].v2),1))
{ cout << e[m].v1 << e[m].v2 << ' '; i++; };
}
}