3

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?

Legend
  • 113,822
  • 119
  • 272
  • 400

2 Answers2

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.

amoebe
  • 4,857
  • 5
  • 37
  • 42
Christoph
  • 3,980
  • 2
  • 40
  • 41
  • @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

Curtis
  • 101,612
  • 66
  • 270
  • 352
d.lam
  • 9
  • 1