The number of spanning trees of Kn is nn-2, for n
2, this is Cayley's Formula.
Prüfer Encoding
A Prüfer sequence of length n-2, for n
2, is any sequence of integers between 1 and n, with repetitions allowed.
A labeled tree gives a Prüfer sequence by:
A Prüfer sequence determines a labeled tree by:
Cayley's formula now follows from counting Prüfer sequences of length n-2.
Prim's Algorithm: Grow a tree by picking the frontier edge with the smallest weight.
This is an example of a greedy algorithm.
If the weights are all equal, the problem reduces to finding the s-t path of shortest length. This can be done by using a breadth-first search starting at s, and ending when t is reached.
When the weights are non-negative, but not all equal we can use Dijkstra's algorithm (for situations where negative weights are used, Floyd's algorithm can be used).
Dijkstra's Algorithm
In this tree growing algorithm, vertices that are added to the tree are labeled with their "distance" from the starting vertex s. For a given vertex x, this label will be denoted dist[x]. Start by setting dist[s]:=0. At each step of the tree growing process, for each frontier edge e, calculate P(e) = dist[x] + wt(e), where x is the vertex of e that is in the tree (and therefore has been labeled) and wt(e) is the weight of edge e. Choose the frontier edge with the smallest P(e) value to add to the tree and label the new vertex with P(e).