Getting all edges from node in jgrapht

I am trying to randomly move around the graph in jgrapht (until I find some target node). To do this, I need to start with the sourceNode, randomly select any output edge and follow it.

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

+4
source share
4 answers

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

+3
source

, - , , . ( Java 8):

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

. TransportGraph - jgrapht, . () SimpleDirectedWeightedGraph,

+1

, Graph<V,E>, edgesOf(V vertex), , API. TransportGraph .

0

, . , . JGrapht, . RandomWalkIterator, , , .

-2

All Articles