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

Source: Internet
Author: User
Tags min

As we have said in the definition of graphs, a graph with a power value is a net structure. The spanning tree of a connected graph is a minimal connected subgraph, which contains all the vertices of the graph, but only enough to form a n-1 edge of a tree. The so-called minimum cost is n vertices, connecting a connected graph with the edge of the n-1, and minimizing the weight value. By combining the above two concepts, we can conclude that the minimum cost spanning tree of the constructed connected network is the minimum spanning tree (Minimum spanning).

To find the minimum spanning tree of connected graphs, there are two classical algorithms, Primm algorithm and Kruskal algorithm, and the Kerim algorithm is introduced here.

In order to be able to understand this algorithm, we first construct the adjacency matrix of the network graph, as shown in the right figure of figure 7-6-3.

That is, now we have an MG with a storage structure of mgraph (see "Adjacency Matrix Creation Diagram"). MG has 9 vertices, and its two-dimensional array is shown in the right figure, and we use 65535 to represent infinity in the array.

Here we look at the program and the diagram of each step loop:

Algorithm code:

The 
/* Prim algorithm generates a minimum spanning tree/void Minispantree_prim (Mgraph MG) {int min, I, J, K;
    int adjvex[maxvex];/* Save the relevant vertex subscript/int lowcost[maxvex];/* The weight of the edge between the related vertices/lowcost[0] = 0;/* Initialize the first weight value is 0, that is, v0 join the Spanning tree * *
    /* Lowcost value is 0, here is the vertex of the subscript has been added to the spanning tree/adjvex[0] = 0;/* initialization of the first vertex subscript 0 */cout << "The edge of the minimum spanning tree is:" << Endl; for (i = 1; i < mg.numvertexes; i++) {Lowcost[i] = mg.arc[0][i];/* to an array of v0 vertex and its side weights * * adjvex[  I] = 0;/* initialization is V0/} for (i = 1; i < mg.numvertexes; i++) {min = INFINITY;/* Initialization minimum weight is ∞.
        * * j = 1;
    
        k = 0; while (J < mg.numvertexes)/* loop all vertices/{if (Lowcost[j]!= 0 && lowcost[j] < min)/* If the weight value
            is not 0 and the weight is less than min/{min = lowcost[j];/* to make the current weight of the minimum value/k = j;/* The current minimum value of the subscript into K */
        } j + +; } cout << "(" << Adjvex[k] << "," << K <&Lt ")" << ";  /* Print the edge of the current vertex edge of the minimum weight/lowcost[k] = 0;/* Set the weight of the current vertex to 0, indicating that this vertex has completed the task */for (j = 1; j < mg.numvertexes; J + +)/* loop all vertices/* * If subscript k vertex values are less than these vertices were not added to generate tree weights/if (Lowcost[j]!= 0 && MG.
                ARC[K][J] < Lowcost[j] {lowcost[j] = mg.arc[k][j];/* deposit lesser weights in lowcost corresponding positions * *
ADJVEX[J] = k;/* the vertex of subscript k into Adjvex/}}} cout << 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.