Dijkstra s Algorithm Shortest Path Problem Directed graph G = (V - - PowerPoint PPT Presentation

dijkstra s algorithm shortest path problem
SMART_READER_LITE
LIVE PREVIEW

Dijkstra s Algorithm Shortest Path Problem Directed graph G = (V - - PowerPoint PPT Presentation

Dijkstra s Algorithm Shortest Path Problem Directed graph G = (V , E) Source s l e = length of edge e l e 0 for all edges e Shortest path problem: (Google Maps!) find shortest path from s to all other nodes 1 a c 2 3 1 1 1 e


slide-1
SLIDE 1

Dijkstra’ s Algorithm

slide-2
SLIDE 2

Shortest Path Problem

Directed graph G = (V , E) Source s le = length of edge e le ≥ 0 for all edges e Shortest path problem: (Google Maps!) find shortest path from s to all other nodes

a e b d c f 1 1 1 1 1 2 3 4

slide-3
SLIDE 3

Simplification

For now, let’ s just find the lengths of the shortest paths to every other node We’ll show how to recover the paths later

slide-4
SLIDE 4

Dijkstra’ s Algorithm

Maintain set S of explored nodes: for u ∈ S, we know the length d(u) of the shortest path from s to u. Initialize S = { s }, d(s) = 0. Repeatedly find shortest path to any node v ∉ S that remains in S until the final edge e = (u, v) S = S ∪ {v}, d(v) = d(u) + le

a e b d c f 1 1 1 1 1 2 3 4

S

Idea: explore outward by distance

slide-5
SLIDE 5

Dijkstra’ s Algorithm

Repeatedly find shortest path to any node v ∉ S that remains in S until the final edge e = (u, v) Minimize: d’(v) = min { d(u) + le : u ∈ S, e = (u, v) ∈ E }

a e b d c f 1 1 1 1 1 2 3 4

S

slide-6
SLIDE 6

Dijkstra’ s Algorithm

a e b d c f 1 1 1 1 1 2 3 4

Node d()

slide-7
SLIDE 7

Dijkstra’ s Algorithm

a e b d c f 1 1 1 1 1 2 3 4

Node d()

a

slide-8
SLIDE 8

Dijkstra’ s Algorithm

a e b d c f 1 1 1 1 1 2 3 4

Node d()

a b 1

slide-9
SLIDE 9

Dijkstra’ s Algorithm

a e b d c f 1 1 1 1 1 2 3 4

Node d()

a b 1 c 1

slide-10
SLIDE 10

Dijkstra’ s Algorithm

a e b d c f 1 1 1 1 1 2 3 4

Node d()

a b 1 c 1 d 2

slide-11
SLIDE 11

Dijkstra’ s Algorithm

a e b d c f 1 1 1 1 1 2 3 4

Node d()

a b 1 c 1 d 2 e 3

slide-12
SLIDE 12

Dijkstra’ s Algorithm

a e b d c f 1 1 1 1 1 2 3 4

Node d()

a b 1 c 1 d 2 e 3 f 4

slide-13
SLIDE 13

Example

Second example on board

slide-14
SLIDE 14

Dijkstra’ s Algorithm: Implementation

Dijkstra’ s Algorithm (G, s) { S = {s} / / S is the set of explored nodes d(s) = 0 / / d is the distance to the node from s while S ≠ V { / / there are unexplored nodes select a node v ∉ S with at least one edge from S to minimize d’(v) = min { d(u) + le : u ∈ S, e = (u, v) ∈ E } add v to S d(v) = d’(v) }

How do we recover a path with length d(v)?

slide-15
SLIDE 15

Dijkstra’ s Algorithm: Implementation

Dijkstra’ s Algorithm (G, s) { S = {s} / / S is the set of explored nodes d(s) = 0 / / d is the distance to the node from s while S ≠ V { / / there are unexplored nodes select a node v ∉ S with at least one edge from S to minimize d’(v) = min { d(u) + le : u ∈ S, e = (u, v) ∈ E } add v to S d(v) = d’(v) prev(v) = argmin { d(u) + le : u ∈ S, e = (u, v) ∈ E } }

Proof of correctness: start in small groups, complete on board

slide-16
SLIDE 16

Dijkstra’ s Algorithm: Implementation

Dijkstra’ s Algorithm (G, s) { S = {s} / / S is the set of explored nodes d(s) = 0 / / d is the distance to the node from s while S ≠ V { / / there are unexplored nodes select a node v ∉ S with at least one edge from S to minimize d’(v) = min { d(u) + le : u ∈ S, e = (u, v) ∈ E } add v to S d(v) = d’(v) prev(v) = argmin { d(u) + le : u ∈ S, e = (u, v) ∈ E } }

How do we implement this efficiently?

slide-17
SLIDE 17

Dijkstra’ s Algorithm (G, s) { S = {s} / / S is the set of explored nodes d’(s) = 0 / / explicitly maintain the d’ values d’(v) = ∞ for all v ∉ S while S != V { / / there are unexplored nodes Let v be the node that minimizes d’(v) d(v) = d’(v) for each edge (v, w) where v ∈ S, w ∉ S { d’(v) = min( d’(v), d(v) + l(v, w) ) } } }

Data structure?

slide-18
SLIDE 18

Running Time

With heap-based priority queue, running time

  • f Dijkstra’

s algorithm is: O(m) - traverse edges O(n log n) - n extractMin operations O(m log n) - m changeKey operations Total: O(m log n)