I have a large graph that I am processing using JUNG. I was wondering if there JUNG provides a way to extract say a 2-hop neighborhood of a vertex (complete with all edges amongst themselves) into a separate graph?
Asked
Active
Viewed 1,651 times
2 Answers
7
In JUNG 2.0 it is edu.uci.ics.jung.algorithms.filters.KNeighborhoodFilter:
A filter used to extract the k-neighborhood around one or more root node(s). The k-neighborhood is defined as the subgraph induced by the set of vertices that are k or fewer hops (unweighted shortest-path distance) away from the root node.
Here's how you'd use it (assuming you already have a graph and vertex/edge types):
Graph<V, E> graph = // ...
int k = 3; // maximum hops
V startVertex = // ... (pick your starting node)
Filter<V, E> filter = new KNeighborhoodFilter<V, E>(
startVertex, k, EdgeType.IN_OUT);
Graph<V, E> neighborhood = filter.transform(graph);
The neighborhood
graph will be of the same class as your original graph. You will have to create a new filter for each different start node.
-
@Christoph, thank you for posting this sample code, it has helped me greatly. I know this post is four years old, but hey I just came across this answer and is the best one by far. This should be marked as the correct answer! – David Dec 01 '14 at 14:04
0
Try edu.uci.ics.jung.algorithms.connectivity.KNeighborhoodExtractor
-
That's from the now-very-out-of-date JUNG 1.x. The above answer is correct. – Joshua O'Madadhain Feb 08 '12 at 23:47