3
CSCI 256
Data Structures and Algorithm Analysis
Lecture 17
Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley
all rights reserved, and some by Iker Gondra
Shortest Paths
• Shortest path problem: Given a directed graph G = (V,
E), with edge weights cvw, find shortest path from node s
to node t
– Ex: Nodes represent agents in a financial setting and cvw is cost
of transaction in which we buy from agent v and sell immediately
to w
s
10
2
9
18
6
6
30
15
-8
5
16
44
6
-16
11
20
7
3
19
4
6
t
Shortest Paths: Failed Attempts
A path that starts on an
expensive edge, but then
compensates with subsequent
edges of negative cost, can be
cheaper than a path that
starts on a cheap edge.
Dijskstra-style greedy
approach will not work!
• Dijkstra: Can fail if negative edge costs
u
2
3
s
v
-6
1
t
• Re-weighting: Adding a constant to every edge weight
can fail
5
2
s
6
3
5
2
0
-3
6
3
t
Unfortunately,
changing the costs
also changes the
minimum-cost path!
Shortest Paths: Negative Cost Cycles
• Negative cost cycle (cycle
-6
whose total cost is < 0 )
-4
7
• Observation: If some path from s to t contains a negative cost cycle,
there does not exist a shortest s-t path; (WHY not??)
s
t
W
c(W) < 0
Shortest paths on graphs with no
negative cycles
• (Assuming there is a path from s to t) If G has no
negative cycles, then there is a shortest path from s to t
that is simple (i.e., has no repeated nodes). Why?
Because all cycles have nonnegative cost, if a path P
includes a cycle, we simply remove it resulting in a path
with no greater cost and fewer edges
• Conclude: Shortest paths have at most n-1 edges (recall
n= number of nodes).
Shortest Paths: Dynamic Programming
• Let’s use DP to solve the Problem: find the shortest
path from s to t when there are negative edge costs but
no negative cycles
• We could try an idea that has worked for us so far:
subproblem i could be to find a shortest path using only
the first i nodes
– This idea does not immediately work, but can be made to work
with some effort
– We will discuss a simpler and more efficient solution, the
Bellman-Ford Algorithm. This algorithm is based on the crucial
and obvious
Observation: (assuming there is a path from s to t) if G has no
negative cycles, then there is a shortest path from s to t that is
simple, and hence has at most n-1 edges
(Why is this obvious??)
Shortest Paths: Dynamic Programming
• Def: OPT(i, v) = length of shortest v-t path P using at
most i edges
v
w
t
– Case 1: P uses at most i-1 edges starting at v
• OPT(i, v) = OPT(i-1, v)
– Case 2: P uses at most i edges, with at most i-1 edges starting at
another node w
• if (v, w) is first edge, then OPT uses (v, w), and then selects best w-t
path using at most i-1 edges
• For case w, which w should we use?? Well --- the one that gives
the lowest overall cost -- Write final recurrence (DONE IN CLASS)
• Remark: By previous observation, if no negative cycles, then OPT(n1, v) = length of shortest v-t path
Shortest Paths: Recurrence
• If i > 1,
Opt(i,v) = min ( Opt( i-1,v), min (Opt ( i-1, w) + cvw ) )
w in V
We need to fill in an array with n2 entries; each
column in the table corresponds to the shortest path
from a particular node to t as we allow the path to use
an increasing number of edges.
Shortest Paths: the Bellman-Ford Algorithm
Shortest-Path (G, s, t) {
foreach node v V
Opt[0, v]
Opt[0, t] 0
for i = 1 to n-1
foreach node v V
Compute Opt[i, v] using the recurrence on the
previous slide
return Opt[n-1, s]
}
• Example (work through some calculations from
text Fig 6.23 (a) – note they have interchanged
the rows and columns in Fig 6. 23(b)
• Analysis: O(n3) time, (n2) space – do you see
why?
• ( Can be implemented in O(mn) time if G does
not have too many edges (i.e., G is sparse).
Memory requirements can also be reduced to
O(n) – explanation is found in the text )
Detecting Negative Cycles
• Lemma: If OPT(n,v) = OPT(n-1,v) for all v, then there are
no negative cycles
– Pf: Suppose OPT(n,v) = OPT(n-1,v); then OPT(n+1,v) =
OPT(n,v). So OPT(n+1,v) = OPT(n-1,v).
– Similarly, OPT(k,v) = OPT(n-1,v) for all nodes v and k > n-1.
– Suppose there were a negative cycle; then the values of
OPT(k,v) would become arbitrarily negative as k got very large;
– Hence there can be no negative cycles
• Lemma: If OPT(n,v) < OPT(n-1,v) for some node
v, then (any) shortest path P from v to t contains
a cycle W. Moreover W has negative cost so it
is a negative cycle.
Detecting Negative Cycles
•
Proof. (by contradiction)
– Since OPT(n,v) < OPT(n-1,v), we know the shortest path P has
exactly n edges
– By pigeonhole principle, P must contain a directed cycle W (n
edges in graph with n nodes – at least 1 nodes visited at least 2
times) .
– Deleting W yields a v-t path with < n edges but greater cost;
thus W has negative cost
v
t
W
c(W) < 0
• Lemma: If OPT(n,v) = OPT(n-1,v) for all v, iff
there are no negative cycles with a path to t
Detecting Negative Cycles: Application
• Currency conversion: Given n currencies and exchange
rates between pairs of currencies, is there an arbitrage
opportunity?
– Remark: Fastest algorithm very valuable!
8
$
F
1/7
800
4/3
2/3
3/10
2
3/50
IBM
1/10000
£
170
DM
56
¥
© Copyright 2026 Paperzz