Leonid B Leonid B - 1 year ago 85
Java Question

How to reverse Jung DirectedGraph?

I am using JUNG DirectedGraph and I need to transform a given directed graph to a reversed graph, so that the newly created graph contains all vertices and edges from the original graph and all edges in the new graph are reversed. I need to perform this transformation on the model.

Is there a JUNG utility that provides this functionality?
I use JUNG version 2.0.1.
I realize it's easy to implement, just prefer to use the provided utility if available.


Answer Source

After searching a large portion of the JUNG2 API, I assume there is no such utility. What would be needed is an edge transformation function, but even if there are many applications for it, there seem to be no traces of any such utility function.

Depending on your application, a different approach might be worth a look: You could subclass DirectedGraph, implementing a wrapper for any existing Graph that inverts edges on the fly. Specifically, getInEdges() would return getOutEdges() and vice versa. You still need to wrap other functions that depend on the Edge direction.

Depending on your application, this approach might or might not be simpler than simply copying the edges.