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