0% found this document useful (0 votes)
2 views29 pages

23 ShortestPaths

The document discusses shortest paths in weighted graphs, emphasizing the need for algorithms like Dijkstra's when edge weights vary. It explains properties of shortest paths, the implications of negative-weight cycles, and provides a detailed overview of Dijkstra's algorithm, including its operations and complexity analysis. Applications of shortest path algorithms include internet routing, flight reservations, and driving directions.

Uploaded by

danny427360
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views29 pages

23 ShortestPaths

The document discusses shortest paths in weighted graphs, emphasizing the need for algorithms like Dijkstra's when edge weights vary. It explains properties of shortest paths, the implications of negative-weight cycles, and provides a detailed overview of Dijkstra's algorithm, including its operations and complexity analysis. Applications of shortest path algorithms include internet routing, flight reservations, and driving directions.

Uploaded by

danny427360
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Shortest Paths

Dr. Hakim Mellah


Department of Computer Science & Software Engineering
Concordia University, Montreal, Canada

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)

Copyright © 2010 Michael T. Goodrich, Roberto Tamassia


All rights reserved
Weighted Graphs
❑ Breadth-first search can be used to find the shortest path from
one starting vertex to all other ones assuming that all edges in
the graph have the same weight (same length, cost, distance,
etc.).

❑ However, in many situations this may not be the case, and


hence BFS is inappropriate.

PVD
ORD
SFO
LGA
HNL
LAX DFW
MIA
Shortest Paths 2
Weighted Graphs
❑ Graphs where edges have different weight, are called weighted
graphs.

❑ In a weighted graph, each edge has an associated numerical value,


called the weight of the edge, which can be different from one edge to
another.

❑ Edge weight may represent, distances, costs, etc. For instance, in a


flight route graph, the weight of an edge represents the distance in
miles between the endpoint airports
ORD PVD
SFO
LGA
HNL
LAX DFW
MIA
Shortest Paths 3
Shortest Paths
❑ Given a weighted graph and two vertices u and v, we want to
find a path of minimum total weight between u and v.
◼ Length of a path is the sum of the weights of its edges.
❑ Example:
◼ Shortest path between Providence and Honolulu
❑ Applications
◼ Internet packet routing
◼ Flight reservations
◼ Driving directions
PVD
ORD
SFO
LGA
HNL
LAX DFW
MIA
Shortest Paths 4
Shortest Path Properties
Property 1:
The length (or weight) of a path is the sum of the weights of the edges
composing that path
Property 2:
A subpath of a shortest path is itself a shortest path
Property 3:
There is a tree of shortest paths from a start vertex to all the other vertices
Example:
Tree of shortest paths from Providence
PVD
ORD
SFO
LGA
HNL
LAX DFW
Shortest Paths
MIA 5
Negative-Weight Cycles
❑ The distance from vertex v to vertex u in a graph G, denoted by d(v, u),
is the length of the minimum length path (the shortest path) from v to u.

❑ d(v, u) = +∞ if there is no path between v and u.

❑ 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).

❑ A negative-weight edge is an edge whose 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.

◼ D[u] will afterwards be updated if a better path is found that results in a


smaller shortest path value to u.

◼ The operation is known as relaxation; the idea is similar to stretching a


spring more than needed then “relax” it back to its true resting distance.
Shortest Paths 9
Dijkstra’s Algorithm
❑ The idea is to grow a “cloud” of vertices, beginning with the
stating vertex v and eventually covering all the vertices

❑ In details, the algorithm starts by defining two sets; let us refer


to them as the C (for Cloud) set and the S set.

❑ Initially, C is empty, while S has vertex v. Additionally, all the


distance approximations, D[u], are initialized as ∞,with the
exception of D[v], which is initialized as 0.

❑ Afterwards C will grow to include specific vertices one at a time


(those are the ones with concrete shortest paths), while S will
include all other vertices that are directly connected either to s or
to other vertices already in C. Algorithm terminates once all
vertices have been moved to C. Shortest Paths 10
Dijkstra’s Algorithm (continues…)

❑ In details, the algorithm performs the repeated moving


