Question: 3 . Random Trees The nx . random _ tree ( n ) command can be used to generate a random tree on the n
Random Trees
The nxrandomtreen command can be used to generate a random tree on the n vertices represented in python by rangenin order
Such a tree can be converted into a sequence of n
numbers, its Prfer code by the python function pruefercode, defined as follows:
def pruefernodetree:
for x in tree:
if tree.degreex:
for y in treex:
tree.removenodex
return y
def pruefercodeT:
tree Tcopy
return pruefernodetree for k in rangetreeorder
T nxrandomtree
code pruefercodeT
code
Conver
def treepruefercode:
# initialize graph and defects
n lencode
tree nxemptygraphn
degrees for x in tree
for y in code:
degreesy
# add edges
for y in code:
for x in tree:
if degreesx:
tree.addedgex y
for z in x y:
degreesz
break
# final edge
e x for x in tree if degreesx
tree.addedgee
return tree
T treepruefercode
Use, if needed, the above functions to experiment with random trees andor sequences and answer the following:
Which labelled tree on n nodes n
corresponds to the Prfer code nNote: you need to describe and justify mathematically an answer for all n
More generally, what can one say about a tree T
whose Prfer code consists of n 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
