Basic Graph Algorithm Minimum Spanning Tree Prim algorithm (adjacent table + priority queue STL)

Source: Internet
Author: User

This article is a summary of the Prim algorithm for finding the minimal spanning tree of a undirected connected graph in the introduction to algorithms. I have a little bit about it.

The specific problem of the minimal spanning tree can be explained in the following languages:
    Input: An undirected Weighted Graph G = (V, E). Each edge (u, v) belongs to E and has a weight of w.

    Output: The minimum spanning tree of the graph, that is, a tree connected to all vertices, and the sum and minimum of edge values in the tree.

The following is an example of the Minimum Spanning Tree:

Tree T1 and T2,

1 # include <iostream> 2 # include <cstdio> 3 # include <vector> 4 # include <queue> 5 using namespace std; 6 7 # define maxn 110 // maximum number of vertices 8 int n, m; // Number of vertices, number of edges 9 10 struct arcnode // edge node 11 {12 int vertex; // vertex number 13 int weight adjacent to the header node; // weight of the edge connecting the two vertices 14 arcnode * next; // point to the next adjacent vertex 15 arcnode () {} 16 arcnode (int v, int w): vertex (v), weight (w), next (NULL) {}17}; 18 19 struct vernode // vertex node, 20 {2 1 int vex; // the current point number 22 arcnode * firarc; // edge formed by the first vertex connected to the vertex 23} Ver [maxn]; 24 25 void Init () // initialize the graph neighbor list and create the vertex node 26 {27 for (int I = 1; I <= n; I ++) 28 {29 Ver [I]. vex = I; 30 Ver [I]. firarc = NULL; 31} 32} 33 34 void Insert (int a, int B, int w) // end method, Insert With a as the start point, B as the end point, if the weight is w, the efficiency is not as good as that of the header insertion, but you can remove the duplicate edge 35 {36 arcnode * q = new arcnode (B, w); 37 if (Ver [a]. firarc = NULL) 38 Ver [a]. firarc = q; 39 else 40 {41 ar Cnode * p = Ver [a]. firarc; 42 if (p-> vertex = B) 43 {44 if (p-> weight> w) 45 p-> weight = w; 46 return; 47} 48 while (p-> next! = NULL) 49 {50 if (p-> next-> vertex = B) 51 {52 if (p-> next-> weight> w ); 53 p-> next-> weight = w; 54 return; 55} 56 p = p-> next; 57} 58 p-> next = q; 59} 60} 61 62 struct node // The node that stores the key value 63 {64 int v; 65 int key; 66 friend bool operator <(node a, node B) // custom priority. If the key is small, the priority is 67 {68 return. key> B. key; 69} 70}; 71 72 # define INF 0 xfffff // The maximum weight is 73 int parent [maxn]; // The parent node of each node 74 bool visited [ma Xn]; // whether 75 node vx [maxn] has been added; // you can specify 76 priority_queue for the edge connecting each node to its parent node. <node> q; // The priority queue stl implements 77 void Prim (int s) // s to indicate the root node 78 {79 for (int I = 1; I <= n; I ++) // initialize 80 {81 vx [I]. v = I; 82 vx [I]. key = INF; 83 parent [I] =-1; 84 visited [I] = false; 85} 86 vx [s]. key = 0; 87 q. push (vx [s]); 88 while (! Q. empty () 89 {90 node nd = q. top (); // get the first team. Remember to pop 91 visited [nd. v] = true; 92 q. pop (); 93 arcnode * p = Ver [nd. v]. firarc; 94 while (p! = NULL) // locate all adjacent nodes. if not accessed, enter the queue 95 {96 if (! Visited [p-> vertex] & p-> weight <vx [p-> vertex]. key) 97 {98 parent [p-> vertex] = nd. v; 99 vx [p-> vertex]. key = p-> weight; 100 vx [p-> vertex]. v = p-> vertex; 101 q. push (vx [p-> vertex]); 102} 103 p = p-> next; 104} 105} 106 107 int main () 108 {109 int, b, w; 111 cout <"input n and m:"; 112 cin> n> m; 113 Init (); 114 cout <"input all edges: "<endl; 115 while (m --) 116 {117 cin> a> B> w; 118 Insert (a, B, w); 119 Insert (B, a, w); 120} 121 Prim (1); 122 cout <"parent node of all output nodes:" <endl; 123 for (int I = 1; I <= n; I ++) 124 cout <parent [I] <"; 125 cout <endl; 126 cout <" The minimum generated tree weight is: "; 127 int cnt = 0; 128 for (int I = 1; I <= n; I ++) 129 cnt + = vx [I]. key; 130 cout <cnt <endl; 131 return 0; 132}View Code

 

The running result is as follows (based on the first example ):

 

Thank you for your support.

 

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.