(les09)-shortestpath
TRANSCRIPT
-
7/25/2019 (les09)-shortestpath
1/26
Algoritmen & Datastructuren2014 2015
Shortest Path
Philip Dutr
Dept. of Computer Science, K.U.Leuven
-
7/25/2019 (les09)-shortestpath
2/26
Overview
Copyright Ph.Dutr, Spring 2015
Shortest path problem Shortest Path Tree
Edge Relexation
Generic SPT Algorithm
Edsger Dijkstra
Dijkstras Algorithm
Acyclic Graphs
Negative Weights and cycles Currency Exchange
-
7/25/2019 (les09)-shortestpath
3/26
Leuven Eindhoven?
Copyright Ph.Dutr, Spring 2015
Make a graph! Every intersection is a vertex, every road is an edge
Edges are weighted (e.g. by length) and directed(one-way roads)
-
7/25/2019 (les09)-shortestpath
4/26
-
7/25/2019 (les09)-shortestpath
5/26
Shortest Path: variants
Copyright Ph.Dutr, Spring 2015
Which vertices? Source-sink: from one vertex to another
Single source: from one vertex to every other
All pairs: between all pairs of vertices.
Restrictions on edge weights? Non-negative weights Arbitrary weights
Euclidean weights
Cycles?
No cycles No "negative cycles"
Simplifying assumption:There exists a shortest path from sto each vertex v
-
7/25/2019 (les09)-shortestpath
6/26
Shortest Path
Copyright Ph.Dutr, Spring 2015
Goal: Find the shortest path froms
to every other vertex Observation: A shortest path tree (SPT) solution exists
Consequence. We can represent the SPT with twovertex-indexed arrays:
distTo[v] is length of shortest path from sto v edgeTo[v] is last edge on shortest path from sto v
-
7/25/2019 (les09)-shortestpath
7/26
Edge relaxation
Copyright Ph.Dutr, Spring 2015
Basic operation Relax edge e= vw
distTo[v]is length of shortestknown path from sto v
distTo[w]is length of shortestknown path from sto w
edgeTo[w]is last edge on shortestknown path from sto w
If e= vwgives shorter path to wthrough v,update distTo[w]and edgeTo[w]
-
7/25/2019 (les09)-shortestpath
8/26
(Very) Generic SPT Algorithm
Copyright Ph.Dutr, Spring 2015
1.
Initialize distTo[s]= 0 and distTo[v]= for all v
!=s
2. Repeat until optimality conditions (*) are satisfied: Relax any edge
Proof:
Throughout algorithm, distTo[v] is the length of a simple path from s to v andedgeTo[v] is last edge on path
Each successful relaxation decreases distTo[v] for some v
The entry distTo[v] can decrease at most a finite number of times
Problem: How to choose which edge to relax?different algorithms
(*) the shortest path for each v from s has been computed
-
7/25/2019 (les09)-shortestpath
9/26
-
7/25/2019 (les09)-shortestpath
10/26
Edsger W. Dijkstra
Copyright Ph.Dutr, Spring 2015
In their capacity as a tool, computers will be but a ripple on
the surface of our culture. In their capacity as intellectualchallenge, they are without precedent in the cultural historyof mankind.
The use of COBOL cripples the mind; its teaching should,therefore, be regarded as a criminal offence.
It is practically impossible to teach good programming tostudents that have had a prior exposure to BASIC: aspotential programmers they are mentally mutilated beyondhope of regeneration.
Asking whether computers can think is like asking whethersubmarines can swim.
Computer science is no more about computers thanastronomy is about telescopes.
-
7/25/2019 (les09)-shortestpath
11/26
Copyright Ph.Dutr, Spring 2015
-
7/25/2019 (les09)-shortestpath
12/26
Dijkstras algorithm
Copyright Ph.Dutr, Spring 2015
Consider vertices in increasingorder of distance froms(non-SPT vertex with the lowest distTo[ ] value)
Add vertex to SPT and relax all edges starting from that
vertex
(Example)
-
7/25/2019 (les09)-shortestpath
13/26
Proof of Dijkstra
Copyright Ph.Dutr, Spring 2015
Each edge e= vwis relaxed exactly once(when v is added to SPT),leaving distTo[w] distTo[v]+ e.weight()
Inequality holds until algorithm terminates because: distTo[w] cannot increase
distTo[v] will not change(because we have non-negative weights & v is not relaxedagain)
Thus, upon termination, shortest-paths optimalityconditions hold
-
7/25/2019 (les09)-shortestpath
14/26
Analysis
Copyright Ph.Dutr, Spring 2015
Almost identical to Prim! Also depends on type of priority queue
Priority queue of Vvertices
Every edge update requires a decrease key operationin the queueproportional to E.log2V)
-
7/25/2019 (les09)-shortestpath
15/26
Acyclic graphs
Copyright Ph.Dutr, Spring 2015
Better solution possible: Use topological order
-
7/25/2019 (les09)-shortestpath
16/26
Acyclic graphs
Copyright Ph.Dutr, Spring 2015
Topological sort algorithm Consider vertices in topologically order
Relax all edges starting at source vertex
Topological sort algorithm computes SPT in any edge-weighted Directed Acyclic Graph in time proportional E + V
Proof: Each edge e = vwis relaxed exactly once (when v is added),
leaving distTo[w] distTo[v] + e.weight().
Inequality holds until algorithm terminates because: distTo[w]cannot increase
distTo[v]will not change(no edge pointing to v wil be relaxed anymore)
Thus, upon termination, shortest-paths optimality conditions hold.
-
7/25/2019 (les09)-shortestpath
17/26
Acyclic graphs: longest path
Copyright Ph.Dutr, Spring 2015
Negate all weights, find shortest (negative weights dont work for Dijkstra, but they do for
Directed Acyclic Graphs)
Application: job scheduling
-
7/25/2019 (les09)-shortestpath
18/26
Critical path
Copyright Ph.Dutr, Spring 2015
-
7/25/2019 (les09)-shortestpath
19/26
Negative weights
Copyright Ph.Dutr, Spring 2015
-
7/25/2019 (les09)-shortestpath
20/26
Negative cycles
Copyright Ph.Dutr, Spring 2015
SPT exists iff nonegative cycles
-
7/25/2019 (les09)-shortestpath
21/26
New algorithm
Copyright Ph.Dutr, Spring 2015
Shortest path with negative weights Bellman-Ford algorithm
In each pass, relax all edges
Make V passes
Idea: After pass i, found shortest path containing at most i
edges
Time ~ E.V
If distTo[v]does not change during pass i, no need torelax any edge starting from v in pass i +1
Maintain queue of vertices whose distTo[] has changed
Reuses values from previous iteration
-
7/25/2019 (les09)-shortestpath
22/26
Copyright Ph.Dutr, Spring 2015
-
7/25/2019 (les09)-shortestpath
23/26
Copyright Ph.Dutr, Spring 2015
-
7/25/2019 (les09)-shortestpath
24/26
Detect negative cycles
Copyright Ph.Dutr, Spring 2015
After V-1 passes
If there are still vertices on the queue
Negative cycle is detected
Check optimality condition for all edges
-
7/25/2019 (les09)-shortestpath
25/26
Currency exchange
Copyright Ph.Dutr, Spring 2015
How to change gold in $?
-
7/25/2019 (les09)-shortestpath
26/26
Currency exchange
Copyright Ph.Dutr, Spring 2015
Find path that maximizes productof weights
Maximum ProductMinimal Sum?
Weights- ln (weight)