Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1 C We now wish to find the set of all nodes in the graph that belong to some cycle. Example Consider the example below:
C
We now wish to find the set of all nodes in the graph that belong to some cycle.
Example
Consider the example below:
Nodes lie on some cycle: example node lies on many cycles one such cycle being whereas the other nodes do not.
Note We do not treat undirected edges as cycles although in our data structure, when an undirected edge is represented as two "directed" edges
and it gives the impression that they are in a cycle of length
Complete the function findallnodesincycle that given a graph returns a python set of nodes in a cycle.
Hint Perform a DFS traversal and for each non trivial back edge discovered, use the DFS tree to figure out the nodes that must be on the cycle corresponding
to that back edge.
In : def findallnodesincycleg: # is an UndirectedGraph class
setofnodes set
# your code here
In : #this is the example that we had for the problem.
g UndirectedGraph
gaddedge
gaddedge
gaddedge
gaddedge
gaddedge
gaddedge
gaddedge
gaddedge
findallnodesincycle
print Your code returns set of nodes:
assert 'Fail: Set of nodes must be
# Let's also add the edge
gaddedge
s findallnodesincycle
print Your code returns set of nodes:
assert 'Fail: Set of nodes must be
printAll tests passedd: points!
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