Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

NEED HELP Question 2 : Is a graph a tree? A tree is a graph ( V , E ) with two special properties: -

NEED HELP Question 2: Is a graph a tree? A tree is a graph (V, E) with two special properties: - Every vertex has at most one incoming edge. - Either there are no vertices, or there is a vertex with no incoming edges, called the root, from which all other vertices are reachable. If the second property does not hold, incidentally, the graph is called a forest. Write an is_tree property that has value True if the graph is a tree, and has value False otherwise. Implementation of tree test [36] #@itle Implementation of tree test def graph_is_tree(self): if len(graph.edges)!= len(graph.vertices)-1: return False incoming_edges ={} for edge in graph.edges: _, v= edge if v in incoming_edges: return False incoming_edges[v]= True root_exists = False for vertex in graph.vertices: if vertex not in incoming_edges: if root_exists: return False root_exists = True [36] #@title Implementation of tree test def graph_is_tree(self): if len(graph.edges)!=len(g r a p h . v e r t i c e s)-1: return False incoming_edges ={} for edge in graph.edges: -, v= edge if v in incoming_edges: return False incoming_edges [v]= True root_exists = False for vertex in graph.vertices: if vertex not in incoming_edges: if root_exists: return False root_exists = True return True Graph.is_tree = property (graph_is_tree)# You may want to write tests here. #YOUR CODE HERE 37] ### 15 points: Tests for tree. g=Graph( vertices =[1,2,3], edges =[(1,2),(1,3)]) assert true (g.is tree) g=Graph (vertices =[1,2,3], edges =[(1,2),(2,3),(1,3)]) assert_false(g.is_tree) g=Graph (vertices =[1,2,3], edges =[(1,3),(2,3)]) assert_false(g.is_tree) g=Graph(. vertices=['a','b'], edges =[(' a ',' b^')]) assert_true (g.is_tree) g = Graph(vertices=['a','b'], edges=[('a','b'),('b','a')]) assert_false(g.is_tree) NameError 3g=Graph (vertices =[1,2,3], edges =[(1,2),(1,3)])4 assert_true(g.is_tree)56g=Graph (vertices =[1,2,3], edges =[(1,2),(2,3),(1,3)])ipython-input-36-4504196f0ba8> in graph_is_tree(self)23 def graph_is_tree(self): 4 if len(graph.edges)!= len(graph.vertices)-1 : 5 return False 6 NameError: name 'graph' is not defined SEARCH STACK OVERFLOW

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Climate And Environmental Database Systems

Authors: Michael Lautenschlager ,Manfred Reinke

1st Edition

1461368332, 978-1461368335

More Books

Students also viewed these Databases questions

Question

7. Explain how to recruit a more diverse workforce.

Answered: 1 week ago

Question

1. Define the nature of interviews

Answered: 1 week ago

Question

2. Outline the different types of interviews

Answered: 1 week ago