1 of 11

Shortest Paths (concept)

Find the cheapest flight from Newark to London.

  • DFS: “Can I fly from Newark to London?”
    • Backs out when all vertices reachable from source have been visited
  • BFS: “What flights from Newark to London have the fewest stops?”
    • Shortest paths in an unweighted graph (hops/edges)
  • What about the cheapest flights?
    • BFS gets the shortest path with respect to edges (in this case, layovers), not weights
    • Newark -> London (fewest edges) doesn’t consider cost – it’s the most expensive
    • Need another algorithm to evaluate minimum-cost paths

2 of 11

Dijkstra’s Algorithm

Finds the shortest path (minimum cost) from a source vertex to every other vertex in a weighted graph, where all weights are nonnegative.

�Analyzes all possibilities for each neighbor of a vertex, picking the neighbor with the shortest distance from source.

It is a greedy algorithm: it solves the problem by making locally optimal decisions at each step with the hope to find the global minimum. (ie: finds the best distance at that time)

We can use Dijkstra’s to generate a spanning tree containing ideal paths from source -> others. (not in assignments)

Spanning tree = covers all vertices, no cycles

3 of 11

Dijkstra’s Algorithm

In a directed graph with n vertices and e edges

G = (V, E) where V and E are the sets with all vertices and edges respectively

Every edge weight must be nonnegative w(u,v) >= 0. Negative edges break the assumption of ideal distances (what if a negative edge gives us a better distance for a done vertex?)

4 of 11

Dijkstra’s Algorithm

Algorithm:

let s be the source vertex

done = {} Vertices with known optimal path from source

fringe = {} Vertices with unknown optimal path from source

for each vertex other than source:

d(v) = infinity

pred(v) = null

d(s) = 0 Optimal distance from source to parameter vertex

pred(s) = null

add s to fringe

while the fringe is not empty fringe = to-do list, not ideal yet

m = remove minimum distance vertex from the fringe

add m to done set

for each neighbor w of m not in the done set

if d(w) is infinity then

d(w) = d(m) + w(m,w)

add w to fringe

pred(w) = m

else if d(w) > (d(m) + w(m,w)) then

d(w) = d(m) + w(m,w); pred(w) = m

endif

endfor

endwhile

Rather than visiting the first vertex (like in BFS), we pick the min distance vertex (min d(v) of vertices still in the fringe to be visited) + update based on distances

  • Fringe: “to-do” list of vertices still yet to be visited
    • Easiest implementation to find min: list (use this for assignment, O(n) linear search)
    • Improvement: min heap (harder for assignment)
  • pred(v): previous vertex on optimal path from source to parameter vertex
  • d(v): optimal distance from source to parameter vertex (so far)
    • If infinite: hasn’t been seen yet, hasn’t been in the fringe, essentially unknown

5 of 11

dijkstra(A)

Algorithm:

let s be the source vertex

done = {} Vertices with known optimal path from source

fringe = {} Vertices with unknown optimal path from source

for each vertex other than source:

d(v) = infinity

pred(v) = null

d(s) = 0 Optimal distance from source to parameter vertex

pred(s) = null

add s to fringe

while the fringe is not empty fringe = to-do list, not ideal yet

m = remove minimum distance vertex from the fringe

add m to done set

for each neighbor w of m not in the done set

if d(w) is infinity then

d(w) = d(m) + w(m,w)

add w to fringe

pred(w) = m

else if d(w) > (d(m) + w(m,w)) then

d(w) = d(m) + w(m,w); pred(w) = m

endif

endfor

endwhile

Step

Done

d(B)

d(C)

d(D)

d(E)

0

inf

inf

inf

inf

1

2

3

4

3

Pred

Value

A

null

B

null

C

null

D

null

E

null

A

Fringe

6 of 11

dijkstra(A)

Algorithm:

let s be the source vertex

done = {} Vertices with known optimal path from source

fringe = {} Vertices with unknown optimal path from source

for each vertex other than source:

d(v) = infinity

pred(v) = null

d(s) = 0 Optimal distance from source to parameter vertex

pred(s) = null

add s to fringe

while the fringe is not empty fringe = to-do list, not ideal yet

m = remove minimum distance vertex from the fringe

add m to done set

for each neighbor w of m not in the done set

if d(w) is infinity then

d(w) = d(m) + w(m,w)

add w to fringe

pred(w) = m

else if d(w) > (d(m) + w(m,w)) then

d(w) = d(m) + w(m,w); pred(w) = m

endif

endfor

endwhile

Step

Done

d(B)

d(C)

d(D)

d(E)

0

inf

inf

inf

inf

1

A

6

inf

1

inf

2

3

4

3

Pred

Value

A

null

B

A

C

null

D

A

E

null

A, B, D

Fringe (B, D newly added)

7 of 11

dijkstra(A)

Algorithm:

let s be the source vertex

done = {} Vertices with known optimal path from source

fringe = {} Vertices with unknown optimal path from source

for each vertex other than source:

d(v) = infinity

pred(v) = null

d(s) = 0 Optimal distance from source to parameter vertex

pred(s) = null

add s to fringe

while the fringe is not empty fringe = to-do list, not ideal yet

