简体   繁体   中英

Single source shortest path in JGraphT for unweighted graph

JGraphT has many implementations for shortest paths (Dijkstra, Belman Ford and more)

I need a single source shortest path for an unweighted graph.

Here are my questions (specific to JGraphT):

  1. First, I assume that using Dijkstra for unweighted graphs is wasteful (uses a priority queue which has slower queue deque than a regular queue that BFS uses, and since on unweighted graph all weights are 1, this is not really adding any value). Is my assumption correct?

  2. Assuming the answer to 1 is "yes", then I assume I'll use BreadthFirstIterator other than rolling my own, (unit tested, and as simple BFS is, I'm sure I'll have some corner case bug, even Java's binary search had an integer overflow thank's to a bug that Josh Bloch himself introduced and it wasn't solved until 2006!). But the question is, there is still a (very small) step to take from a raw BFS to actually getting the single source shortest paths, should I write my own UnweightedSingleSourceShortestPaths class? or is there one hidden in JGraphT core library that I can just plug into?

Since I think I found the answer to the 2nd question, I think it also answers the 1st question (if JgraphT's Dijkstra was the most efficient for the simple case of all weights = 1, then why would CDK create their own?)

Here is the answer to #2 - yes, there is an open source (LGPL) solution: https://github.com/cdk/cdk/blob/master/legacy/src/main/java/org/openscience/cdk/graph/BFSShortestPath.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM