如何过滤KNeighborhoodFilter的结果?

时间:2013-10-23 09:15:08

标签: java filter jung

我有一个UndirectedSparseGraph g,其中我存储了两种Node,即User和Thread,它们都扩展了Node。我想,对于用户u,检索其2-dist邻域,即与u链接的其他用户,因为它们对属于u的1-dist邻域的线程具有边缘。我知道KNeighborhoodFilter是从调用者节点检索“半径”k节点的方法...这意味着,在我的情况下,将返回1跳的线程和2跳的用户,因此我必须过滤由此产生的集合。这就是我到目前为止所做的:

// filter users in 2-dist nei
Predicate<Node> onlyUsers = new Predicate<Node>() {
    @Override
    public boolean apply(Node node) {
        return node.getName().startsWith("u");
    }
};
// find neighbors of nodes with degree i
Filter<Node, Edge> filter = new KNeighborhoodFilter<Node, Edge>(u, 2, KNeighborhoodFilter.EdgeType.IN_OUT);
// retrieve the nodes - but here we have both types of nodes
Collection<Node> twoDistNei = filter.transform(g).getVertices();
// filter the collection to retain only threads
Collection<Node> twoDistUsers = Collections2.filter(twoDistNei, onlyUsers);

我采用这种方法走在正确的轨道上吗?或者我应该遵循不同的模式来完成我的任务,即从选定的用户那里检索距离为2的用户?

祝你好运, 西蒙

1 个答案:

答案 0 :(得分:1)

您正在做的事情将起作用,除了您还需要删除原始的“根”节点。

从根本上说,您有三种选择: 1.做你现在正在做的事。 2.编写自己的代码,收集一组中邻居的邻居。如果这是一个二分图,那么你只需要删除根,你就完成了。 这可能更节省空间,因为您正在构建的集合永远不会超过它必须的大。 3.如果这是一个二分图,则使用超图,其中节点是用户,超边是线程。然后你只需要超图来询问节点的邻居。

相关问题