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?
You can use Graphs.predecessorListOf and Graphs.successorListOf apis directly.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With