0% found this document useful (0 votes)
78 views30 pages

Dijkstra's Shortest Path Algorithm: Outline of This Lecture

The document outlines Dijkstra's algorithm for finding the shortest paths from a single source vertex to all other vertices in a weighted graph, explaining that it works by maintaining estimates of shortest path distances from the source, relaxing edges to improve estimates, and selecting the unprocessed vertex with the smallest estimate to continue relaxing edges.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views30 pages

Dijkstra's Shortest Path Algorithm: Outline of This Lecture

The document outlines Dijkstra's algorithm for finding the shortest paths from a single source vertex to all other vertices in a weighted graph, explaining that it works by maintaining estimates of shortest path distances from the source, relaxing edges to improve estimates, and selecting the unprocessed vertex with the smallest estimate to continue relaxing edges.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Dijkstra’s Shortest Path Algorithm

DPV 4.4, CLRS 24.3


Revised, September 23, 2016

Outline of this Lecture

• Recalling the BFS solution of the shortest path


problem for unweighted (di)graphs.

• The shortest path problem for weighted digraphs.

• Dijkstra’s algorithm.
Given for digraphs but easily modified to work on
undirected graphs.

1
Recall: Shortest Path Problem for Graphs

Let G = (V, E) be a (di)graph.

• The shortest path between two vertices is a path


with the shortest length (least number of edges).
Call this the link-distance.

• Breadth-first-search is an algorithm for finding short-


est (link-distance) paths from a single source ver-
tex to all other vertices.

• BFS processes vertices in increasing order of their


distance from the root vertex.

• BFS has running time O(|V | + |E|).

2
Shortest Path Problem for Weighted Graphs

Let G = (V, E) be a weighted digraph, with weight


function w : E 7→ R mapping edges to real-valued
weights. If e = (u, v), we write w(u, v) for w(e).

• The length of a path p = hv0, v1, ..., vk i is the


sum of the weights of its constituent edges:
k
X
length(p) = w(vi−1, vi).
i=1

• The distance from u to v, denoted δ(u, v), is the


length of the minimum length path if there is a
path from u to v; and is ∞ otherwise.


a 5 - 
d

Q 8
 length(ha, b, c, ei) = 6
Q 
Qs
c distance from a to e is 6
2 Q
*
 H
4
  HH
j
?
b 1 3 
? H

e
3
Single-Source Shortest-Paths Problem

The Problem: Given a digraph with positive edge


weights G = (V, E)
and a distinguished source vertex, s ∈ V ,
determine the distance and a shortest path from the
source vertex to every vertex in the digraph.

Question: How do you design an efficient algorithm


for this problem?

4
Single-Source Shortest-Paths Problem

Important Observation: Any subpath of a shortest


path must also be a shortest path. Why?

Example: In the following digraph, ha, b, c, ei is a short-


est path. The subpath ha, b, ci is also a shortest path.


a 5 - 
d

Q 8
 length(ha, b, c, ei) = 6
Q 
Qs
c distance from a to e is 6
2 Q
*
 H
4

 j
HH
? ?
b 1 3 
H

e

Observation Extending this idea we observe the ex-


istence of a shortest path tree in which distance from
source to vertex v is length of shortest path from source
to vertex in original tree.

5
Intuition behind Dijkstra’s Algorithm

• Report the vertices in increasing order of their dis-


tance from the source vertex.

• Construct the shortest path tree edge by edge; at


each step adding one new edge, corresponding
to construction of shortest path to the current new
vertex.

6
The Rough Idea of Dijkstra’s Algorithm

• Maintain an estimate d[v] of the length δ(s, v) of


the shortest path for each vertex v.

• Always d[v] ≥ δ(s, v) and d[v] equals the length


of a known path
(d[v] = ∞ if we have no paths so far).

• Initially d[s] = 0 and all the other d[v] values are


set to ∞. The algorithm will then process the ver-
tices one by one in some order.
The processed vertex’s estimate will be validated
as being real shortest distance, i.e. d[v] = δ(s, v).

Here “processing a vertex u” means finding new


paths and updating d[v] for all v ∈ Adj[u] if nec-
essary. The process by which an estimate is up-
dated is called relaxation.

When all vertices have been processed,


d[v] = δ(s, v) for all v.

7
The Rough Idea of Dijkstra’s Algorithm

Question 1: How does the algorithm find new paths


and do the relaxation?

Question 2: In which order does the algorithm pro-


cess the vertices one by one?

8
Answer to Question 1

• Finding new paths. When processing a vertex u,


the algorithm will examine all vertices v ∈ Adj[u].
For each vertex v ∈ Adj[u], a new path from s to
v is found (path from s to u + new edge).

• Relaxation. If the new path from s to v is shorter


than d[v], then update d[v] to the length of this
new path.

Remark: Whenever we set d[v] to a finite value, there


exists a path of that length. Therefore d[v] ≥ δ(s, v).

(Note: If d[v] = δ(s, v), then further relaxations cannot change


its value.)

