Question: NEED HELP Question 2 : Is a graph a tree? A tree is a graph ( V , E ) with two special properties: -
NEED HELP Question : 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 istree property that has value True if the graph is a tree, and has value False otherwise. Implementation of tree test #@itle Implementation of tree test def graphistreeself: if lengraphedges lengraphvertices: return False incomingedges for edge in graph.edges: v edge if v in incomingedges: return False incomingedgesv True rootexists False for vertex in graph.vertices: if vertex not in incomingedges: if rootexists: return False rootexists True #@title Implementation of tree test def graphistreeself: if lengraphedgesleng r a p h v e r t i c e s: return False incomingedges for edge in graph.edges: v edge if v in incomingedges: return False incomingedges v True rootexists False for vertex in graph.vertices: if vertex not in incomingedges: if rootexists: return False rootexists True return True Graph.istree property graphistree# You may want to write tests here. #YOUR CODE HERE ### points: Tests for tree. gGraph vertices edges assert true gis tree gGraph vertices edges assertfalsegistree gGraph vertices edges assertfalsegistree gGraph verticesab edges a b asserttrue gistree g Graphverticesab edgesabba assertfalsegistree NameError gGraph vertices edges asserttruegistreegGraph vertices edges ipythoninputfba in graphistreeself def graphistreeself: if lengraphedges lengraphvertices : return False NameError: name 'graph' is not defined SEARCH STACK OVERFLOW
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
