Shortest Path 1
-
Upload
harshit008 -
Category
Documents
-
view
234 -
download
0
Transcript of Shortest Path 1
8/7/2019 Shortest Path 1
http://slidepdf.com/reader/full/shortest-path-1 1/16
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 1
CE806: AAD U. & P. U. Patel Dept. OF Computer Engineering
Introduction To Algorithms
CS 445
8/7/2019 Shortest Path 1
http://slidepdf.com/reader/full/shortest-path-1 2/16
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 2
CE806: AAD U. & P. U. Patel Dept. OF Computer Engineering
This Lecture
Single-source shortest paths in weightedgraphs
Shortest-Path Problems
Properties of Shortest Paths, Relaxation
Dijkstra¶s Algorithm
Bellman-Ford Algorithm
2
8/7/2019 Shortest Path 1
http://slidepdf.com/reader/full/shortest-path-1 3/16
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 3
CE806: AAD U. & P. U. Patel Dept. OF Computer Engineering
Shortest Path
Generalize distance to weighted setting Digraph G = (V ,E ) with weight functionW : E p R (assigning real values toedges)
Weight of path p = v 1 p v 2 p « p v k is
Shortest path = a path of the minimumweight
Applications static/dynamic network routing
robot motion planning
3
1
1
1
( ) ( , )k
i i
i
w p w v v
!
!§
8/7/2019 Shortest Path 1
http://slidepdf.com/reader/full/shortest-path-1 4/16
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 4
CE806: AAD U. & P. U. Patel Dept. OF Computer Engineering
Shortest-Path Problems
Shortest-Path problems Single-source (single-destination).
Find a shortest path from a given source(vertex s) to each of the vertices.
Single-pair. Given two vertices, find ashortest path between them. Solution tosingle-source problem solves this problemefficiently, too.
All-pairs. Find shortest-paths for everypair of vertices. Dynamic programmingalgorithm.
4
8/7/2019 Shortest Path 1
http://slidepdf.com/reader/full/shortest-path-1 5/16
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 5
CE806: AAD U. & P. U. Patel Dept. OF Computer Engineering
Negative Weights and Cycles?
Negative edges are OK, as long as thereare no negati v e w eight cycl es (otherwisepaths with arbitrary small ³lengths´ would be possible)
Shortest-paths can have no cycles(otherwise we could improve them byremoving cycles)
Any shortest-path in graph G can be nolonger than n ± 1 edges, where n is thenumber of vertices
5
8/7/2019 Shortest Path 1
http://slidepdf.com/reader/full/shortest-path-1 6/16
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 6
CE806: AAD U. & P. U. Patel Dept. OF Computer Engineering
Relaxation
For each vertex v in the graph, wemaintain v .d(), the estimate of theshortest path from s, initialized to gatthe start
Relaxing an edge (u,v ) means testingwhether we can improve the shortestpath to v found so far by going
through u
6
u v
vu
2
2
Relax(u,v)
u v
vu
2
2
Relax(u,v)
Relax (u,v,G)if v.d () > u.d ()+G. w(u,v) then
v.setd (u.d ()+G. w(u,v))
v.setparent(u)
8/7/2019 Shortest Path 1
http://slidepdf.com/reader/full/shortest-path-1 7/16
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 7
CE806: AAD U. & P. U. Patel Dept. OF Computer Engineering
Dijkstra's Algorithm
Non-negative edge weights Greedy, similar to Prim's algorithm for
MST
Like breadth-first search (if all weights = 1, one can simply use BFS)
Use Q, a priority queue ADT keyed byv .d() (BFS used FIFO queue, here weuse a PQ, which is re-organizedwhenever some d decreases)
Basic idea maintain a set S of solved vertices
at each step select "closest" vertex u, add itto S and relax all ed es from u7
8/7/2019 Shortest Path 1
http://slidepdf.com/reader/full/shortest-path-1 8/16
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 8
CE806: AAD U. & P. U. Patel Dept. OF Computer Engineering
Dijkstra¶s Pseudo Code
Input: Graph G , start vertex s
8
r elaxing
edges
Dijkstra(G,s)
01 for each vertex u � G. V ()
02 u.setd (g03 u.setparent(NIL)
04 s.setd (0)
05 S n� // Set S is used to explain the algorithm
06 Q.init(G. V ()) // Q is a priority queue ADT
07 while not Q.isEmpty()
08 u n Q.extractMin()09 S n S � {u}
10 for each v � u.adjacent() do
11 Relax(u, v, G)
12 Q. modifyKey(v)
8/7/2019 Shortest Path 1
http://slidepdf.com/reader/full/shortest-path-1 9/16
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 9
CE806: AAD U. & P. U. Patel Dept. OF Computer Engineering
Dijkstra¶s Example
9
g g
g g
s
u v
yx
10
5
1
2 39
4 67
2
g
g
s
u v
yx
10
5
1
2 39
4 67
2
Dijkstra(G,s)
01 for each vertex u � G. V ()
02 u.setd (g
03 u.setparent(NIL)
04 s.setd (0)
05 S n�
06 Q.init(G. V ())
07 while not Q.isEmpty()
08 u n Q.extractMin()
09 S n S � {u}
10 for each v � u.adjacent() do
11 Relax(u, v, G)
12 Q. modifyKey(v)
8/7/2019 Shortest Path 1
http://slidepdf.com/reader/full/shortest-path-1 10/16
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 10
CE806: AAD U. & P. U. Patel Dept. OF Computer Engineering
Dijkstra¶s Example (2)
10
u v
s
yx
10
5
1
2 39
4 67
2
s
u v
yx
10
5
1
2 39
4 67
2
Dijkstra(G,s)
01 for each vertex u � G. V ()
02 u.setd (g
03 u.setparent(NIL)
04 s.setd (0)
05 S n�
06 Q.init(G. V ())
07 while not Q.isEmpty()
08 u n Q.extractMin()
09 S n S � {u}
10 for each v � u.adjacent() do
11 Relax(u, v, G)
12 Q. modifyKey(v)
8/7/2019 Shortest Path 1
http://slidepdf.com/reader/full/shortest-path-1 11/16
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 11
CE806: AAD U. & P. U. Patel Dept. OF Computer Engineering
Dijkstra¶s Example (3)
11
u v
yx
10
5
1
2 39
4 67
2
u v
yx
10
5
1
2 39
4 67
2
Dijkstra(G,s)
01 for each vertex u � G. V ()
02 u.setd (g
03 u.setparent(NIL)
04 s.setd (0)
05 S n�
06 Q.init(G. V ())
07 while not Q.isEmpty()
08 u n Q.extractMin()
09 S n S � {u}
10 for each v � u.adjacent() do
11 Relax(u, v, G)
12 Q. modifyKey(v)
8/7/2019 Shortest Path 1
http://slidepdf.com/reader/full/shortest-path-1 12/16
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 12
CE806: AAD U. & P. U. Patel Dept. OF Computer Engineering
Dijkstra¶s Running Time
Extract-Min executed |V | time
Decrease-Key executed |E | time
Time = |V | T Extract-Min + |E | T Decrease-Key
T depends on different Q implementations
12
Q T(Extract-
Min)
T(Decrease-Key) Total
array 3 (V ) 3 (1) 3 (V 2)binary heap 3 (lg V ) 3 (lg V ) 3 (E lg V )
Fibonacci heap 3 (lg V ) 3 (1) (amort.) 3 (V lgV + E )
8/7/2019 Shortest Path 1
http://slidepdf.com/reader/full/shortest-path-1 13/16
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 13
CE806: AAD U. & P. U. Patel Dept. OF Computer Engineering
Bellman-Ford Algorithm
Dijkstra¶s doesn¶t work when there arenegative edges:
Intuition ± we can not be greedy any moreon the assumption that the lengths of paths
will only increase in the future
Bellman-Ford algorithm detects negativecycles (returns f al se) or returns the
shortest path-tree
13
8/7/2019 Shortest Path 1
http://slidepdf.com/reader/full/shortest-path-1 15/16
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 15
CE806: AAD U. & P. U. Patel Dept. OF Computer Engineering
Bellman-Ford Example
15
5
g g
g g
s
zy
6
7
8-3
72
9
-2xt
-4
g
g
s
zy
6
7
8-3
72
9
-2xt
-4
5
s
zy
6
7
8-3
72
9
-2xt
-4
5
s
zy
6
7
8-3
72
9
-2xt
-4
5
8/7/2019 Shortest Path 1
http://slidepdf.com/reader/full/shortest-path-1 16/16
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 16
CE806: AAD U. & P. U. Patel Dept. OF Computer Engineering
Bellman-Ford Example
Bellman-Ford running time:
(|V|-1)|E| + |E| = 5(VE)
16
s
zy
6
7
8-3
7
2
9
-2xt
-4
5