Prim's algorithm-Minimum Spanning Tree

  • MHB
  • Thread starter evinda
  • Start date
  • Tags
    Tree
In summary, Prim's algorithm finds a minimum spanning tree by considering the weight of the edges leading to each node.
  • #1
evinda
Gold Member
MHB
3,836
0
Hi! (Mmm)

I am looking at Prim's algorithm:

Code:
Prim(G,w,v)

 for each u  ϵ V
      key[u]<-oo // the minimum of the weights of the edges,
                           that connect the vertex u with an other 
                           vertex of the tree
      p[u]<-∅
 key[v]<-0
 Q<-V // priority queue,the vertices are sorted,with respect to key[u]
 while Q≠∅
     u<-Εxtraction_of_minimum(Q)
     for each v ϵ adj[u]
          if v ϵ Q and w(u,v)<key[v] then
             p[v]<-u
             key[v]<-w(u,v)

and..I want to apply it at this graph:

View attachment 3111

These are the keys that I found:

View attachment 3112

Is it right,or have I done something wrong? (Thinking)

Which vertices do I have to connect now,to get a minimum spanning tree? (Thinking)
 

Attachments

  • prim.png
    prim.png
    5.7 KB · Views: 97
  • prim.png
    prim.png
    3.8 KB · Views: 101
Physics news on Phys.org
  • #2
Hey! (Smile)

evinda said:
Is it right,or have I done something wrong? (Thinking)

It is right! (Nod)
Which vertices do I have to connect now,to get a minimum spanning tree? (Thinking)

Well, you were also supposed to track p[v] for every step.
p[v] identifies the node you were coming from when assigning the minimum weight.

Did you track p[v]? (Wondering)
Or can you otherwise still figure out what each p[v] should be? (Wondering)
 
  • #3
evinda said:
Hi! (Mmm)

I am looking at Prim's algorithm:

Code:
Prim(G,w,v)

 for each u  ϵ V
      key[u]<-oo // the minimum of the weights of the edges,
                           that connect the vertex u with an other 
                           vertex of the tree
      p[u]<-∅
 key[v]<-0
 Q<-V // priority queue,the vertices are sorted,with respect to key[u]
 while Q≠∅
     u<-Εxtraction_of_minimum(Q)
     for each v ϵ adj[u]
          if v ϵ Q and w(u,v)<key[v] then
             p[v]<-u
             key[v]<-w(u,v)

and..I want to apply it at this graph:

https://www.physicsforums.com/attachments/3111

These are the keys that I found:

https://www.physicsforums.com/attachments/3112

Is it right,or have I done something wrong? (Thinking)

Which vertices do I have to connect now,to get a minimum spanning tree? (Thinking)

This problem is easy enough to do by hand. To find the minimal spanning tree, the process is always to find the minimum sized edge connecting a node that isn't already connected to the tree, until all the nodes are connected. Just keep a mental track of everything that is now connected.

So looking at the graph, the minimal node is 1, between "g" and "h".

The next minimal node is 2, between "g" and "f". Now f, g, h are connected.

The next minimal node is 2, between "c" and "i". Now f, g, h are connected, and c, i are connected.

The next minimal node is 4, connecting "c" with "f". Now c, f, g, h, i are connected.

The next minimal node is 4, connecting "a" with "b". Now c, f, g, h, i are connected, and a, b are connected.

The next minimal node is 7, connecting "c" with "d". Now c, d, f, g, h, i are connected, and a, b are connected. Notice we didn't use the 6, because g and i are already connected in the tree.

The next minimal node is 8, connecting "a" with "h". Now a, b, c, d, f, g, h, i are connected.

The next minimal node is 9, connecting "d" with "e". Now a, b, c, d, e, f, g, h, i are connected.

