Question: CONVERT FOLLOWING INTO C++ CODE 1) Algoirthm minimumSpanningTreeKruskal(G) G - A weighted , undirected graph. minST = an undirected, weighted graph with the same node
CONVERT FOLLOWING INTO C++ CODE
1)
Algoirthm minimumSpanningTreeKruskal(G) G - A weighted , undirected graph.
minST = an undirected, weighted graph with the same node set as G, but no edges.
UF = a union-find data structure with the node set of G in which each node is initially in its own subset.
Sort the edges of G in order from smallest to largest weight.
for each edge e=(a,b) in sorted order if UF.find(a) != UF.find(b)
minST.addEdge(a,b) set the weight of (a,b) in minST to the weight of (a,b) in G UF.union(a,b)
return minST
2)
Algorithm union(a, b) a, b - elements whose subsets are to be merged
// If a and b are already in the same set, do nothing.
if find(a) == find(b) return
// Otherwise , merge the sets
add the edge (find(a), find(b)) to the union-find graph.
3)
Algorithm find(a) a - element for which we want to determine set membership
// Follow the chain of directed edges starting from a
x=a while x has an outgoing edge (x,y) in the union-find graph
x=y
// Since at this point x has no outgoing edge, it must be the // representative element of the set to which a belongs, so
return x
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
