Guest User

Untitled

a guest
Mar 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. // input: graph G
  2. // output: labeling of edges and partition of the vertices of G
  3. LabelAllConnectedComponents(G):
  4. // initialize all vertices and edges as unexplored (label is 0)
  5. for all u ∈ G.vertices()
  6. setLabel(u, UNEXPLORED)
  7. for all e ∈ G.edges()
  8. setLabel(e, UNEXPLORED)
  9.  
  10. // call BFS on every unlabeled vertex, which results in
  11. // calling BFS once for each connected component
  12. componentID = 1
  13. for all v ∈ G.vertices()
  14. if getLabel(v) == 0:
  15. BFS(G, v, componentID++)
  16.  
  17. // standard breadth-first-search algorithm that works on one component
  18. BFS(G, s, componentID):
  19. L[0] = new empty sequence
  20. insert vertex s at the end of L[0]
  21. setLabel(s, componentID)
  22. i = 0
  23. while L[i] is not empty:
  24. L[i+1] = new empty sequence
  25. for all v ∈ L[i].elements()
  26. for all e ∈ G.incidentEdges(v)
  27. if getLabel(e) == UNEXPLORED
  28. w ← opposite(v,e)
  29. if getLabel(w) == UNEXPLORED
  30. setLabel(e, DISCOVERY)
  31. setLabel(w, componentID)
  32. L[i+1].insertLast(w)
  33. else
  34. setLabel(e, CROSS)
  35. i = i+1
Add Comment
Please, Sign In to add comment