In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the Kruskal algorithm to find the minimum spanning tree of the complete C code, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Basic idea of algorithm
An algorithm for finding the minimum spanning tree of weighted connected graphs. The kruskal algorithm selects a total of n-1 edges, and the greedy criterion is to select an edge with the least cost from the remaining edges that will not produce a loop to join the set of selected edges. Note that it is impossible to form a spanning tree if the selected edge produces a loop. The kruskal algorithm is divided into e steps, where e is the number of edges in the network. Consider the e edges in the order of increasing cost, one edge at a time. When considering an edge, if a loop appears when it is added to the set of selected edges, it is discarded, otherwise, it is selected.
The code is represented by adjacency matrix and edge set, so the code will be greatly simplified. There are many places in the code that can be optimized because they are only used to learn algorithms, not to optimize them.
Complete C code / * Kruskal algorithm for minimum spanning tree * / # include # define MaxSize 20typedef char VertexType;typedef struct Graph {/ / defines the adjacency matrix representation structure of the graph VertexType ver [MaxSize+1]; int edg [MaxSize] [MaxSize];} Graph;typedef struct gEdge {/ / defines an edge set structure to store all edge information int begin; int end of the graph Int weight;} gEdge;void CreateGraph (Graph * g) / / the generating function of the adjacency matrix method graph {int I = 0; int j = 0; int VertexNum; VertexType Ver; printf ("Please enter the vertices of the graph:\ n"); while ('\ n'! = (Ver=getchar () {g-> ver [iTunes +] = Ver G-> ver [I] ='\ 0mm; VertexNum = strlen (g-> ver); printf ("Please enter the corresponding adjacency matrix:\ n"); for (iTun0; I)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.