5

Let = (, ) be a directed graph with edge weights and let be a vertex of . All of the edge weights are integers between 1 and 20. Design an algorithm for finding the shortest paths from . The running time of your algorithm should be asymptotically faster than Dijkstra’s running time.

I know that Dijkstra’s running time is O( e + v log v), and try to find a faster algorithm.

If all weights are 1 or only include 0 and 1, I can use BFS O(e+v) in a directed graph, but how to make a faster algorithm for edge weights are integers between 1 and 20.

Pham Trung
  • 11,204
  • 2
  • 24
  • 43
Talor
  • 81
  • 5

1 Answers1

5
  1. Well let`s say you have an edge that goes from u to v with weight w
  2. We can insert w-1 extra nodes between nodes u and v
  3. So after modifying each edge (which takes O(20*E)) we have a graph where every edge is 1
  4. And on such graph we can use regular BFS, we will have atmost 20*N new nodes, and 20*E new edges so complexity is still O(V+E)

i.e.

enter image description here

This gets transformed to this:

enter image description here

Photon
  • 2,717
  • 1
  • 18
  • 22
  • Thank you for answering my question. I understand this step, and I'm sure it works if this is an undirected graph, but is this works in a directed graph? because All of the edge weights are integers between 1 and 20. – Talor Mar 18 '19 at 06:38
  • @Talor Yes it works for both directed & unidrected graphs, updated answer to include a visual example. – Photon Mar 18 '19 at 06:46