Minimum Spanning Tree

Source: Internet
Author: User

If a communication network is to be established between n cities, only n-1 lines are required for interconnection between n cities. This means that we will naturally consider how to establish this communication network with the greatest cost savings, this is the classic minimal spanning tree problem.

There are multiple algorithms for constructing the minimal spanning tree, most of which use the MST attribute of the minimal spanning tree. Here we use the prim algorithm as an example.


MST nature:

N = {V, {E} is a set of Connected Networks, V vertex sets, and E edge,

TE is the set of edges in the Minimum Spanning Tree on N.

Starting from U = {u0} (U0 belongs to V) and TE = {}, repeat the following errors:

In all u belongs to V, v belongs to the edge of the V-U (u, v) belongs to E find a minimum cost side (u0, v0) merged into the set TE, while v0 merged into U, until U = V. At this time, TE must have n-1 edges, and T = {U, {TE} is the minimum spanning tree.


In this example, the minimal spanning tree is constructed using the undirected network represented by the array method. The network algorithm represented by the adjacent representation is similar to this one.

First, construct an undirected network, and then use the undirected network to construct a minimum spanning tree starting from the first 0th vertices.


Undirected network G



Sweep ky "http://www.bkjia.com/kf/ware/vc/" target = "_ blank" class = "keylink"> vc8L3A + CjxwPjxpbWcgc3JjPQ = "http://www.bkjia.com/uploadfile/Collfiles/20140105/20140105094547138.jpg" alt = "\">


# Include
 
  
# Define MAX 20 // maximum vertex count typedef struct ArcCell // The Arc {int adj; // the weight of the arc} ArcCell, AdjMatrix [MAX] [MAX]; typedef struct MGraph // array representation graph {char vexs [MAX]; // vertex value AdjMatrix arcs; // adjacent matrix int vexnum; // Number of vertices int arcnum; // Number of arcs} MGraph; typedef struct Arc // The Arc {int fir in the spanning tree; // The first vertex of the Arc in the Spanning Tree int sec; // indicates the second vertex of the Arc in the Spanning Tree int weight; // the weight of the Arc} Arc; typedef struct Vex // The vertex {char value in the spanning tree; // store vertex values} Vex; typedef struct MiniTree // Minimum Spanning Tree {Arc arcs [MAX]; // Arc Vex vexs [MAX]; // vertex int vexnum; // Number of vertices int arcnum; // Number of arcs} MiniTree; typedef enum Bool {FALSE, TRUE} Bool; typedef struct Temp // auxiliary structure {int pos; // store the vertex number int lowcost; // store the minimum Bool visited; // access flag} Temp; // determine the position of the input vertex value in the undirected network. int LocateVex (MGraph ** G, char c) {int I; for (I = 0; I <(* G) -> vexnum; I ++) {if (* G)-> vexs [I] = c) return I;} return-1 ;} // array representation to construct undirected network void CreateUDN (MGraph * G) {int I, j, k; printf ("Input vexnum and arcnum: \ n "); // enter the number of vertices and the number of arcs scanf ("% d", & (G-> vexnum); getchar (); scanf ("% d ", & (G-> arcnum); getchar (); printf ("Input vex's value: \ n"); // enter the vertex value for (I = 0; I
  
   
Vexnum; I ++) {scanf ("% c", & (G-> vexs [I]); getchar () ;}for (I = 0; I
   
    
Vexnum; I ++) // initialize the adjacent matrix {for (j = 0; j
    
     
Vexnum; j ++) G-> arcs [I] [j]. adj = 0;} printf ("Input % d arc \ n", G-> arcnum); for (k = 0; k
     
      
Arcnum; k ++) {char v1, v2; // two vertex values: int w; // scanf ("% c % d ", & v1, & v2, & w); getchar (); I = LocateVex (& G, v1); j = LocateVex (& G, v2 ); g-> arcs [I] [j]. adj = G-> arcs [j] [I]. adj = w ;}} // select non-zero minimum value int selectMin (Temp * temp, MiniTree ** mt) not accessed in the secondary Array) {// find the first element not 0 and set it to the minimum value int I, min; for (I = 0; I <(* mt)-> vexnum; I ++) {if (temp [I]. lowcost! = 0 & temp [I]. visited = FALSE) break;} min = I; for (I = 0; I <(* mt)-> vexnum; I ++) {if (temp [I]. lowcost! = 0 & temp [I]. visited = FALSE) {if (temp [min]. lowcost> temp [I]. lowcost) min = I;} return min;} // taking the undirected network as an example, starting from the u vertex, construct the void CreateMiniTree (MGraph * G, int u, miniTree * mt) {int I, j, k; Temp temp [MAX]; mt-> vexnum = G-> vexnum; mt-> arcnum = mt-> vexnum-1; for (I = 0; I
      
        Vexnum; I ++) // auxiliary array initialization {if (I! = U) {Temp t = {u, G-> arcs [u] [I]. adj, FALSE}; temp [I] = t;} Temp t = {u, 0, TRUE}; temp [u] = t; for (I = 1; I
       
         Vexnum; I ++) {k = selectMin (temp, & mt); mt-> arcs [I-1]. fir = temp [k]. pos; mt-> arcs [I-1]. sec = k; mt-> arcs [I-1]. weight = temp [k]. lowcost; temp [k]. lowcost = 0; // The k vertex is merged into Utemp [k]. visited = TRUE; // start to adjust the next line and retain the smaller value and vertex value of the existing path, retain the new value and vertex value for (j = 0; j
        
          Vexnum; j ++) {if (temp [j]. visited = FALSE) {if (temp [j]. lowcost! = 0) {if (G-> arcs [k] [j]. adj <temp [j]. lowcost & G-> arcs [k] [j]. adj! = 0) {Temp t = {k, G-> arcs [k] [j]. adj, FALSE}; temp [j] = t;} else {if (G-> arcs [k] [j]. adj! = 0) {Temp t = {k, G-> arcs [k] [j]. adj, FALSE}; temp [j] = t ;}}}} int main () {int I, j, k; MGraph G; MiniTree mt; createUDN (& G); // construct an undirected network G for (I = 0; I
         
          


Test Data


Number of vertices and edges of input G



Enter the vertex value of G. Here 1 represents v1, 2 represents v2, and so on.



Enter the number of the two vertices of the arc and the weight of the arc.



Displays the vertex values of the constructed undirected network G. Here 1 represents v1, 2 represents v2, and so on.



Displays the constructed undirected network G's Adjacent matrix



Output the arc and weight that constitute the minimum number of generations,

Corresponding to (v1, v3, 1)

(V3, v6, 4)

(V6, v4, 2)

(V3, v2, 5)

(V2, v5, 3)


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.