0

In this MIT video regarding Prims algorithm for minimum spanniing tree the professor explains π[v] ←u at time 71:16 seconds . But I do not understand why we need this step . What does this notation π[v] ←u mean actually ? Also what does the last line in the algorithm that follows mean ? The entire algorithm given in the source is as follows :

Q←V
key[v] ←∞for all v∈V
key[s] ←0for some arbitrary s∈V
while Q≠∅
 do u←EXTRACT-MIN(Q)
   foreach v∈Adj[u]
    do ifv∈Qand w(u, v) < key[v]
      then key[v] ←w(u, v)⊳DECREASE-KEY
           π[v] ←u

At the end, {(v, π[v])}forms the MST
Geek
  • 26,489
  • 43
  • 149
  • 227

1 Answers1

2

π is just any old array variable. So this line of code isn’t really different from the other assignments.

What it does in the algorithm however is save the predecessor node of the current node. π is sometimes also called the predecessor function because for any given node n, π[n] gives you the predecessor of that node (after the algorithm has completed).

So π can be used to reconstruct the path (= the edges of the spanning tree) found by Prim’s algorithm.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • you said "π is just any old array variable" .Why does he not access the array with π[someIndex] then ? – Geek Oct 01 '12 at 15:27
  • @Geek Ok, it’s an [*associative array*](http://en.wikipedia.org/wiki/Associative_array). But the same is true for `key` and `Adj` in your code. An indexed array is simply a special case of an associative array in which the indexers are integers from 0 to N. Most programming languages use “array” to mean the latter, i.e. “associative array indexed by an integer from 0 to N” but this algorithm uses a more general definition. – Konrad Rudolph Oct 01 '12 at 15:28
  • Can you please explain what the notation in the last line mean ? – Geek Oct 01 '12 at 15:31
  • @Geek As close as I can make it out, it’s an error in the pseudocode. It should actually read “At the end, (`v`, `π`) forms the MST”. This simply means that the MST is completely described by giving the node `v` and the predecessor function `π` (i.e. the tuple (`v`, `π`)). But even that is a bit unconventional … as far as I remember, the MST is usually described as a subgraph, i.e. by a set of nodes and vertices (but it can be shown that these two forms are in fact equivalent). – Konrad Rudolph Oct 01 '12 at 15:34