The minimum spanning tree (two)-prim algorithm for graphs

Source: Internet
Author: User

in the previous article wrote the minimum spanning tree of the graph to find a method-- Kruskal algorithm http://blog.csdn.net/wtyvhreal/article/details/43526695

This article uses another method to solve the graph of the minimum spanning tree, prim algorithm.


Choose a vertex at random to see which side of the vertex it is, and find the shortest one on its side. Number 1th has 1-2, 1-3, of which 1-2 is short, select 1-2. It connects 1 and 2 together. The next step is to enumerate the edges of vertices 1 and 2nd, and see which edges can be connected to vertices that are not selected, and the shorter the edges the better.



The basic idea of prim algorithm:

Divides all vertices in a diagram into two categories: tree vertices (vertices that have already been selected into the spanning tree) and non-tree vertices (vertices that have not yet been selected into the spanning tree). First select any vertex to join the spanning tree, then find an edge to add to the spanning tree, which requires enumerating each tree vertex to the edges of each non-tree vertex, and then finding the shortest side to join to the spanning tree. In turn, repeat the operation N-1 times until all vertices are added to the spanning tree.


Flow of the prim algorithm:



Input data:


Operation Result:


time Complexity O (n^2) , the time complexity of the whole algorithm is reduced to O (MLOGN) If the time complexity of each edge is O (LOGM), and then the adjacency table is used to store the graph.


If all edge weights are not equal, the minimum spanning tree is unique.

The Kruskal algorithm is a step-by-step approach to merging the trees in the forest, while the prim algorithm builds a tree by adding one edge at a time.

The Kruskal algorithm is more suitable for sparse graphs (edge finding), the prim algorithm without heap optimization is suitable for dense graphs (find points), and the prim algorithm using heap optimization is more suitable for sparse graphs.

The minimum spanning tree (two)-prim 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.