The minimum spanning tree poj3164 of the direction-definite root of Zhu Liu algorithm

Source: Internet
Author: User

About why you can't solve such problems with prim, as follows

Prim can be seen as maintaining two sets of vertices or as maintaining a continuously generated tree (feeling a little better Before)

If there is a graph with three vertices 1.2.3

Side of the situation is as follows

1->2:5

1->3:6

2->3:1000861

3->2:2

obviously, in accordance with the prim algorithm, the vertex is first pressed into the Set. Then the trend to find the smallest vertex 2, and then 1.2 to three of the shortest side is 1000861, then the cost is 1000866, obviously wrong. ;

And if the graph does not have this problem (that is to say that the least-squares to ensure that the smallest is the best), without a map to find 2, spend 5, and then the trend to find 3, spending 2, which is right.

The following is reproduced Content: http://blog.csdn.net/lynnucas/article/details/51305910

The specific problems of the minimum spanning tree can be explained in the following languages:
Input: a non-weighted graph g= (v,e), for each edge (u, V) belongs to E, there is a weight of W.

Output: the smallest spanning tree of the graph, which is a tree that connects all the vertices, and the weights and minimums of the edges in the Tree.

For example, the minimum spanning tree to be obtained is as follows:

The problem is the process of solving an optimal Solution. So what's the best?

first, we consider the optimal substructure: if the optimal solution of a problem contains the optimal solution of the sub-problem, then the problem has the optimal sub-structure.

The minimum spanning tree satisfies the optimal substructure, which is shown below:

Optimal substructure description: Suppose we've got a graph of the smallest spanning tree (MST) T, (u, v) is any edge in this Tree. :

Now that we've removed this edge, we've got two branches of T1 and T2,

T1 is the smallest spanning tree of Figure g1= (V1, E1), G1 is a sub-graph of Figure G exported by T1 vertex, e1={(x, y) ∈e, x, y∈v1}

similarly, the T2 is the smallest spanning tree of Figure g2= (V2, E2), G2 is a sub-graph of the graph G exported by the T2 vertex, e2={(x, y) ∈e, x, y∈v2}

Now let's prove the conclusion: using the clipping method. W (t) represents the weights and values of the T-tree.

First the weight relationship satisfies: w (T) = w (u, v) +w (T1) +w (T2)

Assuming that there is a tree T1 ' more suitable for figure G1 than T1, then there is a T ' ={(u,v)}ut1 ' UT2 ', then T ' will be better suited to figure g than t, which contradicts T is the optimal Solution. Evidence.

therefore, If the minimum spanning tree has the optimal substructure, does it still have overlapping Sub-problem properties? We can find that, regardless of the deletion of the edge, the above-mentioned optimal substructure properties are satisfied, can be equally solved, so it is to meet the nature of overlapping sub-problems.

Considering this, we might think: that means the minimum spanning tree can be done with dynamic programming? yes, That's fine, but it's very expensive.

We can also see that it has a more powerful nature: the greedy nature of choice. Thus the greedy algorithm can be used to Complete.

Greedy algorithm features: a local optimal solution is also the global optimal Solution.

Greedy selection of the minimum spanning tree: make t the smallest spanning tree of figure g, another a? v, assuming that the edge (u, v) ∈e is the minimum weight edge of the complement (I.E. V-a) connected to a to a, then (u, V) belongs to the smallest spanning tree.

Proof: Suppose (u, v)? T, Use the clipping method. Now for analysis, the point of a in the figure is represented by a hollow point, and the point of the v-a is represented by a solid point:

In the t-tree, consider a simple path from u to v (note that now (u, V) is not in t), and according to the nature of the tree, it is Unique.

Now put (u, v) and the first link in the path of a and v-a, that is, the edge of the red bar, the Edge (u, V) is connected to a and v-a of the weight of the minimum edge, then we have a smaller tree, which is the minimum spanning tree with T is the Contradiction. Evidence.

now, Let's take a look at Prim's idea that the prim algorithm is characterized by the edge of the set E that always forms a single tree. The tree starts at any root vertex s and gradually forms until the tree covers all the vertices in V. Each edge that is added to the tree is the edge that makes the Tree's weight as small as Possible. Thus the above strategy is "greedy".

I am the porter of nature-----http://blog.csdn.net/wsniyufang/article/details/6747392

