/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.cnr.iasi.giant.util;

import cytoscape.CyNode;
import cytoscape.Cytoscape;
import it.cnr.iasi.giant.thread.NetworkLoaderThread;
import java.util.ArrayList;
import java.util.HashSet;

/**
 *
 * @author Fabio Cumbo
 * @info GIANT
 * @version v1.0
 * @organization IASI-CNR : Istituto di Analisi dei Sistemi ed Informatica 
 *                  "Antonio Ruberti" - Consiglio Nazionale delle Ricerche
 * 
 */
public class ComputeParticipationCoefficient {
    
    public static HashSet<String> excluded = new HashSet<String>();
    
    public ComputeParticipationCoefficient() {}
    
    public static double compute(String node, ArrayList<String> cluster) {
        
        /*
         *
         *  Pi = 1 - (Kis / Ki)^2
         *
         * 
         *  Kis: number of links of node i to nodes in module s
         *  Ki: total degree of node i
         * 
         */
        
        CyNode source = Cytoscape.getCyNode(node);
        if (source == null)
            return Double.POSITIVE_INFINITY;
        
        try {
            ArrayList<String> degreeI = NetworkLoaderThread.getDegreeMap().get(node);
            double _Ki = degreeI.size();
            double sum = 0.0;


            double _Kis = 0.0;
            for (String n: cluster) {        
                if (degreeI.contains(n))
                    _Kis++;
            }
            sum = sum + Math.pow((_Kis /_Ki), 2);

            return (double)(1-sum);
        } catch (NullPointerException e) {
            excluded.add(node);
            return Double.POSITIVE_INFINITY;
        }
        
    }
    
    public static HashSet<String> getExcluded() {
        return excluded;
    }
    
}
