Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. #Q4 Loud music for everyone
  2. # using Kruskal's Algorithm
  3. def lowest_cost(conduits_info_str):
  4. """take the information about conduits between classroom and returns the lowest cost the university can pay to get this job done."""
  5. lines = conduits_info_str.splitlines()
  6. num_vertices = int(lines[0].split()[1])
  7. weighty = []
  8. graph = [{i} for i in range(num_vertices)]
  9. T = []
  10. for i in lines[1:]:
  11. nums = [int(j) for j in i.split()]
  12. s = nums[0]
  13. e = nums[1]
  14. w = nums[2]
  15. x = (w, s, e)
  16. weighty.append(x)
  17. count = 0
  18. weighty.sort()
  19. while count < num_vertices-1:
  20. for (w,i,j) in weighty:
  21. if comp(graph, i).isdisjoint(comp(graph, j)):
  22. graph[i] = graph[i].union(graph[j])
  23. graph[j] = graph[i].union(graph[j])
  24. T.append(w)
  25. count+=1
  26. elif w == weighty[-1][0]:
  27. count = num_vertices
  28. return sum(T)*50
  29.  
  30. def comp(lst, n):
  31. new = {n}
  32. for i in lst[n]:
  33. new.update(lst[i])
  34. return new
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement