1 Lecture 9-1 Shortest Path (Chapter 24)

COMP3001/3901 Algorithms
Lecture 9-1
Shortest Path
(Chapter 24)
University of Sydney
COMP3001 Algorithms, 2002
1. Definitions for Shortest Paths
• Example: street map or road map
– Cities or intersections: vertices
– Distance between adjacent intersections:
weight of the edge.
• Directed graph G=(V,E)
• Weight function: E -> R
• Weight of path from vertex u to vertex v: sum of
edge weights along the path.
• shortest path from vertex u to vertex v: path of
minimum weight
University of Sydney
COMP3001 Algorithms, 2002
Single-Source Shortest Paths
Variants
• Given a directed graph G = (V, E) with edge
weights and a distinguished source vertex,
s∈
∈ V, determine the distance from the source
vertex to every vertex in the graph.
• BFS finds short-paths from a single source
vertex to all other vertices in O(n+e) time,
assuming the graph has no edge weights.
• Single-destination shortest-paths problem:
Find a shortest path to a given destination
vertex t from every vertex v ∈ V.
• Edge weights can be negative; but in order for
the problem to be well-defined there must be
no cycle whose total cost is negative.
• All-pairs shortest-paths problem: Find a
shortest path from u to v for every pair of
vertices u and v.
University of Sydney
COMP3001 Algorithms, 2002
• Single-pair shortest-path problem: Find a
shortest path from u to v for given vertices u
and v.
University of Sydney
Triangle inequality
Optimal substructure
• A shortest path contains other shortest paths
within it
– Greedy: Dijkstra’s algorithm
– Dynamic: Floyd-Warshall algorithm
COMP3001 Algorithms, 2002
[Theorem] δ (u,v) ≤ δ (u,x) + δ (x,v)
u
v
x
• [lemma] Subpaths of shortest paths are
shortest paths
Well-definedness
Negative-weight cycle in path: some shortest
…
paths may not exist
<0
University of Sydney
COMP3001 Algorithms, 2002
University of Sydney
COMP3001 Algorithms, 2002
1
If there is a negative-weight cycle on some path
from s to v, we define δ (s,v) = - ∞
Cycles
• Can a shortest path contain a cycle?
– Negative-weight cycle
– Positive-weight cycle
– 0-weight cycle
• WLOG, we can assume that we find a cycle-free
shortest path.
– at most |V|-1 edges
University of Sydney
COMP3001 Algorithms, 2002
University of Sydney
COMP3001 Algorithms, 2002
University of Sydney
COMP3001 Algorithms, 2002
Representing shortest paths
• During execution: Predecessor subgraph
• Similar to BFS
• For each v, maintain a predecessor of v
• At termination: Shortest path tree G’=(V’,E’)
• V’: set of reachable vertices from s
• G’: rooted tree with root s
• For all v in V’, the unique path from s to v in
G’ is a shortest path from s to v in G
University of Sydney
COMP3001 Algorithms, 2002
Relaxation
• For each v, maintain d[v], shortest path estimate:
an upper bound on the weight of the shortest
path for each vertex v.
– This value will always be greater than or equal
to the true shortest path distance from s to v.
– Initially,all d[v]=∞ & d[s]=0 .
– As the algorithm goes on and sees more
vertices, it tries to update d[v] for each vertex
in the graph, until all d[v] values converge to
true shortest distances.
O(V) time
• Relaxing an edge (u,v): testing whether we can
improve the shortest path to v by going through
u (if yes, then update d[v])
University of Sydney
COMP3001 Algorithms, 2002
University of Sydney
COMP3001 Algorithms, 2002
2
Find Shortest Path by Relaxation
• If the solution is not yet an optimal value, then
push a little closer to the optimum.
– If we find a path from s to v shorter than d[v],
then update d[v].
• Consider an edge (u,v) with weight w(u,v).
– Suppose that we have already computed
current estimates on d[u] and d[v].
– We know that there is a path from s to u of
weight d[u].
– By taking this path and following it with the
edge (u,v) we get a path to v of length
d[u]+w(u,v).
– If this path is better than the existing path of
length d[v] to v, we should take it.
University of Sydney
COMP3001 Algorithms, 2002
Relax (u, v, w)
1. if d[v] > d[u] + w(u,v) // is the path thru u shorter?
2. then d[v] ←
← d[u] + w(u,v);
// yes, then take it.
3.
π [v] ←
← u;
// the shortest way back to the source is thru u
// by updating the predecessor pointer
NOTE: If we perform Relax (u, v, w) repeatedly
over all edges of the graph, all the d[v] values
will eventually converge to the true final
distance values from s.
How to do this most efficiently? (how many
times? order?)
University of Sydney
COMP3001 Algorithms, 2002
2. Bellman-Ford Algorithm
• Simple
• Allow negative edge weight
• Can test whether a negative weight cycle is
reachable from the source
• Relax each edge many times: progressively
decreasing an estimate d[v] until it achieves
the actual shortest-path weight δ (s,v)
• O(VE) time
University of Sydney
COMP3001 Algorithms, 2002
University of Sydney
COMP3001 Algorithms, 2002
University of Sydney
COMP3001 Algorithms, 2002
University of Sydney
COMP3001 Algorithms, 2002
3
3. Shortest paths in DAG
• Consider weighted DAG
• Shortest path: always well defined
– no negative weight cycle
• Use topological sort
• Relax edges in topologically sorted order
– just one pass
• Runs in linear time: O(V+E)
University of Sydney
COMP3001 Algorithms, 2002
University of Sydney
COMP3001 Algorithms, 2002
Application
Determine critical paths in PERT(program
evaluation and review technique) chart
analysis
• Edge: job to be performed
• Edge weight: time required to perform the job
• A path: a sequence of jobs that must be
performed in a particular order
• Critical path: a longest path through the DAG
u
v
x
job (u,v) must be performed prior to job (v,x)
University of Sydney
COMP3001 Algorithms, 2002
University of Sydney
COMP3001 Algorithms, 2002
Announcement
• We can find a critical path by either
– Negating thed edge weights
– Run DAG-SHORTEST-PATH
Or
– Run DAG-SHORTEST-PATH with
modification:
• Initialize-single-source: replace ∞ by -∞
∞
• Relax: replace > by <
University of Sydney
COMP3001 Algorithms, 2002
• Assignment 1: Due to Sep 25 Wed 5pm
• Submit: mailbox of your tutor
University of Sydney
COMP3001 Algorithms, 2002
4