Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public static int numberOfConnectedComponents(Graph g) {
- Collection<Vertex> unexplored = g.getAllVertices();
- for (int count = 0;;count++)
- {
- Iterator<Vertex> uIt = unexplored.iterator();
- if (!uIt.hasNext())
- {
- return count;
- }
- Vertex v = uIt.next();
- Iterator<Vertex> it = new GraphIterator(g, v);
- List<Vertex> explored = new ArrayList<>();
- while (it.hasNext())
- {
- explored.add(it.next());
- }
- unexplored.removeAll(explored);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement