Algorithm research: The Kruskal algorithm of the graph minimum spanning tree

Source: Internet
Author: User

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; }
    }
    
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.