Reprint: Minimum spanning tree-prim algorithm and Kruskal algorithm

Source: Internet
Author: User

This digest from: http://www.cnblogs.com/biyeymyhjob/archive/2012/07/30/2615542.html minimum spanning tree-prim algorithm and Kruskal algorithm

Prim algorithm

1. Overview

Primm algorithm (prim algorithm), an algorithm in graph theory, can search for the smallest spanning tree in weighted connected graphs. This means that the subset of edges searched by this algorithm includes not only all the vertices in the connected graph ( English :Vertex (graph theory)), but also the sum of the weights of all the edges is minimal. The algorithm was discovered in 1930 by the Czech mathematician Voytech Jarnik ( English :vojtěch Jarník), and in 1957 by the American computer scientist Robert Prim ( english :Robert c. Prim) independent discovery; In 1959, Aizeg Dicos found the algorithm again. Therefore, in some cases, the Primm algorithm is also known as the DJP algorithm, the Delphi algorithm, or the Primm-Rockwell algorithm.

2. Simple description of the algorithm

1). Input: A weighted connected graph, where the vertex set is V and the edge set is E;

2). Initialize: Vnew = {x}, where x is any node in the Set V (Start point), enew = {}, is empty;

3). Repeat the following operations until Vnew = V:

A. Select the edge <u with the least weight in set E, V>, where U is the element in the collection vnew, and V is not in the Vnew collection, and v∈v (if there are several edges that satisfy the foregoing conditions, one of them can be arbitrarily selected);

B. Add v to the Set vnew, add <u, v> side to the set enew;

4). Output: Use the collection vnew and enew to describe the resulting minimum spanning tree.

The following is a description of the algorithm's legend

legend Description not selectable options available selected (vnew)

This is the original weighted connected graph. The number on each side of the edge represents its weight value. - - -

Vertex D is arbitrarily selected as the starting point. Vertices A,B,E , and F are connected to D through A single edge. a is the closest vertex of the distance D , so a and the corresponding side AD are highlighted. C, G A, B, E, F D

The next vertex is the nearest vertex from D or a . B distance D is 9, distance A is 7,E is a,F is 6. Therefore,F is nearest to D or A , so the vertex F is highlighted with the corresponding edge DF . C, G B, E, F A, D
The algorithm continues to repeat the above steps. Vertex B with distance a of 7 is highlighted. C B, E, G A, D, F

In the current case, you can choose between C,E and G . C distance b is 8,E is 7, andG is from F 11. e is recently highlighted as Vertex e with the corresponding edge be. No C, E, G A, D, F, B

Here, the vertices available for selection are only C and G. c is 5,G is 9, so Cis selected and highlighted with the side EC . No C, G A, D, F, B, E

Vertex G is the only remaining vertex, which is 11 from F , 9 from e , ande is recently highlighted as G and the corresponding edge EG. No G A, D, F, B, E, C

All vertices are now selected, and the green part of the graph is the smallest spanning tree of the connected graph. In this example, the sum of the weights of the minimum spanning tree is 39. No No A, D, F, B, E, C, G

Kruskal algorithm

1. Overview

The Kruskal algorithm is an algorithm for finding the smallest spanning tree, published by Joseph Kruskal in 1956. The prim algorithm and Boruvka algorithm are also used to solve the same problem. Three kinds of algorithms are the application of greedy algorithm. Unlike the Boruvka algorithm, the Kruskal algorithm is also effective when there are edges with the same weights in the diagram.

2. Simple description of the algorithm

1). Graph with v vertices, e-sides

2). The new diagram Graphnew,graphnew has the same e vertex in the original, but no edge

3). Sort all E edges in the original graph by weights from small to large

4). Loop: Expediency The least-valued edge begins to traverse each edge until all the nodes in the graph graph are in the same connected component

If the two nodes connected by this edge are not in the same connected component in Figure Graphnew

Add this edge to figure graphnew

Legend Description:

First step, we have a graph graph with a number of points and edges

The lengths of all edges are sorted, with the result of sorting as the basis for our selection of edges. This once again embodies the idea of the greedy algorithm. Resource sequencing, the selection of local optimal resources, sorting completed, we took the lead in selecting the side AD. So our diagram becomes the right image.

Look for the rest of the changes. We found the CE. The weight of the side is also 5.

And so on we find the 6,7,7, namely Df,ab,be.

Continue selection below, BC or EF Although now the length of the 8 edge is the smallest of the selected edges. But now they are connected (for BC you can connect via Ce,eb, and a similar EF can go through eb,ba,ad,df). So there's no need to choose them. Similar BD is already connected (here the connecting lines are in red).

Finally, eg and FG are left. Of course we have chosen eg. The final success chart is right:

3. Simple Proof Kruskal algorithm

The vertex number N of the graph is summed up, and the Kruskal algorithm is proved to be applicable to any n-order graph.

Inductive basis:

N=1, it is clear that the smallest spanning tree can be found.

Induction process:

Assuming that the Kruskal algorithm is applicable to the n≤k order, then, in the k+1 order G, we take the shortest side of the two endpoints A and b a merge operation, that is, you and V together to a point V ', the original connected to the side of the V ' up to V ' up, so that you can get a K-order G ' (u,v merge is 1 less one edge), the G ' minimum spanning tree T ' can be obtained using the Kruskal algorithm.

We prove T ' +{<u,v>} is the smallest spanning tree of G.

If T ' +{<u,v>} is not the minimum spanning tree, the minimum spanning tree is T, i.e. W (t) <w (t ' +{<u,v>}). It is obvious that T should contain <u,v> otherwise, you can add <u,v> to T to form a ring, delete any of the existing edges on the ring and form a spanning tree with a smaller weight. and T-{<u,v>}, is the generation tree of G '. So W (T-{<U,V>}) <=w (t '), which is W (t) <=w (t ') +w (<u,v>) =w (t ' +{<u,v>}), creates a contradiction. Therefore, the hypothesis is not established, T ' +{<u,v>} is the smallest spanning tree of G, and the Kruskal algorithm is applicable to k+1 order.

By the mathematical induction method, the Kruskal algorithm is proven.

Reprint: Minimum spanning tree-prim algorithm and Kruskal algorithm

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.