0

Given a directed graph and some of its nodes, how to prune the nodes that cannot reach any of the given nodes. (I term it leaf components, which I am not sure is a correct term)

Are there any known algorithms solving this efficiently?

It would be perfect if you could point out some Java Open source code for it.

Thanks.

Karussell
  • 17,085
  • 16
  • 97
  • 197
Infinite
  • 3,198
  • 4
  • 27
  • 36
  • I've implemented this for OSM data to eliminate small subnetworks: https://github.com/graphhopper/graphhopper/blob/master/src/main/java/com/graphhopper/routing/util/PrepareRoutingSubnetworks.java – Karussell Oct 07 '12 at 21:35

2 Answers2

2

Start a Breadth First Search or a Depth First Search starting from your given set of nodes and mark all nodes that the search traverses. Afterwards all non-marked nodes are not reachable from your given set of nodes and can be pruned. If n are the number of vertices and m the number of edges, this would solve your problem in O(n + m).

I personally prefer Tinkerpop Blueprints as my main library for Graph processing in the JVM/Java/Scala.

Uwe L. Korn
  • 8,080
  • 1
  • 30
  • 42
0

If i got you right, you need to find the Strongly Connected Components, which can be found in O(n + m) time by doing 2 depth-first searches.

dreamzor
  • 5,795
  • 4
  • 41
  • 61