Graph-Spanning tree and minimum spanning tree

Source: Internet
Author: User
Tags join

Tree (free tree), unordered tree, and root tree

Free Treeis a non-loop connected graph (no root is determined) (one of the vertices in the free tree is chosen as the root, then a normal tree).
Starting at the root, for each vertex (usually called a node in the tree), the child is left-to-right in order, it becomes aordered Tree
In the application of graphs, we often need to find a sub-graph of a given graph so that the sub-graph is a tree.

Spanning Tree

1. Spanning Tree

If a sub-graph of the connected graph G is a tree that contains all the vertices of G, then the sub-graph is called the Spanning Tree of G (spanningtree).
A spanning tree is a very small connected graph of all the vertices in a connected graph that contain diagrams.
The spanning tree of the graph inflexible one. A different spanning tree can be obtained by traversing from different vertices.

2. Depth-first spanning tree and breadth-first spanning tree
(1) How to solve the spanning tree
Figure g= (V,e) is a connected graph with n vertices. From any vertex of G (source point), make a depth first search (breadth First search), search for n vertices and search process from a visited Vertex VI search to an unknown adjacency point VJ, the passing Edge (VI,VJ) (n-1 bar) composed of the small connected sub-graph is the spanning tree. (source point is the root of the spanning tree)
In general, the spanning tree that is obtained by the depth-first search is called the depth-first spanning tree, which is referred to as the DFS spanning tree, and the spanning tree obtained by breadth-first search is called the breadth-first spanning tree, short of the bps spanning tree.
  
(2) Seeking Dfs spanning tree and BFS spanning tree algorithm
The algorithm for the DFS spanning tree can be obtained as long as the appropriate build edge (VI,VJ) operation is added to the DFS (or DFSM) algorithm's if statement before the recursive call statement (such as outputting or saving the edge).
In the IF statement of the BFS (or BFSM) algorithm, the algorithm of the BFS spanning tree can be obtained by adding the operation of the Spanning Tree Edge (VI,VJ). "See Exercise"
Attention:
The tree height of the breadth-first spanning tree of the ① graph does not exceed the height of the other spanning trees in the graph
The spanning tree of the ② graph is inflexible, and a different spanning tree can be obtained by traversing from different vertices.

3. Common definition of Spanning tree
If you start from a vertex of the graph, you can systematically access all the vertices in the diagram, and the sub-graph of all the edges and all the vertices of the graph is called the spanning tree of the graph. (This definition applies not only to the non-graph, but also to the graph.) )
(1) If G is strongly connected to the graph, then from any of the vertex V, you can access all the vertices in G, so as to obtain a tree with the root of v.
(2) If G is a graph with a root, and the root is V, then from the root V can be done to the g of the traversal, the G to the V-root of the spanning tree.
(3) If G is a non-connected undirected graph, it is necessary to invoke the DFS (or BFS) algorithm from the outside for several times to complete the traversal of G. Each external call can only access the set of vertices of a connected component of G, which make up a Dfs (or bps) spanning tree of that connected component, along with the edges traversed. The DFS (or BFS) spanning tree of the various connected components of G makes up the DFS (or BFS) generation Forest of G.
(4) If G is a non-strongly connected to the graph, and the source point is not the root of the graph, then the traversal generally can only get the generated forest of the graph.


5, Primm (Prim) algorithm
(1) Algorithm idea
T= (U,te) is a collection of MST that is stored.
The initial value of the ①t is ({R},¢)
That is, the minimum spanning tree initially has only one red dot R and no red edge.
②t after n-1 times the following steps, and finally get a tree containing n vertices, n-1 edge of the smallest spanning tree
⒈ Select the purple edge to concentrate a light edge and expand into t
⒉ change the blue point of a light-edged connection to a red dot
⒊ will change the light edge to red edge
⒋ Modifying the purple edge set

