shortest paths: dag and floyd-warshall
TRANSCRIPT
CS/ECE 374: Algorithms & Models of
Computation
Shortest Paths: DAG andFloyd-Warshall
Lecture 18
(UIUC) CS/ECE 374 1 April 6, 2021 1 / 22
Part I
The Crucial Optimality Substructure
(UIUC) CS/ECE 374 2 April 6, 2021 2 / 22
Shortest distance problems
Optimality substructure:
dist(s, u) = minv2In(u) [dist(s, v) + `(v , u)]
Bellman-Ford: d(u) = minv2In(u) [d(v) + `(v , u)]
1 If v is on the shortest path of u, and d(v) = dist(s, v), thend(u) = dist(s, u) in the next iteration.
2 Initialize d(s) = 0, all d(u) = 1, converge to the fixed point.
(UIUC) CS/ECE 374 3 April 6, 2021 3 / 22
Shortest distance problems
Optimality substructure:
dist(s, u) = minv2In(u) [dist(s, v) + `(v , u)]
Bellman-Ford: d(u) = minv2In(u) [d(v) + `(v , u)]
1 If v is on the shortest path of u, and d(v) = dist(s, v), thend(u) = dist(s, u) in the next iteration.
2 Initialize d(s) = 0, all d(u) = 1, converge to the fixed point.
(UIUC) CS/ECE 374 3 April 6, 2021 3 / 22
Shortest distance problems
Optimality substructure:
dist(s, u) = minv2In(u) [dist(s, v) + `(v , u)]
Bellman-Ford: d(u) = minv2In(u) [d(v) + `(v , u)]
1 If v is on the shortest path of u, and d(v) = dist(s, v), thend(u) = dist(s, u) in the next iteration.
2 Initialize d(s) = 0, all d(u) = 1, converge to the fixed point.
(UIUC) CS/ECE 374 3 April 6, 2021 3 / 22
Shortest distance problems
Optimality substructure:
dist(s, u) = minv2In(u) [dist(s, v) + `(v , u)]
Bellman-Ford: d(u) = minv2In(u) [d(v) + `(v , u)]
1 If v is on the shortest path of u, and d(v) = dist(s, v), thend(u) = dist(s, u) in the next iteration.
2 Initialize d(s) = 0, all d(u) = 1, converge to the fixed point.
(UIUC) CS/ECE 374 3 April 6, 2021 3 / 22
Example
s
a c
b
d f
e
6 3
4
�1�3
0 5
8
�3
�8 2
1
(UIUC) CS/ECE 374 16 April 1, 2021 16 / 36
0 I 2 3 4 5 G
S O 0 O O O O 0
a I 6 L I I 2
s Esa b 4 2 2 2 2 2
Idf c 3 3 3 3 3 3d I 2 2 I I
Feb e I 9 T 9IT f I 7 7 7 7s c
t 3 fI gd fee f
Example
s
a c
b
d f
e
6 3
4
�1�3
0 5
8
�3
�8 2
1
(UIUC) CS/ECE 374 4 April 6, 2021 4 / 22
TI s c b fe d a
Parsimonious updates of Dijkstra
Optimality substructure:
dist(s, u) = minv2In(u) [dist(s, v) + `(v , u)]
Dijkstra: d(u) = minv2In(u),v2X [d(v) + `(v , u)]
1 v in X is known to have d(v) = d(s, v)2 Only update u adjacent to X . Each edge is only updated once.
3 A good evaluation order saves a lot of work. We will see it again
with DAG.
(UIUC) CS/ECE 374 5 April 6, 2021 5 / 22
Parsimonious updates of Dijkstra
Optimality substructure:
dist(s, u) = minv2In(u) [dist(s, v) + `(v , u)]
Dijkstra: d(u) = minv2In(u),v2X [d(v) + `(v , u)]
1 v in X is known to have d(v) = d(s, v)
2 Only update u adjacent to X . Each edge is only updated once.
3 A good evaluation order saves a lot of work. We will see it again
with DAG.
(UIUC) CS/ECE 374 5 April 6, 2021 5 / 22
Parsimonious updates of Dijkstra
Optimality substructure:
dist(s, u) = minv2In(u) [dist(s, v) + `(v , u)]
Dijkstra: d(u) = minv2In(u),v2X [d(v) + `(v , u)]
1 v in X is known to have d(v) = d(s, v)2 Only update u adjacent to X . Each edge is only updated once.
3 A good evaluation order saves a lot of work. We will see it again
with DAG.
(UIUC) CS/ECE 374 5 April 6, 2021 5 / 22
Shortest distance problems
Why didn’t we use
dist(s, u) = minv [dist(s, v) + dist(v , u)] ?
Bellman-Ford? d(u) = minv [d(v) + d(v , u)]?
1 We will need to compute d(v , u), for all v , when we only need
distances from s. Extra work.
2 Will be useful for computing all-pair shortest distance.
Floyd-Warshall
(UIUC) CS/ECE 374 6 April 6, 2021 6 / 22
Shortest distance problems
Why didn’t we use
dist(s, u) = minv [dist(s, v) + dist(v , u)] ?
Bellman-Ford? d(u) = minv [d(v) + d(v , u)]?
1 We will need to compute d(v , u), for all v , when we only need
distances from s. Extra work.
2 Will be useful for computing all-pair shortest distance.
Floyd-Warshall
(UIUC) CS/ECE 374 6 April 6, 2021 6 / 22
Shortest distance problems
Why didn’t we use
dist(s, u) = minv [dist(s, v) + dist(v , u)] ?
Bellman-Ford? d(u) = minv [d(v) + d(v , u)]?
1 We will need to compute d(v , u), for all v , when we only need
distances from s. Extra work.
2 Will be useful for computing all-pair shortest distance.
Floyd-Warshall
(UIUC) CS/ECE 374 6 April 6, 2021 6 / 22
Part II
Shortest Paths in DAGs
(UIUC) CS/ECE 374 7 April 6, 2021 7 / 22
Shortest Paths in a DAG
Single-Source Shortest Path Problems
Input A directed acyclic graph G = (V ,E) with arbitrary
(including negative) edge lengths. For edge e = (u, v),`(e) = `(u, v) is its length.
1 Given nodes s, t find shortest path from s to t.
2 Given node s find shortest path from s to all other nodes.
Simplification of algorithms for DAGs
1 No cycles and hence no negative length cycles!
2 Can order nodes using topological sort
(UIUC) CS/ECE 374 8 April 6, 2021 8 / 22
Shortest Paths in a DAG
Single-Source Shortest Path Problems
Input A directed acyclic graph G = (V ,E) with arbitrary
(including negative) edge lengths. For edge e = (u, v),`(e) = `(u, v) is its length.
1 Given nodes s, t find shortest path from s to t.
2 Given node s find shortest path from s to all other nodes.
Simplification of algorithms for DAGs
1 No cycles and hence no negative length cycles!
2 Can order nodes using topological sort
(UIUC) CS/ECE 374 8 April 6, 2021 8 / 22
Algorithm for DAGs
1 Want to find shortest paths from s. Ignore nodes not reachable
from s.
2 Let s = v1, v2, vi+1, . . . , vn be a topological sort of G
Observation:
1 shortest path from s to vi cannot use any node from
vi+1, . . . , vn
2 can find shortest paths in topological sort order.
(UIUC) CS/ECE 374 9 April 6, 2021 9 / 22
Algorithm for DAGs
1 Want to find shortest paths from s. Ignore nodes not reachable
from s.
2 Let s = v1, v2, vi+1, . . . , vn be a topological sort of G
Observation:
1 shortest path from s to vi cannot use any node from
vi+1, . . . , vn
2 can find shortest paths in topological sort order.
(UIUC) CS/ECE 374 9 April 6, 2021 9 / 22
Algorithm for DAGs
for i = 1 to n do
d(s, vi ) = 1d(s, s) = 0
for i = 1 to n � 1 do
for each edge (vi , vj ) in Out(vi ) do
d(s, vj ) = min{d(s, vj ), d(s, vi ) + `(vi , vj )}
return d(s, ·) values computed
Correctness: induction on i and observation in previous slide.
Running time: O(m + n) time algorithm!
(UIUC) CS/ECE 374 10 April 6, 2021 10 / 22
Part III
All Pairs Shortest Paths
(UIUC) CS/ECE 374 11 April 6, 2021 11 / 22
Shortest Path Problems
Shortest Path Problems
Input A (undirected or directed) graph G = (V ,E) with edge
lengths (or costs). For edge e = (u, v),`(e) = `(u, v) is its length.
1 Given nodes s, t find shortest path from s to t.
2 Given node s find shortest path from s to all other nodes.
3 Find shortest paths for all pairs of nodes.
(UIUC) CS/ECE 374 12 April 6, 2021 12 / 22
Single-Source Shortest Paths
Single-Source Shortest Path Problems
Input A (undirected or directed) graph G = (V ,E) with edge
lengths. For edge e = (u, v), `(e) = `(u, v) is its
length.
1 Given nodes s, t find shortest path from s to t.
2 Given node s find shortest path from s to all other nodes.
Dijkstra’s algorithm for non-negative edge lengths. Running time:
O((m + n) log n) with heaps and O(m + n log n)
with advanced priority queues.
Bellman-Ford algorithm for arbitrary edge lengths. Running time:
O(nm).
(UIUC) CS/ECE 374 13 April 6, 2021 13 / 22
Single-Source Shortest Paths
Single-Source Shortest Path Problems
Input A (undirected or directed) graph G = (V ,E) with edge
lengths. For edge e = (u, v), `(e) = `(u, v) is its
length.
1 Given nodes s, t find shortest path from s to t.
2 Given node s find shortest path from s to all other nodes.
Dijkstra’s algorithm for non-negative edge lengths. Running time:
O((m + n) log n) with heaps and O(m + n log n)
with advanced priority queues.
Bellman-Ford algorithm for arbitrary edge lengths. Running time:
O(nm).
(UIUC) CS/ECE 374 13 April 6, 2021 13 / 22
m n 0cm mum OCP
All-Pairs Shortest Paths
All-Pairs Shortest Path Problem
Input A (undirected or directed) graph G = (V ,E) with edge
lengths. For edge e = (u, v), `(e) = `(u, v) is its
length.
1 Find shortest paths for all pairs of nodes.
Apply single-source algorithms n times, once for each vertex.
1 Non-negative lengths. O(nm log n) with heaps and
O(nm + n2log n) using advanced priority queues.
2 Arbitrary edge lengths: O(n2m).
Can we do better?
(UIUC) CS/ECE 374 14 April 6, 2021 14 / 22
All-Pairs Shortest Paths
All-Pairs Shortest Path Problem
Input A (undirected or directed) graph G = (V ,E) with edge
lengths. For edge e = (u, v), `(e) = `(u, v) is its
length.
1 Find shortest paths for all pairs of nodes.
Apply single-source algorithms n times, once for each vertex.
1 Non-negative lengths. O(nm log n) with heaps and
O(nm + n2log n) using advanced priority queues.
2 Arbitrary edge lengths: O(n2m).
Can we do better?
(UIUC) CS/ECE 374 14 April 6, 2021 14 / 22
O.cn4 if man
All-Pairs Shortest Paths
All-Pairs Shortest Path Problem
Input A (undirected or directed) graph G = (V ,E) with edge
lengths. For edge e = (u, v), `(e) = `(u, v) is its
length.
1 Find shortest paths for all pairs of nodes.
Apply single-source algorithms n times, once for each vertex.
1 Non-negative lengths. O(nm log n) with heaps and
O(nm + n2log n) using advanced priority queues.
2 Arbitrary edge lengths: O(n2m).
Can we do better?
(UIUC) CS/ECE 374 14 April 6, 2021 14 / 22
Optimality substructure
Why don’t we use
dist(s, u) = minv [dist(s, v) + dist(v , u)] ?
What is a smart recursion?
(UIUC) CS/ECE 374 15 April 6, 2021 15 / 22
Optimality substructure
Why don’t we use
dist(s, u) = minv [dist(s, v) + dist(v , u)] ?
What is a smart recursion?
(UIUC) CS/ECE 374 15 April 6, 2021 15 / 22
A naive recursion
i
4
1
100
1
102 j
3
5
11
2
(UIUC) CS/ECE 374 16 April 6, 2021 16 / 22
I I 2 3 ji 0 4 1 A 100I o 0 1 526 2 0 A 10
is 0
i 1 2 3 jJ 6000 O
i 0 i 11 00 o i 72 12 1 112 32 o s O n is
3 o ou oi 73 13 I p
j s oi jtj
A naive recursion
Running Time: O(n4), Space: O(n
3).
Worse than Bellman-Ford: O(n2m), when m = O(n
2).
1 It’s wasteful because the intermediate nodes can be any node.
As a result, we compute the same path many times.
2 Idea: Restrict the set of intermediate nodes.
(UIUC) CS/ECE 374 17 April 6, 2021 17 / 22
A naive recursion
Running Time: O(n4), Space: O(n
3).
Worse than Bellman-Ford: O(n2m), when m = O(n
2).
1 It’s wasteful because the intermediate nodes can be any node.
As a result, we compute the same path many times.
2 Idea: Restrict the set of intermediate nodes.
(UIUC) CS/ECE 374 17 April 6, 2021 17 / 22
A naive recursion
Running Time: O(n4), Space: O(n
3).
Worse than Bellman-Ford: O(n2m), when m = O(n
2).
1 It’s wasteful because the intermediate nodes can be any node.
As a result, we compute the same path many times.
2 Idea: Restrict the set of intermediate nodes.
(UIUC) CS/ECE 374 17 April 6, 2021 17 / 22
A naive recursion
Running Time: O(n4), Space: O(n
3).
Worse than Bellman-Ford: O(n2m), when m = O(n
2).
1 It’s wasteful because the intermediate nodes can be any node.
As a result, we compute the same path many times.
2 Idea: Restrict the set of intermediate nodes.
(UIUC) CS/ECE 374 17 April 6, 2021 17 / 22
All-Pairs: Recursion on index of intermediate nodes
1 Number vertices arbitrarily as v1, v2, . . . , vn
2 dist(i , j , k): length of shortest walk from vi to vj among all
walks in which the largest index of an intermediate node is at
most k (could be �1 if there is a negative length cycle).
i
4
1
100
1
102 j
3
5
11
2
dist(i , j , 0) =
100
dist(i , j , 1) =
9
dist(i , j , 2) =
8
dist(i , j , 3) =
5
(UIUC) CS/ECE 374 18 April 6, 2021 18 / 22
All-Pairs: Recursion on index of intermediate nodes
1 Number vertices arbitrarily as v1, v2, . . . , vn
2 dist(i , j , k): length of shortest walk from vi to vj among all
walks in which the largest index of an intermediate node is at
most k (could be �1 if there is a negative length cycle).
i
4
1
100
1
102 j
3
5
11
2
dist(i , j , 0) = 100
dist(i , j , 1) =
9
dist(i , j , 2) =
8
dist(i , j , 3) =
5
(UIUC) CS/ECE 374 18 April 6, 2021 18 / 22
All-Pairs: Recursion on index of intermediate nodes
1 Number vertices arbitrarily as v1, v2, . . . , vn
2 dist(i , j , k): length of shortest walk from vi to vj among all
walks in which the largest index of an intermediate node is at
most k (could be �1 if there is a negative length cycle).
i
4
1
100
1
102 j
3
5
11
2
dist(i , j , 0) = 100
dist(i , j , 1) = 9
dist(i , j , 2) =
8
dist(i , j , 3) =
5
(UIUC) CS/ECE 374 18 April 6, 2021 18 / 22
Igo If
All-Pairs: Recursion on index of intermediate nodes
1 Number vertices arbitrarily as v1, v2, . . . , vn
2 dist(i , j , k): length of shortest walk from vi to vj among all
walks in which the largest index of an intermediate node is at
most k (could be �1 if there is a negative length cycle).
i
4
1
100
1
102 j
3
5
11
2
dist(i , j , 0) = 100
dist(i , j , 1) = 9
dist(i , j , 2) = 8
dist(i , j , 3) =
5
(UIUC) CS/ECE 374 18 April 6, 2021 18 / 22
All-Pairs: Recursion on index of intermediate nodes
1 Number vertices arbitrarily as v1, v2, . . . , vn
2 dist(i , j , k): length of shortest walk from vi to vj among all
walks in which the largest index of an intermediate node is at
most k (could be �1 if there is a negative length cycle).
i
4
1
100
1
102 j
3
5
11
2
dist(i , j , 0) = 100
dist(i , j , 1) = 9
dist(i , j , 2) = 8
dist(i , j , 3) = 5
(UIUC) CS/ECE 374 18 April 6, 2021 18 / 22
v.v I
For the following graph, dist(i, j, 2) is...
i
8
5
200
1
10
2j
3
5
11
2
2
(A) 9
(B) 10
(C) 11
(D) 12
(E) 15
(UIUC) CS/ECE 374 19 April 6, 2021 19 / 22
i 2 i j12
All-Pairs: Recursion on index of intermediate nodes
i j
kdist(i, k, k � 1) dist(k, j, k � 1)
dist(i, j, k � 1)
dist(i , j , k) = min
(dist(i , j , k � 1)
dist(i , k, k � 1) + dist(k, j , k � 1)
Base case: dist(i , j , 0) = `(i , j) if (i , j) 2 E , otherwise 1
(UIUC) CS/ECE 374 20 April 6, 2021 20 / 22
Not use k
use K
All-Pairs: Recursion on index of intermediate nodes
If i can reach k and k can reach j and dist(k, k, k � 1) < 0 then
G has a negative length cycle containing k and dist(i , j , k) = �1.
Recursion below is valid only if dist(k, k, k � 1) � 0. We can
detect this during the algorithm or wait till the end.
dist(i , j , k) = min
(dist(i , j , k � 1)
dist(i , k, k � 1) + dist(k, j , k � 1)
(UIUC) CS/ECE 374 21 April 6, 2021 21 / 22
Floyd-Warshall Algorithm
for All-Pairs Shortest Paths
for i = 1 to n do
for j = 1 to n do
dist(i , j , 0) = `(i , j) (* `(i , j) = 1 if (i , j) /2 E, 0 if i = j *)
for k = 1 to n do
for i = 1 to n do
for j = 1 to n do
dist(i , j , k) = min
(dist(i , j , k � 1),
dist(i , k, k � 1) + dist(k, j , k � 1)
for i = 1 to n do
if (dist(i , i , n) < 0) then
Output that there is a negative length cycle in G
Running Time: O(n3), Space: O(n
3).
(UIUC) CS/ECE 374 22 April 6, 2021 22 / 22
Floyd-Warshall Algorithm
for All-Pairs Shortest Paths
for i = 1 to n do
for j = 1 to n do
dist(i , j , 0) = `(i , j) (* `(i , j) = 1 if (i , j) /2 E, 0 if i = j *)
for k = 1 to n do
for i = 1 to n do
for j = 1 to n do
dist(i , j , k) = min
(dist(i , j , k � 1),
dist(i , k, k � 1) + dist(k, j , k � 1)
for i = 1 to n do
if (dist(i , i , n) < 0) then
Output that there is a negative length cycle in G
Running Time:
O(n3), Space: O(n
3).
(UIUC) CS/ECE 374 22 April 6, 2021 22 / 22
Floyd-Warshall Algorithm
for All-Pairs Shortest Paths
for i = 1 to n do
for j = 1 to n do
dist(i , j , 0) = `(i , j) (* `(i , j) = 1 if (i , j) /2 E, 0 if i = j *)
for k = 1 to n do
for i = 1 to n do
for j = 1 to n do
dist(i , j , k) = min
(dist(i , j , k � 1),
dist(i , k, k � 1) + dist(k, j , k � 1)
for i = 1 to n do
if (dist(i , i , n) < 0) then
Output that there is a negative length cycle in G
Running Time: O(n3), Space: O(n
3).
(UIUC) CS/ECE 374 22 April 6, 2021 22 / 22
CS/ECE 374: Algorithms & Models ofComputation
Graph Modeling
Lecture
(UIUC) CS/ECE 374 1 April 6, 2021 1 / 13
Part I
An Application to make
(UIUC) CS/ECE 374 2 April 6, 2021 2 / 13
Make/Makefile
(A) I know what make/makefile is.
(B) I do NOT know what make/makefile is.
(UIUC) CS/ECE 374 3 April 6, 2021 3 / 13
make Utility [Feldman]
1 Unix utility for automatically building large software applications
2 A makefile specifies
1 Object files to be created,2 Source/object files to be used in creation, and3 How to create them
(UIUC) CS/ECE 374 4 April 6, 2021 4 / 13
An Example makefile
project: main.o utils.o command.occ -o project main.o utils.o command.o
main.o: main.c defs.hcc -c main.c
utils.o: utils.c defs.h command.hcc -c utils.c
command.o: command.c defs.h command.hcc -c command.c
(UIUC) CS/ECE 374 5 April 6, 2021 5 / 13
makefile as a Digraph
project
main.o
utils.o
command.o
main.c
utils.c
defs.h
command.h
command.c
(UIUC) CS/ECE 374 6 April 6, 2021 6 / 13
Computational Problems for make
1 Is the makefile reasonable?
2 If it is reasonable, in what order should the object files be
created?
3 If it is not reasonable, provide helpful debugging information.
4 If some file is modified, find the fewest compilations needed to
make application consistent.
(UIUC) CS/ECE 374 7 April 6, 2021 7 / 13
Algorithms for make
1 Is the makefile reasonable? Is G a DAG?
2 If it is reasonable, in what order should the object files be
created? Find a topological sort of a DAG.
3 If it is not reasonable, provide helpful debugging information.
Output a cycle. More generally, output all strong connected
components.
4 If some file is modified, find the fewest compilations needed to
make application consistent.
1 Find all vertices reachable (using DFS/BFS) from modifiedfiles in directed graph, and recompile them in proper order.Verify that one can find the files to recompile and the orderingin linear time.
(UIUC) CS/ECE 374 8 April 6, 2021 8 / 13
Part II
Application to Currency Trading
(UIUC) CS/ECE 374 9 April 6, 2021 9 / 13
Why Negative Lengths?
Several Applications
1 Shortest path problems useful in modeling many situations — in
some negative lengths are natural
2 Negative length cycle can be used to find arbitrage opportunities
in currency trading
3 Important sub-routine in algorithms for more general problem:
minimum-cost flow
(UIUC) CS/ECE 374 10 April 6, 2021 10 / 13
Negative cyclesApplication to Currency Trading
Currency Trading
Input: n currencies and for each ordered pair (a, b) the exchangerate for converting one unit of a into one unit of b.Questions:
1 Is there an arbitrage opportunity?
2 Given currencies s, t what is the best way to convert s to t(perhaps via other intermediate currencies)?
Concrete example:
1 1 Chinese Yuan = 0.1116 Euro
2 1 Euro = 1.3617 US dollar
3 1 US Dollar = 7.1 Chinese Yuan.
Thus, if exchanging 1 $ !Yuan ! Euro ! $, we get:
0.1116 ⇤ 1.3617 ⇤ 7.1 =
1.07896$.
(UIUC) CS/ECE 374 11 April 6, 2021 11 / 13
Reducing Currency Trading to Shortest Paths
Observation: If we convert currency i to j via intermediate
currencies k1, k2, . . . , kh then one unit of i yieldsexch(i , k1) ⇥ exch(k1, k2) . . . ⇥ exch(kh, j) units of j .
Create currency trading directed graph G = (V ,E):
1 For each currency i there is a node vi 2 V2 E = V ⇥ V : an edge for each pair of currencies
3 edge length `(vi , vj) = � log(exch(i , j)) can be negative
Exercise: Verify that
1 There is an arbitrage opportunity if and only if G has a negative
length cycle.
2 The best way to convert currency i to currency j is via a
shortest path in G from i to j . If d is the distance from i to jthen one unit of i can be converted into 2
�dunits of j .
(UIUC) CS/ECE 374 12 April 6, 2021 12 / 13
Reducing Currency Trading to Shortest Paths
Observation: If we convert currency i to j via intermediate
currencies k1, k2, . . . , kh then one unit of i yieldsexch(i , k1) ⇥ exch(k1, k2) . . . ⇥ exch(kh, j) units of j .
Create currency trading directed graph G = (V ,E):
1 For each currency i there is a node vi 2 V2 E = V ⇥ V : an edge for each pair of currencies
3 edge length `(vi , vj) =
� log(exch(i , j)) can be negative
Exercise: Verify that
1 There is an arbitrage opportunity if and only if G has a negative
length cycle.
2 The best way to convert currency i to currency j is via a
shortest path in G from i to j . If d is the distance from i to jthen one unit of i can be converted into 2
�dunits of j .
(UIUC) CS/ECE 374 12 April 6, 2021 12 / 13
Reducing Currency Trading to Shortest Paths
Observation: If we convert currency i to j via intermediate
currencies k1, k2, . . . , kh then one unit of i yieldsexch(i , k1) ⇥ exch(k1, k2) . . . ⇥ exch(kh, j) units of j .
Create currency trading directed graph G = (V ,E):
1 For each currency i there is a node vi 2 V2 E = V ⇥ V : an edge for each pair of currencies
3 edge length `(vi , vj) = � log(exch(i , j)) can be negative
Exercise: Verify that
1 There is an arbitrage opportunity if and only if G has a negative
length cycle.
2 The best way to convert currency i to currency j is via a
shortest path in G from i to j . If d is the distance from i to jthen one unit of i can be converted into 2
�dunits of j .
(UIUC) CS/ECE 374 12 April 6, 2021 12 / 13
Reducing Currency Trading to Shortest Paths
Observation: If we convert currency i to j via intermediate
currencies k1, k2, . . . , kh then one unit of i yieldsexch(i , k1) ⇥ exch(k1, k2) . . . ⇥ exch(kh, j) units of j .
Create currency trading directed graph G = (V ,E):
1 For each currency i there is a node vi 2 V2 E = V ⇥ V : an edge for each pair of currencies
3 edge length `(vi , vj) = � log(exch(i , j)) can be negative
Exercise: Verify that
1 There is an arbitrage opportunity if and only if G has a negative
length cycle.
2 The best way to convert currency i to currency j is via a
shortest path in G from i to j . If d is the distance from i to jthen one unit of i can be converted into 2
�dunits of j .
(UIUC) CS/ECE 374 12 April 6, 2021 12 / 13
Reducing Currency Trading to Shortest PathsMath recall - relevant information
1 log(↵1 ⇤ ↵2 ⇤ · · · ⇤ ↵k) = log↵1 + log↵2 + · · · + log↵k .
2 log x > 0 if and only if x > 1 .
(UIUC) CS/ECE 374 13 April 6, 2021 13 / 13