Shortest Path Problems: Dijkstra's Algorithm
Shortest Path Problems: Dijkstra's Algorithm
Dijkstras Algorithm
TAT 01 Michelle Wang
Introduction
Many problems can be modeled using
graphs with weights assigned to their
edges:
Wheres my motivation?
Fastest way to get to school by car
Finding the cheapest flight home
How much wood could a woodchuck
chuck if a woodchuck could chuck
wood?
In N Out
25
19
The Red
Garter
36
31
21
Setup:
G = weighted graph
In our version, need POSITIVE weights.
G is a simple connected graph.
Outline of Algorithm
Label a with 0 and all
others with .
L0(a) = 0 and L0(v) =
Labels are shortest
paths from a to vertices
Sk = the distinguished
set of vertices after k
iterations. S0 = . The
set Sk is formed by
adding a vertex u NOT
in Sk-1 with the smallest
label.
Once u is added to Sk
we update the labels of
all the vertices not in Sk
To update labels:
Lk(a, v) = min{Lk-1(a, v),
Lk-1(a, u) + w(u, v)}
25
19
b
16
31
i
36
21
25
L0(r) =
19
L0(b) =
16
31
L0(i) =
36
L0(c) =
21
25
L1(r) =
19
L1(b) =
16
31
L1(i) = 9
36
L1(c) =
21
25
L2(r) =
19
L2(b) = 19
16
31
L2(i) = 9
36
L2(c) =
21
S3 = {a, i, b, r}
L3(a) = 0
25
L3(r) = 24
19
L3(b) = 19
16
31
L3(i) = 9
36
L3(c) =
21
S4 = {a, i, b, r, c}
L4(a) = 0
25
L4(r) = 24
19
L4(b) = 19
16
31
L4(i) = 9
36
L4(c) = 45
21
Remarks
Implementing this algorithm as a computer
program, it uses O(n2) operations [additions,
comparisons]
Other algorithms exist that account for
negative weights
Dijkstras algorithm is a single source one.
Floyds algorithm solves for the shortest path
among all pairs of vertices.
Endnotes 1
Endnotes 2