Question: 3 . Random Trees The nx . random _ tree ( n ) command can be used to generate a random tree on the n

3. Random Trees
The nx.random_tree(n) command can be used to generate a random tree on the n vertices represented in python by range(n)(in order).
Such a tree can be converted into a sequence of n2
numbers, its Prfer code by the python function pruefer_code, defined as follows:
def pruefer_node(tree):
for x in tree:
if tree.degree(x)==1:
for y in tree[x]:
tree.remove_node(x)
return y
def pruefer_code(T):
tree = T.copy()
return [pruefer_node(tree) for k in range(tree.order()-2)]
T = nx.random_tree(12)
code = pruefer_code(T)
code
Conver
def tree_pruefer(code):
# initialize graph and defects
n = len(code)+2
tree = nx.empty_graph(n)
degrees =[1 for x in tree]
for y in code:
degrees[y]+=1
# add edges
for y in code:
for x in tree:
if degrees[x]==1:
tree.add_edge(x, y)
for z in (x, y):
degrees[z]-=1
break
# final edge
e =[x for x in tree if degrees[x]==1]
tree.add_edge(*e)
return tree
T2= tree_pruefer(code)
Use, if needed, the above functions to experiment with random trees and/or sequences and answer the following:
1. Which labelled tree on n nodes {0,1,...,n1}
corresponds to the Prfer code [0,1,2,...,n3]?(Note: you need to describe and justify mathematically an answer for all n)
2.More generally, what can one say about a tree T
whose Prfer code consists of n2 distinct entries? (Note: you need to describe and justify mathematically an answer for all n.)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!