I'd like to find all node's neighbors at distance two for a not so small network (5000 nodes and 25k undirected edges). Right now I am using:
ArrayList<Node> twoDistNei = new ArrayList<Node>();
Collection<Node> myThreads = g.getNeighbors(u);
for(Node t:myThreads){
Collection<Node> neighbors = g.getNeighbors(t);
for(Node uu:neighbors){
if(!twoDistNei.contains(uu)){
twoDistNei.add(uu);
}
}
}
but it is really slow and I am wondering if there are more efficient and fast way to accomplish this.
EDIT: I managed to use KNeighborhoodFilter as mentioned, and this is what I came out with:
KNeighborhoodFilter filter = new KNeighborhoodFilter(u, 2,KNeighborhoodFilter.EdgeType.IN_OUT);
Graph<Node, Edge> transform = filter.transform(zpa);
Collection<Node> vertices = transform.getVertices();
Set<Node> twoDistThreads = new HashSet<Node>();
for (Node v : vertices) {
if(v.getColor().equals("blue")){
twoDistThreads.add((Thread)v);
}
System.out.println("thread " + v.getName() + " has color " + v.getColor());
}
Now I see that the filter only allows to transform() the original network and induce a subgraph with all the selected nodes (plus the nodes linked to the selected nodes... but why?). This imply that I have to filter the new collection of nodes to catch only the 2-dist vertices I am interested in - I have a bipartite graph where a set of nodes is "blue" and the other is "red". Am I doing things right here, @Joshua? Thank you for your help!
Best regards, Simone
This is what KNeighborhoodFilter is for: http://jung.sourceforge.net/doc/api/edu/uci/ics/jung/algorithms/filters/KNeighborhoodFilter.html