Analysis of prim minimum spanning tree algorithm

Source: Internet
Author: User

In the previous blog, the algorithm has been implemented in code. Just I think from the algorithm to the code implementation, this is a great transition, do not like the logical transition of textbook style, although the following written unintelligible, although I am very dish.

After reading the code in the previous blog, think about some of these issues,
1, how to map the relationship between, with the appropriate data structure to represent
2. How to search for a weighted path around a node
3. How to add a new node to the new search library.

The first one.

For this graph relationship, what kind of data structure to represent, here are 22 relations between, can think of trees, queues, and so on, as if can not,
I found a blog post.
http://blog.csdn.net/zscacm/article/details/7209098
Study the representation of a diagram. Essentially, the relationship between the other is too complex, without a fixed precursor and successor. A common array can represent this relationship,

A second question
In accordance with the first step of the prim algorithm,
Select a point as the entry (assuming a), add a to the alternate combination, find the node connected to the set, and find the smallest weight in it.


How to program this idea, has established a two-dimensional array storage of the graph, from the array can be seen, the node connected with a is B and C, is the first line in the array of the relationship embodied.
Here are a lot of problems, a little messy, why to find this relationship, the nature of this relationship is what. What is the core idea of prim.

So put aside the process of looking at the prim algorithm in front

There is an algorithmic process step:
1. Place the first point into the collection of the smallest spanning tree (the tag visit[i]=1 means the minimum spanning tree collection).
2. Initialize Lowcost[i] The weight of the edge (Lowcost[i) that is connected to the 1 point (only connected) is not the minimum weight of this point! will be updated incrementally later).
3. Enumeration of n vertices
4. Add the vertices of the least-weighted edge to the set of the smallest spanning tree (Mark visit[i]= 1) and the weights to add.
5. Update Lowcost[j] collection.
Key steps: The essence is that each add a point to the minimum spanning tree collection, you need to compare this point to the point outside the set, and constantly look for the smallest edge between the two sets.
6. Cycle through the above steps to guide the addition of all vertices to the minimum spanning tree collection.

Looking at the diagram above, think of the process as a cyclic process, so what is the subject of the cycle,
Each cycle is a point added to a collection, and the reason for the decision to join this point is that at all points, it has the smallest weight to the set.

Loop all the time, adding the least-weighted points to the collection until all points have been added.

How do I represent the concept of a collection? What is a collection? A collection is an object that has certain properties that are always together. The simplest is to use variable tags, such as each object contains a member variable, which is used to describe the properties of this object. Here, you can use an array to represent the set, the array subscript indicates which object, and the corresponding array element indicates whether it belongs to the collection element

How do I get the relationship weights between a collection and a node? If the following situation occurs

In other words, there is more than one weight path for a set and a node, and the prim algorithm is required to update the weighted path of the joint and node, guaranteeing the shortest, and then looking for the next node to join the joint.

How to update or, when a new node joins a collection, how to find new nodes that are connected to the Union.

Like what

The current collection contains node 1, and the node to which it is connected is 2, 3, 4
Find the next node Yes, 3
The new collection is then generated, and the nodes connected to the new collection are 3 or 5 less.

And the more out of the node, is actually brought by the newly added node 3. So to expand the update of this connection node library, is to look at node 3 node library.

So summing up the process of prim is

1) Add the first node to the collection, update the outer node library of the collection,
2) Find the node with the least weight from the Outer node library, join the collection, update the outer node library of this set
3) loop 2 until all nodes are joined to the collection.

Here is a piece of code that is re-organized, corresponding to the above steps

vector < int > que;
For storing the out-of-collection node library,
Vector < int > flag;
Used to mark nodes within a collection

The first step:
Add the first node to the collection, update the outer node library of the collection,

loc=0;
Flag[loc]=1; Adding a node for location 0 to a collection
for (i=0;i

intStree (int MapN [N],intNUM) {intI=0;intj=0;intMinintLocintminsum=0; vector < int >Que vector < int >Flag Que.resize (num); Flag.resize (num); for(i=0; i<num;++i) Que[i]=max; Loc=0; flag[loc]=1; for(i=0; i<num;++i) {if(I!=loc) que[i]=Map[Loc] [i]; } for(i=0; i<num-1; ++i) {Min=max; for(j=0; j<num;++j) {if(Min>que[j] && flag[j]!=1) {Min=que[j];   Loc=j;  }} minsum+=min; flag[loc]=1; for(j=0; j<num;++j) {if(flag[j]!=1&&Map[Loc] [J]<que[j]) que[j]=Map[Loc]  [j]; } }returnMinsum;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Analysis of prim minimum spanning tree 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.