prufer_tree#
- graph_tool.generation.prufer_tree(seq)[git][source]#
Return the labeled tree with the given Prüfer sequence.
- Parameters:
- seqiterable of
int Prüfer sequence of length \(N - 2\), with values in \([0, N)\).
- seqiterable of
- Returns:
- g
Graph The undirected tree with \(N\) vertices.
- g
See also
graph_tool.structure.prufer_sequencePrüfer sequence of a tree
generate_random_treeUniformly random trees
Notes
The Prüfer sequence [prufer-neuer-1918] is a bijection between the labeled trees with \(N\) vertices and the sequences of length \(N - 2\) with values in \([0, N)\). The tree is reconstructed by connecting, for every element \(s\) of the sequence, the smallest leaf that does not appear in the rest of the sequence to \(s\), and finally the two remaining vertices. This is the inverse of
prufer_sequence(), and runs in time \(O(N)\).References
[prufer-neuer-1918]Heinz Prüfer, “Neuer Beweis eines Satzes über Permutationen”, Archiv der Mathematik und Physik 27, 742-744 (1918)
Examples
>>> g = gt.prufer_tree([3, 3, 3, 4]) >>> print(sorted(tuple(sorted(e)) for e in g.get_edges().tolist())) [(0, 3), (1, 3), (2, 3), (3, 4), (4, 5)] >>> print(gt.prufer_sequence(g)) [3 3 3 4]
The tree is drawn as follows, with the vertices labeled by their indices:
>>> gt.graph_draw(g, pos=gt.sfdp_layout(g), vertex_text=g.vertex_index, ... output="prufer_tree-example.pdf") <...>
Labeled tree with the Prüfer sequence \((3, 3, 3, 4)\). The vertex labels are the indices.#