The smallest tree diagram, which specifies a special point root for a weighted graph, asks for a root-rooted, forward spanning tree t, and minimizes the total weight of all edges in T. The first algorithm for the minimum tree diagram is the one proposed by Zhu Yongzin and Liuzhenhong in the 1965 for the complexity of O (VE).
It is very simple to determine if there is a tree diagram, so it is only possible to do a graph traversal with V as the root, so the following algorithm no longer considers the absence of tree diagrams.
Before all operations begin, we need to clear all the self-loops in the Diagram. It is clear that the self-loop is not possible on any tree chart. Only by doing this, finally the complexity of the method is really guaranteed to be O (VE).
First for each point outside of the root, one is selected into the edge, which must be the smallest of all into the Edge. Now all of the smallest incoming edges have been selected, and if there is no Shang in this set of edges, we can prove that this set is the smallest tree of the Graph. The proof is not very difficult. If there is a Shang, we are going to call this an artificial vertex of the ring, and change the right side of the Graph. Assuming that a certain point u is on the ring and that the Benquan that points to u in this ring are in[u], then for each edge starting from U (u, i, w), the edge of the new diagram is connected (new, i, w), where new is the newly added artificial vertex; For each edge entering u (i, u, w), Edge (i, new, W-in[u]) is established in the new Diagram. Why the right to enter the edge minus in[u], This is explained later, here first gives the steps of the ALGORITHM. Then it can be proved that the right of the smallest tree diagram in the new graph plus the right of the ring that was shrunk in the old graph is the right of the smallest tree diagram in the original Image.
The above conclusions do not Prove. now, based on the above conclusions, explain why the right of the edge is not changed, the right to enter the edge minus in [u]. For the minimum tree map T in the new diagram, the edge that points to the human node is E. When the human node is expanded, e points to a ring. Assuming that the original E is pointing to u, this time we will be the ring on the edge of U in[u] delete, so that the original image of a tree. We will find that if the right W ' (e) of E in the new diagram is the right of E in the original image (e) minus in[u], then when we remove in[u] and restore E to the original state, the right of the tree is still the right plus ring of the new graph, and this weight is the weight of the least-squares Graph. So after the expansion of the node, we still get the smallest tree figure. By gradually expanding all the artificial nodes, you will get the minimum tree diagram of the initial Graph.
If the implementation is very smart, you can reach the minimum incoming O (e), look for the ring o (v), contraction o (e), where the ring O (v) requires a bit of skill here. So the complexity of each contraction is O (E), and then it shrinks a few times? Since we have taken all the rings from the beginning, we can know that each ring contains at least 2 points and shrinks to 1 points, reducing the total number of points by at least 1. When the entire graph shrinks to only 1 points, the smallest tree diagram does not need to be asked. So we can only do V-1 shrinkage at most, so the complexity is naturally o (VE). thus, If the self-ring is not removed at first, the theoretical complexity will be related to the number of self-loops.
======================== Split line from Sasuke_scut blog=====================================================

The following is the structure diagram of Zhu Liu algorithm

#include <cstdio>#include<iostream>#include<queue>#include<Set>#include<ctime>#include<algorithm>#include<cmath>#include<vector>#include<map>#include<cstring>using namespacestd;Const Doubleeps=1e-Ten;#defineM 109#defineType DoubleConstType inf= (1) << -;structpoint {Doublex, y;} p[m];Doubledis (point a,point b) {returnSQRT ((a.x-b.x) * (a.x-b.x) + (a.y-b.y) * (a.y-b.y));}structnode{intu, v; Type cost;} E[m*m+5];intPre[m],id[m],vis[m];type in[m];intn,m; type Directed_mst (intRootintNV,intNE) {TYPE ret=0;  while(true) {        //1. Find the smallest entry edge         for(intI=0; I<nv;i++) in[i] =inf;  for(intI=0; i<ne;i++){            intU =e[i].u; intv =e[i].v; if(e[i].cost < in[v] && u! =V) {pre[v]=u; in[v]=e[i].cost; }        }         for(intI=0; i<nv;i++) {            if(i = = Root)Continue; if(in[i] = = Inf)return-1;//The root cannot reach it except with a little in the Side.        }        //2. Find a ring        intCntnode =0; //CC (id,-1); //CC (vis,-1);Memset (id,-1,sizeof(ID)); Memset (vis,-1,sizeof(vis)); in[root]=0;  for(intI=0; I<nv;i++) {//Mark each ringRET + =in[i]; intv =i;  while(vis[v]! = i && id[v] = =-1&& v! =Root) {vis[v]=i; V=pre[v]; }            if(v! = root && id[v] = =-1) {                 for(intU = pre[v]; u! = v; U =Pre[u]) {id[u]=cntnode; } id[v]= Cntnode + +; }        }        if(cntnode = =0) break;//No ring         for(intI=0; I<nv;i++)if(id[i] = =-1) {id[i]= Cntnode + +; }        //3. indent, Re-mark         for(intI=0; i<ne;i++) {            intv =e[i].v; E[I].U=id[e[i].u]; E[I].V=id[e[i].v]; if(e[i].u! =E[i].v) {e[i].cost-=in[v]; }} NV=cntnode; Root=id[root]; }    returnret;}intmain () { while(SCANF ("%d%d", &n,&m)! =EOF) {        //memset (pre,0,sizeof (pre));         for(intI=0; i<n;i++) scanf ("%LF%LF",&p[i].x,&p[i].y);  for(intI=0; i<m;i++) {scanf ("%d%d",&e[i].u,&e[i].v); E[I].U--; E[I].V--; if(e[i].u!=E[i].v) E[i].cost=Dis (p[e[i].u],p[e[i].v]); Elsee[i].cost=1<< -; } type ans=directed_mst (0, n,m); if(ans==-1) printf ("Poor snoopy\n"); Elseprintf ("%.2f\n", ans); }    return 0;}

The minimum spanning tree poj3164 of the direction-definite root of Zhu Liu algorithm

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.