简体   繁体   English

使用JUNG的图形中的可达节点集

[英]Set of reachable nodes in graph using JUNG

I am looking for an efficient way of finding the set of all nodes that are reachable from specific collection of nodes in JUNG. 我正在寻找一种有效的方法来查找JUNG中特定节点集合可访问的所有节点的集合。 I am not sure how to do that. 我不确定该怎么做。 One solution would be to get the neighbors of each node of specific collection and do that until no new node added regarding to this process. 一种解决方案是获取特定集合的每个节点的邻居,并进行此操作,直到没有为此过程添加新节点为止。 But I think probably a more efficient way would be available. 但我认为可能会有更有效的方法。 Could you please tell me what would it be? 你能告诉我那是什么吗? (below is the code of my implementation) (下面是我的实现代码)

private HashSet<Customer> getReachableNodes(Collection<Customer> churners, DirectedSparseGraph<Customer, Transaction> net) {

        HashSet<Customer> reachableNode = new HashSet<Customer>();
        for (Customer churner : churners) {
            for(Customer neighbor:net.getVertices()){
                 if(isNeighbor(neighbor,churners,net))   reachableNode.add(neighbor)
            }
        }
        return reachableNode ;
    }

A straightforward approach could be to do a simple Breadth-first traversal ( http://en.wikipedia.org/wiki/Breadth-first_search ), but with a collection of "start nodes" instead of a single one: 一种简单的方法可能是进行简单的广度优先遍历( http://en.wikipedia.org/wiki/Breadth-first_search ),但是要使用“开始节点”的集合而不是一个:

private static <V> Set<V> findReachable(
    Collection<? extends V> startNodes, Graph<V, ?> graph)
{
    Queue<V> queue = new LinkedList<V>();
    Set<V> visited = new LinkedHashSet<V>();
    queue.addAll(startNodes);
    visited.addAll(startNodes);
    while(!queue.isEmpty()) 
    {
        V v = queue.poll();
        Collection<V> neighbors = graph.getNeighbors(v);
        for (V n : neighbors)
        {
            if (!visited.contains(n))
            {
                queue.offer(n);
                visited.add(n);
            }
        }
    }
    return visited;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM