ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

prim最小生成树代码

2021-08-29 03:31:46  阅读:217  来源: 互联网

标签:prim parent int graph 代码 MST 最小 vertices key


  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,parent,int,graph,代码,MST,最小,vertices,key
来源: https://www.cnblogs.com/hulianxingkong/p/15201775.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有