Algorithm to convert a tree into a Prüfer sequence
One can generate a labeled tree's Prüfer sequence by iteratively removing vertices from the tree until only two vertices remain. Specifically, consider a labeled tree with vertices . At step , remove the leaf with the smallest label and set the -th element of the Prüfer sequence to be the label of this leaf's neighbour. The Prüfer sequence of a labeled tree is unique and has length . Both coding and decoding can be reduced to integer radix sorting and parallelized.Example
Algorithm to convert a Prüfer sequence into a tree
Let [1 a[2">[1.html" ;"title="[1">[1 a[2 ..., a[n">[1.html"_;"title="[1">[1<_a>_a[2.html" ;"title="[1.html" ;"title="[1">[1 a[2">[1.html" ;"title="[1">[1 a[2 ..., a[n
be a Prüfer sequence:
The tree will have n+2
nodes, numbered from 1
to n+2
.
For each node set its degree to the number of times it appears in the sequence plus 1.
For instance, in pseudo-code:
Convert-Prüfer-to-Tree()
1 ← ''length''[]
2 ← a graph with + 2 isolated nodes, numbered 1 to + 2
3 ''degree'' ← an array of integers
4 for each node in do
5 ''degree''[] ← 1
6 for each value in do
7 ''degree''[] ← ''degree''[] + 1
Next, for each number in the sequence a /code>, find the first (lowest-numbered) node, j
, with degree equal to 1, add the edge (j, a[i])
to the tree, and decrement the degrees of j
and a /code>. In pseudo-code:
8 for each value in do
9 for each node in do
10 if ''degree''[] = 1 then
11 Insert ''edge''[, ] into
12 ''degree''[] ← ''degree''[] - 1
13 ''degree''[] ← ''degree''[] - 1
14 break
At the end of this loop two nodes with degree 1 will remain (call them u
, v
). Lastly, add the edge (u,v)
to the tree.
15 ← ← 0
16 for each node in
17 if ''degree''[] = 1 then
18 if = 0 then
19 ←
20 else
21 ←
22 break
23 Insert ''edge''[, ] into
24 ''degree''[] ← ''degree''[] - 1
25 ''degree''[] ← ''degree''[] - 1
26 return
Cayley's formula
The Prüfer sequence of a labeled tree on vertices is a unique sequence of length on the labels 1 to . For a given sequence of length on the labels 1 to , there is a ''unique'' labeled tree whose Prüfer sequence is .
The immediate consequence is that Prüfer sequences provide a bijection
In mathematics, a bijection, bijective function, or one-to-one correspondence is a function between two sets such that each element of the second set (the codomain) is the image of exactly one element of the first set (the domain). Equival ...
between the set of labeled trees on vertices and the set of sequences of length on the labels 1 to . The latter set has size , so the existence of this bijection proves Cayley's formula
In mathematics, Cayley's formula is a result in graph theory named after Arthur Cayley. It states that for every positive integer n, the number of trees on n labeled vertices is n^.
The formula equivalently counts the spanning trees of a ...
, i.e. that there are labeled trees on vertices.
Other applications
Source:
* Cayley's formula can be strengthened to prove the following claim:
:The number of spanning trees in a complete graph with a degree specified for each vertex is equal to the multinomial coefficient
In mathematics, the multinomial theorem describes how to expand a power of a sum in terms of powers of the terms in that sum. It is the generalization of the binomial theorem from binomials to multinomials.
Theorem
For any positive integer ...
::
:The proof follows by observing that in the Prüfer sequence number appears exactly times.
* Cayley's formula can be generalized: a labeled tree is in fact a spanning tree
In the mathematical field of graph theory, a spanning tree ''T'' of an undirected graph ''G'' is a subgraph that is a tree which includes all of the vertices of ''G''. In general, a graph may have several spanning trees, but a graph that is no ...
of the labeled complete graph
In the mathematical field of graph theory, a complete graph is a simple undirected graph in which every pair of distinct vertices is connected by a unique edge. A complete digraph is a directed graph in which every pair of distinct vertices i ...
. By placing restrictions on the enumerated Prüfer sequences, similar methods can give the number of spanning trees of a complete bipartite graph
In the mathematics, mathematical field of graph theory, a bipartite graph (or bigraph) is a Graph (discrete mathematics), graph whose vertex (graph theory), vertices can be divided into two disjoint sets, disjoint and Independent set (graph theo ...
. If is the complete bipartite graph with vertices 1 to in one partition and vertices to in the other partition, the number of labeled spanning trees of is , where .
* Generating uniformly distributed random Prüfer sequences and converting them into the corresponding trees is a straightforward method of generating uniformly distributed random labelled trees.
References
External links
Prüfer code
– from MathWorld
''MathWorld'' is an online mathematics reference work, created and largely written by Eric W. Weisstein. It is sponsored by and licensed to Wolfram Research, Inc. and was partially funded by the National Science Foundation's National Science ...
{{DEFAULTSORT:Prufer Sequence
Enumerative combinatorics
Trees (graph theory)