"Graph theory" "minimum spanning tree" prim and Kruskal algorithm

Source: Internet
Author: User

In the textbook of data structure, there are two kinds of minimum spanning tree algorithm for graph, one is prim (premium Manaus) algorithm and one is Kruskal (Kruskal) algorithm.

The two algorithms are generated in different ways:

Prim algorithm:

Algorithm idea:

The algorithm idea is to find a distance to generate the nearest point of the collection, join, and then update the distance from the remaining points, continue to iterate.

Algorithm steps:

1. Arbitrarily select a point as the starting point, mark the point as a minimum spanning tree, use an array to indicate that the point has been added, join[i] = 1 to indicate that I point has been added to the set;

2. The minimum spanning tree collection as a whole, calculated to the distance between other points not joined to the collection (jion[i] = = 0), using dis[i], Dis[i] represents the distance between the set distance point I;

3. Then find the smallest distance in dis[i], then I is the next point to join the collection

4. Assign a value of Join[i] to 1 to indicate that you have joined

5. Recalculate Dis[i]. Because there are new points added, determine whether the addition of the point makes the distance between the set distance from the other non-joined points smaller, if there is a smaller, then update dis[i].

6. Proceed to step 3 until all points are added to the join array (in fact it is automatically ended after finding n-1, because the specified starting point, all only need to find n-1 times, that is, for loop from 2 to N)

Question: Prim algorithm has obtained the minimum cost, can not keep the production path, do not know whether their own understanding is not in place or code implementation is unreasonable.

Kruskal algorithm:

Algorithm idea:

First, all edges are sorted by weight from small to large, then an edge is removed, and an attempt is made to make up the collection. If the two points are already in the same set, the description has been added, then skipped, if the collection has not been added, this makes up a new collection, and if it is in a different collection, the collection is merged to form a new collection until the N-1 bar is removed or all points are in a collection, which means that it has been generated.

1. Sort the edges by weight from small to large using the sort algorithm

2. Initially, the point is the constituent set (or self-assembled, which is understood to be not a set), using the join array to represent the collection, initially Join[i] is 0.

3. Take out an edge and judge two points i,j

1) If GROUP[I],GROUP[J] is 0, the description has not been added to the collection, then set group[i] = Group[j] = count (count is used to indicate the number of edges joined and can be used to distinguish the collection)

2) If GROUP[I],GROUP[J] is not equal, the two points are not in the same collection.

If the i,j has a join value of 0 and the description is not joined, then simply set the join value for this point to be the same as the point at which it was added.

If two points have been added to the collection but are in different combinations, then you need to merge the collections at this point. (variable group, set the point of the two set to the same value)

4. All points in the group correspond to the same values, representing the minimum spanning tree collection.

"Graph theory" "minimum spanning tree" prim 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.