Question: Please help me find the problem and fix it def find ( u ) : global parent if ( u = = parent [ u

Please help me find the problem and fix it
def find(u):
global parent
if(u == parent[u]):
return u
parent[u]= find(parent[u])
return parent[u]
# Read in the number of vertices (n) and edges (m)
n = int(input())
m = int(input())
edges =[]
n_edges =[[0 for i in range(n)] for j in range(n)]
for i in range(m):
u, v, w = input().split()
u = int(u)
v = int(v)
w = float(w)
edges.append([u,v,w])
n_edges[u][v]=w
n_edges[u][v]=w
sorted_eg = sorted(edges, key=lambda x: x[2])
parent=[i for i in range(n)]
count = int(input())
costs =0
for i in range(count):
u,v = input().split()
u = int(u)
v = int(v)
parent_u = find(u)
parent_v = find(v)
parent[parent_u]=parent_v
costs +=n_edges[u][v]
q =0
for u, v, w in sorted_eg:
parent_u = find(u)
parent_v = find(v)
if parent_u != parent_v:
parent[parent_u]= parent_v
q += w
result = q +costs
print(round(result,2))
 Please help me find the problem and fix it def find(u):

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 Databases Questions!