Data Structure exercise 22-query the set and the Minimum Spanning Tree of the graph

Source: Internet
Author: User
Tags cmath
Preface

This blog introduces and queries the set and the Minimum Spanning Tree of the graph. For the query set, we introduce its basic concepts, analyze its complexity, and finally use the query set to analyze the Minimum Spanning Tree.

And query the reference documents 《 Algorithm Introduction: Blog: http://blog.csdn.net/dm_vincent/article/details/7655764. The query set includes three operations: make_set, find_set, and union_set. Linked List representation: each node includes a value and two pointers: one pointing to the next node, one pointing to the header node, and one head and one tail. Each set represents a header node, therefore, each node has to point to the header node, so that you can quickly locate the header node. Complexity:

The root of the tree indicates that two optimizations can be introduced, which are merged by rank and compressed by path. When this optimization is used at the same time, the worst run time is: O (M *α (N)), M is the total number of operations,α (n) is a growth and slow function, usuallyα (n) <= 4; therefore, the complexity of the worst case can be regarded as linear;

Minimum Graph Generation tree

As a complex data structure, a graph is a simple example of graph traversal. Next, we will briefly introduce the Minimum Spanning Tree.

Related Concepts:

Connected Graph: For an undirected graph, if there is a path between any two vertices, the undirected graph is connected;

Strongly Connected Graph: For a directed graph, if any two vertices in the graph have a directed path, the directed graph is strongly connected;

Connected Component: each connected subgraph in a non-connected graph is the connected component of the graph.

To solve the problem of least spanning tree, we use greedy ideas, query sets, and recursion.

Kruskal + and query set implementation:
 # include 
  
    # include 
   
     # include 
    
      # include 
     
       # define bug puts ("here !!! "); Using namespace STD; const int n = 5005; int pre [N]; // space complexity int n, m; struct node {int U, V; int W ;} E [N]; bool CMP (const node A, const Node B) {return. W 
      
        N, N) {CIN> m; For (INT I = 0; I 
       
         E [I]. u> E [I]. v> E [I]. W ;}kruskal () ;}
       
      
     
    
   
  

Kruskal is an edge-based search. You must first sort the edge and view the current optimal and greedy idea each time.

Prim algorithm: Click to open the link

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.