connectivity issues for graphs: the connected component and spanning tree of undirected graphs, all vertices are by edges connected together, but There is no diagram of the loop.
Figure g= (V, E) is a connected graph that divides the edge set E (g) into two sets of T (g) and B (g) When traversing the graph G from any vertex of the graph. where T (g) is the set of edges traversed by the graph, B (g) is a collection of edges that are not traversed while traversing the graph. Obviously, G1 (V, T) is a very small connected sub-graph of Figure g, that is, the sub-graph G1 is the spanning tree of the connected graph G.
Depth-first generation of forests
On the right is the depth-first build forest:
The spanning tree of a connected graph is not necessarily unique,Different spanning graphs are used to get different generation trees, and different spanning trees can be obtained from different vertices. The connected graph itself is the connected component, where the vertex set + traversed edge = spanning tree.the generation of non-connected graph forests is not necessarily unique. The vertex set of each connected component of a non-connected graph + elapsed edge = Several spanning trees (spanning forest)
Minimum spanning tree
Given a non-direction network, in all spanning trees of the net, the spanning tree with the smallest sum of edge weights is called the smallest spanning tree of the network.
Question: To establish a transport network between the N cities, how to consider the most cost savings under the previous question of ensuring n-point connectivity?
How to find the minimum spanning tree of connected graphs?
There are many algorithms for constructing a minimal spanning tree, most of which use a property called MST.
MST Properties : Set N = (V, E) is a connected network, and U is a non-empty subset of the vertex set V. If the Edge (U, v) is an edge with a minimum weight, where u∈u,v∈v-u, there must be a minimum spanning tree with edges (U, v).
Method One: Primm (Prim) algorithm.
Algorithm idea:
1, set n= (V, E) is a connected network, TE is the smallest generation of N on the edge of the set of trees. Initial Order u={u0}, (u0 belongs to V), te={}. 2, at all u belong to u, V belongs to the side of the V-u (U, V) belongs to E,find the least expensive side (u0, V0). Merges (u0, V0) into the set TE, while V0 incorporates U. 3, repeat the above operation until U=v, then t= (V, TE) is N the most
Small spanning tree.
In a way , the Primm algorithm is based on the tree, looking for the smallest right side, characterized by a non-directed graph! It is only related to vertices, not to edges, and is suitable for dense graphs. Algorithm time complexity of O (n^2)
Primm algorithm to find the minimum spanning tree
Initial Order u={u0}, (u0 belongs to V), te={}.
At all U-belong u, v belongs to the side of the V-u (U, V) belongs to E, looking for a cost-least edge (u0, V0). Merges (u0, V0) into the set TE, while V0 incorporates U.
Repeating the above operation until U=v, the t= (V, TE) is N the mostsmall spanning tree.
Go on
Finally, after traversing the
Implementation of Prim algorithm
How are vertex collections represented? How is the minimum edge selected? How does a vertex join the U-set represent? As in the following example: When a new vertex is added to the U set, the minimum cost edge of the vertex to u in the V-u collection may be updated, the K represents the final selected vertex, K=3, which represents the V3 vertex, because the 1-3 price is the smallest =1 selected V3, and then continues with the newest tree, To find the smallest weight edge, by looking at which vertex to connect to. K=6, representing the selection is V6 this vertex, because the 3-6 price is the smallest = 4, in all and newest tree adjacency vertices, the weight of the smallest edge. After selecting V6, continue with the newest tree, find the nearest vertex, see which side of the weight of the smallest, find 6-4 this edge, weights =2 new tree to continue to the latest tree units, find the nearest vertex, see which side of the least weight, find 3-2 this edge, the weight =5 new tree continues to the latest tree units, Find the nearest vertex, see which side of the weight is the smallest, find 2-5 this edge, the weight =3 until all vertices are merged into the spanning tree, the program ends
Method Two: Kruskal (Kruskal) algorithm.
Use and check set, directly from the edge to find the smallest edge of the non-ring (the simplest algorithm for finding the minimum spanning tree), features: Only for the graph, package good Primm algorithm, are only for the non-directed graph.
Algorithm idea:
1, set the connected network N = (V, E), so that the minimum spanning tree initial state is only N vertices and boundless non-connected graph t= (V, {}), each vertex is a connected component. 2.Select the lowest-cost edge in E, if the edge is attachedthe vertices fall in the different connected components of T (i.e.:cannot form a ring), the edge is added to T;then, remove this edge and select the next least expensive edge. 3, and so on, until all vertices in T are on the same connected component.
The minimum spanning tree may be inflexible one (including the Primm algorithm is the same reason)
Put all the edges in ascending order of weights, starting from the smallest side (cannot form a loop), selecting, forming the minimum spanning tree. Until all sides are merged then end (not vertex!) )The Kruskal algorithm is the most time-consuming when ordering the weighted value sequence of the edge, and his algorithm time complexity is related to the sorting algorithm, while the time complexity of the sorting algorithm is relative to the edge e of the graph, and the vertex v has no relation. Therefore, it is suitable for sparse graphs. (While the PRIMM algorithm is suitable for dense graphsThe following is a schematic step: in ascending order, the sorting sequence of weights is found: 1 2 3 4 5 5 5 6 6 6 Note that when the right edge is selected, do not form loops in ascending order of weights to find the right edge to continue, in order to find the right edge of the weight of the selection of the right side to choose 5, Avoid the generation of rings until all edges are incorporated.So how do you avoid forming loops in the Kruskal algorithm by finding the right side? In other words, how do you determine if a loop is formed?
Use and check set can judge whether to form a loop,Kruskal algorithm used a greedy strategy , first of all, the edge set array to the edge of the weight from small to large sort, and then an edge of the search, if the edge of the two endpoint is not within a set, then add this edge to the growing forest, and merges the collection of two endpoints until the minimum spanning tree has been generated.
and check the set:is a tree-based data structure that handles merge and query problems for some disjoint collections (disjoint sets). Often used in the forest to express. A set is a collection of elements that make up each element, that is, the collection of elements belonging to the same group in a certain order.
And check set is a very simple data structure, it mainly involves two basic operations, namely:
A Merge two disjoint collections
B Determine whether two elements belong to the same collection
1) Merge two disjoint sets (Union (x, y))
The merge operation is simple: set an array father[x], in the Kruskal algorithm, you need to use a parent storage structure that represents the number of the "Father" of X. So, the way to merge two disjoint collections is to find one of the most father's fathers (that is, the oldest ancestor), pointing to the father of the oldest ancestor of the other set.
In layman's words, it is the root of one of the trees that serves as a child node for the root node of another tree.
For two disjoint sets, you can see: Father (b) =father (g) =f node
2) Determine whether two elements belong to the same set (Find_set (x)), this operation can be converted to find two elements of the oldest ancestor is the same. Recursive implementations can be used.
The optimization problem of the check set
When looking for ancestors, we generally use recursive lookup, but when the elements are many or the whole tree becomes a chain, each time Find_set (x) is the complexity of O (n). To avoid this, we need to compress the path, that is, when we "recursive" to find the ancestor node, "backtracking" by the way, the descendant nodes are pointed directly to the ancestors, so that again Find_set (x) when the complexity becomes O (1), as shown in. Visible, path compression facilitates later lookups.
Back to the Kruskal algorithm, use and check set to realize the formation of the judgment Loop No
For example starting from V1 (altogether is V1, V2, v3, v4, V5, V6), then began to v1-v6 as each single tree, forest to represent, let each element constitute a single element of the collection, need to use array representation, storage is the parent structure (easy to find a common father).
Each time you use and check the set, another node on the back edge is used as the child node, and the node is not joined as a single tree:
, the selection weight =5 the edge, and there are two trees at this time
And
If you choose 3-4 or 1-4 of these two edges of any one, a single tree will not produce the same root situation, and joined (as the child's root), will certainly find a common ancestor, so you can find the existence of the loop! and choose 2-3 this side words, in and check concentration, will not find common ancestors, that is, the formation of no ring.
In layman's parlance, it is through the nodes where the two elements are launched with the node, if the root is the same, then the same set, otherwise not the same set (that is, not forming a loop)
Graph spanning Tree (forest) (Kruskal Kruskal algorithm and Primm Prim algorithm), and the use of the and check set