如何查找图表中找到的每个学位的节点集合

时间:2013-10-11 21:28:10

标签: distribution netlogo jung degrees jung2

我正在尝试填充哈希映射,其中键是节点度,值是具有该度值的所有节点的集合。现在我想出了这段代码:

// hashmap to hold the result 
HashMap<Integer, Collection<Node>> result = new HashMap<Integer, Collection<Node>>();
// for each node in the list
for (Node n : nodes) {
    // find node's neighbors
    n.setNei(g.getNeighbors(n));
    // find node's degree
    n.setDegree(n.getNei().size());
    // placeholder
    Integer degree = n.getDegree();
    // if that degree is already present as a key in result
    if (result.containsKey(degree)) {
        // add n to the list of nodes that has that degree value
        boolean add = result.get(degree).add(n);
        // check
        if (!add) {
            // raise exception
            throw new ExtensionException("ERROR: failed to add node to list of nodes with degree " + degree);
        }
        // if that degree is not already present in result
    } else {
        // create a new empty collection of nodes
        List<Node> newList = new ArrayList<Node>();
        // add n as the first element in the new collection
        boolean add = newList.add(n);
        // check
        if (add) {
            // add degree to the key and the collection of nodes with such degree
            result.put(degree, newList);
        } else {
            // raise exception
            throw new ExtensionException("ERROR: failed to add node to list of nodes with degree " + degree);
        }
    }
}

但是我想知道JUNG是否有一些比我的代码更有效的课程来完成这项任务。关键是我不仅需要有学位分布,还需要在一定程度上保存节点集合。

无论如何,我感谢任何指向更高效解决方案的指针。

祝你好运, 西蒙

1 个答案:

答案 0 :(得分:0)

你所做的事实质上是正确的,但可以提高效率: * Graph已经有一个degree()方法 *节点不需要存储邻居或程度;图表为您完成此操作 *您可以使用Guava MultiMap *