9
Implementing the Idea of Relaxation
Consider an edge from a vertex u to v whose weight is w(u, v).
Suppose that we have already processed u so that we know
d[u] = δ(s, u) and also computed a current estimate for d[v].
Then

• There is a (shortest) path from s to u with length d[u].

• There is a path from s to v with length d[v].

Combining this path from s to u with the edge (u, v), we obtain
another path from s to v with length d[u] + w(u, v).

If d[u]+w(u, v) < d[v], then we replace the old path hs, . . . , w, vi


with the new shorter path hs, . . . , u, vi. Hence we update

• d[v] = d[u] + w(u, v)

• pred[v] = u (originally, pred[v] == w).

s w d[v]
v
u
d[u]

10
The Algorithm for Relaxing an Edge

Relax(u,v)
{
if (d[u] + w(u, v) < d[v])
{ d[v] = d[u] + w(u, v);
pred[v] = u;
}
}

Remark: The predecessor pointer pred[ ] is for deter-


mining the shortest paths.

11
Idea of Dijkstra’s Algorithm: Repeated Relaxation

• Dijkstra’s algorithm operates by maintaining a sub-


set of vertices, S ⊆ V , for which we know the true
distance, that is d[v] = δ(s, v).

• Initially S = ∅, the empty set, and we set d[s] =


0 and d[v] = ∞ for all others vertices v. One by
one we select vertices from V \ S to add to S.

• The set S can be implemented using an array of


vertex colors. Initially all vertices are white, and
we set color[v] = black to indicate that v ∈ S.

12
The Selection in Dijkstra’s Algorithm

Recall Question 2: What is the best order in which


to process vertices, so that the estimates are guaran-
teed to converge to the true distances.
That is, how does the algorithm select which vertex
among the vertices of V \ S to process next?

Answer: We use a greedy algorithm. For each ver-


tex in u ∈ V \ S, we have computed a distance es-
timate d[u]. The next vertex processed is always a
vertex u ∈ V \ S for which d[u] is minimum, that is,
we take the unprocessed vertex that is closest (by our
estimate) to s.

Question: How do we implement this selection of ver-


tices efficiently?

13
The Selection in Dijkstra’s Algorithm

Question: How do we perform this selection efficiently?

Answer: We store the vertices of V \ S in a priority


queue, where the key value of each vertex v is d[v].

[Note: if we implement the priority queue using a heap,


we can perform the operations Insert(), Extract Min(),
and Decrease Key(), each in O(log n) time.]

14
Review of Priority Queues

A Priority Queue is a data structure (can be imple-


mented as a heap) which supports the following oper-
ations:

insert(u, key): Insert u with the key value key in Q.

u = extractMin(): Extract the item with the minimum


key value in Q.

decreaseKey(u, new-key): Decrease u’s key value to


new-key.

Remark: Priority Queues can be implemented such that each


operation takes time O(log |Q|).
In Class we only saw insert, extractMin and Delete. DecreaseKey
can either be implemented directly in O(log |Q|) time or could be
implemented by a delete followed by an insert.
15
Description of Dijkstra’s Algorithm
Dijkstra(G,w,s)
{ % Initialize
for (each u ∈ V )
{
d[u] = ∞;
color[u] =white;
}
d[s] = 0;
pred[s] = NIL;
Q = (queue with all vertices);

while (Non-Empty(Q)) % Process all vertices


{
u = Extract-Min(Q); % Find new vertex
for (each v ∈ Adj[u])
if (d[u] + w(u, v) < d[v]) % If estimate improves
{
d[v] = d[u] + w(u, v); relax
Decrease-Key(Q, v, d[v]);
pred[v] = u;
}
color[u] =black;
}
}

16
Dijkstra’s Algorithm

Example:

b inf 1 inf c
7
8
0 3 2
4 5
s
2
inf inf
a 5 d

Step 0: Initialization.

v s a b c d
d[v] 0 ∞ ∞ ∞ ∞
pred[v] nil nil nil nil nil
color[v] W W W W W

v s a b c d
Priority Queue:
d[v] 0 ∞ ∞ ∞ ∞
17
Dijkstra’s Algorithm

Example:

b 7 1 inf c
7
8
0 3 2
4 5
s
2
2 inf
a 5 d

Step 1: As Adj[s] = {a, b}, work on a and b and


update information.

v s a b c d
d[v] 0 2 7 ∞ ∞
pred[v] nil s s nil nil
color[v] B W W W W

v a b c d
Priority Queue:
d[v] 2 7 ∞ ∞
18
Dijkstra’s Algorithm

Example:

b 5 1 10 c
7
8
0 3 2
4 5
s
2
2 7
a 5 d

Step 2: After Step 1, a has the minimum key in the


priority queue. As Adj[a] = {b, c, d}, work on b, c, d
and update information.

v s a b c d
d[v] 0 2 5 10 7
pred[v] nil s a a a
color[v] B B W W W

v b c d
Priority Queue:
d[v] 5 10 7
19
Dijkstra’s Algorithm

Example:

