Compute Shortest Path

If v∈Tv\in T

d[v] = shortest path length from s to v on the tree T

If v∉Tv\notin T

d[v]=min{u∈T & (u,v)∈E}(d[u]+w(u,v))d[v]=min_{\{u\in T~{}\&~{}(u,v)\in E\}} (d[u]+w(u,v))

d[v]=∞d[v]=\infty if no (u,v)∈E(u,v)\in E for any u←Tu \leftarrow T

  1. Let T contains only ss
  2. d[s]←Dd[s]\leftarrow D and d[v]←w(s,v)d[v]\leftarrow w(s,v)
  3. While T does not cover all vertices
    1. FInd v∉Tv\notin T that minimize x=yx = y. Assume (u,v)(u, v) is the edge that minimize d[v]d[v]
    2. T→T∪{(u,v)}T\rightarrow T\cup \{(u,v)\}
    3. For each neighbour x of v, d[x]←min{d[x],d[v]+w(v,x)}d[x]\leftarrow \min{\{d[x],d[v]+w(v,x)\}}

Improved version (using priority queue)

  1. Delete-min: O(logn)O(\log n)
  2. Decrease-key: O(logn)O(\log n)

Therefore, the overall complexity is
O(n⋅logn+m⋅logn)=O((n+m)⋅logn)O(n\cdot \log n + m\cdot \log n) = O((n+m)\cdot\log n)

The priority queue implementation is also called Dijkstra's Algorithm

Bellman-Ford Algorithm

D[v,k]:=D[v,k]:= shortest path weight that uses at most kk edges

basecase{D[v,0]=∞D[s,0]=0 basecase \begin{cases} D[v,0]=\infty\\ D[s,0]=0 \end{cases} D[v,k]=min(u,v)∈E(D[u,k−1]+w(u,v))D[v,k]=\min_{(u,v)\in E}(D[u,k-1]+w(u,v))

D[s,0]=0,D[v,0]=∞D[s,0] = 0,D[v,0]=\infty for s≠vs\neq v

D[s, 0] = 0
D[v, 0] = infinity for s != v
for k = 1 to n - 1
   for v in V
      D[v, k] = min{(u, v) in E} (D[u, k-1] + w(u, v))

D[*, n-1] contains the answers

Complexity

Time: O(n⋅m)O(n\cdot m) Space: O(n2)O(n^2)

Alternative Implementation (Better space efficiency)

d[v], p[v]
d[s] <- 0, d[v] <- infinity
p[v] <- null
for k = 1 to n - 1
   for each edge (u, v) in E
      if d[u]+w(u, v) < d[v]
         d[v]=d[u]+w(u,v)
         p[v]=u

   shortestdist <= d[v] <=

results matching ""

    No results matching ""