Talk:Prim's algorithm

From Wikipedia, the free encyclopedia

Contents

[edit] C++ code for Prim's using adjacency matrix A

A[i][j] is dstance from node i to node j. Sentinels NONE and INF are used to avoid complex logic. The code is written in "computer olympiad style", using static allocation over STL containers or malloc'd memory. Jettlogic 15:35, 11 October 2006 (UTC)

 #include <algorithm>
 using namespace std;
 #define MAXN 5
 const int NONE = MAXN;
 const int INF = 1000;
 void mst_prim(int N, int A[MAXN][MAXN], int parent[MAXN+1]) {
     bool intree[MAXN+1];
     int distance[MAXN+1];
     fill(distance, distance+MAXN+1, INF);
     fill(parent, parent+MAXN+1, NONE);
     fill(intree, intree+MAXN+1, false);
     int u = 0;
     while(u != NONE) {
         intree[u] = true;
         // next u is closest not-in-tree vertex
         int nu = NONE;
         for(int v = 0; v < N; v++)
             if(!intree[v]) {
                 if(A[u][v] < distance[v]) {
                     distance[v] = A[u][v];
                     parent[v] = u;
                 }
                 if(distance[v] < distance[nu])
                     nu = v;
             }
         u = nu;
     }
 }


[edit] Colored lines in graph

Are there red and green lines on this graph? I am colourblind and cannot tell. A more apropriate choice of colours would be red and blue.

  • JA: They all look black on my browser. I suggest solid and dashed. Jon Awbrey 23:10, 15 February 2006 (UTC)
    • I agree, or thick and thin lines would work too. They should also be SVGs. Deco 23:19, 15 February 2006 (UTC)
      • Hi, I made those graphs; sorry about the colours, I did not realise it could be problematic. I will try some other styles (solid and dashed sounds good) and add them here when they are done. Shen 14:54, 16 February 2006 (UTC)
        • Hi, thanks very much for taking the time to make the graphs and considering my suggested revisions

Ok, I made some new graphs:

The current image - no change
Enlarge
The current image - no change
Thicker and thinner lines
Enlarge
Thicker and thinner lines
Dotted and dashed lines
Enlarge
Dotted and dashed lines
Opacity changes
Enlarge
Opacity changes
Which type would work best? Shen 16:44, 17 February 2006 (UTC)
I made some new images using thinner and thicker lines, it should be clearer now. Shen 22:06, 19 February 2006 (UTC)

[edit] Suggestion of a better explanation

I propose to replace the item list in the beginning of the page with this:

  1. Choose a vertex of the graph G as inital tree E
  2. Create a set of all edges in G connecting a vertice of E to a vertice outside of E
  3. Out of this set, choose an edge with the lowest weight and add it with the respective vertice to E
  4. Repeat 2-3 until all vertices of G are in E

E is the minimum spanning tree.


I agree with this one. The step "remove from the set an edge with minimum weight that connects a vertex in the tree with a vertex not in the tree" is imho not O(log E). --Pukeye 09:04, 28 August 2006 (UTC)

It is if you use a heap.

[edit] Don't Forget!

I agree that a better pseudo algorithm is needed here I also think that you NEED to mention that a edge should only be used if it results in a cycle, which is obvious to some, but really should be spelt out.

Don't you mean if it *doesn't* result in a cycle? - Solberg 06:57, 7 July 2006 (UTC)Solberg

[edit] Something isn't right

From Cormen et all, the psuedocode for this Algorithm is: (Used only for discussion, do not use in article)

MST-PRIM(G, w, r)
01  for each u ∈ V [G]
02    do key[u] ← ∞
03      π[u] ← NIL
04  key[r] ← 0
05  Q ← V [G]
06  while Q ≠ Ø
07    do u ← EXTRACT-MIN(Q)
08      for each v ∈ Adj[u]
09        do if v ∈ Q and w(u, v) < key[v]
10          then π[v] ← u
11            key[v] ← w(u, v)

From this it appears that the explanation above (and in the article) is wrong, also it appears to me that the graphs are wrong.

The important difference: For each iteration of the while loop (lines 6-11), the minimum accessible vertex (and not edge) is used.

For this vertex u, each adjacent vertex v which hasn't been explored is considered and if the 'cut' crossing to it from u is least found so far, u is set as parent to v and v's key is updated.

The algorithm's progress should be as follows (when starting from the article's graph):

When starting from a (in the graphs), both b and d should have been connected to a at the first iteration. The next iteration would have explored d as it is the vertex with the least key that is still in the queue.

From d, the exploration would have considered b but would not have connected them in the MST, because b's weight from d is larger than from a. Also e and f would have been connected initially from d, but later e might have been updated to connect from b when it is explored. --Opium 12:01, 5 September 2006 (UTC) Updated again Opium 08:40, 6 September 2006 (UTC)

  • I don't think that's right. From my understanding, Prim's works like this:
- Let T the subtree of G consisting solely of the starting node
- While there are nodes in G that are not in T:
  - Find the node in G but not in T which has the least edge connecting to a node in T
  - Add that node and the corresponding least edge to T
- T is then the minimum spanning tree


[edit] Corrections

I agree with the last poster directly above this ("I don't think that's right...").. I made some corrections to the algorithm description, updated the runtime costs section, and tied the pseudocode to the initial algorithm description. The approach presented is a reworking of the ideas presented in n the Levitin 'Design and Analysis of Algorithms' text. To me the pictorial description of the process is fine. Let me know what you think.. maybe we can remove the 'contradictions' tag? --Turketwh 05:06, 17 November 2006 (UTC)