analysis of dijkstra’s algorithm: shortest path (non-negative ......dijkstra’s algorithm:...
Post on 09-Mar-2021
6 Views
Preview:
TRANSCRIPT
Fang SongTexas A&M U
Fall’19 CSCE 629
Analysis of Algorithms
F, 10/04/19
Lecture 14
Fang SongTexas A&M U
• Dijkstra’s algorithm: shortest path (non-negative weight)
Recall: shortest path problem
1
§ Input. graph !, node " and #§Output. $%"#(", #)
• Every edge has a length )* (can be negative)• Length of a path ) + = ∑*∈/ )*• Distance $%"# 0, 1 = 2%3/:5↝7 )(+)
§ Special cases• All edge of equal length: BFS 8(2 + 3)• DAG: DP in topological order 8(2 + 3)
§General: Bellman-Ford 8(23)Non-negative length:Dijkstra 8((2 + 3)log3)
Dynamic Programing ReRecap
2
1. Formulate the problem recursively• Overlapping subproblems• May be easy to first compute optimal value & then construct an optimal solution
2. Build solutions to your recurrence• Bottom-up: determine dependencies & find a right order (topo. order in DAG)• Top-down: smart recursion (i.e. without repetition) by momoization
Examples• Explicit DAG: shortest path in DAG; longest increasing subsequences (a.k.a.
longest path in DAG)• Binary choice: weighted interval scheduling• Multi-way choice: matrix-chain mult.; longest common subsequence; • Adding a variable: shortest path with negative length (Bellman-Ford)
Edsger W. Dijkstra
3
§ Pioneer in graph algorithms, distributed computing, concurrent computing, programming …
§ 1984 - 1999 UT Austin, TX§ 2002 passed away in Nuenen, Netherlands
https://cacm.acm.org/magazines/2010/8/96632-an-interview-with-edsger-w-dijkstra/fulltext
“What's the shortest way to travel from Rotterdam to Groningen? It is the algorithm for the shortest path, which I designed in about 20 minutes. One morning I was shopping in Amsterdam with my young fiancée, and tired, we sat down on the café terrace to drink a cup of coffee and I was just thinking about whether I could do this, and I then designed the algorithm for the shortest path. ”
Reducing to BFS
4
A
C
B D
E
A
C
B D2
1E
3
2
21
4
AC
B D
E
%1 %2BFS(*)
A
C
B D
E
An alarm-clock algorithm
5
A
C
B100
20050 A
C
B
………
……
%&'()*+(-, /) // set alarm clock for 1 at time 0Repeat until no more alarms; Suppose next alarm goes off at T for node 2
3415 1, 2 ← 7For each neighbor 8 of 2
If no alarm for 8, set one for time 7 + :(2, 8)Else If current alarm larger, reset it for time 7 + :(2, 8)
Idea. Convert ; to ;’ by inserting dummy nodes. Run BFS on ;’
Dijkstra’s algorithm: priority queue for alarms
6
PriorityQueue Q: set of ! elements w. associated key values (alarm)• Change-key(x). change key value of an element• Delete-min. Return the element with smallest key, and remove it. • Can be done in " log ! time (by a heap)
&'()*+,-(/, *) // initialize 2(3) = 0, others 2 6 = ∞Make Q from V using 2(⋅) as key valueWhile Q not empty
6 ← Delete−min @// pick node with shortest distance to sFor all edges 6, A ∈ C
If 2 A > 2 6 + F(6, A)2 A ← 2 6 + F(6, A) and Change-key(v)
"(Glog!)
"(!log!)
Dijkstra"((G + !)log!)
Further improvement possible by Fibonacci heap [More to come]
N.B. BFS uses ordinary Queue. Dijkstra = BFS+Priority Queue
Demo
7
!A 0B ∞C ∞D ∞E ∞
A
C
B D4
2E
3
24
113
5
Source!
A 0B 4C 2D ∞E ∞
A
C
B D4
2E
3
24
113
5
Source
While Q not empty( ← Delete−min 1// pick node with shortest distance to sFor all edges (, 3 ∈ 5If ! 3 > ! ( + 8((, 3)
! 3 ← ! ( + 8((, 3) and Change−key(v)
Demo
8
!A 0B 3C 3D 6E 7
A
C
B D4
2E
3
24
113
5
Source!
A 0B 3C 3D 5E 6
A
C
B D4
2E
3
24
113
5
Source
While Q not empty) ← Delete−min 2// pick node with shortest distance to sFor all edges ), 4 ∈ 6If ! 4 > ! ) + 9(), 4)
! 4 ← ! ) + 9(), 4) and Change-key(v)
Demo
9
!A 0B 3C 3D 5E 6
A
C
B D4
2E
3
24
113
5
Source!
A 0B 3C 3D 5E 6
A
C
B D4
2E
3
24
113
5
Source
While Q not empty( ← Delete−min 1// pick node with shortest distance to sFor all edges (, 3 ∈ 5If ! 3 > ! ( + 8((, 3)
! 3 ← ! ( + 8((, 3) and Change-key(v)
How it fails on negative lengths?
10
!
"
#2
1 −6(
3
Jumping to a short one too early!
Reflection on Dijkstra: greedy stays ahead
11
! "
#Known Region
§Known region R: in which the shortest distance to ! is known§Growing R: adding # that has the shortest distance to !§How to Identify #? The one that minimizes % " + '(", #) for " ∈ ,
Shortest path to some " in known region, followed by a single edge (", #)
-./0(1, 2) // initialize %(!) = 0, % " = ∞ , R=∅While R ≠ 8
Pick # ∉ , w. smallest %(") // by Priority QAdd # to ,For all edges #,: ∈ ;If % # > % " + '(", #)
% # ← % " + '(", #)
Contrast with Bellman-Ford
12
OPT $, & = min OPT $ − 1, & , min-→/∈1{OPT $ − 1,3 + 5-→/}
§Bellman-Ford (Dynamic programming)
7 & = min8∈9 7 : + 5(:, &)
§Dijkstra (Greedy)
• Positive weight: no need to wait; more edges in a path do not help
vGlobal vs. Local• Dijkstra’s requires global information: known region & which to add• Bellman-Ford uses only local knowledge of neighbors, suits distributed setting
Network routing: distance-vector protocol
13
§Communication network• Nodes: routers• Edges: direct communication links• Cost of edge: delay on link. naturally nonnegative, but
Bellman-Ford used anyway!
§Distance-vector protocol [“routing by rumor”]• Each router maintains a vector of shortest-path lengths to every other node
(distances) and the first hop on each path (directions). • Algorithm: each router performs ! separate computations, one for each potential
destination node.
§ Path-vector protocol: coping with dynamic costs
top related