Explain in detail the minimum spanning tree (one to understand!) & Kruskal algorithm

0. Introduction

Because it was too betel a 

I now even NOIP of the preliminary round are scared and I can not even minimum spanning tree have not learned 

So that a blog must be the most detailed QAQ Haha

Please carefully read if there are omissions please correct me a message thanks!

Thanks ♪ (· ω ·) Bruno

1. The minimum spanning tree concept

Minimum Spanning Tree in the end what is it? Puzzled face

 

A connected graph with n vertices of minimal spanning tree is connected subgraph original image, and contains all the n nodes in the original graph, and have a minimum of side holding FIG communication

                     - from Baidu Encyclopedia

 

 

In a given non-= (V, E) in, (u, v) represents a linking edge vertex u to vertex v (i.e.) to FIG. G, and W (u, v) on behalf of this side of the right weight, if present E T is a subset (i.e.,) and is acyclic graph, such that

 

The W (T) minimum, this minimum spanning tree T of G.

 

 

Minimum spanning tree is actually a minimum weight spanning tree for short.
 

 

 Then we will understand

The so-called minimum spanning tree is not so difficult

No minimum spanning tree is selected in a figure to the edge weights and a minimum of a sub-tree, and contains all the nodes!

So we are very happy ♪ (^ ∇ ^ *) completed the definition of understanding!

Cartoon fight off! (* ^ ▽ ^ *)

2.kruskal algorithm and explain template

Next we explain how to achieve the minimum spanning tree above it

Here we must elicit our kruskal

 

The core idea of ​​Kruskal algorithm is: constantly find the smallest edge in the set of edges in the weighted connected graph if the edge to meet the conditions to get the minimum spanning tree, it will be constructed, until finally get a minimum spanning Tree.

Step Kruskal algorithm:
Step 1: the right to communicate with the figures, the values are sorted right side;
the second step: determining whether to choose this edge (in this case by edges in the graph has small weight to large sorted). Based on the determination whether a vertex has two communication sides, if the next communication continues; if none, then choose to make communication.
The third step: The second step cycle, until all of the figures are in the same vertices in a connected component, i.e., to obtain the minimum spanning tree.

 

 It looks very simple matter

Template as follows (my hard finishing)

#include <bits / STDC ++ H.>
 the using  namespace STD;
 struct Edge { int U, V, W;} Edge [ 200005 ];
 int FA [ 5005 ], n-, m, ANS, EU, EV, CNT; 
inline BOOL CMP (Edge a, Edge B) { return AW <BW;} // fast row basis 
inline int Find ( int X) {
     the while (! FA X = [X]) = FA X [X] = FA [FA [X ]];
     return X; 
} // disjoint-set template, while loop faster than the recursive version 
inline void Kruskal () { 
   
   
    Sort (Edge, Edge + m, CMP); //The weight of the edge values are sorted 
   
    for ( int I = 0 ; I <m; I ++ ) { 
   
        EU = Find (Edge [I] .u), EV = Find (Edge [I] .v);
         IF (EU == EV ) Continue ; // if ring occurs, Continue 
        ANS = Edge + [I] .W; // update answer 
        FA [EV] = EU; CNT ++ ;
         IF (CNT N- == . 1 ) BREAK ; // loop end condition 
    } 
} 
int main () { 
    Scanf ( " % D% D " , & n-, & m);
     for ( int I = . 1;i<=n;i++) fa[i]=i;//初始化并查集
    for(int i=0;i<m;i++)
        scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
    kruskal();
    printf("%d",ans);
    return 0;
}

3. Postscript

Are there any questions after reading it?

In fact, just think about it and then combine information, take a look at the code and schematics is easy to understand

Chan is a point to look at and then go - thanks to slightly Thanks ♪ (· ω ·) Techno

 

Guess you like

Origin www.cnblogs.com/Tidoblogs/p/11402753.html