Answered step by step
Verified Expert Solution
Question
1 Approved Answer
N PYTHON, COMPLETE THE FOLLOWING %matplotlib inline import matplotlib.pyplot as plt import networkx as nx def get_number(): ''' ''' num = 0 while True: yield
N PYTHON, COMPLETE THE FOLLOWING
%matplotlib inline import matplotlib.pyplot as plt import networkx as nx def get_number(): ''' ''' num = 0 while True: yield num num += 1 def build_graph(tree, G, parent=None, value=None, gen=get_number()): ''' #complete this part ''' if tree in [True, False]: unique_label = str(tree) + str(next(gen)) G.add_node(unique_label) # add leaf node G.add_edge(parent, unique_label, label=value) else: attribute, subtree_dict = tree attribute = str(attribute) G.add_node(attribute) # add attribute node if parent is not None: G.add_edge(parent, attribute, label=value) for attribute_value, subtree in subtree_dict.items(): attribute_value = str(attribute_value) build_graph(subtree, G, attribute, attribute_value, gen) tree = ['level', \ {None: True, \ 'Senior': ('tweets', \ {'no': False, None: False, 'yes': True}), \ 'Junior': ('phd', \ {'no': True, None: True, 'yes': False}), \ 'Mid': True}] G = nx.DiGraph() build_graph(tree, G) pos = nx.spring_layout(G) # draw nodes and edges nx.draw_networkx_nodes(G, pos) nx.draw_networkx_edges(G, pos) # add node and edge labels labels = {node:str(node) for i, node in enumerate(G.nodes())} elabels = {edge:G[edge[0]][edge[1]]["label"] for i, edge in enumerate(G.edges())} nx.draw_networkx_labels(G, pos, labels) nx.draw_networkx_edge_labels(G, pos, edge_labels=elabels) plt.axis('off')
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started