"Graph theory" minimum spanning tree

Source: Internet
Author: User
the concept of minimum spanning tree

Objective: For n nodes and the connected graphs with weighted edges of M strips, cut off several edges to make the remaining edges weighted and minimal, and the graph is a tree (that is, a connected subgraph containing n nodes and n-1) Why the minimum spanning tree is needed

If the weight of the edge is considered to be the cost of connecting two nodes, then the minimum spanning tree can make the original image of each node remain interoperable state, and the lowest. Its essence is a problem of optimization. The basic idea of Kruskal algorithm

The core essence is reverse thinking. You're not letting me get rid of some edges. Then I simply remove all the edges and then start with a small weight to add it back. Each additional edge will be connected to two nodes, if the two nodes themselves are connected, then this side will not be added, if the two nodes themselves are not connected, then this side will need to add back. The last addition of the number of edges to reach the number of nodes minus 1, is a minimum generation number.

Pseudo code is as follows:

Node_list = [A,b,c,d]//All nodes array
tree_list = [{A}, {B}, {C}, {D}]//An array of all the independent trees, so the a,b,c,d four nodes become orphaned single node trees because of the beginning of removing all the edges, so the most The end goal is to make the tree_list gradually become [{A, B}, {C}, {d}] => [{A, B}, {C, D}] => [{A, B, C, d}] by adding edges.
edge_list = [E1, E2, E3, E4, E5]//All sides of the container
selected_edge_list = []//The result array of the selected edges, the first is null while

length (selected_e Dge_list) < Length (node_list)-1://Final need to pick out the n-1 edge, because a tree is n-1 edge
    //each time from the edge of the container pops out a minimum weight of the edge
    Min_edge = Get_and_ Pop_min_edge (edge_list)
    /Check whether the Min_edge nodes are in two trees in tree_list, and if so, can connect the two trees if
    can_merge_tree (tree_ List, Min_edge) is True:
        tree_list = Merge_tree (tree_list, Min_edge)
        selected_edge_list.append (Min_edge) return
Selected_edge_list
The basic idea of prim algorithm

The essence of the prim algorithm is to add some method. First randomly select a point as the starting point in the minimum spanning tree A, and other points as a candidate. Each time from the set of candidate points, select a point that reaches the shortest distance from the tree A and add the point and the point to the side of the tree A to the smallest spanning tree a (i.e. one at a time). Until the minimum spanning tree A contains all the points.

This algorithm is relatively easy to understand, pseudo code slightly. Attention

It is obvious that Kruskal and prim algorithms are based on greedy search for local optimization to solve problems. But you have to remember that the local optimal solution is not necessarily the global optimal . Of course, on the problem of minimum spanning tree, both Kruskal and prim algorithms have been shown to be globally optimal. Complexity of Time

The time complexity of the prim algorithm is O (N2), it is independent of the number of edges in the net, which is suitable for dense graphs (i.e., fewer edges), and the Kruskal algorithm is O (Elog e), which is related to the number of edges in the net, and is suitable for sparse graphs (that is, little multilateral).

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.