The Shortest Path problem - UCSB Computer Science …suri/cs130a/Dijkstra.pdf · The Shortest Path...
Transcript of The Shortest Path problem - UCSB Computer Science …suri/cs130a/Dijkstra.pdf · The Shortest Path...
The Shortest Path problem
I Given graph and a vertex s find shortest paths from s to all othervertices.
I Map routing, robot navigation, urban traffic planning
I Optimal pipelining of VLSI chip
I Routing of telecommunication messages
I Network routing protocols (OSPF, BGP, RIP)
I Seam carving, texture mapping, typesetting in TeX!
1 / 27
Example with positive edge weights
ν1 ν2
ν3 ν4 ν5
ν6 ν7
2
1 310
2
64
1
85
2
4
2 / 27
Example with negative edge weights
ν1
ν2
ν3
2
10
−15
3 / 27
Unweighted shortest paths
I Given unweighted graph G
I Can assume all edge weights are 1
I Find shortest paths from s
I There is what is known as a shortest path tree!
I Can be found using Breadth First Search (BFS)
4 / 27
Naive implementation: pseudo code
void Graph::unweighted( Vertex s ){
Vertex v,w;
s.dist = 0;
for(int currDist=0; currDist < NUM_VERTICES; currDist++)
for each vertex v
if( !v.known && v.dist == currDist ){
v.known = true;
for each w adjacent to v
if( w.dist == INFINITY ){
w.dist = currDist + 1;
w.path = v;
}
}
}
5 / 27
Smarter implementation: pseudo code
void Graph::unweighted( Vertex s ){
Queue q( NUM_VERTICES );
Vertex v,w;
q.enqueue(s);
s.dist = 0;
while( !q.isEmpty() ){
v = q.dequeue();
v.known = true;
for each w adjacent to v
if( w.dist == INFINITY ){
w.dist = v.dist + 1;
w.path = v;
q.enqueue( w );
}
}
}
6 / 27
Main structural properties of Shortest Paths
I Prefixes of shortest paths are themselves shortest paths
I Does a shortest path always exist?
I What about a shortest path tree?
I How can we compute such a tree
7 / 27
Main concepts
I known vertices
I Relaxation of an edge (v, w) : d(w) = min(d(w), d(v) + cvw)
I Next: The Djikstra algorithm
8 / 27
25
Edsger W. Dijkstra: select quotes
Edsger W. Dijkstra
Turing award 1972
“ Do only what only you can do. ”
“ In their capacity as a tool, computers will be but a ripple on the
surface of our culture. In their capacity as intellectual challenge,
they are without precedent in the cultural history of 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 to
students that have had a prior exposure to BASIC: as potential
programmers they are mentally mutilated beyond hope of
regeneration. ”
“ APL is a mistake, carried through to perfection. It is the
language of the future for the programming techniques
of the past: it creates a new generation of coding bums. ”
9 / 27
26
Edsger W. Dijkstra: select quotes
10 / 27
Djikstra algorithm : arbitrary non-negative edge weights
I Store dv, known, pv
I Pick vertex with minimum dv (that is not known)
I Relax all edges outgoing from it
I Repeat until all vertices are known
11 / 27
Example of Djikstra in action
ν1 ν2
ν3 ν4 ν5
ν6 ν7
2
1 310
2
64
1
85
2
4
12 / 27
Example of Djikstra in action
ν1 ν2
ν3 ν4 ν5
ν6 ν7
2
1 310
2
64
1
85
2
4
0 ∞
∞∞∞
∞∞
12 / 27
Example of Djikstra in action
ν1 ν2
ν3 ν4 ν5
ν6 ν7
2
1 310
2
64
1
85
2
4
0 2
∞∞1
∞∞
12 / 27
Example of Djikstra in action
ν1 ν2
ν3 ν4 ν5
ν6 ν7
2
1 310
2
64
1
85
2
4
0 2
331
95
12 / 27
Example of Djikstra in action
ν1 ν2
ν3 ν4 ν5
ν6 ν7
2
1 310
2
64
1
85
2
4
0 2
331
95
12 / 27
Example of Djikstra in action
ν1 ν2
ν3 ν4 ν5
ν6 ν7
2
1 310
2
64
1
85
2
4
0 2
331
95
12 / 27
Example of Djikstra in action
ν1 ν2
ν3 ν4 ν5
ν6 ν7
2
1 310
2
64
1
85
2
4
0 2
331
85
12 / 27
Example of Djikstra in action
ν1 ν2
ν3 ν4 ν5
ν6 ν7
2
1 310
2
64
1
85
2
4
0 2
331
65
12 / 27
Example of Djikstra in action
ν1 ν2
ν3 ν4 ν5
ν6 ν7
2
1 310
2
64
1
85
2
4
0 2
331
65
12 / 27
Djikstra data-structuresstruct Vertex
{
List adj; // Adjacency list
bool known;
DistType dist;
Vertex path; // ref to parent in path
};
void Graph::createTable( vector<Vertex> & t){
readGraph( t ); //Read graph, fill in adj
for(int i=0; i < t.size(); i++){
t[i].known = false;
t[i].dist = INFINITY;
t[i].path = NOT_A_VERTEX;
}
NUM_VERTICES = t.size();
}
13 / 27
Shortest Paths after Djikstra run
void Graph::printPath( Vertex v )
{
if(v.path != NOT_A_VERTEX)
{
printPath( v.path );
cout << " to ";
}
cout << v;
}
14 / 27
The Djikstra algorithm: pseudo-code
void Graph::djikstra( Vertex s ){
Vertex v,w;
1. s.dist = 0;
2. for( ; ; ){
3. v = smallest unknown distance vertex;
4. if( v == NOT_A_VERTEX )
5. break;
6. v.known = true;
7. for each w adjacent to v;
8. if( !w.known )
9. if( v.dist + c(v,w) < w.dist ){
10. decrease w.dist to v.dist + c(v,w);
11. w.path = v;
}
}
}
15 / 27
Implementing Djikstra
I Naive implementation (using array to find min dv) :O(|E|+ |V 2|) = O(|V |2)
I Could we be better for sparse graphs?
16 / 27
Implementing Djikstra
I Naive implementation (using array to find min dv) :O(|E|+ |V 2|) = O(|V |2)
I Could we be better for sparse graphs?
PQ impl insert delete-min decrease-key
total
unorderedarray
1 V 1 V 2
binary heap log V log V log V E log V
d-way heap logd V d logd V logd V logEVV
Fibonacciheap
1 log V 1 E +V log V
16 / 27
Negative edge weights!
ν1
ν2
ν3
2
10
−15
17 / 27
Acyclic Graphs
I Important special case : Nonreversible chemcial reactions, criticalpath analysis
I Running time is O(|E|+ |V |)I Djikstra can be implemented along with Topological sort
18 / 27
Example: Activity-node graph
start finish
A(3)
B(2)
C(3)
D(2)
E(1)
F (3)
G(2)
K(4)
H(1)
19 / 27
Event-node graph
1
2
3
6′
4
6
5
7′
8′
7
89
10′ 10
A/3
B/2 0
0
C/3
D/2
E/1
0
0
0
0
F/3
G/2
K/4
0
0
0
H/1
20 / 27
Earliest Completion times
1
2
3
6′
4
6
5
7′
8′
7
89
10′ 10
A/3
B/2 0
0
C/3
D/2
E/1
0
0
0
0
F/3
G/2
K/4
0
0
0
H/1
0
3
2
3
6
5
3
6
5
9
7
7
9 10
21 / 27
Latest Completion times
1
2
3
6′
4
6
5
7′
8′
7
89
10′ 10
A/3
B/2 0
0
C/3
D/2
E/1
0
0
0
0
F/3
G/2
K/4
0
0
0
H/1
0
3
4
4
6
6
5
6
7
9
8
9
9 10
22 / 27
EC, LC, Slack, Critical Path
Earliest and Latest Completion times
EC1 = 0
ECw = max(v,w)∈E
(ECv + cv,w)
LCn = ECn
LCv = min(v,w)∈E
(LCw − cv,w)
Slack of an edge (v, w)
Slack(v,w) = LCw − ECv − c(v,w)
23 / 27
The Bellman Ford algorithm
Basic Pseudo code
d[s] = 0for i = 1 to |V |
Relax each edge
I Why does this work?
24 / 27
Bellman Ford: Queue Based Implementation
void Graph::weightedNegative( Vertex s ){
Queue q(NUM_VERTICES);
Vertex v,w;
q.enqueue(s);
s.dist = 0;
while(! q.isEmpty()){
v=q.dequeue();
for each w adjacent to v
if(v.dist + c(v,w) < w.dist){
w.dist = v.dist + c(v,w);
w.path = v;
if(w is not already in q)
q.enqueue(w);
}
}
}
25 / 27
Bellman Ford contd.
I Runtime is O(EV )
I Can be used to detect negative cycles
I Useful in finding arbitrage opportunities!
26 / 27
All-Pairs Shortest Path
I Can run |V | Djikstra’s - O(|E||V | log |V |)I Floyd Warshall : Dynamic programming algorithm
I Works in O(|V |3)
27 / 27