Both the Spanning Tree and the least spanning tree are in the undirected graph.
Spanning Tree: contains n nodes of a connected graph and n-1 edges. A connected graph can have multiple spanning trees.
Minimum Spanning Tree: it is a weighted undirected connected graph. Based on the weights, the spanning tree with the minimum cost is the minimum spanning tree based on certain rules.
Find the Minimum Spanning Tree, the classic two algorithms, the split algorithm and the cruise card algorithm.
1. Split Prim) Algorithm
The minimal spanning tree is an important application in the data structure graph, it requires that n-1 edges be selected from an undirected full graph and the graph is still connected to a spanning tree. At the same time, the tree's weight should be minimized. Many algorithms have been designed to obtain the minimum spanning tree. The most famous algorithms are prim and kruskal. The prim algorithm is introduced in the textbook, but it is not detailed enough and difficult to understand. To help you better understand this algorithm, this article further refines the content of the book, hope to help you.
If V is the vertex set in the graph, E is the edge set in the graph, and TE is the edge set in the minimal spanning tree, the prim algorithm obtains the minimum spanning tree through the following steps: 1: initialization: U = {u 0}, TE = {f }. This step sets up a node set u with only node U 0 and an empty edge set TE as the initial form of the Minimum Spanning Tree. In subsequent Algorithm Execution, this form will change constantly until the Minimum Spanning Tree is obtained. 2: In all u in U, v in V-U side u, v) in E, find a weight of the smallest edge u 0, v 0 ), add this edge to the set TE, and add non-U vertices of this edge to the U. The function of this step is to find an edge in edge set E that meets the following conditions: first, the two vertices of the edge are in the vertex set U and V-U, respectively, the secondary edge must have the minimum weight. After finding this edge, place it in the edge set TE, and add the vertex that is not in the U to the U. This step should be performed multiple times in the algorithm. Each execution time, the set TE and U will change, adding an edge and a vertex respectively. Therefore, TE and U are two dynamic sets. pay close attention to this when understanding algorithms. 3: if U = V, the algorithm ends; otherwise, repeat Step 2. This step can be considered as a condition for cyclic termination. We can calculate that when U = V, step 2 executes n-1 and sets n as the number of vertices in the graph.) TE also adds n-1 edges, the n-1 edge is the edge of the minimum spanning tree to be obtained. 2. Kruskal) algorithm for finding the Minimum Spanning Tree of a weighted connected graph. The greedy principle used by the kruskal algorithm to select n-1 edge each time is as follows: select an edge that does not generate a loop from the remaining edge and add it to the selected edge set. Note that if the selected edge generates a loop, it is impossible to form a spanning tree. The kruskal algorithm is divided into steps e, where e is the number of edges in the network. Consider the e-edge in the ascending order of consumption, and consider one edge each time. When you consider an edge, if it is added to the selected edge set, a loop will appear, then it will be discarded; otherwise, it will be selected. Assume that WN = (V, {E}) is a connected network with n vertices. The process of constructing the Minimum Spanning Tree Based on the kicar algorithm is as follows: first, construct a subgraph that contains only n vertices and the edge set is empty. If each vertex in the subgraph is considered as the root node of each tree, it is a forest Containing n trees. Then, select an edge with the smallest weight from edge set E of the network. If the two vertices of the edge belong to different trees, add them to the subgraph, that is, combine the two vertices of the two vertices into a tree. If the two vertices of the edge have fallen into the same tree, the two vertices of the edge cannot be obtained. Instead, we should take the edge with the smallest weight and try again. And so on until there is only one tree in the forest, that is, the subgraph contains n-1 edges. For example, the Kruskal's algorithm is a simple understanding of the two classic least Spanning Tree algorithms. This fully embodies the essence of greedy algorithms. The general process can be expressed in a diagram. The figure here is the one on Wikipedia. Very clear and intuitive. First, we have a graph with several vertices and edges, as shown in 650) this. width = 650; "class =" editorImg log-set-param "src =" http://www.bkjia.com/uploads/allimg/131228/2013013E3-0.jpg "Width =" 220 "height =" 184 "log-set-param =" img_view "/>
... The first step is to sort the length of all edges and use the sorting result as the basis for edge selection. The greedy algorithm is embodied here again. Sort resources to select the local optimal resources. After sorting, we chose edge AD first. In this way, our graph becomes 650) this. length = 650; "class =" editorImg log-set-param "src =" http://www.bkjia.com/uploads/allimg/131228/2013013V7-1.jpg "width =" 220 "height =" 184 "log-set-param =" img_view "/>
... Step 2: search for the remaining edge. We found CE. Here the weight is also 5650) this. length = 650; "class =" editorImg log-set-param "src =" http://www.bkjia.com/uploads/allimg/131228/2013011206-2.jpg "width =" 220 "height =" 184 "log-set-param =" img_view "/>
And so on, we found 6, 7, and 7. The figure is changed to this. 650) this. length = 650; "class =" editorImg log-set-param "src =" http://www.bkjia.com/uploads/allimg/131228/20130125D-3.jpg "width =" 220 "height =" 184 "log-set-param =" img_view "/>
... The next step is the key. Which side is selected below? BC or EF? None, although the side with the current length of 8 is the smallest unselected side. However, they have already connected BC through Ce and EB, and similar EF can be connected through EB, BA, AD and DF ). Therefore, we do not need to select them. Similar BD has connected the connection line here in red ). At last, the EG and FG are left. Of course we chose EG. The final success figure is: 650) this. length = 650; "class =" editorImg log-set-param "src =" http://www.bkjia.com/uploads/allimg/131228/2013016001-4.jpg "width =" 220 "height =" 184 "log-set-param =" img_view "/>
All edge points are connected, and a minimal spanning tree is built. The time complexity of the Kruskal algorithm is determined by the sorting algorithm. If fast sorting is adopted, the time complexity is O (N log N ).
This article is from "Li Haichuan" blog, please be sure to keep this source http://lihaichuan.blog.51cto.com/498079/1293449