to_nested_tuple(T, root, canonical_form=False)
The nested tuple representation of a tree is defined recursively. The tree with one node and no edges is represented by the empty tuple, ()
. A tree with k
subtrees is represented by a tuple of length k
in which each element is the nested tuple representation of a subtree.
This function is not the inverse of from_nested_tuple
; the only guarantee is that the rooted trees are isomorphic.
An undirected graph object representing a tree.
The node in T
to interpret as the root of the tree.
If True
, each tuple is sorted so that the function returns a canonical form for rooted trees. This means "lighter" subtrees will appear as nested tuples before "heavier" subtrees. In this way, each isomorphic rooted tree has the same nested tuple representation.
A nested tuple representation of the tree.
Returns a nested tuple representation of the given tree.
>>> T = nx.Graph() >>> T.add_edges_from([(0, 1), (0, 2), (0, 3)]) >>> T.add_edges_from([(1, 4), (1, 5)]) >>> T.add_edges_from([(3, 6), (3, 7)]) >>> root = 0 >>> nx.to_nested_tuple(T, root) (((), ()), (), ((), ()))
>>> nx.to_nested_tuple(T, root, canonical_form=True) ((), ((), ()), ((), ()))
See :>>> T = nx.path_graph(4) >>> root = 0 >>> nx.to_nested_tuple(T, root) ((((),),),)
The following pages refer to to this document either explicitly or contain code examples using this.
networkx.algorithms.tree.coding.from_nested_tuple
networkx.algorithms.tree.coding.to_prufer_sequence
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them