Question: Please consider my code in the picture and I need help with the below code ( i dont need help with code in the picture
Please consider my code in the picture and I need help with the below code i dont need help with code in the picture for A and B only the #your code hereand 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 SCCHELP assertion error
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
for i j wij in gedges:
if wij W:
dunioni j
# Create a dictionary to store the SCCs
sccdict ddictionaryofsets
return sccdict
Test for A
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'
Bcompute mstHELP AssertionError: You are calling find on an element that is not part of the family yet. Please call makeset first.
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
# Iterate over all edges, adding to MST if they don't form a cycle using unionfind
for i j wij in gedges:
# If i and j are in different sets, add the edge to MST
if dfindi dfindj:
dunioni j
mstedges.appendi j wij
mstweight wij
return mstedges, mstweight
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
