Shortest path algorithms differ in
the number of times the edges are
relaxed and the order in which they
are relaxed.
Shortests Paths in DAGs
edges according to a topological ordering of vertices.
When the graph is a directed acyclic graph (DAG), relax
An Example
Topological order: s, r, y, x, z, t
s
t
z
y
r
9
∞
∞
∞
0
∞
∞
3
2
9
6
5
3
4
1
x
s
t
z
y
r
9
3
∞
9
0
∞
2
2
9
6
5
3
4
Relax edges 〈s, r〉, 〈s, x〉, 〈s,
t〉.
1
3
x
Topological order: s, r, y, x, z, t
s
t
z
y
r
9
3
7
9
0
1
1
2
2
9
6
5
3
4
Relax edges 〈r, x〉, 〈r, y〉, 〈r, z〉.
1
3
x
Topological order: s, r, y, x, z, t
s
t
z
y
r
9
3
7
9
0
1
0
2
2
9
6
5
3
4
Relax edge 〈y, z〉.
1
3
x
Topological order: s, r, y, x, z, t
s
t
z
y
r
9
3
7
9
0
7
2
2
9
6
5
3
4
Relax edge 〈x, z〉.
1
3
x
Topological order: s, r, y, x, z, t
Finish
s
t
z
y
r
9
3
7
8
0
7
2
2
9
6
5
3
4
Relax edge 〈z, t〉.
1
3
x
Topological order: s, r, y, x, z, t
Correctness
Path relaxation property: If is a shortest
The topological order guarantees that edges on every path from the
source will be relaxed in the order in which they appear on the path.
Thus, the path relaxation property is satisfied and the algorithm
works correctly.
Dijkstra’s Algorithm
Applicable when all edge weights are non-negative.
Greedy strategy (same as breadth-first search if all weights are 1):
...
How to Find the Next Closest?
Relaxation
An Example
b
d
2
7
5
4
1
4
3
4
1
5
7
0
∞
2
∞
∞
∞
∞
∞
a
e
f
s
c
b
d
2
7
5
4
1
4
3
4
1
5
7
0
2
5
4
2
∞
∞
∞
k
s
e
f
c
a
b
d
s
2
7
5
4
1
4
3
4
1
5
7
0
2
4
4
9
2
∞
∞
a
c
e
f
a
b
d
s
2
7
5
4
1
4
3
4
1
5
7
0
2
4
4
8
7
2
∞
e
f
c
a
b
d
s
2
7
5
4
1
4
3
4
1
5
7
0
2
4
4
8
7
2
∞
c
e
f
a
f
c
b
d
s
2
7
5
4
1
4
3
4
1
5
7
0
2
4
4
8
7
14
2
e
a
b
d
s
2
7
5
4
1
4
3
4
1
5
7
0
2
4
4
8
7
13
2
f
e
c
a
fb
d
s
2
7
5
4
1
4
3
4
1
5
7
0
2
4
4
8
7
13
2
c
e