operations from S to C as follows:
◼ Move one vertex from S to C. Let us refer to that vertex as X. This vertex, X, must
satisfy the following:
1) It is either directly connected to v or directly connected to another vertex that is already in C
2) Has the smallest distance to v among all the potential vertices that are considered for movement to C
◼ Whenever the vertex X is moved to C, its distance is the smallest distance
(shortest path) to v
◼ Additionally, once this vertex X is chosen, move it to C and recheck all distances
of all vertices that have direct connection to X and are NOT yet in C. If a smaller
value than what we already have is found, update this value
◼ In each step, we also update how to go to these vertices (that is, through which
vertex); we refer to this vertex as the prior function
◼ The steps are repeated until all the graph vertices are moved to C. The final
obtained distances represent the shortest paths, and the prior functions indicate
the direction from v (through which vertex) to obtain these shortest paths to
each of the other vertices in the graph

Shortest Paths 11
Dijkstra’s Algorithm (continues…)

C S

Distances yet to be determined


v
Distances to potential vertices
u
Potential vertex with shortest distance
X Selected vertex to move to C

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?*

*Figure is © Understanding Data Communications and Networks by William A. Shay - PWS


publishing company, Third Edition. ISBN-10: 0534383173, ISBN-13: 978-0534383176.
13
Dijkstra’s Algorithm
Cost function Prior function
C Potential X A B C D E F A B C D E F
elements
of S
0 {} {A} A 0 ∞ ∞ ∞ ∞ ∞ A - - - - -

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)

❑ Removal from Priority queue (the while loop)


◼ Each vertex is removed once from the priority queue, where each
removal takes O(log n) time, resulting in a total of O(n log n)
◼ Method getValue is called once for each vertex, resulting in O(n)

➔ so far, complexity is O(2 n log n) ➔ O(n log n)

Shortest Paths 19
Analysis of Dijkstra’s Algorithm
❑ Following all incident edges of removed vertex (the inner for
loop)

◼ Now, the method incidentEdges is called for every removed vertex u,


which means that the method is called as many times as the degree
of that vertex u, resulting in O(deg(u)), provided the graph is
represented by the adjacency list structure.

◼ 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 ?

▪ The methods: opposite, getDistance, weight, and setDistance are


called, however each of these cost O(1)

▪ 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

◼ This consequently results in O(log n) for each incident edge

◼ Since incidentEdges method is eventually called O(v deg(v)), since all


vertices are removed, a total complexity of O(v deg(v) log n) is incurred
Shortest Paths 21
Analysis of Dijkstra’s Algorithm
❑ So, what is the total complexity?

▪ From initial operations and insertion into the queue, we have:


O(n log n)

◼ From following all the incident edges, we have


O(v deg(v) log n)

❑ Recall that v deg(v) = 2m


❑ O(v deg(v) log n) = O(2m log n) ➔ O(m log n)

◼ So, total complexity of Dijkstra’s algorithm is:


◼ O(n log n) + O(m log n) ➔ O((n + m) log n)
Shortest Paths 22
Analysis of Dijkstra’s Algorithm
❑ In conclusion, Dijkstra’s algorithm runs in
O((n + m) log n)
provided the graph is represented by the adjacency list structure.

❑ The running time can also be expressed as a function of n only as


O(n2 log n)
◼ Recall that m <= n(n-1)/2

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

C’s true distance is 1, but it is


already in the cloud with
d(C)=5 and possibly has
already affected many other
Shortest Paths 25
distances!
Bellman-Ford Algorithm
(not in book)
❑ Works even with negative- Algorithm BellmanFord(G, s)
weight edges for all v  G.vertices()
if v = s
❑ Must assume directed
setDistance(v, 0)
edges (for otherwise we else
would have negative- setDistance(v, )
weight cycles) for i  1 to n − 1 do
❑ Iteration i finds all shortest for each e  G.edges()
{ relax edge e }
paths that use i edges. u  G.origin(e)
❑ Running time: O(nm). z  G.opposite(u,e)
❑ Can be extended to detect r  getDistance(u) + weight(e)
if r  getDistance(z)
a negative-weight cycle if it setDistance(z,r)
exists
◼ How?

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy