The many cases of finding shortest paths
Dynamic programming
Bellman-Ford algorithm
We’ve already seen how to calculate the shortest path in an
unweighted graph (BFS traversal)
We’ll now study how to compute the shortest path in different
circumstances for weighted graphs
Tyler Moore
CSE 3353, SMU, Dallas, TX
1
Lecture 18
2
3
Single-source shortest path on a weighted DAG
Single-source shortest path on a weighted graph with nonnegative
weights (Dijkstra’s algorithm)
Single-source shortest path on a weighted graph including negative
weights (Bellman-Ford algorithm)
Some slides created by or adapted from Dr. Kevin Wayne. For more information see
http://www.cs.princeton.edu/~wayne/kleinberg-tardos. Some code reused from Python Algorithms by Magnus Lie
Hetland.
2 / 13
��������������
Shortest path problem. Given a digraph ����������, with arbitrary edge
6. D YNAMIC P ROGRAMMING II
weights or costs ���, find cheapest path from node � to node �.
‣ sequence alignment
‣ Hirschberg's algorithm
‣ Bellman-Ford
1
‣ distance vector protocols
5
��������
‣ negative cycles in a digraph
3
-3
4
12
0
-5
8
7
9
�����������
-1
9
2
7
1
11
5
5
-3
4
13
10
����������������������������������
6
�������������
22
3 / 13
4 / 13
��������������������������������
���������������
Dijkstra. Can fail if negative edge weights.
Def. A negative cycle is a directed cycle such that the sum of its edge
weights is negative.
s
u
2
3
1
v
w
-8
5
-3
-3
Reweighting. Adding a constant to every edge weight can fail.
-4
4
u
5
5
2
2
����������������������
t
s
6
ce < 0
c(W ) =
e W
6
3
3
0
v
w
-3
23
24
5 / 13
6 / 13
����������������������������������
����������������������������������
Lemma 1. If some path from � to � contains a negative cycle, then there
Lemma 2. If � has no negative cycles, then there exists a cheapest path
does not exist a cheapest path from ��to �.
from � to � that is simple (and has ≤������� edges).
Pf. If there exists such a cycle �, then can build a �↝� path of arbitrarily
Pf.
�Consider a cheapest �↝� path � that uses the fewest number of edges.
�If � contains a cycle �, can remove portion of � corresponding to �
negative weight by detouring around cycle as many times as desired. ▪
without increasing the cost. ▪
v
v
t
�
t
�
��������
�����≥��
25
26
7 / 13
8 / 13
�����������������������������������������
������������������������������������
Shortest path problem. Given a digraph ���������� with edge weights ��� and
Def. ��������� = cost of shortest �↝� path that uses ≤ � edges.
no negative cycles, find cheapest �↝� path for each node �.
�Case 1:
Cheapest �↝� path uses ≤ ����� edges.
� �������������������������
Negative cycle problem. Given a digraph ���������� with edge weights ���,
find a negative cycle (if one exists).
optimal substructure property
(proof via exchange argument)
�Case 2:
Cheapest �↝� path uses exactly � edges.
� if ������ is first edge, then ��� uses ������, and then selects best �↝�
4
path using ≤ ����� edges
5
1
2
-3
5
t
�������������������
⎧�� �∞
⎪�
⎧�
������ �� � ⎨�
�� ��� ⎨������ −�� �� �
⎪�
⎩�
⎩�
��
-3
-3
-4
4
��� � ����� −�� �� � ���
��� �� ∈ �
����� � �
⎫�
� ⎬� ���������
⎭�
��������������
Observation. If no negative cycles, ������������� = cost of cheapest �↝� path.
Pf. By Lemma 2, cheapest �↝� path is simple. ▪
27
28
9 / 13
Bellman-Ford algorithm intuition
10 / 13
Bellman-Ford algorithm code
With negative edges, we can’t select the next vertex to visit cleverly
Instead, we just relax all m edges n times in a row
If, after n rounds, the upper bounds don’t shrink, then we’ve found
the shortest path from the source
But if the upper bounds still shrink after n rounds, then there is a
negative cycle in the graph, which is a problem (why?)
Running time: Θ(m · n)
11 / 13
def b e l l m a n f o r d (G , s ) :
D, P = { s : 0 } , {}
# Zero−d i s t t o s ; no p a r e n t s
for rnd in G:
# n = l e n (G) r o u n d s
changed = F a l s e
# No c h a n g e s i n r o u n d s o f a r
for u in G:
# For e v e r y from−node . . .
for v in G[ u ] :
# . . . and i t s to−n o d e s . . .
i f r e l a x (G , u , v , D, P ) : # S h o r t c u t t o v from u ?
changed = True
# Yes ! So s o m e t h i n g changed
i f not changed : break # No ch a n g e i n r o u n d : Done
else :
# Not done b e f o r e r o u n d n ?
raise ValueError ( ’ negative cycle ’ )
# Negative cycle detecte
r e t u r n D, P
# O t h e r w i s e : D and P c o r r e c t
12 / 13
Bellman-Ford algorithm example
5
t
x
−
−2
7
7
−
6
3
8
4
s
9
y
z
2
Node
s
t
x
y
z
d[Node]: upper bd. dist. from s
init. 1
2
3 4
5
0
∞
∞
∞
∞
0
6
∞
7
∞
0
6
114
7
2
0
2
4
7
2
0
2
4
7
-2
0
2
4
7
-2
Edge
Eval.
Order
(t,x)
(t,y)
(t,z)
(x,t)
(y,x)
(y,z)
(z,x)
(z,s)
(s,t)
(s,y)
13 / 13
© Copyright 2026 Paperzz