1
votes

Is there a way to find the shortest path from a single source to a vertex in a graph with arbitrary weights in O(E) but you only need to worry about it if the shortest path has 7 edges or less.

Bellman-Ford algorithm has a best case run time of O(E), would that apply here?

1
complexity is the same, you just need to modify the algorithm correspondingly. the execution time is going to be even faster because lots of cases are pruned away - mangusta

1 Answers

1
votes

If you know the shortest paths with <= N steps to all vertices, then it's easy to calculate the shortest paths with <= N+1 steps by iterating over the edges and evaluating the longer paths you could make with each one.

At N=0, the shortest path to the source vertex has length 0 and the shortest paths to all other vertices have length infinity (i.e., you can't get there). You only have to iterate over the edges 7 times to find the shortest paths to everywhere you can get in <= N=7 steps, for a total running time of O(E) if you're a little bit careful about your data structures.