Lec17 Dijkstra and Bellman-Ford (Shortest Path)
Lec17 Dijkstra and Bellman-Ford (Shortest Path)
• Source *, destination +.
• Shortest path problem: find shortest directed path from s to t.
Total path weight = sum of edge weights in path
2 23 3
9
s 18
14
Path s-2-3-5-t
2 6 = 9 + 23 + 2 + 16
6
30 4 19 = 50.
11
15 5
5
6
20 16
7 t
44
1
11/2/20
Dijkstra's algorithm
This idea is similar to Prim’s MST algorithm.
Prim keeps track of minimum weight single
Dijkstra's algorithm. edge to attach, rather than total distance.
Dijkstra's algorithm
Dijkstra's algorithm.
• Maintain a set of finalized nodes ! for which we have determined the
shortest path distance "($) from & to $.
• Initialize ! = {&}, "(&) = 0.
• Repeatedly choose unfinalized node , which minimizes total distance
from currently known nodes to reach it.
NOTE: we are
π (v) = min d(u) + we , assumed non-negative
e = (u, v) : u∈ S
2
11/2/20
Dijkstra's algorithm
Dijkstra ( G=(V,E,w), s )
1. Let H = V–{s};
2. For every vertex v do
3. dist[v]= ∞, parent[v]=null
4. dist[s]=0, parent[s] = none
5. Update(s)
6. For i=1 to n-1 do
7. u=extract vertex from H
of smallest weight
8. Update(u)
• Return dist[]
Update (u)
1. For every neighbor v of u (such that v in H)
2. If dist[v]>dist[u]+w(u,v) then
3. dist[v]=dist[u]+w(u,v)
4. parent[v]=u
S u
v
3
11/2/20
Dijkstra – Complexity
Complexity Analysis for ! = ($, &), |$| = ), |&| = *
• We store minimum path weight for each vertex in a min-heap.
• Each heap operation requires +(log )) time
• ) − 1 iterations in which an EXTRACT_MIN is performed
• +() log ))
• Each edge can result in at most one CHANGE_KEY
• +(* log ))
• Overall complexity: +(* log ))
• (This is the same analysis as for Prim’s MST algorithm)
Dijkstra – Complexity
Complexity Analysis for ! = ($, &), |$| = ), |&| = *
• Alternatively, store edge information in an adjacency matrix and store
minimum path weight information for each vertex in a separate list
• ) iterations in which the vertex with minimum path weight is chosen:
• +()) per iteration, +()2) total
• In each iteration, neighbors of the added vertex may have their minimum
path weight updated:
• +()) per iteration, +()2) total
• Overall complexity: +()2)
• Note: this is better than using a binary heap in the case that ! is dense (* ≈ )2)
• (This is the same analysis is for Prim’s MST algorithm)
4
11/2/20
Dijkstra's algorithm
Dijkstra's algorithm:
• Can be specified for a single source,!, and single destination,".
• However, each iteration determines the shortest path from ! to some
vertex #.
• Dijkstra’s algorithm run for $ iterations will find the shortest path
from ! to all vertices.
• Dijkstra works for directed as well as undirected graphs.
• Requires non-negative weights.
5
11/2/20
Bellman-Ford Algorithm
Basic Idea:
• Iterate over the number of vertices
• Keep track of current shortest path (distance and parent) for each vertex
• For each iteration, “relax” all edges
• Consider whether this edge can be used to improve the current shortest path of the
vertex at its endpoint
• After ! iterations, the first ! steps of any shortest path are correct and will
never change from that point (we’ll prove shortly)
• Because every shortest path is simple, we know that after at most " − 1
iterations, we’ll have every shortest path determined.
6
11/2/20
Bellman-Ford – Correctness
Correctness Argument assuming no negative weight cycles:
Proof by induction over the iterations of the algorithm
Claim: Consider any vertex ! and a shortest path " from # to !. Let " be defined as
$0$1$2 … $) , where $0 = # and $) = !. After + ≤ ) iterations of Bellman-Ford, all vertices
along the path $0$1 … $+ have had their shortest path computed.
• Base case: + = 0. Trivial.
• Inductive hypotheses: Suppose true for - iterations. Then the shortest path from # to
$- has been calculated.
• Inductive step: in iteration j+1, all edges are relaxed again, in particular edge . =
($-, $123 ), such that the shortest path is correctly computed for $123 .
Note that by definition of the Bellman-Ford algorithm, the shortest path to a vertex can
never increase from one iteration to the next, and it can never get lower than the true
shortest path.
Bellman-Ford – Correctness
Negative Cycles
• Assuming there are no negative cycles, every shortest path is simple and
contains at most ! − 1 edges
• Therefore the Bellman-Ford algorithm correctly identifies all shortest paths
from source vertex $ in at most ! − 1 iterations.
• As soon as you have an iteration in which nothing changes (no vertices
receive improved shortest paths) the algorithm is finished – nothing can
change from that point.
• So to check for cycles, after completing ! − 1 iterations of Bellman-Ford,
simply scan all edges one more time to see if there is a vertex that could
still be improved.
• If so, that implies a path longer than ! − 1 edges to achieve the shortest path, which
implies a negative cycle.
7
11/2/20
Bellman-Ford Algorithm
Bellman-Ford ( G=(V,E,w), s )