Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of Prim algorithm and the implementation of complete C code

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about the principle of the Prim algorithm and the implementation of the complete C code, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Several basic knowledge involved in Prim algorithm

Spanning tree: the spanning tree of a connected graph is its minimal connected subgraph. In the case of n vertices, it has 1 edge. Spanning tree is for a connected graph. It is a minimal connected subgraph of a connected graph, which contains all the vertices along the way and has only one edge. The spanning tree of an unconnected graph forms a claimed forest; if there are n vertices and m connected components in the graph, there are nMum edges in the spanning forest.

Traversal of a graph: similar to the traversal of a tree, if you start from a vertex in the graph and visit every vertex on the way, and each vertex is visited only once, this process is called traversal of the graph. The traversal algorithm of a graph is the basis of solving the connectivity problem of a graph, topological sorting and finding critical paths. There are two common traversal orders of graphs: depth-first search (DFS) and breadth-first search (BFS). For each search order, the order of visiting vertices is not unique.

In an undirected connected graph G, the subgraph G formed by all its vertices and all edges traversing the graph is called the spanning tree of G. A graph can have multiple spanning trees, divide from different vertices, adopt different traversal order, and pass through different edges.

Minimum spanning tree: in graph theory, a tree is often defined as a loop-free connected graph. For a weighted undirected connected graph, the sum of weights on all edges of each spanning tree may be different. We turn the spanning tree with the smallest sum of weights on all edges into the minimum spanning tree (MST) of the graph.

MST property: MST property: suppose G = (VMagneE) is a connected network and U is a non-empty subset of vertex V. If (uMaginv) is an edge with minimum weight, where u ∈ Urecov ∈ Vmuru, then there must be a minimum spanning tree containing the edge (uQuery v).

Prim algorithm

The basic idea is to assume that G = (VMagne E) is connected, and TE is the set of edges in the smallest spanning tree on G. The algorithm starts with U = {u0} (u0 ∈ V), TE= {}. Repeat the following:

Find an edge with the least weight (u0meme v0) among all the edges of u ∈ U ∈ V ∈ U and merge it into the set TE, at the same time, v0 is merged into U until VMAX U.

In this case, there must be an edge in TE, and T = (V _ ther te) is the minimum spanning tree of G.

The core of Prim algorithm: always keep the edge set in TE to form a spanning tree.

An example of Prim algorithm:

An undirected connected graph with 6 vertices is used:

Let V = {A _ Magi B _ r C _ P D _ D _ E _ F}, that is, the set of all vertices.

The set U is the node of the minimum spanning tree.

According to the Prim algorithm:

Add A to U, at this time, U = {A}, Vmuru = {BMagceCMagneDMagee E}

The vertex adjacent to An is Bjime Cpeng D. (Amena B), (Amena C), (Amena D), the weights are 6, 1, 5 respectively, so (Amena C) is selected as an edge of the minimum spanning tree.

Add the vertex C selected in the previous step to U, and at this time, U = {A ~ ~ C}, V ~ ~ U = {B ~ D ~ F}.

The edges composed of vertices in Vmere U and vertices in U are (A _ Magi B), (A ~ ~ D), (C ~ ~ B), (C ~ ~ D), (C ~ ~ E), (C ~ F), and the weights are 6, 5, 5, 5, 6, 4 respectively, so (C ~ F) is selected as an edge of the minimum spanning tree.

Add F to U, at this time U = {A _ Magi C ~ F}, V ~ m U = {B, D ~ ~ E}

The edges composed of vertices in Vmere U and vertices in U are (A _ ~ ~ B), (A ~ D), (C ~ B), (C ~ D), (C ~ E), (F ~ E) and (F ~ E), with weights of 6, 5, 5, 5, 6, 2, 6, respectively, and selected as an edge of minimum spanning tree.

Add D to U, at this time, U = {A _ Magi C ~ F ~ D}, V _ M _ U = {Bjue E}

The edges composed of vertices in Vmere U and vertices in U are (A _ (_)), (C _ () ~ () B), (C _ () ~ () E) and (F _ () ~ E), with weights of 6, 5, 6 and 6, respectively. (C _ () ~ () B) is selected as an edge of the minimum spanning tree.

Add B to U, where U = {A _ Magne C ~ F, D _ M B}, V-U = {E}

The edges composed of vertices in Vmuru and vertices in U are (BMague E), (Cpene E) and (Fgraine E), with weights of 3, 6 and 6, respectively. (BPME E) is selected as an edge of the minimum spanning tree.

Add E to U, and at this time U = {A, C ·Magi F ·D ·M ·B ·E}, complete the generation of MST.

The generation process is shown as follows:

C code of Prim algorithm

The difficulty is the specific meaning of the two auxiliary arrays in the prim function: the lowcost array, as the name implies, the minimum cost. That is, lowcost [k] holds the minimum weight from the vertex numbered k in VMI U to all vertices in U. The closest array, as its name implies, is the closest. That is, closest [k] holds the number of the vertex from U to the vertex with the least weight of the vertex numbered K in Vmuru. The elements of these two arrays change dynamically as vertices are added to the U set. In the program, the adjacency matrix method is used to create the graph.

/ * prim algorithm for finding the minimum spanning tree * / # include # include # define MaxSize 20#define MAX 10000typedef char VertexType;// adjacency matrix representation structure typedef struct Graph {VertexType ver [MaxSize+1]; int edg [MaxSize] [MaxSize];} Graph;// adjacency matrix generating function void CreateGraph (Graph * g) {int I = 0; int j = 0; int VertexNum; VertexType Ver Printf ("Please enter vertices of the graph:\ n"); while ('\ n'! = (Ver=getchar ()) g-> ver [iTunes +] = Ver; g-> ver [I] ='\ 0neighbor; VertexNum = strlen (g-> ver); printf ("Please enter the corresponding adjacency matrix:\ n"); for (iS0; 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report