Question: Please refer to this first part of code to code for the question: class DFSTimeCounter: def _ _ init _ _ ( self ) :

Please refer to this first part of code to code for the question:
class DFSTimeCounter:
def __init__(self):
self.count =0
def reset(self):
self.count =0
def increment(self):
self.count = self.count +1
def get(self):
return self.count
class UndirectedGraph:
# n is the number of vertices
# we will label the vertices from 0 to self.n -1
# Initialize to an empty adjacency list
# We will store the outgoing edges using a set data structure
def __init__(self, n):
self.n = n
self.adj_list =[ set() for i in range(self.n)]
def add_edge(self, i, j):
assert 0= i self.n
assert 0= j self.n
assert i != j
# Make sure to add edge from i to j
self.adj_list[i].add(j)
# Also add edge from j to i
self.adj_list[j].add(i)
# get a set of all vertices that
# are neighbors of the
# vertex i
def get_neighboring_vertices(self, i):
assert 0= i self.n
return self.adj_list[i]
# Function: dfs_visit
# Program a DFS visit of a graph.
# We maintain a list of discovery times and finish times.
# Initially all discovery times and finish times are set to None.
# When a vertex is first visited, we will set discovery time
# When DFS visit has processed all the neighbors then
# set the finish time.
# DFS visit should update the list of discovery and finish times in-place
# Arguments
# i --> id of the vertex being visited.
# dfs_timer --> An instance of DFSTimeCounter structure provided for you.
# discovery --> discovery time of each vertex -- a list of size self.n
# None if the vertex is yet to be visited.
# finish --> finish time of each vertex -- a list of size self.n
# None if the vertex is yet to be finished.
# dfs_tree_parent --> the parent for for each node
# if we visited node j from node i, then j's parent is i.
# Do not forget to set tree_parent when you call dfs_visit
# on node j from node i.
# dfs_back_edges --> a list of back edges.
# a back edge is an edge from i to j wherein
# DFS has already discovered j when i is discovered
# but not finished j
def dfs_visit(self, i, dfs_timer, discovery_times, finish_times,
dfs_tree_parent, dfs_back_edges):
assert 0= i self.n
assert discovery_times[i]== None
assert finish_times[i]== None
discovery_times[i]= dfs_timer.get()
dfs_timer.increment()
# your code here
for v in self.get_neighboring_vertices(i):
if discovery_times[v] is not None and finish_times[v] is None:
dfs_back_edges.append((i,v))
if discovery_times[v] is None:
dfs_tree_parent[v]= i
self.dfs_visit(v, dfs_timer, discovery_times, finish_times, dfs_tree_parent, dfs_back_edges)
finish_times[i]= dfs_timer.get()
dfs_timer.increment()
# Function: dfs_traverse_graph
# Traverse the entire graph.
def dfs_traverse_graph(self):
dfs_timer = DFSTimeCounter()
discovery_times =[None]*self.n
finish_times =[None]*self.n
dfs_tree_parents =[None]*self.n
dfs_back_edges =[]
for i in range(self.n):
if discovery_times[i]== None:
self.dfs_visit(i,dfs_timer, discovery_times, finish_times,
dfs_tree_parents, dfs_back_edges)
# Clean up the back edges so that if (i,j) is a back edge then j cannot
# be i's parent.
non_trivial_back_edges =[(i,j) for (i,j) in dfs_back_edges if dfs_tree_parents[i]!= j]
return (dfs_tree_parents, non_trivial_back_edges, discovery_times, finish_times)
The second question is in the image.Find the number of (maximal) strongly connected components in an undirected graph from the results of a DFS. Implement the function
num_connected_components that takes in a graph g and returns a number that indicates the number of MSCCs in the directed graph.
Example
Consider the graph below
It has 3 maximal strongly connected components that have vertices {0,1,2,3,4},{5,6} and {7}, respectively. Given such a graph, your function must
return the number 3.
Hint Examine the dfs_traverse_graph function carefully. How do you distinguish different connected components in a graph from the DFS tree?
In []: def num_connected_components(g): # g is an UndirectedGraph classIn []: # create the graph from problem 1A.
g = UndirectedGraph(5)
g.add_edge (0,1)
g.add_edge (0,2)
Please refer to this first part of code to code

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!