Question: Please consider my code in the picture and help with the following code ( only the #your code here ) and pass the testing. class

Please consider my code in the picture and help with the following code (only the #your code here) and pass the testing.
class UndirectedGraph:
# n is the number of vertices
# we will label the vertices from 0 to self.n -1
# We simply store the edges in a list.
def __init__(self, n):
assert n >=1, 'You are creating an empty graph -- disallowed'
self.n = n
self.edges =[]
self.vertex_data =[None]*self.n
def set_vertex_data(self, j, dat):
assert 0= j self.n
self.vertex_data[j]= dat
def get_vertex_data(self, j):
assert 0= j self.n
return self.vertex_data[j]
def add_edge(self, i, j, wij):
assert 0= i self.n
assert 0= j self.n
assert i != j
# Make sure to add edge from i to j with weight wij
self.edges.append((i, j, wij))
def sort_edges(self):
# sort edges in ascending order of weights.
self.edges = sorted(self.edges, key=lambda edg_data: edg_data[2])
2A: compute scc
def compute_scc(g, W):
# create a disjoint forest with as many elements as number of vertices
# Next compute the strongly connected components using the disjoint forest data structure
d = DisjointForests(g.n)
# your code here
**Test**
g3= UndirectedGraph(8)
g3.add_edge(0,1,0.5)
g3.add_edge(0,2,1.0)
g3.add_edge(0,4,0.5)
g3.add_edge(2,3,1.5)
g3.add_edge(2,4,2.0)
g3.add_edge(3,4,1.5)
g3.add_edge(5,6,2.0)
g3.add_edge(5,7,2.0)
res = compute_scc(g3,2.0)
print('SCCs with threshold 2.0 computed by your code are:')
assert len(res)==2, f'Expected 2 SCCs but got {len(res)}'
for (k, s) in res.items():
print(s)
# Let us check that your code returns what we expect.
for (k, s) in res.items():
if (k in [0,1,2,3,4]):
assert (s == set([0,1,2,3,4])),'{0,1,2,3,4} should be an SCC'
if (k in [5,6,7]):
assert (s == set([5,6,7])),'{5,6,7} should be an SCC'
# Let us check that the thresholding works
print('SCCs with threshold 1.5')
res2= compute_scc(g3,1.5) # This cutsoff edges 2,4 and 5,6,7
for (k, s) in res2.items():
print(s)
assert len(res2)==4, f'Expected 4 SCCs but got {len(res2)}'
for (k, s) in res2.items():
if k in [0,1,2,3,4]:
assert (s == set([0,1,2,3,4])),'{0,1,2,3,4} should be an SCC'
if k in [5]:
assert s == set([5]),'{5} should be an SCC with just a single node.'
if k in [6]:
assert s == set([6]),'{6} should be an SCC with just a single node.'
if k in [7]:
assert s == set([7]),'{7} should be an SCC with just a single node.'
print('All tests passed: 10 points')
2B:
def compute_mst(g):
# return a tuple of two items
# 1. list of edges (i,j) that are part of the MST
# 2. sum of MST edge weights.
d = DisjointForests(g.n)
mst_edges =[]
g.sort_edges()
# your code here
**Test**
g3= UndirectedGraph(8)
g3.add_edge(0,1,0.5)
g3.add_edge(0,2,1.0)
g3.add_edge(0,4,0.5)
g3.add_edge(2,3,1.5)
g3.add_edge(2,4,2.0)
g3.add_edge(3,4,1.5)
g3.add_edge(5,6,2.0)
g3.add_edge(5,7,2.0)
g3.add_edge(3,5,2.0)
(mst_edges, mst_weight)= compute_mst(g3)
print('Your code computed MST: ')
for (i,j,wij) in mst_edges:
print(f'\t {(i,j)} weight {wij}')
print(f'Total edge weight: {mst_weight}')
assert mst_weight ==9.5, 'Optimal MST weight is expected to be 9.5'
assert (0,1,0.5) in mst_edges
assert (0,2,1.0) in mst_edges
assert (0,4,0.5) in mst_edges
assert (5,6,2.0) in mst_edges
assert (5,7,2.0) in mst_edges
assert (3,5,2.0) in mst_edges
assert (2,3,1.5) in mst_edges or (3,4,1.5) in mst_edges
print('All tests passed: 10 points!')
Please consider my code in the picture and help

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!