m = remove minimum distance vertex from the fringe

add m to done set

for each neighbor w of m not in the done set

if d(w) is infinity then

d(w) = d(m) + w(m,w)

add w to fringe

pred(w) = m

else if d(w) > (d(m) + w(m,w)) then

d(w) = d(m) + w(m,w); pred(w) = m

endif

endfor

endwhile

Step

Done

d(B)

d(C)

d(D)

d(E)

0

inf

inf

inf

inf

1

A

6

inf

1

inf

2

D

3

inf

1

2

3

1

4

1

3

1

B, D, E

Fringe (E newly added)

d(b) = 6, d(D) = 1

D’s not-done neighbors: B, E

Pred

Value

A

null

B

D

C

null

D

A

E

D

d(D) + w(d, b) is better than d(B)

8 of 11

dijkstra(A)

Algorithm:

let s be the source vertex

done = {} Vertices with known optimal path from source

fringe = {} Vertices with unknown optimal path from source

for each vertex other than source:

d(v) = infinity

pred(v) = null

d(s) = 0 Optimal distance from source to parameter vertex

pred(s) = null

add s to fringe

while the fringe is not empty fringe = to-do list, not ideal yet

m = remove minimum distance vertex from the fringe

add m to done set

for each neighbor w of m not in the done set

if d(w) is infinity then

d(w) = d(m) + w(m,w)

add w to fringe

pred(w) = m

else if d(w) > (d(m) + w(m,w)) then

d(w) = d(m) + w(m,w); pred(w) = m

endif

endfor

endwhile

Step

Done

d(B)

d(C)

d(D)

d(E)

0

inf

inf

inf

inf

1

A

6

inf

1

inf

2

D

3

inf

1

2

3

E

3

7

1

2

4

1

2

3

1

2

B, E, C

Fringe (C newly added)

d(b) = 3, d(e) = 2

E’s not-done neighbors: B, C

Pred

Value

A

null

B

D

C

E

D

A

E

D

d(E) + w(E, B) is WORSE than d(B)

9 of 11

dijkstra(A)

Algorithm:

let s be the source vertex

done = {} Vertices with known optimal path from source

fringe = {} Vertices with unknown optimal path from source

for each vertex other than source:

d(v) = infinity

pred(v) = null

d(s) = 0 Optimal distance from source to parameter vertex

pred(s) = null

add s to fringe

while the fringe is not empty fringe = to-do list, not ideal yet

m = remove minimum distance vertex from the fringe

add m to done set

for each neighbor w of m not in the done set

if d(w) is infinity then

d(w) = d(m) + w(m,w)

add w to fringe

pred(w) = m

else if d(w) > (d(m) + w(m,w)) then

d(w) = d(m) + w(m,w); pred(w) = m

endif

endfor

endwhile

Step

Done

d(B)

d(C)

d(D)

d(E)

0

inf

inf

inf

inf

1

A

6

inf

1

inf

2

D

3

inf

1

2

3

E

3

7

1

2

4

B

3

7

1

2

3

3

1

2

B, C

Fringe

d(b) = 3, d(c) = 7

B’s not-done neighbors: C

Pred

Value

A

null

B

D

C

E

D

A

E

D

d(B) + w(B, C) is WORSE than d(B)

10 of 11

dijkstra(A)

Algorithm:

let s be the source vertex

done = {} Vertices with known optimal path from source

fringe = {} Vertices with unknown optimal path from source

for each vertex other than source:

d(v) = infinity

pred(v) = null

d(s) = 0 Optimal distance from source to parameter vertex

pred(s) = null

add s to fringe

while the fringe is not empty fringe = to-do list, not ideal yet

m = remove minimum distance vertex from the fringe

add m to done set

for each neighbor w of m not in the done set

if d(w) is infinity then

d(w) = d(m) + w(m,w)

add w to fringe

pred(w) = m

else if d(w) > (d(m) + w(m,w)) then

d(w) = d(m) + w(m,w); pred(w) = m

endif

endfor

endwhile

Step

Done

d(B)

d(C)

d(D)

d(E)

0

inf

inf

inf

inf

1

A

6

inf

1

inf

2

D

3

inf

1

2

3

E

3

7

1

2

4

B

3

7

1

2

3

C

3

7

1

2

C

Fringe

d(c) = 7

Every other vertex is done already

Pred

Value

A

null

B

D

C

E

D

A

E

D

11 of 11

dijkstra(A)

Find predecessor to v on ideal path:

return pred(v)

Find ideal distance from source -> vertex v:

return d(v)

Algorithm to find an ideal path:

v = target vertex (most vertex)

path = []

while v is not null:

add v to path

v = pred[v]

reverse path to start from source

This algorithm is similar to find in quick-union: we can work with pred and chase through.

Step

Done

d(B)

d(C)

d(D)

d(E)

0

inf

inf

inf

inf

1

A

6

inf

1

inf

2

D

3

inf

1

2

3

E

3

7

1

2

4

B

3

7

1

2

3

C

3

7

1

2

A -> D -> E -> C

Ideal path from A to C

Pred

Value

A

null

B

D

C

E

D

A

E

D