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 to self.n
# We simply store the edges in a list.
def initself n:
assert n 'You are creating an empty graph disallowed'
self.n n
self.edges
self.vertexdata Noneselfn
def setvertexdataself j dat:
assert j self.n
self.vertexdataj dat
def getvertexdataself j:
assert j self.n
return self.vertexdataj
def addedgeself i j wij:
assert i self.n
assert j self.n
assert i j
# Make sure to add edge from i to j with weight wij
self.edges.appendi j wij
def sortedgesself:
# sort edges in ascending order of weights.
self.edges sortedselfedges, keylambda edgdata: edgdata
A: compute scc
def computesccg 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 DisjointForestsgn
# your code here
Test
g UndirectedGraph
gaddedge
gaddedge
gaddedge
gaddedge
gaddedge
gaddedge
gaddedge
gaddedge
res computesccg
printSCCs with threshold computed by your code are:
assert lenres f'Expected SCCs but got lenres
for k s in res.items:
prints
# Let us check that your code returns what we expect.
for k s in res.items:
if k in :
assert s set should be an SCC
if k in :
assert s set should be an SCC
# Let us check that the thresholding works
printSCCs with threshold
res computesccg # This cutsoff edges and
for k s in resitems:
prints
assert lenres f'Expected SCCs but got lenres
for k s in resitems:
if k in :
assert s set should be an SCC
if k in :
assert s set should be an SCC with just a single node.
if k in :
assert s set should be an SCC with just a single node.
if k in :
assert s set should be an SCC with just a single node.
printAll tests passed: points'
B:
def computemstg:
# return a tuple of two items
# list of edges ij that are part of the MST
# sum of MST edge weights.
d DisjointForestsgn
mstedges
gsortedges
# your code here
Test
g UndirectedGraph
gaddedge
gaddedge
gaddedge
gaddedge
gaddedge
gaddedge
gaddedge
gaddedge
gaddedge
mstedges, mstweight computemstg
printYour code computed MST:
for ijwij in mstedges:
printft ij weight wij
printfTotal edge weight: mstweight
assert mstweight 'Optimal MST weight is expected to be
assert in mstedges
assert in mstedges
assert in mstedges
assert in mstedges
assert in mstedges
assert in mstedges
assert in mstedges or in mstedges
printAll tests passed: points!
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
