Question: Basing on the code in the picture and class undirectedgraph, please help with 2 B code with error * * Your code computed MST: -

Basing on the code in the picture and class undirectedgraph, please help with 2B code with error **Your code computed MST:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in ()14 # Iterate through the edges in mst_edges, which are tuples of (i, j)15 for edge in mst_edges: --->16 i, j = edge # Unpack the edge into node indices i and j 17 # Find the weight corresponding to the edge (i, j) in the original graph g318 wij = next(w for u, v, w in g3.edges if (u == i and v == j) or (u == j and v == i))
ValueError: too many values to unpack (expected 2)**
**Other preious code as FYI**
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])
***Question I need help***2B:MST
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 =[]
mst_weight =0
# Sort edges based on their weight
g.sort_edges()
#Please only fix the below part
# Iterate over all edges, adding to MST if they don't form a cycle (using union-find)
for i, j, wij in g.edges:
if d.find(i)!= d.find(j): # No cycle
d.union(i, j) # Union the sets
mst_edges.append((i, j)) # Add edge to MST
mst_weight += wij # Add edge weight to MST weight
return mst_edges, mst_weight
**Test implementation**
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: ')
# Iterate through the edges in mst_edges, which are tuples of (i, j)
for edge in mst_edges:
i, j = edge # Unpack the edge into node indices i and j
# Find the weight corresponding to the edge (i, j) in the original graph g3
wij = next(w for u, v, w in g3.edges if (u == i and v == j) or (u == j and v == i))
print(f'\t {(i,j)} weight {wij}') # Print the edge and its weight
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 g3.edges # Check if the edge exists in the original graph
assert (0,2,1.0) in g3.edges
assert (0,4,0.5) in g3.edges
assert (5,6,2.0) in g3.edges
assert (5,7,2.0) in g3.edges
assert (3,5,2.0) in g3.edges
assert (2,3,1.5) in g3.edges or (3,4,1.5) in g3.edges # check if either edge is present
print('All tests passed: 10 points!')
Basing on the code in the picture and class

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!