Minimum Spanning Tree-Kruskal Algorithm

Source: Internet
Author: User

Because this algorithm is relatively simple and the content pages on the Internet are quite rich, I will simply talk about it here. The core idea of the Kruskal algorithm is to take edge as the main character and put the short edge into the set in order to select only those that do not constitute the ring, until all vertices exist in the set.

The theoretical basis of this algorithm is the nature of the Minimum Spanning Tree:

 

(For example: V, V-U), and (u, v) has the minimum weight, then the minimum spanning tree property: Set G = (V, E) is a connected network, U is a non-empty real subset of vertex set v. If (u, v) is an entry in G ), A minimum spanning tree whose edge is not in u must exist in G contains this edge (u, v ).

To sum up, the minimum spanning tree will certainly pass through the edge with a small weight, which is consistent with our intuitive cognition.

 

Here we want to scatter several key points:

1. Edge sorting.

There are a variety of sorting algorithms. If you are lazy, you can use the ready-made library. Otherwise, you can pick a proper one.

2. Determine whether to connect.

The connectivity issue is included in Chapter 1 in introduction to algorithm C language implementation. The core idea is to use an array to indicate each vertex, for example, the vertex 0-3 is initialized to [0, 1, 2, 3]. These four vertices are not connected, if point 0 and 3 are connected, the array can be changed to [3, 1, 2, 3]. The numbers 0 and 3 are connected. in the program, a [A [0] = 3 can be seen through the subscript. At the same time, a [3] = 3. Is it like a tree that returns from the leaf node to the root.

 

The code below:

some contents are not in the Code, such as the encapsulation of graph, and many connectors that determine whether to connect are not in the following code.

# Encoding: utf-8require '. /graph. RB 'def build_tree graph = graph. new [0, 1, 2, 3, 4, 5, 6]. each {| VT | graph. add_v (vertex. new VT)} newg = connector. new Graph. vertexes. size # Three-element array, indicating vertices, vertices, weights [0, 1, 7], [0, 3, 5], [1, 3, 9], [1, 2, 8], [2, 4, 7], [0, 4, 5], [3, 4, 15], [3, 5, 6], [4, 5, 8], [4, 6, 9], [5, 6, 11]. each {| edge | graph. add_e (edge. new Edge [0], edge [1], edge [2])} # Kruskal algorithm require 'Set' Vs = set. New Graph. vertexes. Each {| v | vs. Add v. ID} edges = graph. edges. Clone edges. sort_by! {| Edge. weight} puts "edges is # {edges}" des = set. new Path = set. new # If vs is empty, loop finish while true # vertex set, edge set if edges. nil? Or edges. Size. Equal? 0 break end # Pick shortest edge shortest = edges [0] # puts "shortes # {shortest}" edges = edges [1 .. -1] If newg. is_connect shortest. from, shortest. to # If newg. connected? Shortest. to, shortest. from next else des. add shortest. from des. add shortest. to. delete shortest. from. delete shortest. to path. add shortest newg. connect shortest. from, shortest. to end if. empty? Break end Total = 0 path. Each {| edge | puts edge. to_s; Total + = edge. Weight} puts totalendbuild_tree

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.