Now that everything is connected we have our minimal spanning tree (I'll leave you to draw it and to evaluate the minimal distance).
 
  • #4
I like Serena said:
Well, you were also supposed to track p[v] for every step.
p[v] identifies the node you were coming from when assigning the minimum weight.

Did you track p[v]? (Wondering)
Or can you otherwise still figure out what each p[v] should be? (Wondering)

I found the following:

$$p[e]=d$$
$$p[h]=g$$
$$p[g]=f$$
$$p[d]=c$$
$$p[f]=c$$
$$p=c$$
$$p[c]=b$$
$$p=a$$

So..applying the algorithm,do we get this minimum spanning tree? (Thinking)

View attachment 3119
 

Attachments

  • spanning.png
    spanning.png
    4.3 KB · Views: 90
  • #5
evinda said:
So..applying the algorithm,do we get this minimum spanning tree? (Thinking)

Yep! (Happy)

Did you notice that the number you had for each node, was actually the weight of edge that led to that node?
So you could construct the minimum spanning tree like that as well.

Would that always work? (Wondering)
 
  • #6
I like Serena said:
Yep! (Happy)

Since I found these:

$$p[e]=d$$
$$p[h]=g$$
$$p[g]=f$$
$$p[d]=c$$
$$p[f]=c$$
$$p=c$$
$$p[c]=b$$
$$p=a$$

does this mean that this minimum spanning tree is the only one? (Thinking)
I like Serena said:
Did you notice that the number you had for each node, was actually the weight of edge that led to that node?
So you could construct the minimum spanning tree like that as well.

Would that always work? (Wondering)

So,if we have , for example, a node $u$ and the edges $(u,v)$, $(u,w)$ and $(u,r)$ and $w(u,v)< w(u,w)<w(u,r)$,then do we connect $u$ with $v$? (Thinking)
 
  • #7
evinda said:
does this mean that this minimum spanning tree is the only one? (Thinking)

Why do you think so? (Wasntme)
So,if we have , for example, a node $u$ and the edges $(u,v)$, $(u,w)$ and $(u,r)$ and $w(u,v)< w(u,w)<w(u,r)$,then do we connect $u$ with $v$? (Thinking)

Yes.

Now suppose $w(u,v)= w(u,w)$, doesn't that give you 2 choices leading to different trees? (Wondering)
 
  • #8
I like Serena said:
Why do you think so? (Wasntme)

I thought so,because I found specific $p[v], \forall v \in V$..So,could I also find other ones? (Thinking)
I like Serena said:
Yes.

Now suppose $w(u,v)= w(u,w)$, doesn't that give you 2 choice leading to different trees? (Wondering)

Yes! (Nod)
 
  • #9
evinda said:
I thought so,because I found specific $p[v], \forall v \in V$..So,could I also find other ones? (Thinking)

Generally, yes, because $p[v]$ is selected to be the first neighbor with lowest weight. There might be other neighbors with the same weight.

In this particular example, there are no other ones though. (Wink)
 

FAQ: Prim's algorithm-Minimum Spanning Tree

What is Prim's algorithm and how does it work?

Prim's algorithm is a greedy algorithm used to find the minimum spanning tree in a weighted graph. It works by starting at a random vertex and then repeatedly adding the cheapest edge that connects that vertex to a new vertex until all vertices are included in the tree.

How is Prim's algorithm different from Kruskal's algorithm?

Both Prim's and Kruskal's algorithms are used to find the minimum spanning tree, but they differ in their approach. Prim's algorithm starts from a single vertex and grows the tree, while Kruskal's algorithm starts with the cheapest edge and grows the tree by adding edges that do not form a cycle.

What is the time complexity of Prim's algorithm?

The time complexity of Prim's algorithm is O(E log V), where E is the number of edges and V is the number of vertices in the graph. This makes it a more efficient algorithm for finding the minimum spanning tree compared to other algorithms like Kruskal's which has a time complexity of O(E log E).

How do you implement Prim's algorithm?

To implement Prim's algorithm, we need to maintain a priority queue to keep track of the cheapest edges. We also need to keep track of the vertices that have been added to the tree and the ones that are still waiting to be added. The algorithm can be implemented using a variety of data structures, such as arrays, linked lists, or heaps.

When is Prim's algorithm used in real-world applications?

Prim's algorithm is commonly used in network design and routing protocols, such as in telecommunication networks and computer networks. It is also used in circuit design and in finding the shortest path in a road network. In general, any application that involves finding the minimum cost of connecting multiple points can benefit from using Prim's algorithm.

Similar threads

Replies
11
Views
3K
Replies
13
Views
3K
Replies
6
Views
2K
Replies
1
Views
2K
Replies
22
Views
1K
Replies
2
Views
2K
Replies
6
Views
2K
Back
Top