The minimum spanning tree (a)-kruskal algorithm for graphs

Source: Internet
Author: User

Minimum spanning tree to ask:


The data are given as follows:


The first line has two numbers, n represents n cities, m represents M roads, and the next M row, three numbers per row a,b,c represents city A to City B's distance C.


What needs to be solved now is to require a minimum of Bentour-Guenton (any two points can be reached between each other). To connect the graphs of n vertices, you need to n-1 at least one edge. In fact, this is the smallest spanning tree to find a graph.


Basic ideas:

First, according to the weight of the edge of the order from small to large, each time from the remaining edge of the selection of the weight is small and the edge of the two vertices are not in the same set ( all the vertices into one and check the concentration, to determine whether the two vertices are connected, just to determine whether the two vertices in the same set, that is, whether The edge of the time Complexity O (LOGN)) is added to the spanning tree until the n-1 edge is added.



Run results after entering the above data:


Time complexity of the Kruskal algorithm:

The Edge is fast O (MLOGM), in the M-bar to find n-1 Edge is O (Mlogn), all Kruskal algorithm time complexity is O (MLOGM+MLOGN).

Usually M is much larger than N, so the final time complexity is O (MLOGM).




The minimum spanning tree (a)-kruskal algorithm for 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.