shortest path from A to B without superfluous edges (in Java) [closed]

If you implemented it using a priority queue, see Djikstra’s algorithm, you only need to put an “infinite” weight on the path that has reached the dead end and it won’t be visited again.

A “min” priority queue is a datastructure that pops the element with the least weight attached to it. So when you’re traversing, you build up the queue for each traversal (by walking to the node’s neighbours). So if you’ve reached a node that either has:

  • no edges (is a leaf node)
  • edges to nodes you’ve already traversed (in order to avoid the roundabout cyclic traversal)

Then you only need to put a maxed out weight on the path. In Java you would use Integer.MAX_VALUE for int or Long.MAX_VALUE for long.

Leave a Comment