/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package medusa.stats;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import medusa.graph.BasicGraph;
import medusa.graph.Node;

/**
 *
 * @author Sean
 */
public class Stats {

    private static Comparator nodeComparator = new NodeConnectionComparator();

    /**
     * Returns the k most connected nodes along with their
     * connection number
     * @param k
     * @return
     */
    public static String nodeRanking(BasicGraph g, int k) {
        List nodes = new ArrayList(g.getNodes().values());
        Collections.sort(nodes, nodeComparator);
        StringBuffer sb = new StringBuffer();
        // append the top k to the stringbuffer
        int l=nodes.size();
        for (int i = l-k; i < l; i++) {
            Node node = nodes.get(i);
            sb.append(node.getLabel()).append("\t");
            sb.append(node.getAnnotation()).append("\t");

            sb.append(node.getConnections()).append("\n");

        }
        return sb.toString();
    }

    static class NodeConnectionComparator implements Comparator {

        public int compare(Object o1, Object o2) {
            Node a = (Node) o1;
            Node b = (Node) o2;
            int c1 = a.getConnections();
            int c2 = b.getConnections();
            if (c1 < c2) {
                return -1;
            }
            if (c1 > c2) {
                return 1;

            }
            return 0;
        //System.out.println(as+" "+bs);
        // return a.getConnections().compareTo(b.getConnections());
        }
    }
}

 

Ads help maintain this website.