(2) Construction of smaller purple edge sets
If there is a k vertex in the currently formed T, | u|=k,| V-u|=n-k, so the number of possible purple edges is K (n-k). Choosing a light edge from such a large purple edge is inefficient. Therefore, a smaller set of violet edges must be constructed.
For each blue dot v∈v-u, from V to the purple edge of each red dot, only the shortest one can be a light edge. Therefore, only the shortest purple edges associated with all n-k blue dots should be retained as candidate sets for the light edge.

(3) Modification of the candidate Purple Edge set
When the light Edge (U,V) is extended to T, because v from blue to red, so for each of the remaining Blue Point J, the Edge (V,J) from the non-violet edge into a purple edge, the length of the new purple edge may be less than the blue point J originally associated with the shortest purple edge length. Therefore, replace the original shortest purple edge with a new purple edge with a smaller length.

(4) Pseudo-code description of prim algorithm
Primmst (g,t,r) {
The MST with R root for figure G, and the result in t= (U,te)
Initcandidateset (...) ;//Initialize: Sets the initial light edge candidate set and t= ({R},¢)
for (k=0;k<n-1;k++) {//n-1 tree edge for T
(u,v) =selectlishtedge (...) ;//Select Light Edge (U,V);
t←t∪{(u,v)};//expansion T, i.e. (u,v) Tu join te, Blue Dot V and man red dot set u
Modifycandidateset (...) ;//Adjust the candidate light edge set according to the new Red Dot V
}
}

(5) The execution process of the algorithm
The process of obtaining the minimum spanning tree using the prim algorithm "See animation Demo"


Attention:
If there is more than one light edge in the light edge of the candidate, you can expand it to T in any one of the two.
The smallest spanning tree of connected networks is not necessarily unique, but their weights are equal.

(6) Algorithm features
The algorithm is characterized by the current formation of the set T is always a tree. Consider the u and TE in T as the red and red edge sets respectively, and v-u as the blue point set. Each step of the algorithm chooses a light-edged expansion into T in the purple edge of the red and Blue point set. The MST nature guarantees that the edge is safe. T starts with any root R and grows to U=v, that is, T contains all the vertices in C. The MST property ensures that T is an MST of G at this time. This is a "greedy" strategy because each added edge is to make the right in the tree as small as possible.
(7) Algorithm analysis
The time complexity of the algorithm is O (N2). Independent of the number of edges in the graph, the algorithm is suitable for dense graphs.

(1) Algorithm idea
Initial state of the ①t
Only n vertices and boundless forest t= (V,¢)
② Select the N-1 secure Edge (U,V) in E in the order of the edge increments and add T to generate the MST
Attention:
A safe edge is a two-point edge that is the vertex of two trees in a forest t. Join the safe side to connect two trees in the forest to a larger tree
Because each edge added to T is a safe edge with the lowest current weight, the MST property also guarantees that the final T is the smallest spanning tree.

(2) algorithm features
The characteristic of this algorithm is that the current set of T is a forest in addition to the final result.

(3) Abstract description of the Kruskal algorithm
Kruskalmst (g) {//For an MST of connected net G
T= (V,¢);//initialization, T is a forest containing only n vertices that do not contain edges
Ordering the edges in E (G) according to the increment order of weights, and setting the result in e[0..e-1]
for (i=0;i<e;i++) {//e is the total number of edges in the graph
Take e[0. e-1) in the section I (U,V);
if u and V belong to two different trees in T
t=t∪{(u,v)};//(U,V) is a secure edge, adding it to T
If T is already a spanning tree then
"Return T;
}//endfor
return T;
}

(4) The process of constructing minimum spanning tree with Kruskal algorithm
The process of constructing a minimal spanning tree using the Kruskal algorithm "See animation Demo"


(5) algorithm analysis
The time complexity of the algorithm is O (elge).
The time of the Kruskal algorithm depends primarily on the number of sides. It is more suitable for sparse graphs.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.