C ++ explanation of Prim algorithm (II)

Source: Internet
Author: User

Prim is an algorithm used to calculate the Minimum Spanning Tree of a weighted connected graph. The basic idea is that V is the set of all vertices for graph G. Now, set two new sets U and T, where U is used to store vertices in the smallest Spanning Tree of G, T stores the edges in the smallest Spanning Tree of G. Select the edge (u, v) with the smallest weight from the edge of all U Є U, v Є (V-U) indicating all vertices out u ), add vertex v to the set U, and add edge (u, v) to the set T. This repeats until U = V, the Minimum Spanning Tree is constructed, in this case, the set T contains all the edges in the minimal spanning tree. The diagram of the prim algorithm uses G4 as an example to demonstrate prim (the minimum spanning tree is generated from the first Vertex A through the prim algorithm ). Initial State: V is A set of all vertices, that is, V = {A, B, C, D, E, F, G}; U and T are empty! Step 1: Add vertex A to U. At this time, U = {}. Step 2: Add vertex B to U. After the previous operation, U = {A}, V-U = {B, C, D, E, F, G}; therefore, the weight of the edge (A, B) is the smallest. Add vertex B to U. At this time, U = {A, B }. Step 2: Add vertex F to U. After the previous operation, U = {A, B}, V-U = {C, D, E, F, G}; therefore, the weight of the edge (B, F) is the smallest. Add vertex F to U. At this time, U = {A, B, F }. Step 1: Add vertex E to U. After the previous operation, U = {A, B, F}, V-U = {C, D, E, G}; therefore, the weight of the edge (F, E) is the smallest. Add vertex E to U. At this time, U = {A, B, F, E }. Step 2: add vertex D to U. After the previous operation, U = {A, B, F, E}, V-U = {C, D, G}; therefore, the weight of the edge (E, D) is the smallest. Add vertex D to U. At this time, U = {A, B, F, E, D }. Step 2: Add vertex C to U. After the previous operation, U = {A, B, F, E, D}, V-U = {C, G}; therefore, the weight of the edge (D, C) is the smallest. Add vertex C to U. At this time, U = {A, B, F, E, D, C }. Step 2: Add vertex G to U. After the previous operation, U = {A, B, F, E, D, C}, V-U = {G}; therefore, the edge (F, G) has the smallest weight. Add vertex G to U. At this time, U = V. The minimum spanning tree structure is complete! The vertex in sequence is a B F E D C G. The Code Description of the prim algorithm uses the "Adjacent matrix" as an example to describe the prim algorithm. The source code of the "adjacent table" implementation is provided later. 1. Basic definition copy code class MatrixUDG {# define MAX 100 # define INF (~ (0x1 <31) // infinity (0X7FFFFFFF) private: char mVexs [MAX]; // vertex set int mVexNum; // Number of vertices int mEdgNum; // Number of edges int mMatrix [MAX] [MAX]; // adjacent matrix public: // create a graph (input data by yourself) MatrixUDG (); // create a graph (using the provided matrix) // MatrixUDG (char vexs [], int vlen, char edges [] [2], int elen ); matrixUDG (char vexs [], int vlen, int matrix [] [9]); ~ MatrixUDG (); // depth-first traversal graph void DFS (); // breadth-first search (similar to tree-level traversal) void BFS (); // prim Minimum Spanning Tree (Minimum Spanning Tree generated from start) void prim (int start); // print matrix queue diagram void print (); private: // read an input character char readChar (); // returns the position of ch in the mMatrix int getPosition (char ch); // returns the index of the first adjacent vertex of vertex v, -1 int firstVertex (int v); // returns the index of vertex v relative to the next adjacent vertex of w. If it fails,-1 int nextVertex (int v, int w); // recursively implement void DFS (int I, int * visited) in the depth-first traversal graph;}; copy The Code MatrixUDG is the structure corresponding to the adjacent matrix. MVexs is used to store vertices. mVexNum is the number of vertices, mEdgNum is the number of edges, and mMatrix is a two-dimensional array used to store matrix information. For example, mMatrix [I] [j] = 1 indicates "vertex I (mVexs [I])" and "vertex j (mVexs [j]). "Is the adjacent contact. mMatrix [I] [j] = 0 indicates that they are not the adjacent contact. 2. prim algorithm copy code/** prim minimal spanning tree ** parameter description: * start -- the start element of the minimum tree to be generated */void MatrixUDG: prim (int start) {int min, I, j, k, m, n, sum; int index = 0; // the index of the prim minimal tree, that is, the index of the prims array char prims [MAX]; // The result array int weights [MAX] of the prim minimal tree; // the weight of the edge between vertices // The first number in the prim least spanning tree is "start vertex in the figure ", because it starts from start. Prims [index ++] = mVexs [start]; // initialize "vertex weight array ", // initialize the weight of each vertex to the "start vertex" to "This vertex. For (I = 0; I <mVexNum; I ++) weights [I] = mMatrix [start] [I]; // initialize the weight of the start vertex to 0. // It can be understood as "the distance from the start vertex to itself is 0 ". Weights [start] = 0; for (I = 0; I <mVexNum; I ++) {// since it starts from start, therefore, you do not need to process the start vertex. If (start = I) continue; j = 0; k = 0; min = INF; // find the vertex with the smallest weight in the vertex not added to the smallest spanning tree. While (j <mVexNum) {// If weights [j] = 0, it means that "Node j has been sorted" (or has been added to the minimal spanning tree ). If (weights [j]! = 0 & weights [j] <min) {min = weights [j]; k = j;} j ++;} // after processing, in the vertex that is not added to the smallest Spanning Tree, the vertex with the smallest weight is the k vertex. // Add the k vertex to the result array of the minimal spanning tree. prims [index ++] = mVexs [k]; // mark the "Weight of the k vertex" as 0, which means that the k vertex has been sorted (or has been added to the minimum tree result ). Weights [k] = 0; // when the k vertex is added to the result array of the minimal spanning tree, the weights of other vertices are updated. For (j = 0; j <mVexNum; j ++) {// It is updated only when node j is not processed. If (weights [j]! = 0 & mMatrix [k] [j] <weights [j]) weights [j] = mMatrix [k] [j];} // calculate the Minimum Spanning Tree Weight sum = 0; for (I = 1; I <index; I ++) {min = INF; // obtain the position n = getPosition (prims [I]) of prims [I] In mMatrix; // In vexs [0... in I], find the vertex with the smallest weight of j. For (j = 0; j <I; j ++) {m = getPosition (prims [j]); if (mMatrix [m] [n] <min) min = mMatrix [m] [n];} sum + = min;} // print the Minimum Spanning Tree cout <"PRIM (" <mVexs [start] <") = "<sum <": "; for (I = 0; I <index; I ++) cout <prims [I] <""; 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.