analysis of dijkstra’s algorithm: shortest path (non-negative ......dijkstra’s algorithm:...

14
Fang Song Texas A&M U Fall’19 CSCE 629 Analysis of Algorithms F, 10/04/19 Lecture 14 Fang Song Texas A&M U Dijkstra’s algorithm: shortest path (non-negative weight)

Upload: others

Post on 09-Mar-2021

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Analysis of Dijkstra’s algorithm: shortest path (non-negative ......Dijkstra’s algorithm: priority queue for alarms 6 PriorityQueueQ: set of !elements w. associated key values

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)

Page 2: Analysis of Dijkstra’s algorithm: shortest path (non-negative ......Dijkstra’s algorithm: priority queue for alarms 6 PriorityQueueQ: set of !elements w. associated key values

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)

Page 3: Analysis of Dijkstra’s algorithm: shortest path (non-negative ......Dijkstra’s algorithm: priority queue for alarms 6 PriorityQueueQ: set of !elements w. associated key values

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)

Page 4: Analysis of Dijkstra’s algorithm: shortest path (non-negative ......Dijkstra’s algorithm: priority queue for alarms 6 PriorityQueueQ: set of !elements w. associated key values

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. ”

Page 5: Analysis of Dijkstra’s algorithm: shortest path (non-negative ......Dijkstra’s algorithm: priority queue for alarms 6 PriorityQueueQ: set of !elements w. associated key values

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

Page 6: Analysis of Dijkstra’s algorithm: shortest path (non-negative ......Dijkstra’s algorithm: priority queue for alarms 6 PriorityQueueQ: set of !elements w. associated key values

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 ;’

Page 7: Analysis of Dijkstra’s algorithm: shortest path (non-negative ......Dijkstra’s algorithm: priority queue for alarms 6 PriorityQueueQ: set of !elements w. associated key values

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

Page 8: Analysis of Dijkstra’s algorithm: shortest path (non-negative ......Dijkstra’s algorithm: priority queue for alarms 6 PriorityQueueQ: set of !elements w. associated key values

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)

Page 9: Analysis of Dijkstra’s algorithm: shortest path (non-negative ......Dijkstra’s algorithm: priority queue for alarms 6 PriorityQueueQ: set of !elements w. associated key values

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)

Page 10: Analysis of Dijkstra’s algorithm: shortest path (non-negative ......Dijkstra’s algorithm: priority queue for alarms 6 PriorityQueueQ: set of !elements w. associated key values

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)

Page 11: Analysis of Dijkstra’s algorithm: shortest path (non-negative ......Dijkstra’s algorithm: priority queue for alarms 6 PriorityQueueQ: set of !elements w. associated key values

How it fails on negative lengths?

10

!

"

#2

1 −6(

3

Jumping to a short one too early!

Page 12: Analysis of Dijkstra’s algorithm: shortest path (non-negative ......Dijkstra’s algorithm: priority queue for alarms 6 PriorityQueueQ: set of !elements w. associated key values

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 % # > % " + '(", #)

% # ← % " + '(", #)

Page 13: Analysis of Dijkstra’s algorithm: shortest path (non-negative ......Dijkstra’s algorithm: priority queue for alarms 6 PriorityQueueQ: set of !elements w. associated key values

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

Page 14: Analysis of Dijkstra’s algorithm: shortest path (non-negative ......Dijkstra’s algorithm: priority queue for alarms 6 PriorityQueueQ: set of !elements w. associated key values

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