The Kerim algorithm, which we talked about earlier, is to build a minimum spanning tree by looking for the edges of the smallest weights on each vertex at the beginning of a vertex. The same idea, we can also directly to the edge as the goal to build, because the weight of the edge, directly to find the minimum weight of the edge to build a spanning tree is also a natural idea, but the construction to consider whether it will form a loop, at this time we use the graph of the storage structure of the edge set array structure, such as Figure 7-6-7
Let's say now that we've got the edge set array edges by the adjacency matrix and the weighted values from small to large as shown above.
Here we look at the program and the diagram of each step loop:
Algorithm code:
typedef struct {int begin;
int end;
int weight;
} Edge;
/* Find line Vertex's tail subscript * * * (int *parent, int f) {while (Parent[f] > 0) f = parent[f];
return F;
*/* Generate minimum spanning tree/void Minispantree_kruskal (Mgraph G) {int I, j, N, M;
int k = 0;
int parent[maxvex];/* defines an array to determine whether the edge and edge form a loop/edge edges[maxedge];/* define an array of edges, the edge of the structure is begin,end,weight, are integral type * *
/* Here is omitted to convert the adjacency matrix G to an array of edges and the code by weight from small to large order (i = 0; i < g.numvertexes; i++) parent[i] = 0;
cout << "Print minimum spanning tree:" << endl;
for (i = 0; i < g.numedges i++)/* Loop each edge * * {n = find (parent, edges[i].begin);
M = Find (parent, edges[i].end); if (n!= m)/* If n differs from M, this side does not form a loop with the existing spanning tree/{parent[n] = m;/* The end vertex of this edge into parent with subscript starting point. */* Indicates that the vertex is already in the Spanning tree collection/* cout << "(" << edges[i].begin << "," << Edge S[i].end << ")" <<Edges[i].weight << Endl; }
}
}