prim最小生成树代码

2021/8/29 6:06:34

本文主要是介绍prim最小生成树代码,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

  1 // A C program for Prim's Minimum
  2 // Spanning Tree (MST) algorithm. The program is
  3 // for adjacency matrix representation of the graph
  4 #include <limits.h>
  5 #include <stdbool.h>
  6 #include <stdio.h>
  7 // Number of vertices in the graph
  8 #define V 5
  9 
 10 // A utility function to find the vertex with
 11 // minimum key value, from the set of vertices
 12 // not yet included in MST
 13 int minKey(int key[], bool mstSet[])
 14 {
 15     // Initialize min value
 16     int min = INT_MAX, min_index;
 17 
 18     for (int v = 0; v < V; v++)
 19         if (mstSet[v] == false && key[v] < min)
 20             min = key[v], min_index = v;
 21 
 22     return min_index;
 23 }
 24 
 25 // A utility function to print the
 26 // constructed MST stored in parent[]
 27 int printMST(int parent[], int graph[V][V])
 28 {
 29     printf("Edge \tWeight\n");
 30     for (int i = 1; i < V; i++)
 31         printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);
 32 }
 33 
 34 // Function to construct and print MST for
 35 // a graph represented using adjacency
 36 // matrix representation
 37 void primMST(int graph[V][V])
 38 {
 39     // Array to store constructed MST
 40     int parent[V];
 41     // Key values used to pick minimum weight edge in cut
 42     int key[V];
 43     // To represent set of vertices included in MST
 44     bool mstSet[V];
 45 
 46     // Initialize all keys as INFINITE
 47     for (int i = 0; i < V; i++)
 48         key[i] = INT_MAX, mstSet[i] = false;
 49 
 50     // Always include first 1st vertex in MST.
 51     // Make key 0 so that this vertex is picked as first vertex.
 52     key[0] = 0;
 53     parent[0] = -1; // First node is always root of MST
 54 
 55     // The MST will have V vertices
 56     for (int count = 0; count < V - 1; count++) {
 57         // Pick the minimum key vertex from the
 58         // set of vertices not yet included in MST
 59         int u = minKey(key, mstSet);
 60 
 61         // Add the picked vertex to the MST Set
 62         mstSet[u] = true;
 63 
 64         // Update key value and parent index of
 65         // the adjacent vertices of the picked vertex.
 66         // Consider only those vertices which are not
 67         // yet included in MST
 68         for (int v = 0; v < V; v++)
 69 
 70             // graph[u][v] is non zero only for adjacent vertices of m
 71             // mstSet[v] is false for vertices not yet included in MST
 72             // Update the key only if graph[u][v] is smaller than key[v]
 73             if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v])
 74                 parent[v] = u, key[v] = graph[u][v];
 75     }
 76 
 77     // print the constructed MST
 78     printMST(parent, graph);
 79 }
 80 
 81 // driver program to test above function
 82 int main()
 83 {
 84     /* Let us create the following graph
 85        2     3
 86     (0)--(1)--(2)
 87     |   /  \    |
 88    6| 8/    \5  |7
 89     | /       \ |
 90     (3)-------(4)
 91           9         */
 92     int graph[V][V] = { { 0, 2, 0, 6, 0 },
 93                         { 2, 0, 3, 8, 5 },
 94                         { 0, 3, 0, 0, 7 },
 95                         { 6, 8, 0, 0, 9 },
 96                         { 0, 5, 7, 9, 0 } };
 97 
 98     // Print the solution
 99     primMST(graph);
100 
101     return 0;
102 }
$gcc -o main *.c
$main
Edge 	Weight
0 - 1 	2 
1 - 2 	3 
0 - 3 	6 
1 - 4 	5 


这篇关于prim最小生成树代码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程