Design an algorithm for the single source shortest path problem that runs in time O(k(|V|+|E|))

五迷三道 提交于 2019-12-01 21:16:56

Here`s O(k(|V | + |E|)) approach:

  1. We can use Bellman-Ford algorithm with some modifications
  2. Create array D[] to store shortest path from node s to some node u
  3. initially D[s]=0, and all other D[i]=+oo (infinity)
  4. Now after we iterate throught all edges k times and relax them, D[u] holds shortest path value from node s to u after <=k edges
  5. Because any s-u shortest path is atmost k edges, we can end algorithm after k iterations over edges

    Pseudocode:

    for each vertex v in vertices:
    D[v] := +oo

    D[s] = 0

    repeat k times:
    for each edge (u, v) with weight w in edges:
    if D[u] + w < D[v]:
    D[v] = D[u] + w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!