Shortest Path Algorithm Review and the k-shortest path Algorithm
Finding the shortest path - University of Bristol › ~csxam › teaching › dsa ›...
Transcript of Finding the shortest path - University of Bristol › ~csxam › teaching › dsa ›...
![Page 1: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/1.jpg)
COMS21103
Finding the shortest path
Ashley [email protected]
Department of Computer Science, University of BristolBristol, UK
28 October 2013
Ashley [email protected]
COMS21103: Finding the shortest path Slide 1/39
![Page 2: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/2.jpg)
Given a (weighted, directed) graph G and a pair of vertices s and t , wewould like to find a shortest path from s to t .
A fundamental task with many applications:
Ashley [email protected]
COMS21103: Finding the shortest path Slide 2/39
![Page 3: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/3.jpg)
Given a (weighted, directed) graph G and a pair of vertices s and t , wewould like to find a shortest path from s to t .
A fundamental task with many applications:
Ashley [email protected]
COMS21103: Finding the shortest path Slide 2/39
![Page 4: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/4.jpg)
Other applicationsI Internet routing (e.g. the OSPF routing algorithm)I VLSI routingI Traffic information systemsI Robot motion planningI Routing telephone callsI Avoiding nuclear contaminationI Destabilising currency marketsI . . .
Pics: Wikipedia, autoevolution.com, autoblog.com
Ashley [email protected]
COMS21103: Finding the shortest path Slide 3/39
![Page 5: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/5.jpg)
Shortest paths problemFormally, a shortest path from s to t in a graph G is a sequencev1, v2, . . . , vm such that the total weight of the edges s → v1, v1 → v2, . . . ,vm → t is minimal.
A GD
C F
EB
1
-2
1
3-1
2
2
2
1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 4/39
![Page 6: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/6.jpg)
Shortest paths problemFormally, a shortest path from s to t in a graph G is a sequencev1, v2, . . . , vm such that the total weight of the edges s → v1, v1 → v2, . . . ,vm → t is minimal.
A GD
C F
EB
1
-2
1
3-1
2
2
2
1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 4/39
![Page 7: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/7.jpg)
Shortest paths problemFormally, a shortest path from s to t in a graph G is a sequencev1, v2, . . . , vm such that the total weight of the edges s → v1, v1 → v2, . . . ,vm → t is minimal.
A GD
C F
EB
1
-2
1
3-1
2
2
2
1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 4/39
![Page 8: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/8.jpg)
Single-source shortest pathsI In fact, the algorithms we will discuss for this problem give us more:
given a source s, they output a shortest path from s to every othervertex.
I This is known as the single-source shortest path problem (SSSP).
1
-2
1
3-1
2
2
2
1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 5/39
![Page 9: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/9.jpg)
Single-source shortest pathsI In fact, the algorithms we will discuss for this problem give us more:
given a source s, they output a shortest path from s to every othervertex.
I This is known as the single-source shortest path problem (SSSP).
A D
C F
EB
G
1
-2
1
3-1
2
2
2
1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 5/39
![Page 10: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/10.jpg)
Single-source shortest pathsI In fact, the algorithms we will discuss for this problem give us more:
given a source s, they output a shortest path from s to every othervertex.
I This is known as the single-source shortest path problem (SSSP).
A D
C F
EB
G
1
-2
1
3-1
2
2
2
1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 5/39
![Page 11: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/11.jpg)
Single-source shortest pathsI In fact, the algorithms we will discuss for this problem give us more:
given a source s, they output a shortest path from s to every othervertex.
I This is known as the single-source shortest path problem (SSSP).
A D
C F
EB
G
1
-2
1
3-1
2
2
2
1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 5/39
![Page 12: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/12.jpg)
Negative-weight edgesI If some of the edges have negative weights, the idea of a shortest
path might not make sense.
I If there is a cycle in G which is reachable on a path from s to t , andthe sum of the weights of the edges in the cycle is negative, then wecan get from s to t with a path of arbitrarily low weight by repeatedlygoing round the cycle.
A GD
C F
EB1
-2
1
3-1
2
2
2
-1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 6/39
![Page 13: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/13.jpg)
Negative-weight edgesI If some of the edges have negative weights, the idea of a shortest
path might not make sense.
I If there is a cycle in G which is reachable on a path from s to t , andthe sum of the weights of the edges in the cycle is negative, then wecan get from s to t with a path of arbitrarily low weight by repeatedlygoing round the cycle.
A GD
C F
EB1
-2
1
3-1
2
2
2
-1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 6/39
![Page 14: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/14.jpg)
Negative-weight edgesI If some of the edges have negative weights, the idea of a shortest
path might not make sense.
I If there is a cycle in G which is reachable on a path from s to t , andthe sum of the weights of the edges in the cycle is negative, then wecan get from s to t with a path of arbitrarily low weight by repeatedlygoing round the cycle.
A GD
C F
EB1
-2
1
3-1
2
2
2
-1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 6/39
![Page 15: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/15.jpg)
Today’s lecture
I Today we will discuss an algorithm for the single-source shortestpaths problem called the Bellman-Ford algorithm.
I The algorithm can be used for graphs with negative weights and candetect negative-weight cycles.
I It also has applications to solving systems of difference constraintsand detecting arbitrage.
Remark: One algorithmic idea to solve the SSSP that doesn’t work is to tryevery possible path from s to t in turn.
I There can be exponentially many paths so such an algorithm cannotbe efficient.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 7/39
![Page 16: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/16.jpg)
Today’s lecture
I Today we will discuss an algorithm for the single-source shortestpaths problem called the Bellman-Ford algorithm.
I The algorithm can be used for graphs with negative weights and candetect negative-weight cycles.
I It also has applications to solving systems of difference constraintsand detecting arbitrage.
Remark: One algorithmic idea to solve the SSSP that doesn’t work is to tryevery possible path from s to t in turn.
I There can be exponentially many paths so such an algorithm cannotbe efficient.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 7/39
![Page 17: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/17.jpg)
Today’s lecture
I Today we will discuss an algorithm for the single-source shortestpaths problem called the Bellman-Ford algorithm.
I The algorithm can be used for graphs with negative weights and candetect negative-weight cycles.
I It also has applications to solving systems of difference constraintsand detecting arbitrage.
Remark: One algorithmic idea to solve the SSSP that doesn’t work is to tryevery possible path from s to t in turn.
I There can be exponentially many paths so such an algorithm cannotbe efficient.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 7/39
![Page 18: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/18.jpg)
Today’s lecture
I Today we will discuss an algorithm for the single-source shortestpaths problem called the Bellman-Ford algorithm.
I The algorithm can be used for graphs with negative weights and candetect negative-weight cycles.
I It also has applications to solving systems of difference constraintsand detecting arbitrage.
Remark: One algorithmic idea to solve the SSSP that doesn’t work is to tryevery possible path from s to t in turn.
I There can be exponentially many paths so such an algorithm cannotbe efficient.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 7/39
![Page 19: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/19.jpg)
Today’s lecture
I Today we will discuss an algorithm for the single-source shortestpaths problem called the Bellman-Ford algorithm.
I The algorithm can be used for graphs with negative weights and candetect negative-weight cycles.
I It also has applications to solving systems of difference constraintsand detecting arbitrage.
Remark: One algorithmic idea to solve the SSSP that doesn’t work is to tryevery possible path from s to t in turn.
I There can be exponentially many paths so such an algorithm cannotbe efficient.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 7/39
![Page 20: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/20.jpg)
NotationWe will use the following notation (essentially the same as CLRS):
I We always let G denote the graph in which we want to find a shortestpath. We use V for the number of vertices in G, and E for the numberof edges. s always denotes the source.
I We write u → v for an edge from u to v , and w(u, v) for the weight ofthis edge.
I We write δ(u, v) for the distance from u to v , i.e. the length (totalweight) of a shortest path from u to v .
I We write δ(u, v) =∞ when there is no path from u to v .(Mathematical note: in practice,∞ would be represented by a numberso large it could never occur in distance calculations...)
I For each vertex v , we will maintain a guess for its distance from s; callthis v .d .
Ashley [email protected]
COMS21103: Finding the shortest path Slide 8/39
![Page 21: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/21.jpg)
NotationWe will use the following notation (essentially the same as CLRS):
I We always let G denote the graph in which we want to find a shortestpath. We use V for the number of vertices in G, and E for the numberof edges. s always denotes the source.
I We write u → v for an edge from u to v , and w(u, v) for the weight ofthis edge.
I We write δ(u, v) for the distance from u to v , i.e. the length (totalweight) of a shortest path from u to v .
I We write δ(u, v) =∞ when there is no path from u to v .(Mathematical note: in practice,∞ would be represented by a numberso large it could never occur in distance calculations...)
I For each vertex v , we will maintain a guess for its distance from s; callthis v .d .
Ashley [email protected]
COMS21103: Finding the shortest path Slide 8/39
![Page 22: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/22.jpg)
NotationWe will use the following notation (essentially the same as CLRS):
I We always let G denote the graph in which we want to find a shortestpath. We use V for the number of vertices in G, and E for the numberof edges. s always denotes the source.
I We write u → v for an edge from u to v , and w(u, v) for the weight ofthis edge.
I We write δ(u, v) for the distance from u to v , i.e. the length (totalweight) of a shortest path from u to v .
I We write δ(u, v) =∞ when there is no path from u to v .(Mathematical note: in practice,∞ would be represented by a numberso large it could never occur in distance calculations...)
I For each vertex v , we will maintain a guess for its distance from s; callthis v .d .
Ashley [email protected]
COMS21103: Finding the shortest path Slide 8/39
![Page 23: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/23.jpg)
NotationWe will use the following notation (essentially the same as CLRS):
I We always let G denote the graph in which we want to find a shortestpath. We use V for the number of vertices in G, and E for the numberof edges. s always denotes the source.
I We write u → v for an edge from u to v , and w(u, v) for the weight ofthis edge.
I We write δ(u, v) for the distance from u to v , i.e. the length (totalweight) of a shortest path from u to v .
I We write δ(u, v) =∞ when there is no path from u to v .(Mathematical note: in practice,∞ would be represented by a numberso large it could never occur in distance calculations...)
I For each vertex v , we will maintain a guess for its distance from s; callthis v .d .
Ashley [email protected]
COMS21103: Finding the shortest path Slide 8/39
![Page 24: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/24.jpg)
NotationWe will use the following notation (essentially the same as CLRS):
I We always let G denote the graph in which we want to find a shortestpath. We use V for the number of vertices in G, and E for the numberof edges. s always denotes the source.
I We write u → v for an edge from u to v , and w(u, v) for the weight ofthis edge.
I We write δ(u, v) for the distance from u to v , i.e. the length (totalweight) of a shortest path from u to v .
I We write δ(u, v) =∞ when there is no path from u to v .(Mathematical note: in practice,∞ would be represented by a numberso large it could never occur in distance calculations...)
I For each vertex v , we will maintain a guess for its distance from s; callthis v .d .
Ashley [email protected]
COMS21103: Finding the shortest path Slide 8/39
![Page 25: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/25.jpg)
NotationWe will use the following notation (essentially the same as CLRS):
I We always let G denote the graph in which we want to find a shortestpath. We use V for the number of vertices in G, and E for the numberof edges. s always denotes the source.
I We write u → v for an edge from u to v , and w(u, v) for the weight ofthis edge.
I We write δ(u, v) for the distance from u to v , i.e. the length (totalweight) of a shortest path from u to v .
I We write δ(u, v) =∞ when there is no path from u to v .(Mathematical note: in practice,∞ would be represented by a numberso large it could never occur in distance calculations...)
I For each vertex v , we will maintain a guess for its distance from s; callthis v .d .
Ashley [email protected]
COMS21103: Finding the shortest path Slide 8/39
![Page 26: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/26.jpg)
Predecessors and shortest paths
I For each vertex v , we try to determine its predecessor v .π, which isthe previous vertex in some shortest path from s to v .
I Knowledge of v ’s predecessor suffices to compute the whole pathfrom s to v , by following the predecessors back to s and reversing thepath.
1
-2
1
3-1
2
2
2
1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 9/39
![Page 27: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/27.jpg)
Predecessors and shortest paths
I For each vertex v , we try to determine its predecessor v .π, which isthe previous vertex in some shortest path from s to v .
I Knowledge of v ’s predecessor suffices to compute the whole pathfrom s to v , by following the predecessors back to s and reversing thepath.
A D
C F
EB
G
1
-2
1
3-1
2
2
2
1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 9/39
![Page 28: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/28.jpg)
Predecessors and shortest pathsI For each vertex v , we try to determine its predecessor v .π, which is
the previous vertex in some shortest path from s to v .
I Knowledge of v ’s predecessor suffices to compute the whole pathfrom s to v , by following the predecessors back to s and reversing thepath.
A D
C
CB FD
ED
BA
G F
1
-2
1
3-1
2
2
2
1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 9/39
![Page 29: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/29.jpg)
Predecessors and shortest pathsI For each vertex v , we try to determine its predecessor v .π, which is
the previous vertex in some shortest path from s to v .
I Knowledge of v ’s predecessor suffices to compute the whole pathfrom s to v , by following the predecessors back to s and reversing thepath.
A D
C
CB FD
ED
BA
G F
1
-2
1
3-1
2
2
2
1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 9/39
![Page 30: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/30.jpg)
A general frameworkThe basic idea behind both shortest-path algorithms we will discuss is:
1. Initialise a guess v .d for the distance from the source s: s.d = 0, andv .d =∞ for all other vertices v .
2. Update our guesses by relaxing edges:
I If there is an edge u → v and our guess for the distance from s to v isgreater than our guess for the distance from s to u, plus w(u, v), thenwe can improve our guess by using this edge.
Relax(u, v )
1. if v .d > u.d + w(u, v)
2. v .d ← u.d + w(u, v)
3. v .π = u
Note that∞+ x =∞ for any real number x .
Ashley [email protected]
COMS21103: Finding the shortest path Slide 10/39
![Page 31: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/31.jpg)
A general frameworkThe basic idea behind both shortest-path algorithms we will discuss is:
1. Initialise a guess v .d for the distance from the source s: s.d = 0, andv .d =∞ for all other vertices v .
2. Update our guesses by relaxing edges:
I If there is an edge u → v and our guess for the distance from s to v isgreater than our guess for the distance from s to u, plus w(u, v), thenwe can improve our guess by using this edge.
Relax(u, v )
1. if v .d > u.d + w(u, v)
2. v .d ← u.d + w(u, v)
3. v .π = u
Note that∞+ x =∞ for any real number x .
Ashley [email protected]
COMS21103: Finding the shortest path Slide 10/39
![Page 32: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/32.jpg)
A general frameworkThe basic idea behind both shortest-path algorithms we will discuss is:
1. Initialise a guess v .d for the distance from the source s: s.d = 0, andv .d =∞ for all other vertices v .
2. Update our guesses by relaxing edges:
I If there is an edge u → v and our guess for the distance from s to v isgreater than our guess for the distance from s to u, plus w(u, v), thenwe can improve our guess by using this edge.
Relax(u, v )
1. if v .d > u.d + w(u, v)
2. v .d ← u.d + w(u, v)
3. v .π = u
Note that∞+ x =∞ for any real number x .
Ashley [email protected]
COMS21103: Finding the shortest path Slide 10/39
![Page 33: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/33.jpg)
A general frameworkThe basic idea behind both shortest-path algorithms we will discuss is:
1. Initialise a guess v .d for the distance from the source s: s.d = 0, andv .d =∞ for all other vertices v .
2. Update our guesses by relaxing edges:
I If there is an edge u → v and our guess for the distance from s to v isgreater than our guess for the distance from s to u, plus w(u, v), thenwe can improve our guess by using this edge.
Relax(u, v )
1. if v .d > u.d + w(u, v)
2. v .d ← u.d + w(u, v)
3. v .π = u
Note that∞+ x =∞ for any real number x .
Ashley [email protected]
COMS21103: Finding the shortest path Slide 10/39
![Page 34: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/34.jpg)
The Bellman-Ford algorithmThis algorithm simply consists of repeatedly relaxing every edge in G.
BellmanFord(G, s)
1. for each vertex v ∈ G: v .d ←∞, v .π ← nil2. s.d ← 0
3. for i = 1 to V − 14. for each edge u → v in G5. Relax(u, v )6. for each edge u → v in G7. if v .d > u.d + w(u, v)
8. error(“Negative-weight cycle detected”)
I Time complexity: Θ(V ) + Θ(VE) + Θ(E) = Θ(VE).
Ashley [email protected]
COMS21103: Finding the shortest path Slide 11/39
![Page 35: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/35.jpg)
The Bellman-Ford algorithmThis algorithm simply consists of repeatedly relaxing every edge in G.
BellmanFord(G, s)
1. for each vertex v ∈ G: v .d ←∞, v .π ← nil2. s.d ← 03. for i = 1 to V − 14. for each edge u → v in G5. Relax(u, v )
6. for each edge u → v in G7. if v .d > u.d + w(u, v)
8. error(“Negative-weight cycle detected”)
I Time complexity: Θ(V ) + Θ(VE) + Θ(E) = Θ(VE).
Ashley [email protected]
COMS21103: Finding the shortest path Slide 11/39
![Page 36: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/36.jpg)
The Bellman-Ford algorithmThis algorithm simply consists of repeatedly relaxing every edge in G.
BellmanFord(G, s)
1. for each vertex v ∈ G: v .d ←∞, v .π ← nil2. s.d ← 03. for i = 1 to V − 14. for each edge u → v in G5. Relax(u, v )6. for each edge u → v in G7. if v .d > u.d + w(u, v)
8. error(“Negative-weight cycle detected”)
I Time complexity: Θ(V ) + Θ(VE) + Θ(E) = Θ(VE).
Ashley [email protected]
COMS21103: Finding the shortest path Slide 11/39
![Page 37: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/37.jpg)
The Bellman-Ford algorithmThis algorithm simply consists of repeatedly relaxing every edge in G.
BellmanFord(G, s)
1. for each vertex v ∈ G: v .d ←∞, v .π ← nil2. s.d ← 03. for i = 1 to V − 14. for each edge u → v in G5. Relax(u, v )6. for each edge u → v in G7. if v .d > u.d + w(u, v)
8. error(“Negative-weight cycle detected”)
I Time complexity: Θ(V ) + Θ(VE) + Θ(E) = Θ(VE).
Ashley [email protected]
COMS21103: Finding the shortest path Slide 11/39
![Page 38: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/38.jpg)
Example 1: no negative-weight cycles
Imagine we want to find shortest paths from vertex A in the following graph:
A GDD
C F
EB
1
-2
1
3-1
2
2
2
1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 12/39
![Page 39: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/39.jpg)
Example 1: no negative-weight cyclesAt the start of the algorithm:
A
0, nil
G
∞, nil
D
∞, nil
D
∞, nil
C∞, nil F∞, nil
E
∞, nil
B
∞, nil
1
-2
1
3-1
2
2
2
1
1
4
I In the above diagram, the red text is the distance from the source A,(i.e. v .d), and the green text is the predecessor vertex (i.e. v .π).
Ashley [email protected]
COMS21103: Finding the shortest path Slide 13/39
![Page 40: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/40.jpg)
Example 1: no negative-weight cyclesThe first iteration of the for loop:
I Note that the edges are picked in arbitrary order.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 14/39
![Page 41: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/41.jpg)
Example 1: no negative-weight cyclesThe second iteration of the for loop:
I Note that the edges are picked in arbitrary order.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 15/39
![Page 42: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/42.jpg)
Example 1: no negative-weight cyclesThe 4 iterations of the for loop that follow do not update any distance orpredecessor values, so the final state is:
A
0, nil
B
1, A
C−1, B
D
−2, C
E
0, D
F0, D
G
1, F1
-2
1
3-1
2
2
2
1
1
4
I So the shortest path from A to G (for example) has weight 1.I To output a shortest path itself, we can trace back the predecessor
values from G.Ashley Montanaro
COMS21103: Finding the shortest path Slide 16/39
![Page 43: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/43.jpg)
Example 1: no negative-weight cyclesThe 4 iterations of the for loop that follow do not update any distance orpredecessor values, so the final state is:
A
0, nil
B
1, A
C−1, B
D
−2, C
E
0, D
F0, D
G
1, F1
-2
1
3-1
2
2
2
1
1
4
I So the shortest path from A to G (for example) has weight 1.I To output a shortest path itself, we can trace back the predecessor
values from G.Ashley Montanaro
COMS21103: Finding the shortest path Slide 16/39
![Page 44: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/44.jpg)
Example 2: negative-weight cycle
We now consider an input graph that has a negative-weight cycle.
A
B
C
1
-3
1
Ashley [email protected]
COMS21103: Finding the shortest path Slide 17/39
![Page 45: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/45.jpg)
Example 2: negative-weight cycle
At the start of the algorithm:
A
0, nil
B ∞, nil
C ∞, nil
1
-3
1
Ashley [email protected]
COMS21103: Finding the shortest path Slide 18/39
![Page 46: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/46.jpg)
Example 2: negative-weight cycleThe first iteration of the for loop:
I As before, the order in which we consider the edges is arbitrary (herewe use the order A→ B, C→ A, B→ C).
Ashley [email protected]
COMS21103: Finding the shortest path Slide 19/39
![Page 47: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/47.jpg)
Example 2: negative-weight cycleThe second iteration of the for loop:
I At the end of the algorithm, B.d > A.d + w(A,B).I So the algorithm terminates with “Negative-weight cycle detected”.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 20/39
![Page 48: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/48.jpg)
Example 2: negative-weight cycleThe second iteration of the for loop:
I At the end of the algorithm, B.d > A.d + w(A,B).I So the algorithm terminates with “Negative-weight cycle detected”.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 20/39
![Page 49: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/49.jpg)
Proof of correctness: Preliminaries
Claim (cycles)
If G does not contain any negative-weight cycles reachable from s, ashortest path from s to t cannot contain a cycle.
ProofIf a path p contains a cycle v0 → v1 → · · · → v0 such that the sum of theweights of the edges is non-negative, deleting this cycle from p cannotincrease p’s total weight.
A B C D
E F
1 -3 2-1
3-1
Ashley [email protected]
COMS21103: Finding the shortest path Slide 21/39
![Page 50: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/50.jpg)
Proof of correctness: Preliminaries
Claim (cycles)
If G does not contain any negative-weight cycles reachable from s, ashortest path from s to t cannot contain a cycle.
ProofIf a path p contains a cycle v0 → v1 → · · · → v0 such that the sum of theweights of the edges is non-negative, deleting this cycle from p cannotincrease p’s total weight.
A B C D
E F
1 -3 2-1
3-1
Ashley [email protected]
COMS21103: Finding the shortest path Slide 21/39
![Page 51: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/51.jpg)
Proof of correctness: Preliminaries
Claim (cycles)
If G does not contain any negative-weight cycles reachable from s, ashortest path from s to t cannot contain a cycle.
ProofIf a path p contains a cycle v0 → v1 → · · · → v0 such that the sum of theweights of the edges is non-negative, deleting this cycle from p cannotincrease p’s total weight.
A B C D
E F
1 -3 2-1
3-1
Ashley [email protected]
COMS21103: Finding the shortest path Slide 21/39
![Page 52: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/52.jpg)
Proof of correctness: PreliminariesClaim (triangle inequality)
For any vertices a, b, c, δ(a, c) ≤ δ(a,b) + δ(b, c).
ProofGiven a shortest path from a to b and a shortest path from b to c,combining these two paths gives a path from a to c with total weightδ(a,b) + δ(b, c).
Note that this holds even if some edge weights are negative.
A G
C F
EB1
-2
13
-12
2
2
1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 22/39
![Page 53: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/53.jpg)
Proof of correctness: PreliminariesClaim (triangle inequality)
For any vertices a, b, c, δ(a, c) ≤ δ(a,b) + δ(b, c).
ProofGiven a shortest path from a to b and a shortest path from b to c,combining these two paths gives a path from a to c with total weightδ(a,b) + δ(b, c).
Note that this holds even if some edge weights are negative.
A G
C F
EB1
-2
13
-12
2
2
1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 22/39
![Page 54: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/54.jpg)
Proof of correctness: PreliminariesClaim (triangle inequality)
For any vertices a, b, c, δ(a, c) ≤ δ(a,b) + δ(b, c).
ProofGiven a shortest path from a to b and a shortest path from b to c,combining these two paths gives a path from a to c with total weightδ(a,b) + δ(b, c).
Note that this holds even if some edge weights are negative.
A G
C F
EB1
-2
13
-12
2
2
1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 22/39
![Page 55: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/55.jpg)
Proof of correctness: PreliminariesClaim (triangle inequality)
For any vertices a, b, c, δ(a, c) ≤ δ(a,b) + δ(b, c).
ProofGiven a shortest path from a to b and a shortest path from b to c,combining these two paths gives a path from a to c with total weightδ(a,b) + δ(b, c).
Note that this holds even if some edge weights are negative.
A GD
C F
EB1
-2
1
3-1
2
2
2
1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 22/39
![Page 56: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/56.jpg)
Proof of correctness: PreliminariesClaim (triangle inequality)
For any vertices a, b, c, δ(a, c) ≤ δ(a,b) + δ(b, c).
ProofGiven a shortest path from a to b and a shortest path from b to c,combining these two paths gives a path from a to c with total weightδ(a,b) + δ(b, c).
Note that this holds even if some edge weights are negative.
A GD
C F
EB1
-2
1
3-1
2
2
2
1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 22/39
![Page 57: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/57.jpg)
Proof of correctness: PreliminariesClaim (triangle inequality)
For any vertices a, b, c, δ(a, c) ≤ δ(a,b) + δ(b, c).
ProofGiven a shortest path from a to b and a shortest path from b to c,combining these two paths gives a path from a to c with total weightδ(a,b) + δ(b, c).
Note that this holds even if some edge weights are negative.
A GD
C F
EB1
-2
1
3-1
2
2
2
1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 22/39
![Page 58: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/58.jpg)
Proof of correctness: PreliminariesClaim (triangle inequality)
For any vertices a, b, c, δ(a, c) ≤ δ(a,b) + δ(b, c).
ProofGiven a shortest path from a to b and a shortest path from b to c,combining these two paths gives a path from a to c with total weightδ(a,b) + δ(b, c).
Note that this holds even if some edge weights are negative.
A GD
C F
EB1
-2
1
3-1
2
2
2
1
1
4
Ashley [email protected]
COMS21103: Finding the shortest path Slide 22/39
![Page 59: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/59.jpg)
Proof of correctness: Preliminaries
Finally, an important property of relaxation, which can be proven byinduction and using the triangle inequality, is called path-relaxation:
Claim (path-relaxation)
Assume that:I p = s → v1 → · · · → vk → v is a shortest path from s to v ;
I s.d is initially set to 0 and u.d is initially set to∞ for all u 6= s;I the edges in p are relaxed in the order they appear in p (possibly with
other edges relaxed in between).Then, at the end of this process, v .d = δ(s, v).
Proof: exercise.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 23/39
![Page 60: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/60.jpg)
Proof of correctness: Preliminaries
Finally, an important property of relaxation, which can be proven byinduction and using the triangle inequality, is called path-relaxation:
Claim (path-relaxation)
Assume that:I p = s → v1 → · · · → vk → v is a shortest path from s to v ;I s.d is initially set to 0 and u.d is initially set to∞ for all u 6= s;
I the edges in p are relaxed in the order they appear in p (possibly withother edges relaxed in between).
Then, at the end of this process, v .d = δ(s, v).
Proof: exercise.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 23/39
![Page 61: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/61.jpg)
Proof of correctness: Preliminaries
Finally, an important property of relaxation, which can be proven byinduction and using the triangle inequality, is called path-relaxation:
Claim (path-relaxation)
Assume that:I p = s → v1 → · · · → vk → v is a shortest path from s to v ;I s.d is initially set to 0 and u.d is initially set to∞ for all u 6= s;I the edges in p are relaxed in the order they appear in p (possibly with
other edges relaxed in between).
Then, at the end of this process, v .d = δ(s, v).
Proof: exercise.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 23/39
![Page 62: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/62.jpg)
Proof of correctness: Preliminaries
Finally, an important property of relaxation, which can be proven byinduction and using the triangle inequality, is called path-relaxation:
Claim (path-relaxation)
Assume that:I p = s → v1 → · · · → vk → v is a shortest path from s to v ;I s.d is initially set to 0 and u.d is initially set to∞ for all u 6= s;I the edges in p are relaxed in the order they appear in p (possibly with
other edges relaxed in between).Then, at the end of this process, v .d = δ(s, v).
Proof: exercise.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 23/39
![Page 63: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/63.jpg)
Proof of correctness: Preliminaries
Finally, an important property of relaxation, which can be proven byinduction and using the triangle inequality, is called path-relaxation:
Claim (path-relaxation)
Assume that:I p = s → v1 → · · · → vk → v is a shortest path from s to v ;I s.d is initially set to 0 and u.d is initially set to∞ for all u 6= s;I the edges in p are relaxed in the order they appear in p (possibly with
other edges relaxed in between).Then, at the end of this process, v .d = δ(s, v).
Proof: exercise.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 23/39
![Page 64: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/64.jpg)
Proof of correctness
ClaimIf G does not contain a negative-weight cycle reachable from s, then at thecompletion of BellmanFord, v .d = δ(s, v) for all vertices v .
Proof
I Write v0 = s, vm = v . If v is reachable from s, there must exist ashortest path v0 → v1 → · · · → vm.
I A shortest path cannot contain a cycle, so m ≤ V − 1.I In the i ’th iteration of the for loop, the edge vi−1 → vi is relaxed
(among others).I By the path-relaxation property, after V − 1 iterations, v .d = δ(s, v).I So V − 1 iterations suffice to set v .d correctly for all v .
�
Ashley [email protected]
COMS21103: Finding the shortest path Slide 24/39
![Page 65: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/65.jpg)
Proof of correctness
ClaimIf G does not contain a negative-weight cycle reachable from s, then at thecompletion of BellmanFord, v .d = δ(s, v) for all vertices v .
Proof
I Write v0 = s, vm = v . If v is reachable from s, there must exist ashortest path v0 → v1 → · · · → vm.
I A shortest path cannot contain a cycle, so m ≤ V − 1.I In the i ’th iteration of the for loop, the edge vi−1 → vi is relaxed
(among others).I By the path-relaxation property, after V − 1 iterations, v .d = δ(s, v).I So V − 1 iterations suffice to set v .d correctly for all v .
�
Ashley [email protected]
COMS21103: Finding the shortest path Slide 24/39
![Page 66: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/66.jpg)
Proof of correctness
ClaimIf G does not contain a negative-weight cycle reachable from s, then at thecompletion of BellmanFord, v .d = δ(s, v) for all vertices v .
Proof
I Write v0 = s, vm = v . If v is reachable from s, there must exist ashortest path v0 → v1 → · · · → vm.
I A shortest path cannot contain a cycle, so m ≤ V − 1.
I In the i ’th iteration of the for loop, the edge vi−1 → vi is relaxed(among others).
I By the path-relaxation property, after V − 1 iterations, v .d = δ(s, v).I So V − 1 iterations suffice to set v .d correctly for all v .
�
Ashley [email protected]
COMS21103: Finding the shortest path Slide 24/39
![Page 67: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/67.jpg)
Proof of correctness
ClaimIf G does not contain a negative-weight cycle reachable from s, then at thecompletion of BellmanFord, v .d = δ(s, v) for all vertices v .
Proof
I Write v0 = s, vm = v . If v is reachable from s, there must exist ashortest path v0 → v1 → · · · → vm.
I A shortest path cannot contain a cycle, so m ≤ V − 1.I In the i ’th iteration of the for loop, the edge vi−1 → vi is relaxed
(among others).
I By the path-relaxation property, after V − 1 iterations, v .d = δ(s, v).I So V − 1 iterations suffice to set v .d correctly for all v .
�
Ashley [email protected]
COMS21103: Finding the shortest path Slide 24/39
![Page 68: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/68.jpg)
Proof of correctness
ClaimIf G does not contain a negative-weight cycle reachable from s, then at thecompletion of BellmanFord, v .d = δ(s, v) for all vertices v .
Proof
I Write v0 = s, vm = v . If v is reachable from s, there must exist ashortest path v0 → v1 → · · · → vm.
I A shortest path cannot contain a cycle, so m ≤ V − 1.I In the i ’th iteration of the for loop, the edge vi−1 → vi is relaxed
(among others).I By the path-relaxation property, after V − 1 iterations, v .d = δ(s, v).
I So V − 1 iterations suffice to set v .d correctly for all v .
�
Ashley [email protected]
COMS21103: Finding the shortest path Slide 24/39
![Page 69: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/69.jpg)
Proof of correctness
ClaimIf G does not contain a negative-weight cycle reachable from s, then at thecompletion of BellmanFord, v .d = δ(s, v) for all vertices v .
Proof
I Write v0 = s, vm = v . If v is reachable from s, there must exist ashortest path v0 → v1 → · · · → vm.
I A shortest path cannot contain a cycle, so m ≤ V − 1.I In the i ’th iteration of the for loop, the edge vi−1 → vi is relaxed
(among others).I By the path-relaxation property, after V − 1 iterations, v .d = δ(s, v).I So V − 1 iterations suffice to set v .d correctly for all v .
�
Ashley [email protected]
COMS21103: Finding the shortest path Slide 24/39
![Page 70: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/70.jpg)
Proof of correctness
ClaimIf G does not contain a negative-weight cycle reachable from s, thenBellmanFord does not exit with an error.
Proof
I By the triangle inequality, for all edges u → v ,δ(s, v) ≤ δ(s,u) + w(u, v).
I By the claim on the previous slide, v .d = δ(s, v) for all vertices v .I So, for all edges u → v , v .d ≤ u.d + w(u, v).I So the check in step (7) of the algorithm never fails.
�
Ashley [email protected]
COMS21103: Finding the shortest path Slide 25/39
![Page 71: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/71.jpg)
Proof of correctness
ClaimIf G does not contain a negative-weight cycle reachable from s, thenBellmanFord does not exit with an error.
Proof
I By the triangle inequality, for all edges u → v ,δ(s, v) ≤ δ(s,u) + w(u, v).
I By the claim on the previous slide, v .d = δ(s, v) for all vertices v .I So, for all edges u → v , v .d ≤ u.d + w(u, v).I So the check in step (7) of the algorithm never fails.
�
Ashley [email protected]
COMS21103: Finding the shortest path Slide 25/39
![Page 72: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/72.jpg)
Proof of correctness
ClaimIf G does not contain a negative-weight cycle reachable from s, thenBellmanFord does not exit with an error.
Proof
I By the triangle inequality, for all edges u → v ,δ(s, v) ≤ δ(s,u) + w(u, v).
I By the claim on the previous slide, v .d = δ(s, v) for all vertices v .
I So, for all edges u → v , v .d ≤ u.d + w(u, v).I So the check in step (7) of the algorithm never fails.
�
Ashley [email protected]
COMS21103: Finding the shortest path Slide 25/39
![Page 73: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/73.jpg)
Proof of correctness
ClaimIf G does not contain a negative-weight cycle reachable from s, thenBellmanFord does not exit with an error.
Proof
I By the triangle inequality, for all edges u → v ,δ(s, v) ≤ δ(s,u) + w(u, v).
I By the claim on the previous slide, v .d = δ(s, v) for all vertices v .I So, for all edges u → v , v .d ≤ u.d + w(u, v).
I So the check in step (7) of the algorithm never fails.
�
Ashley [email protected]
COMS21103: Finding the shortest path Slide 25/39
![Page 74: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/74.jpg)
Proof of correctness
ClaimIf G does not contain a negative-weight cycle reachable from s, thenBellmanFord does not exit with an error.
Proof
I By the triangle inequality, for all edges u → v ,δ(s, v) ≤ δ(s,u) + w(u, v).
I By the claim on the previous slide, v .d = δ(s, v) for all vertices v .I So, for all edges u → v , v .d ≤ u.d + w(u, v).I So the check in step (7) of the algorithm never fails.
�
Ashley [email protected]
COMS21103: Finding the shortest path Slide 25/39
![Page 75: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/75.jpg)
Proof of correctnessClaimIf G contains a negative-weight cycle reachable from s, then BellmanFordexits with an error.
Proof
I We will assume that G contains a negative-weight cycle reachablefrom s, and that BellmanFord does not exit with an error, and provethat this implies a contradiction.
I Let v0, . . . , vk be a negative-weight cycle, where vk = v0.I Then by definition
∑ki=1 w(vi−1, vi ) < 0.
I As BellmanFord does not exit with an error, for all 1 ≤ i ≤ k ,
vi .d ≤ vi−1.d + w(vi−1, vi ).
. . .
Ashley [email protected]
COMS21103: Finding the shortest path Slide 26/39
![Page 76: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/76.jpg)
Proof of correctnessClaimIf G contains a negative-weight cycle reachable from s, then BellmanFordexits with an error.
Proof
I We will assume that G contains a negative-weight cycle reachablefrom s, and that BellmanFord does not exit with an error, and provethat this implies a contradiction.
I Let v0, . . . , vk be a negative-weight cycle, where vk = v0.I Then by definition
∑ki=1 w(vi−1, vi ) < 0.
I As BellmanFord does not exit with an error, for all 1 ≤ i ≤ k ,
vi .d ≤ vi−1.d + w(vi−1, vi ).
. . .
Ashley [email protected]
COMS21103: Finding the shortest path Slide 26/39
![Page 77: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/77.jpg)
Proof of correctnessClaimIf G contains a negative-weight cycle reachable from s, then BellmanFordexits with an error.
Proof
I We will assume that G contains a negative-weight cycle reachablefrom s, and that BellmanFord does not exit with an error, and provethat this implies a contradiction.
I Let v0, . . . , vk be a negative-weight cycle, where vk = v0.
I Then by definition∑k
i=1 w(vi−1, vi ) < 0.I As BellmanFord does not exit with an error, for all 1 ≤ i ≤ k ,
vi .d ≤ vi−1.d + w(vi−1, vi ).
. . .
Ashley [email protected]
COMS21103: Finding the shortest path Slide 26/39
![Page 78: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/78.jpg)
Proof of correctnessClaimIf G contains a negative-weight cycle reachable from s, then BellmanFordexits with an error.
Proof
I We will assume that G contains a negative-weight cycle reachablefrom s, and that BellmanFord does not exit with an error, and provethat this implies a contradiction.
I Let v0, . . . , vk be a negative-weight cycle, where vk = v0.I Then by definition
∑ki=1 w(vi−1, vi ) < 0.
I As BellmanFord does not exit with an error, for all 1 ≤ i ≤ k ,
vi .d ≤ vi−1.d + w(vi−1, vi ).
. . .
Ashley [email protected]
COMS21103: Finding the shortest path Slide 26/39
![Page 79: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/79.jpg)
Proof of correctnessClaimIf G contains a negative-weight cycle reachable from s, then BellmanFordexits with an error.
Proof
I We will assume that G contains a negative-weight cycle reachablefrom s, and that BellmanFord does not exit with an error, and provethat this implies a contradiction.
I Let v0, . . . , vk be a negative-weight cycle, where vk = v0.I Then by definition
∑ki=1 w(vi−1, vi ) < 0.
I As BellmanFord does not exit with an error, for all 1 ≤ i ≤ k ,
vi .d ≤ vi−1.d + w(vi−1, vi ).
. . .
Ashley [email protected]
COMS21103: Finding the shortest path Slide 26/39
![Page 80: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/80.jpg)
Proof of correctnessClaimIf G contains a negative-weight cycle reachable from s, then BellmanFordexits with an error.
Proof
I Summing this inequality over i between 1 and k ,
k∑i=1
vi .d ≤k∑
i=1
vi−1.d + w(vi−1, vi ) =k∑
i=1
vi−1.d +k∑
i=1
w(vi−1, vi )
<
k∑i=1
vi−1.d =k−1∑i=0
vi .d .
I Subtracting∑k−1
i=1 vi .d from both sides, we get vk .d < v0.d .I But v0 = vk , so we have a contradiction. �
Ashley [email protected]
COMS21103: Finding the shortest path Slide 27/39
![Page 81: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/81.jpg)
Proof of correctnessClaimIf G contains a negative-weight cycle reachable from s, then BellmanFordexits with an error.
Proof
I Summing this inequality over i between 1 and k ,
k∑i=1
vi .d ≤k∑
i=1
vi−1.d + w(vi−1, vi ) =k∑
i=1
vi−1.d +k∑
i=1
w(vi−1, vi )
<
k∑i=1
vi−1.d =k−1∑i=0
vi .d .
I Subtracting∑k−1
i=1 vi .d from both sides, we get vk .d < v0.d .
I But v0 = vk , so we have a contradiction. �
Ashley [email protected]
COMS21103: Finding the shortest path Slide 27/39
![Page 82: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/82.jpg)
Proof of correctnessClaimIf G contains a negative-weight cycle reachable from s, then BellmanFordexits with an error.
Proof
I Summing this inequality over i between 1 and k ,
k∑i=1
vi .d ≤k∑
i=1
vi−1.d + w(vi−1, vi ) =k∑
i=1
vi−1.d +k∑
i=1
w(vi−1, vi )
<
k∑i=1
vi−1.d =k−1∑i=0
vi .d .
I Subtracting∑k−1
i=1 vi .d from both sides, we get vk .d < v0.d .I But v0 = vk , so we have a contradiction. �
Ashley [email protected]
COMS21103: Finding the shortest path Slide 27/39
![Page 83: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/83.jpg)
Application 1: difference constraints
I A system of difference constraints is a set of inequalities of the formxi − xj ≤ bij , where xi and xj are variables and bij is a real number.
I For example:
x1 − x2 ≤ 5, x2 − x3 ≤ −2, x1 − x4 ≤ 0.
I Given a system of m difference constraints in n variables, we wouldlike to find an assignment of real numbers to the variables such thatthe constraints are all satisfied, if such an assignment exists.
I For example, the above system is satisfied by x1 = 0, x2 = −1, x3 = 1,x4 = 7 (among other solutions).
I We will show that this problem can be solved using Bellman-Ford intime O(nm + n2).
Ashley [email protected]
COMS21103: Finding the shortest path Slide 28/39
![Page 84: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/84.jpg)
Application 1: difference constraints
I A system of difference constraints is a set of inequalities of the formxi − xj ≤ bij , where xi and xj are variables and bij is a real number.
I For example:
x1 − x2 ≤ 5, x2 − x3 ≤ −2, x1 − x4 ≤ 0.
I Given a system of m difference constraints in n variables, we wouldlike to find an assignment of real numbers to the variables such thatthe constraints are all satisfied, if such an assignment exists.
I For example, the above system is satisfied by x1 = 0, x2 = −1, x3 = 1,x4 = 7 (among other solutions).
I We will show that this problem can be solved using Bellman-Ford intime O(nm + n2).
Ashley [email protected]
COMS21103: Finding the shortest path Slide 28/39
![Page 85: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/85.jpg)
Application 1: difference constraints
I A system of difference constraints is a set of inequalities of the formxi − xj ≤ bij , where xi and xj are variables and bij is a real number.
I For example:
x1 − x2 ≤ 5, x2 − x3 ≤ −2, x1 − x4 ≤ 0.
I Given a system of m difference constraints in n variables, we wouldlike to find an assignment of real numbers to the variables such thatthe constraints are all satisfied, if such an assignment exists.
I For example, the above system is satisfied by x1 = 0, x2 = −1, x3 = 1,x4 = 7 (among other solutions).
I We will show that this problem can be solved using Bellman-Ford intime O(nm + n2).
Ashley [email protected]
COMS21103: Finding the shortest path Slide 28/39
![Page 86: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/86.jpg)
Application 1: difference constraints
I A system of difference constraints is a set of inequalities of the formxi − xj ≤ bij , where xi and xj are variables and bij is a real number.
I For example:
x1 − x2 ≤ 5, x2 − x3 ≤ −2, x1 − x4 ≤ 0.
I Given a system of m difference constraints in n variables, we wouldlike to find an assignment of real numbers to the variables such thatthe constraints are all satisfied, if such an assignment exists.
I For example, the above system is satisfied by x1 = 0, x2 = −1, x3 = 1,x4 = 7 (among other solutions).
I We will show that this problem can be solved using Bellman-Ford intime O(nm + n2).
Ashley [email protected]
COMS21103: Finding the shortest path Slide 28/39
![Page 87: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/87.jpg)
Application 1: difference constraints
I A system of difference constraints is a set of inequalities of the formxi − xj ≤ bij , where xi and xj are variables and bij is a real number.
I For example:
x1 − x2 ≤ 5, x2 − x3 ≤ −2, x1 − x4 ≤ 0.
I Given a system of m difference constraints in n variables, we wouldlike to find an assignment of real numbers to the variables such thatthe constraints are all satisfied, if such an assignment exists.
I For example, the above system is satisfied by x1 = 0, x2 = −1, x3 = 1,x4 = 7 (among other solutions).
I We will show that this problem can be solved using Bellman-Ford intime O(nm + n2).
Ashley [email protected]
COMS21103: Finding the shortest path Slide 28/39
![Page 88: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/88.jpg)
Graph representation of difference constraintsGiven m difference constraints in n variables, we create a graph on n + 1vertices v0, . . . , vn with m + n edges where:
I for each constraint xi − xj ≤ bij , we add an edge vj → vi with weight bij
I for all 1 ≤ i ≤ n there is an additional edge v0 → vi with weight 0.
For example:
x1 − x2 ≤ 5, x2 − x3 ≤ −2, x1 − x4 ≤ 0
corresponds to
v0
v1 v2
v3v4
0
5
-20 0
00
Ashley [email protected]
COMS21103: Finding the shortest path Slide 29/39
![Page 89: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/89.jpg)
Graph representation of difference constraintsGiven m difference constraints in n variables, we create a graph on n + 1vertices v0, . . . , vn with m + n edges where:
I for each constraint xi − xj ≤ bij , we add an edge vj → vi with weight bij
I for all 1 ≤ i ≤ n there is an additional edge v0 → vi with weight 0.
For example:
x1 − x2 ≤ 5, x2 − x3 ≤ −2, x1 − x4 ≤ 0
corresponds to
v0
v1 v2
v3v4
0
5
-20 0
00
Ashley [email protected]
COMS21103: Finding the shortest path Slide 29/39
![Page 90: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/90.jpg)
ClaimLet G be the graph corresponding to a system of difference constraints. IfG does not contain a negative-weight cycle, the assignment xi = δ(v0, vi ),for all 1 ≤ i ≤ n, is a valid solution to the system of constraints.
Proof
I We need to prove that
δ(v0, vi )− δ(v0, vj ) ≤ bij
for all i , j in the list of constraints.I This follows from the triangle inequality
δ(v0, vi ) ≤ δ(v0, vj ) + δ(vj , vi ) ≤ δ(v0, vj ) + w(vj , vi ) = δ(v0, vj ) + bij
and rearranging. �
Ashley [email protected]
COMS21103: Finding the shortest path Slide 30/39
![Page 91: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/91.jpg)
ClaimLet G be the graph corresponding to a system of difference constraints. IfG does not contain a negative-weight cycle, the assignment xi = δ(v0, vi ),for all 1 ≤ i ≤ n, is a valid solution to the system of constraints.
Proof
I We need to prove that
δ(v0, vi )− δ(v0, vj ) ≤ bij
for all i , j in the list of constraints.
I This follows from the triangle inequality
δ(v0, vi ) ≤ δ(v0, vj ) + δ(vj , vi ) ≤ δ(v0, vj ) + w(vj , vi ) = δ(v0, vj ) + bij
and rearranging. �
Ashley [email protected]
COMS21103: Finding the shortest path Slide 30/39
![Page 92: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/92.jpg)
ClaimLet G be the graph corresponding to a system of difference constraints. IfG does not contain a negative-weight cycle, the assignment xi = δ(v0, vi ),for all 1 ≤ i ≤ n, is a valid solution to the system of constraints.
Proof
I We need to prove that
δ(v0, vi )− δ(v0, vj ) ≤ bij
for all i , j in the list of constraints.I This follows from the triangle inequality
δ(v0, vi ) ≤ δ(v0, vj ) + δ(vj , vi ) ≤ δ(v0, vj ) + w(vj , vi ) = δ(v0, vj ) + bij
and rearranging. �
Ashley [email protected]
COMS21103: Finding the shortest path Slide 30/39
![Page 93: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/93.jpg)
ClaimLet G be the graph corresponding to a system of difference constraints. IfG contains a negative-weight cycle, there is no valid solution to the systemof constraints.
Proof (sketch)
I We prove the converse: if the system has a valid solution, there is nonegative-weight cycle.
I Let c = v1, . . . , vk , v1 be an arbitrary cycle on vertices v1, . . . , vk(without loss of generality). This corresponds to the inequalities
x2 − x1 ≤ b12, x3 − x2 ≤ b23, . . . , x1 − xk ≤ bk1.
I If there is a valid solution xi , then all the inequalities are satisfied.I Summing the inequalities we get 0 for the left-hand side, and the
weight of c for the right-hand side.I So c has weight at least 0, and is not a negative-weight cycle. �
Ashley [email protected]
COMS21103: Finding the shortest path Slide 31/39
![Page 94: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/94.jpg)
ClaimLet G be the graph corresponding to a system of difference constraints. IfG contains a negative-weight cycle, there is no valid solution to the systemof constraints.
Proof (sketch)
I We prove the converse: if the system has a valid solution, there is nonegative-weight cycle.
I Let c = v1, . . . , vk , v1 be an arbitrary cycle on vertices v1, . . . , vk(without loss of generality). This corresponds to the inequalities
x2 − x1 ≤ b12, x3 − x2 ≤ b23, . . . , x1 − xk ≤ bk1.
I If there is a valid solution xi , then all the inequalities are satisfied.I Summing the inequalities we get 0 for the left-hand side, and the
weight of c for the right-hand side.I So c has weight at least 0, and is not a negative-weight cycle. �
Ashley [email protected]
COMS21103: Finding the shortest path Slide 31/39
![Page 95: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/95.jpg)
ClaimLet G be the graph corresponding to a system of difference constraints. IfG contains a negative-weight cycle, there is no valid solution to the systemof constraints.
Proof (sketch)
I We prove the converse: if the system has a valid solution, there is nonegative-weight cycle.
I Let c = v1, . . . , vk , v1 be an arbitrary cycle on vertices v1, . . . , vk(without loss of generality). This corresponds to the inequalities
x2 − x1 ≤ b12, x3 − x2 ≤ b23, . . . , x1 − xk ≤ bk1.
I If there is a valid solution xi , then all the inequalities are satisfied.I Summing the inequalities we get 0 for the left-hand side, and the
weight of c for the right-hand side.I So c has weight at least 0, and is not a negative-weight cycle. �
Ashley [email protected]
COMS21103: Finding the shortest path Slide 31/39
![Page 96: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/96.jpg)
ClaimLet G be the graph corresponding to a system of difference constraints. IfG contains a negative-weight cycle, there is no valid solution to the systemof constraints.
Proof (sketch)
I We prove the converse: if the system has a valid solution, there is nonegative-weight cycle.
I Let c = v1, . . . , vk , v1 be an arbitrary cycle on vertices v1, . . . , vk(without loss of generality). This corresponds to the inequalities
x2 − x1 ≤ b12, x3 − x2 ≤ b23, . . . , x1 − xk ≤ bk1.
I If there is a valid solution xi , then all the inequalities are satisfied.
I Summing the inequalities we get 0 for the left-hand side, and theweight of c for the right-hand side.
I So c has weight at least 0, and is not a negative-weight cycle. �
Ashley [email protected]
COMS21103: Finding the shortest path Slide 31/39
![Page 97: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/97.jpg)
ClaimLet G be the graph corresponding to a system of difference constraints. IfG contains a negative-weight cycle, there is no valid solution to the systemof constraints.
Proof (sketch)
I We prove the converse: if the system has a valid solution, there is nonegative-weight cycle.
I Let c = v1, . . . , vk , v1 be an arbitrary cycle on vertices v1, . . . , vk(without loss of generality). This corresponds to the inequalities
x2 − x1 ≤ b12, x3 − x2 ≤ b23, . . . , x1 − xk ≤ bk1.
I If there is a valid solution xi , then all the inequalities are satisfied.I Summing the inequalities we get 0 for the left-hand side, and the
weight of c for the right-hand side.
I So c has weight at least 0, and is not a negative-weight cycle. �
Ashley [email protected]
COMS21103: Finding the shortest path Slide 31/39
![Page 98: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/98.jpg)
ClaimLet G be the graph corresponding to a system of difference constraints. IfG contains a negative-weight cycle, there is no valid solution to the systemof constraints.
Proof (sketch)
I We prove the converse: if the system has a valid solution, there is nonegative-weight cycle.
I Let c = v1, . . . , vk , v1 be an arbitrary cycle on vertices v1, . . . , vk(without loss of generality). This corresponds to the inequalities
x2 − x1 ≤ b12, x3 − x2 ≤ b23, . . . , x1 − xk ≤ bk1.
I If there is a valid solution xi , then all the inequalities are satisfied.I Summing the inequalities we get 0 for the left-hand side, and the
weight of c for the right-hand side.I So c has weight at least 0, and is not a negative-weight cycle. �
Ashley [email protected]
COMS21103: Finding the shortest path Slide 31/39
![Page 99: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/99.jpg)
ExampleThe set of inequalities
x1 − x2 ≤ 5, x2 − x3 ≤ −2, x1 − x4 ≤ 0
corresponds to the graph
v0
v1 v2
v3v4
0
5
-20 0
00
with shortest paths
δ(v0, v1) = 0, δ(v0, v2) = −2, δ(v0, v3) = 0, δ(v0, v4) = 0.
So
x1 = 0, x2 = −2, x3 = 0, x4 = 0
is a solution to the constraints.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 32/39
![Page 100: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/100.jpg)
ExampleThe set of inequalities
x1 − x2 ≤ 5, x2 − x3 ≤ −2, x1 − x4 ≤ 0
corresponds to the graph
v0
v1 v2
v3v4
0
5
-20 0
00
with shortest paths
δ(v0, v1) = 0, δ(v0, v2) = −2, δ(v0, v3) = 0, δ(v0, v4) = 0.
So
x1 = 0, x2 = −2, x3 = 0, x4 = 0
is a solution to the constraints.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 32/39
![Page 101: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/101.jpg)
Solving difference constraints
I We can run Bellman-Ford with v0 as the source.
I If there is a negative-weight cycle, the algorithm detects it (and weoutput “no solution”); otherwise, we output xi = δ(v0, vi ) as thesolution.
I For a solution to a system of m difference constraints on n variables,the graph produced has n + 1 vertices and m + n edges.
I The running time of Bellman-Ford is thus O(VE) = O(mn + n2).
I This can be improved to O(mn) time (CLRS exercise 24.4-5).
Ashley [email protected]
COMS21103: Finding the shortest path Slide 33/39
![Page 102: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/102.jpg)
Solving difference constraints
I We can run Bellman-Ford with v0 as the source.
I If there is a negative-weight cycle, the algorithm detects it (and weoutput “no solution”); otherwise, we output xi = δ(v0, vi ) as thesolution.
I For a solution to a system of m difference constraints on n variables,the graph produced has n + 1 vertices and m + n edges.
I The running time of Bellman-Ford is thus O(VE) = O(mn + n2).
I This can be improved to O(mn) time (CLRS exercise 24.4-5).
Ashley [email protected]
COMS21103: Finding the shortest path Slide 33/39
![Page 103: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/103.jpg)
Solving difference constraints
I We can run Bellman-Ford with v0 as the source.
I If there is a negative-weight cycle, the algorithm detects it (and weoutput “no solution”); otherwise, we output xi = δ(v0, vi ) as thesolution.
I For a solution to a system of m difference constraints on n variables,the graph produced has n + 1 vertices and m + n edges.
I The running time of Bellman-Ford is thus O(VE) = O(mn + n2).
I This can be improved to O(mn) time (CLRS exercise 24.4-5).
Ashley [email protected]
COMS21103: Finding the shortest path Slide 33/39
![Page 104: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/104.jpg)
Solving difference constraints
I We can run Bellman-Ford with v0 as the source.
I If there is a negative-weight cycle, the algorithm detects it (and weoutput “no solution”); otherwise, we output xi = δ(v0, vi ) as thesolution.
I For a solution to a system of m difference constraints on n variables,the graph produced has n + 1 vertices and m + n edges.
I The running time of Bellman-Ford is thus O(VE) = O(mn + n2).
I This can be improved to O(mn) time (CLRS exercise 24.4-5).
Ashley [email protected]
COMS21103: Finding the shortest path Slide 33/39
![Page 105: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/105.jpg)
Application 2: Currency exchangeImagine we have n different currencies, and a table T whose (i , j)’th entryTij represents the exchange rate we get when converting currency i tocurrency j . For example:
£ $ e£ 1 1.61 1.18$ 0.62 1 0.74e 0.85 1.35 1
I If we convert currency i → j → k , the rate we get is the product of theindividual rates.
I If we convert i → j → · · · → i , and the product of the rates is greaterthan 1, we have made money by exploiting the exchange rates! This iscalled arbitrage.
I We can use Bellman-Ford to determine whether arbitrage is possible.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 34/39
![Page 106: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/106.jpg)
Application 2: Currency exchangeImagine we have n different currencies, and a table T whose (i , j)’th entryTij represents the exchange rate we get when converting currency i tocurrency j . For example:
£ $ e£ 1 1.61 1.18$ 0.62 1 0.74e 0.85 1.35 1
I If we convert currency i → j → k , the rate we get is the product of theindividual rates.
I If we convert i → j → · · · → i , and the product of the rates is greaterthan 1, we have made money by exploiting the exchange rates! This iscalled arbitrage.
I We can use Bellman-Ford to determine whether arbitrage is possible.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 34/39
![Page 107: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/107.jpg)
Application 2: Currency exchangeImagine we have n different currencies, and a table T whose (i , j)’th entryTij represents the exchange rate we get when converting currency i tocurrency j . For example:
£ $ e£ 1 1.61 1.18$ 0.62 1 0.74e 0.85 1.35 1
I If we convert currency i → j → k , the rate we get is the product of theindividual rates.
I If we convert i → j → · · · → i , and the product of the rates is greaterthan 1, we have made money by exploiting the exchange rates! This iscalled arbitrage.
I We can use Bellman-Ford to determine whether arbitrage is possible.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 34/39
![Page 108: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/108.jpg)
Application 2: Currency exchangeImagine we have n different currencies, and a table T whose (i , j)’th entryTij represents the exchange rate we get when converting currency i tocurrency j . For example:
£ $ e£ 1 1.61 1.18$ 0.62 1 0.74e 0.85 1.35 1
I If we convert currency i → j → k , the rate we get is the product of theindividual rates.
I If we convert i → j → · · · → i , and the product of the rates is greaterthan 1, we have made money by exploiting the exchange rates! This iscalled arbitrage.
I We can use Bellman-Ford to determine whether arbitrage is possible.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 34/39
![Page 109: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/109.jpg)
Application: Currency exchangeWe produce a weighted graph G from the currency table, where the weightof edge i → j is − log2 Tij . For example:
£
$
e
-0.69
0.69
-0.23
0.230.43-0.43
I Then the weight of a cycle c0 → c1 → · · · → ck (with ck = c0) is
−k∑
j=1
log2 Tcj cj−1 = − log2
k∏j=1
Tcj cj−1 .
I This will be negative if and only if∏
j Tcj cj−1 > 1, i.e. the sequence oftransactions corresponds to an arbitrage opportunity.
I So G has a negative-weight cycle if and only if arbitrage is possible.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 35/39
![Page 110: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/110.jpg)
Application: Currency exchangeWe produce a weighted graph G from the currency table, where the weightof edge i → j is − log2 Tij . For example:
£
$
e
-0.69
0.69
-0.23
0.230.43-0.43
I Then the weight of a cycle c0 → c1 → · · · → ck (with ck = c0) is
−k∑
j=1
log2 Tcj cj−1 = − log2
k∏j=1
Tcj cj−1 .
I This will be negative if and only if∏
j Tcj cj−1 > 1, i.e. the sequence oftransactions corresponds to an arbitrage opportunity.
I So G has a negative-weight cycle if and only if arbitrage is possible.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 35/39
![Page 111: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/111.jpg)
Application: Currency exchangeWe produce a weighted graph G from the currency table, where the weightof edge i → j is − log2 Tij . For example:
£
$
e
-0.69
0.69
-0.23
0.230.43-0.43
I Then the weight of a cycle c0 → c1 → · · · → ck (with ck = c0) is
−k∑
j=1
log2 Tcj cj−1 = − log2
k∏j=1
Tcj cj−1 .
I This will be negative if and only if∏
j Tcj cj−1 > 1, i.e. the sequence oftransactions corresponds to an arbitrage opportunity.
I So G has a negative-weight cycle if and only if arbitrage is possible.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 35/39
![Page 112: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/112.jpg)
Application: Currency exchangeWe produce a weighted graph G from the currency table, where the weightof edge i → j is − log2 Tij . For example:
£
$
e
-0.69
0.69
-0.23
0.230.43-0.43
I Then the weight of a cycle c0 → c1 → · · · → ck (with ck = c0) is
−k∑
j=1
log2 Tcj cj−1 = − log2
k∏j=1
Tcj cj−1 .
I This will be negative if and only if∏
j Tcj cj−1 > 1, i.e. the sequence oftransactions corresponds to an arbitrage opportunity.
I So G has a negative-weight cycle if and only if arbitrage is possible.Ashley Montanaro
COMS21103: Finding the shortest path Slide 35/39
![Page 113: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/113.jpg)
Summary
I The Bellman-Ford algorithm solves the single-source shortest pathsproblem in time O(VE).
I It works if the input graph has negative-weight edges, and can detectnegative-weight cycles.
I Although the proof of correctness is a bit technical, the algorithm iseasy to implement and doesn’t use any complicated data structures.
I It can be used to solve a system of difference constraints and todetermine whether arbitrage is possible.
Ashley [email protected]
COMS21103: Finding the shortest path Slide 36/39
![Page 114: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/114.jpg)
Further Reading
I Introduction to AlgorithmsT.H. Cormen, C.E. Leiserson, R.L. Rivest and C. Stein.MIT Press/McGraw-Hill, ISBN: 0-262-03293-7.
I Chapter 24 – Single-Source Shortest Paths
I AlgorithmsS. Dasgupta, C.H. Papadimitriou and U.V. Vaziranihttp://www.cse.ucsd.edu/users/dasgupta/mcgrawhill/
I Chapter 4, Section 4.6 – Shortest paths in the presence of negativeedges
I Algorithms lecture notes, University of IllinoisJeff Ericksonhttp://www.cs.uiuc.edu/~jeffe/teaching/algorithms/
I Lecture 19 – Single-source shortest paths
Ashley [email protected]
COMS21103: Finding the shortest path Slide 37/39
![Page 115: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/115.jpg)
Biographical notes
Richard E. Bellman (1920–1984)
I American mathematician who worked atPrinceton, Stanford, the RAND Corporationand the University of Southern California.
I Author of at least 621 papers and 41 books,including 100 papers after the removal of abrain tumour left him severely disabled.
I Winner of the IEEE Medal of Honor in 1979 forhis invention of dynamic programming.
Pic: IEEE Global History Network
Ashley [email protected]
COMS21103: Finding the shortest path Slide 38/39
![Page 116: Finding the shortest path - University of Bristol › ~csxam › teaching › dsa › ...Negative-weight edges I If some of the edges havenegative weights, the idea of a shortest path](https://reader034.fdocuments.in/reader034/viewer/2022042409/5f24e02dd40c3c1c481cee69/html5/thumbnails/116.jpg)
Biographical notes
Lester Ford, Jr. (1927–)
I Another American mathematicianwhose other contributions include theFord-Fulkerson algorithm for maximumflow problems.
I His father was also a mathematicianand, at one point, President of theMathematical Association of America.
Pic: tangrammit.com
Ashley [email protected]
COMS21103: Finding the shortest path Slide 39/39