8

I am trying to randomly traverse through the graph in jgrapht (until I find some target node). To do it, I need to start at the sourceNode, randomly pick any coming out edge and follow it.

I know there there is a method getAllEdges(sourceVertex, targetVertex) that returns all the edges between two given nodes. But how can I get all edges while having only sourceNode, without the target one?

TheMP
  • 8,257
  • 9
  • 44
  • 73
  • Can't see any direct API for doing that. What you can do is either 1. get all the vertices (perhaps using `vertexSet()` method) and then pass each vertex from this set as `targetVertex` for the method `getAllEdges()` and combine the results of all these calls. or 2. get all edges using `edgeSet()` method. Then for each of these edges call `getEdgeSource(E e)` to get sourceVertext. Then compare it eith given vertex to see if this edge starts from the given vertex. Collect these edges and you have your desired result. – Balkrishna Rawool Jun 16 '15 at 20:13
  • It will probably work, but how efficient is this? I've got quite a big graph to analyze. – TheMP Jun 16 '15 at 20:37
  • If you are concerned about performance, as there is not out-of-the-box API, I'd suggest you to take a look at the source and then extend the class to create your own implementation of say getAllEdgesStartingFromVertex(V vertex). That also enables you optimize the method the way you want. – Balkrishna Rawool Jun 16 '15 at 20:42

5 Answers5

5

You can use Graphs.predecessorListOf and Graphs.successorListOf apis directly.

adarsh003
  • 158
  • 1
  • 2
  • 9
2

You can access the outgoing edges of a node(vertex) with outgoingEdgesOf method of a graph object.

Set<MyEdge> edges = myGraph.outgoingEdgesOf(sourceNode);

Also, you can use incomingEdgesOf for the incoming edges.

If you want to access all edges of a node then use

graph.edgesOf(source)
Memin
  • 3,788
  • 30
  • 31
1

Any object that implements the interface Graph<V,E> should have the method edgesOf(V vertex), at least according to the API. Your TransportGraph should be able to do this.

milez
  • 2,201
  • 12
  • 31
0

In case anyone wondered, I did not find any direct way to achieve this, so I went with Balkrishna suggestion from comments. My implementation (Java 8 style) is:

   private List<WeightedEdge> getAllEdgesFromNode(TransportGraph graph, MyNode startNode) {
    return graph.unwrap().edgeSet().stream()
            .filter(weightedEdge -> graph.unwrap().getEdgeSource(weightedEdge).equals(startNode))
            .collect(Collectors.toList());
}

Note: TransportGraph is a wrapper for jgrapht graph I have written myself. My method unwrap() returns SimpleDirectedWeightedGraph,

TheMP
  • 8,257
  • 9
  • 44
  • 73
-2

I was trying to comment milez answer, but I mistakenly wrote it as an answer. So, let's write the answer. Milez proposed using JGrapht library, which I have used myself several times and works quite fine. This library has a RandomWalkIterator class which I think meets the requirements.

Juan
  • 430
  • 3
  • 9
  • Sorry, this was intended to be a comment to the answer by milez and it went into the wrong place. My mistake. milez proposed JGrapht library, which has a RandomWalkIterator class. In my opinion, it implements exactly the requested functionality. – Juan Oct 25 '17 at 15:52