@Override
    public Color getDominantClusterInNeighbourhood(Node node) {
        
        Set<Node> neighbors = IteratorUtils.toSet(this.LPAClusteringEngine.getGraph().getNeighbors(node).iterator());
        if (neighbors.isEmpty())
            return this.LPAClusteringEngine.getNodeClusterMapping().get(node); // no clusters in neighbourhood, a disconnected node.

        Map<Color, Double> neighborClusterWeights = new HashMap<Color, Double>();
        
        // Calculating the clusters' weights in the neighbourhood of the processed node.
        for(Node currentNeighbor:neighbors){
            if (this.LPAClusteringEngine.getIsCancelled())
                break;
            
            Color neighboringCluster = this.LPAClusteringEngine.getNodeClusterMapping().get(currentNeighbor);
            // Although the original LPA algorithm doesn't consider the weight on edges, Barber and Clark 
            // in (Detecting network communities by propagating labels under constraints. Physical Review E,80(2),026129.)
            // indicate that the discrete nature of the objective function being optimized by LPA makes it possible to support weighted networks too.
            double clusterWeight = this.LPAClusteringEngine.getGraph().getEdge(node, currentNeighbor).getWeight();
            
            if (neighborClusterWeights.containsKey(neighboringCluster)) {
                clusterWeight += neighborClusterWeights.get(neighboringCluster);
            }
            neighborClusterWeights.put(neighboringCluster, clusterWeight);
        }
        

        // picking the cluster with the heighest weight. In case two or more clusters exhibit the same weight, the LPA rule must pick a random one from the dominant group.
        // Shuffling the clusters list and picking the cluster with the highest weight.
        neighborClusterWeights = MapUtils.shuffle(neighborClusterWeights, this.LPAClusteringEngine.getRandomizer());
        double maxWeight = Collections.max(neighborClusterWeights.values());
        Color prevailingClusterColor = MapUtils.getKeyByValue(neighborClusterWeights, maxWeight);

        // Check whether the current node's cluster is already a dominant one.
        Color currentNodeCluster = this.LPAClusteringEngine.getNodeClusterMapping().get(node);
        if (neighborClusterWeights.containsKey(currentNodeCluster)) {
            // if it is the case, then keep it.
            if (neighborClusterWeights.get(currentNodeCluster) == maxWeight)
                prevailingClusterColor = currentNodeCluster;
        }
        
        return prevailingClusterColor;
    }