Advertisement
Guest User

8.2

a guest
Jan 23rd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. class Solution {
  2. public static int numberOfConnectedComponents(Graph g) {
  3. Collection<Vertex> unexplored = g.getAllVertices();
  4.  
  5. for (int count = 0;;count++)
  6. {
  7. Iterator<Vertex> uIt = unexplored.iterator();
  8.  
  9. if (!uIt.hasNext())
  10. {
  11. return count;
  12. }
  13.  
  14. Vertex v = uIt.next();
  15. Iterator<Vertex> it = new GraphIterator(g, v);
  16. List<Vertex> explored = new ArrayList<>();
  17.  
  18. while (it.hasNext())
  19. {
  20. explored.add(it.next());
  21. }
  22.  
  23. unexplored.removeAll(explored);
  24. }
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement