@SuppressWarnings("unchecked")
	@Override
	public Set<Set<V>> getCommunities(Graph<V, E> graph) {
		Graph<V, E> loopGraph = graph;

		totalIterations = 0;

		// default - no edges values in the graph
		if (edgesValues == null) {
			edgesValues = new HashMap<E, Double>();
			for (E e : loopGraph.getEdges()) {
				edgesValues.put(e, new Double(1));
			}
		}

		// main loop
		while (true) {
			if (!phaseOne(loopGraph)) {
				break;
			}

			loopGraph = phaseTwo(loopGraph);
			totalIterations++;
		}

		communities = new HashSet<>();
		for (V g : loopGraph.getVertices()) {
			if (!(g instanceof Set<?>)) { // case when main loop does only one iteration
				Set<V> group = new HashSet<>();
				group.add(g);
				communities.add(separateElements(group));
			} else {
				communities.add(separateElements((Set<V>) g));
			}
		}

		System.out.println("Total two-phase iterations:  " + totalIterations);
		return communities;
	}