The document discusses different single-source shortest path algorithms. It begins by defining shortest path and different variants of shortest path problems. It then describes Dijkstra's algorithm and Bellman-Ford algorithm for solving the single-source shortest paths problem, even in graphs with negative edge weights. Dijkstra's algorithm uses relaxation and a priority queue to efficiently solve the problem in graphs with non-negative edge weights. Bellman-Ford can handle graphs with negative edge weights but requires multiple relaxation passes to converge. Pseudocode and examples are provided to illustrate the algorithms.
Md. Shafiuzzaman 2
ShortestPath
Shortest Path = Path of minimum weight
δ(u,v)= min{ω(p) : u v}; if there is a path from u to v,
otherwise.
p
3.
Md. Shafiuzzaman 3
Shortest-PathVariants
• Shortest-Path problems
– Single-source shortest-paths problem: Find the shortest path from s
to each vertex v. (e.g. BFS)
– Single-destination shortest-paths problem: Find a shortest path to a
given destination vertex t from each vertex v.
– Single-pair shortest-path problem: Find a shortest path from u to v
for given vertices u and v.
– All-pairs shortest-paths problem: Find a shortest path from u to v
for every pair of vertices u and v.
4.
Md. Shafiuzzaman 4
Negative-weightedges
• No problem, as long as no negative-weight cycles are
reachable from the source
• Otherwise, we can just keep going around it, and
get w(s, v) = −∞ for all v on the cycle.
5.
Md. Shafiuzzaman 5
Relaxation
•Maintain d[v] for each v V
• d[v] is called shortest-path weight estimate
and it is upper bound on δ(s,v)
INIT(G, s)
for each v V do
d[v] ← ∞
π[v] ← NIL
d[s] ← 0
Md. Shafiuzzaman 7
Propertiesof Relaxation
Algorithms differ in
how many times they relax each edge, and
the order in which they relax edges
Question: How many times each edge is relaxed in BFS?
Answer: Only once!
8.
Md. Shafiuzzaman 8
Single-SourceShortest Paths in DAGs
• Shortest paths are always well-defined in dags
no cycles => no negative-weight cycles even if
there are negative-weight edges
• Idea: If we were lucky
To process vertices on each shortest path from left
to right, we would be done in 1 pass
9.
Md. Shafiuzzaman 9
Single-SourceShortest Paths in DAGs
In a dag:
• Every path is a subsequence of the topologically sorted
vertex order
• If we do topological sort and process vertices in that order
• We will process each path in forward order
Never relax edges out of a vertex until have processed
all edges into the vertex
• Thus, just 1 pass is sufficient
Md. Shafiuzzaman 16
Single-SourceShortest Paths in DAGs
DAG-SHORTEST PATHS(G, s)
TOPOLOGICALLY-SORT the vertices of G
INIT(G, s)
for each vertex u taken in topologically sorted order do
for each v Adj[u] do
RELAX(u, v)
17.
Md. Shafiuzzaman 17
Single-SourceShortest Paths in DAGs
Runs in linear time: Θ(V+E)
topological sort: Θ(V+E)
initialization: Θ(V+E)
for-loop: Θ(V+E)
each vertex processed exactly once
=> each edge processed exactly once: Θ(V+E)
18.
Md. Shafiuzzaman 18
•Non-negative edge weight
• Like BFS: If all edge weights are equal, then use BFS,
otherwise use this algorithm
• Use Q = priority queue keyed on d[v] values
(note: BFS uses FIFO)
Dijkstra’s Algorithm For Shortest Paths
Md. Shafiuzzaman 25
DIJKSTRA(G,s)
INIT(G, s)
S←Ø > set of discovered nodes
Q←V[G]
while Q ≠Ø do
u←EXTRACT-MIN(Q)
S←S U {u}
for each v Adj[u] do
RELAX(u, v) > May cause
> DECREASE-KEY(Q, v, d[v])
Dijkstra’s Algorithm For Shortest Paths
26.
Md. Shafiuzzaman 26
Observe:
• Each vertex is extracted from Q and inserted into S
exactly once
• Each edge is relaxed exactly once
• S = set of vertices whose final shortest paths have already
been determined
i.e. , S = {v V: d[v] = δ(s, v) ≠ ∞ }
Dijkstra’s Algorithm For Shortest Paths
27.
Md. Shafiuzzaman 27
•Similar to BFS algorithm: S corresponds to the set of
black vertices in BFS which have their correct breadth-
first distances already computed
• Greedy strategy: Always chooses the closest(lightest)
vertex in Q = V-S to insert into S
• Relaxation may reset d[v] values thus updating
Q = DECREASE-KEY operation.
Dijkstra’s Algorithm For Shortest Paths
28.
Md. Shafiuzzaman 28
•Similar to Prim’s MST algorithm: Both algorithms
use a priority queue to find the lightest vertex outside a
given set S
• Insert this vertex into the set
• Adjust weights of remaining adjacent vertices outside the
set accordingly
Dijkstra’s Algorithm For Shortest Paths
29.
Md. Shafiuzzaman 29
Example:Run algorithm on a sample graph
43
210
5 1
s0
∞ 5
∞ 10 9 8
∞ 6
Dijkstra’s Algorithm For Shortest Paths
30.
Md. Shafiuzzaman 30
•Look at different Q implementation, as did for Prim’s
algorithm
• Initialization (INIT) : Θ(V) time
• While-loop:
• EXTRACT-MIN executed |V| times
• DECREASE-KEY executed |E| times
• Time T = |V| x TE-MIN +|E| x TD-KEY
Running Time Analysis of
Dijkstra’s Algorithm
31.
Md. Shafiuzzaman 31
Bellman-FordAlgorithm for Single
Source Shortest Paths
• More general than Dijkstra’s algorithm:
Edge-weights can be negative
• Detects the existence of negative-weight cycle(s)
reachable from s.
Md. Shafiuzzaman 37
BELMAN-FORD(G, s )
INIT( G, s )
for i ←1 to |V|-1 do
for each edge (u, v) E do
RELAX( u, v )
for each edge ( u, v ) E do
if d[v] > d[u]+w(u,v) then
return FALSE > neg-weight cycle
return TRUE
Bellman-Ford Algorithm for Single
Source Shortest Paths
38.
Md. Shafiuzzaman 38
Observe:
•First nested for-loop performs |V|-1 relaxation passes;
relax every edge at each pass
• Last for-loop checks the existence of a negative-weight
cycle reachable from s
Bellman-Ford Algorithm for Single
Source Shortest Paths
39.
Md. Shafiuzzaman 39
•Running time = O(VxE)
Constants are good; it’s simple, short code(very
practical)
• Example: Run algorithm on a sample graph with
no negative weight cycles.
Bellman-Ford Algorithm for Single
Source Shortest Paths
40.
Md. Shafiuzzaman 40
•Converges in just 2 relaxation passes
• Values you get on each pass & how early converges
depend on edge process order
• d value of a vertex may be updated more than once in a
pass
Bellman-Ford Algorithm for Single
Source Shortest Paths
41.
Assignment
• Implement Dijkstra’sAlgorithm and
Bellman–Ford Algorithm
• Input: Take number of vertices, adjacency
matrix and starting node as input
• Output: Print distance from each node and
the path
Md. Shafiuzzaman 41