23 ShortestPaths
23 ShortestPaths
These slides has been extracted, modified and updated from original slides of :
• Data Structures and Algorithms in Java, 5th edition. John Wiley& Sons, 2010. ISBN 978-0-470-38326-1.
• Dr. Hanna’s slides (http://aimanhanna.com/concordia/comp352/index.htm)
PVD
ORD
SFO
LGA
HNL
LAX DFW
MIA
Shortest Paths 2
Weighted Graphs
❑ Graphs where edges have different weight, are called weighted
graphs.
❑ There are cases however where the distance between v and u may not be
defined even if there is a path form v to u in G. This is the case if the
graph has a negative-weight cycle (a cycle whose total weight is
negative).
❑ For instance, assume that the edges in the airport graphs have weights
that define the cost to travel between these cities. Further, assume that
someone has offered to pay travelers an amount that is larger than the
actual cost to travel between PVD and ORD.
Shortest Paths 6
Negative-Weight Cycles
❑ Consequently, the cost from PVD to ORD (that edge e = (PVD, ORD)) would carry a negative
value, which makes e = (PVD, ORD) a negative-weight edge.
❑ Further, assume that someone else has then offered travelers to pay an amount that is larger
than the actual cost to travel between ORD and PVD.
❑ Consequently, there is now a negative-weight cycle (cycle with a total weight that is
negative) between PVD and ORD. These cycles are very undesirable and must be avoided when
edge weights are used to represent distances.
❑ Now, the distances of that graph can no longer be defined since someone can build a path from
any city A to another B going through PVD and making as many cycles as desired between PVD
and ORD before continuing to B.
PVD
ORD
SFO
LGA
HNL
LAX DFW
Shortest Paths MIA 7
Dijkstra’s Algorithm
❑ Dijkstra’s algorithm computes the distances (the shortest paths)
of all the vertices from a given start vertex s
❑ Assumptions:
◼ the graph is connected
◼ the edges are undirected
◼ the edge weights are nonnegative
Shortest Paths 8
Dijkstra’s Algorithm
❑ Edge Relaxation:
◼ Let us define a label D[u] for each vertex u in the graph, where D[u]
stores an approximation of the shortest path distance from a starting
vertex v to u.
◼ In other words, D[u] will always store the length of the best path to u
that could have been found so far.
◼ Initially, D[v] = 0, and D[u] = +∞ for each other vertex u in the graph.
Shortest Paths 11
Dijkstra’s Algorithm (continues…)
C S
Different routes to u.
Is there a cheaper route through X to other vertices in S? If so, update distances to these vertices. 12
Shortest Paths
Dijkstra’s Algorithm
❑ Example: What are the shortest paths from A to other vertices?*
1 {A} {B,C} C 0 2 1 ∞ ∞ ∞ A A A - - -
2 {A, C} {B,D,E,F} B 0 2 1 4 7 8 A A A C C C
3 {A,B,C} {D,E,F} D 0 2 1 4 6 8 A A A C B C
4 {A,B,C,D} {E,F} E 0 2 1 4 6 6 A A A C B D
5 {A,B,C,D,E} {F} F 0 2 1 4 6 6 A A A C B D
Note: See comments attached to this slide for detailed information on how the operations progressed. 14
Edge Relaxation
❑ Consider an edge e = (u,z)
such that d(u) = 50
◼ u is the vertex most recently d(z) = 75
u e
added to the cloud s z
◼ z is not in the cloud d(v) = 40
v
❑ The relaxation of edge e
updates distance d(z) as
follows:
d(u) = 50
d(z) min{d(z),d(u) + weight(e)} d(z) = 60
u e
s z
d(v) = 40
v
Shortest Paths 15
Example 2 – Visual Approach
0 0
8 A 4 8 A 4
2 2
8 7 2 1 4 8 7 2 1 3
B C D B C D
3 9 5 3 9 8
2 5 2 5
E F E F
0 0
8 A 4 8 A 4
2 2
8 7 2 1 3 7 7 2 1 3
B C D B C D
5 3 9 11 5 3 9 8
2 5 2 5
E F E F
Shortest Paths 16
Example 2 (cont.)
0
8 A 4
2
7 7 2 1 3
B C D
5 3 9 8
2 5
E F
0
8 A 4
2
7 7 2 1 3
B C D
5 3 9 8
2 5
E F
Shortest Paths 17
Dijkstra’s Algorithm
❑ A heap-based adaptable Algorithm DijkstraDistances(G, s)
Q new heap-based priority queue
priority queue with for all v G.vertices()
location-aware entries if v = s
stores the vertices setDistance(v, 0)
outside the cloud else
◼ Key: distance setDistance(v, )
l Q.insert(getDistance(v), v)
◼ Value: vertex setEntry(v, l)
◼ Recall that method while Q.isEmpty()
replaceKey(l,k) changes l Q.removeMin()
the key of entry l u l.getValue()
for all e G.incidentEdges(u) { relax e }
❑ We store two labels
z G.opposite(u,e)
with each vertex: r getDistance(u) + weight(e)
◼ Distance if r getDistance(z)
◼ Entry in priority queue setDistance(z,r)
Q.replaceKey(getEntry(z), r)
Shortest Paths 18
Analysis of Dijkstra’s Algorithm
❑ Initial operations (the 1st for all loop)
◼ Method setDistance is called once for each vertex, resulting in O(n)
◼ Method setEntry is called once for each vertex, resulting in O(n)
◼ Each vertex is then inserted into the Heap-based Priority Queue,
where each insertion costs O(log n), resulting in a total of O(n log n)
Shortest Paths 19
Analysis of Dijkstra’s Algorithm
❑ Following all incident edges of removed vertex (the inner for
loop)
◼ Since all vertices are removed one by one, and since each will cost
O(deg(v)) of that vertex v, a total of O(v deg(v)) for all vertices is
eventually incurred.
◼ What happens however, each time you follow one of the incident
edges of u ?
Shortest Paths 20
Analysis of Dijkstra’s Algorithm
❑ What happens however, each time you follow one of the incident
edges of u ?
▪ BUT ……
▪ The method replaceKey, which affects the values of the key in the
heap, may result in O(log n) operations since the heap may need to
be corrected each time
Shortest Paths 23
Shortest Paths Tree
❑ Using the template Algorithm DijkstraShortestPathsTree(G, s)
method pattern, we
…
can extend Dijkstra’s
algorithm to return a for all v G.vertices()
tree of shortest paths …
from the start vertex setParent(v, )
to all other vertices …
❑ We store with each for all e G.incidentEdges(u)
vertex a third label: { relax edge e }
◼ parent edge in the z G.opposite(u,e)
shortest path tree r getDistance(u) + weight(e)
if r getDistance(z)
❑ In the edge relaxation
setDistance(z, r)
step, we update the setParent(z,e)
parent label Q.replaceKey(getEntry(z),r)
Shortest Paths 24
Why It Doesn’t Work for
Negative-Weight Edges
Dijkstra’s algorithm is based on the greedy
method. It adds vertices by increasing distance.
0
8 A 4
◼ If a node with a negative 6
incident edge were to be added 7 7 5 1 4
late to the cloud, it could mess B C D
up distances for vertices already 0 -8
5 9
in the cloud. 2 5
E F
Shortest Paths 26
Bellman-Ford Example
Nodes are labeled with their d(v) values
8 0 4 8 0 4
-2 -2
7 1 8 7 -2 1 4
3 9 3 9
-2 5 -2 5
8 0 4 8 0 4
-2 -2
5 8 7 1 -1 7 1
-2 4 5 -2 -1
1 3 9 3 9
-2 6 9 5 -2 4 5
1 9
Shortest Paths 27
DAG-based Algorithm
(not in book)
Algorithm DagDistances(G, s)
for all v G.vertices()
❑ Works even with if v = s
negative-weight edges setDistance(v, 0)
❑ Uses topological order else
❑ Doesn’t use any fancy setDistance(v, )
data structures { Perform a topological sort of the vertices }
for u 1 to n do {in topological order}
❑ Is much faster than for each e G.outEdges(u)
Dijkstra’s algorithm { relax edge e }
❑ Running time: O(n+m). z G.opposite(u,e)
r getDistance(u) + weight(e)
if r getDistance(z)
setDistance(z,r)
Shortest Paths 28
DAG Example
Nodes are labeled with their d(v) values
1 1
8 0 4 8 0 4
-2 -2
3 7 2 1 4 3 8 7 2 -2 1 4 4
3 9 3 9
-5 5 -5 5
6 5 6 5
1 1
8 0 4 8 0 4
-2 -2
3 5 7 2 1 4
-1 3 7 2 1 4
8 -2 4 5 -2 -1
3 9 3 9
-5 1 7 5 -5 0 4 5
1 7
6 5 6 5
Shortest Paths (two steps) 29