Guest User

Untitled

a guest
Jan 21st, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. #We add another countries in the loop
  2. graph = Graph(g)
  3. graph.add_edge(("Mumbai", "Delhi"),400)
  4. graph.add_edge(("Delhi", "Kolkata"),500)
  5. graph.add_edge(("Kolkata", "Bangalore"),600)
  6. graph.add_edge(("TX", "NY"),1200)
  7. graph.add_edge(("ALB", "NY"),800)
  8.  
  9. g = graph.adj_mat()
  10.  
  11. def bfs_connected_components(graph):
  12. connected_components = []
  13. nodes = graph.keys()
  14.  
  15. while len(nodes)!=0:
  16. start_node = nodes.pop()
  17. queue = [start_node] #FIFO
  18. visited = [start_node]
  19. while len(queue)!=0:
  20. start = queue[0]
  21. queue.remove(start)
  22. neighbours = graph[start]
  23. for neighbour,_ in neighbours.iteritems():
  24. if neighbour not in visited:
  25. queue.append(neighbour)
  26. visited.append(neighbour)
  27. nodes.remove(neighbour)
  28. connected_components.append(visited)
  29.  
  30. return connected_components
  31.  
  32. print bfs_connected_components(g)
Add Comment
Please, Sign In to add comment