如何过滤KNeighborhoodFilter的结果? [英] How to filter the result of KNeighborhoodFilter?

查看:124
本文介绍了如何过滤KNeighborhoodFilter的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UndirectedSparseGraph g,其中存储了两种Node,即User和Thread,它们都扩展了Node.我希望对于用户u检索其2距离邻域,即与u链接的其他用户,因为它们对属于u的1距离邻域的线程具有优势.我知道KNeighborhoodFilter是从调用者节点中检索半径为k"的节点的方法...在我的情况下,这意味着将返回1跳的线程和2跳的用户,因此我必须进行过滤结果集合.这是我到目前为止所拥有的:

I have a UndirectedSparseGraph g in which I have stored two kind of Node, namely User and Thread that both extend Node. I would like, for a User u, to retrieve its 2-dist neighborhood, i.e. other users that are linked with u because they have an edge to the threads belonging to u's 1-dist neighborhood. I know that KNeighborhoodFilter is the way to retrieve nodes "in radius" k from the caller node... this means, in my case, that both threads at 1 hop and users at 2 hops will be returned, and thus I have to filter the resulting collection. This is what I have so far:

// 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的用户?

Am I on the right track with this approach? or should I follow a different pattern to accomplish my task, which is to retrieve users at distance two from a selected user?

最诚挚的问候, 西蒙妮(Simone)

Best regards, Simone

推荐答案

您正在执行的操作将正常运行,只是您还需要删除原始的根"节点.

What you're doing will work, except that you need to remove the original 'root' node as well.

从根本上讲,您有三个选择: 1.做你现在正在做的事情. 2.编写自己的代码以将u的邻居收集到一个集合中.如果这是二部图,则只需删除根即可. 这可能会更节省空间,因为您要构建的集合永远不会比必须的更大. 3.如果这是二部图,请改用超图,其中节点是用户,超边缘是线程.然后,您只需要向超图查询节点的邻居.

Fundamentally, you have three choices: 1. Do what you're doing now. 2. Write your own code to collect the neighbors of u's neighbors in a set. If this is a bipartite graph, then you only have to remove the root and you're done. This is probably more space efficient, because the set you're building is never bigger than it has to be. 3. If this is a bipartite graph, use a hypergraph instead, where the nodes are users and the hyperedges are threads. Then you just ask the hypergraph for the nodes' neighbors.

这篇关于如何过滤KNeighborhoodFilter的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