b 5 1 6 c
7
8
0 3 2
4 5
s
2
2 7
a 5 d

Step 3: After Step 2, b has the minimum key in the


priority queue. As Adj[b] = {a, c}, work on a, c and
update information.

v s a b c d
d[v] 0 2 5 6 7
pred[v] nil s a b a
color[v] B B B W W

v c d
Priority Queue:
d[v] 6 7
20
Dijkstra’s Algorithm

Example:

b 5 1 6 c
7
8
0 3 2
4 5
s
2
2 7
a 5 d

Step 4: After Step 3, c has the minimum key in the pri-


ority queue. As Adj[c] = {d}, work on d and update
information.

v s a b c d
d[v] 0 2 5 6 7
pred[v] nil s a b a
color[v] B B B B W

v d
Priority Queue:
d[v] 7
21
Dijkstra’s Algorithm

Example:

b 5 1 6 c
7
8
0 3 2
4 5
s
2
2 7
a 5 d

Step 5: After Step 4, d has the minimum key in the pri-


ority queue. As Adj[d] = {c}, work on c and update
information.

v s a b c d
d[v] 0 2 5 6 7
pred[v] nil s a b a
color[v] B B B B B

Priority Queue: Q = ∅.

We are done.
22
Dijkstra’s Algorithm
Shortest Path Tree: T = (V, A), where
A = {(pred[v], v)|v ∈ V \ {s}}.
The array pred[v] is used to build the tree.

b 5 1 6 c

0 3
s
2
2 7
a 5 d
Example:

v s a b c d
d[v] 0 2 5 6 7
pred[v] nil s a b a

23
Correctness of Dijkstra’s Algorithm
Lemma: When a vertex u is added to S (i.e., dequeued from the
queue), d[u] = δ(s, u).

Proof: Suppose to the contrary that at some point Dijkstra’s al-


gorithm first attempts to add a vertex u to S for which d[u] 6=
δ(s, u). By our observations about relaxation, d[u] > δ(s, u).

Consider the situation just prior to the insertion of u. Consider


the true shortest path from s to u. Because s ∈ S and u ∈ V \ S,
at some point this path must first take a jump out of S. Let (x, y)
be the edge taken by the path, where x ∈ S and y ∈ V \ S (it
may be that x = s and/or y = u).
S u

y
x

24
Correctness of Dijkstra’s Algorithm – Continued
We now prove that d[y] = δ(s, y). We have done relaxation
when processing x, so
d[y] ≤ d[x] + w(x, y). (1)
Since x is added to S earlier, by hypothesis,
d[x] = δ(s, x). (2)
Since hs, . . . , x, yi is subpath of a shortest path, by (2)
δ(s, y) = δ(s, x) + w(x, y) = d[x] + w(x, y). (3)
By (1) and (3),
d[y] ≤ δ(s, y).
Hence
d[y] = δ(s, y).
So y 6= u (because we suppose d[u] > δ(s, u)).

Now observe that since y appears midway on the path from s to


u, and all subsequent edges are positive, we have
δ(s, y) < δ(s, u), and thus
d[y] = δ(s, y) < δ(s, u) ≤ d[u].
Thus y would have been added to S before u, in contradiction to
our assumption that u is the next vertex to be added to S.

25
Proof of the Correctness of Dijkstra’s Algorithm

• By the lemma, d[v] = δ(s, v) when v is added


into S, that is when we set color[v] = black.

• At the end of the algorithm, all vertices are in S,


then all distance estimates are correct.

26
Analysis of Dijkstra’s Algorithm:

Insert: Performed once for each vertex. n times. .

Non-Empty(): Called one for each vertex. n times.


Each time does one Extract-Min() and one color
setting

inner loop for (each v ∈ Adj[u]) :


is called once for each of the m edges in the graph.
O(m) times.
Each call of the inner loop does O(1) work plus, pos-
sibly, one Decrease-Key operation.

Recall that all of the priority queue operations require


O(log |Q|) = O(log n) time
we have that the algorithm uses

nO(log n)+nO(log n)+O(e log n) = O((n+e) log n)


time.
27
Further Analysis of Dijkstra’s Algorithm:

Dijkstra’s original implementation did not use a prior-


ity queue but, instead, ran through the entire list of
white vertices at each step, finding the one with small-
est weight. Each run through requires O(n) time and
there are only n steps so his implementation runs in
O(n2) time.

This is slightly better than the priority queue based


algorithm for dense graphs but much worse for sparse
ones.

28
Further Improvements to Dijkstra’s Algorithm:

A more advanced pririty queue data structure called a


Fibonacci Heap implements

• Insert:in O(1) time

• Extract-Min(): O(log n) time

• Decrease-Key: O(1) (amortized) time.

This reduces the running time down to

nO(log n) + nO(log n) + O(e) = O((n log n + e))


time.

This is a huge improvement for dense graphs

29
Prove: Dijkstra’s algorithm processes vertices in non-
decreasing order of their actual distance from the source
vertex.

30

You might also like