Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Demonstration of converting a Prufer code to a tree

152 views
ubuntu2004
Kernel: SageMath 9.7
# See Networkx's docuemntation -- https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.tree.coding.from_prufer_sequence.html import networkx as nx from sage.graphs.graph_input import from_networkx_graph
# Convert Prufer code to a tree code = [i-1 for i in [6, 6, 4, 3, 1, 4, 3]] # In networkx, node-labeling starts with 0 g_nx = nx.from_prufer_sequence(code) list(g_nx.edges())
[(0, 7), (0, 3), (1, 5), (2, 6), (2, 3), (2, 8), (3, 5), (4, 5)]
# Convert the Networkx graph to a Sagemath graph g_sage = Graph() from_networkx_graph(g_sage, g_nx) g_sage
Image in a Jupyter notebook
# A second example. code = [i-1 for i in [7, 5, 5, 3, 1]] g_nx = nx.from_prufer_sequence(code) g_sage = Graph() from_networkx_graph(g_sage, g_nx) g_sage
Image in a Jupyter notebook