0% found this document useful (0 votes)
43 views39 pages

CSE245 - Algorithms: Single Source Shortest Path (Dijkstra's Algorithm)

Dijkstra's algorithm is used to find the shortest path between a single source vertex to all other vertices in a weighted graph. It maintains two sets - S, the set of vertices whose shortest path is known, and Q, the set of remaining vertices. It repeatedly extracts the vertex with the minimum shortest path estimate from Q, relaxes its edges to update path lengths, and adds it to S. This continues until Q is empty, at which point shortest paths are known from the source to all vertices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views39 pages

CSE245 - Algorithms: Single Source Shortest Path (Dijkstra's Algorithm)

Dijkstra's algorithm is used to find the shortest path between a single source vertex to all other vertices in a weighted graph. It maintains two sets - S, the set of vertices whose shortest path is known, and Q, the set of remaining vertices. It repeatedly extracts the vertex with the minimum shortest path estimate from Q, relaxes its edges to update path lengths, and adds it to S. This continues until Q is empty, at which point shortest paths are known from the source to all vertices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

CSE245 – Algorithms

Single Source Shortest Path


(Dijkstra’s Algorithm)
Shortest Path Problems
• What is shortest path ?
 shortest length between two vertices for an
unweighted graph:
 smallest cost between two vertices for a weighted
graph:
B 210 B

A A
450
60 190

C unweighted C weighted
graph graph
200 130
D D
E E
Shortest Path Problems
• How can we find the shortest route between two
points on a map?
• Model the problem as a graph problem:
– Road map is a weighted graph:
vertices = cities
edges = road segments between cities
edge weights = road distances
– Goal: find a shortest path between two vertices (cities)

3
Shortest Path Problems
• Input: t x
6
3 9
– Directed graph G = (V, E) 3
4
2 1
– Weight function w : E → R s 0
2 7
5 3
• Weight of path p = v0, v1, . . . , vk 5 11
k 6
w( p )   w( vi 1 , vi ) y z
i 1

• Shortest-path weight from u to v:


p
δ(u, v) = min w(p) : u v if there exists a path from u to v
∞ otherwise
• Shortest path u to v is any path p such that w(p) = δ(u, v)
4
Variants of Shortest Paths
• Single-source shortest path
– G = (V, E)  find a shortest path from a given source vertex s to
each vertex v  V
• Single-destination shortest path
– Find a shortest path to a given destination vertex t from each
vertex v
– Reverse the direction of each edge  single-source
• Single-pair shortest path
– Find a shortest path from u to v for given vertices u and v
– Solve the single-source problem
• All-pairs shortest-paths
– Find a shortest path from u to v for every pair of vertices u and v

5
Optimal Substructure of Shortest Paths

Given: vj
pij pjk
– A weighted, directed graph G = (V, E) v1
p1i
– A weight function w: E  R, pij’ vk
– A shortest path p = v1, v2, . . . , vk from v1 to vk vi

– A subpath of p: pij = vi, vi+1, . . . , vj, with 1  i  j  k

Then: pij is a shortest path from vi to vj


p1i pij pjk
Proof: p = v1 vi vj vk
w(p) = w(p1i) + w(pij) + w(pjk)
Assume  pij’ from vi to vj with w(pij’) < w(pij)
 w(p’) = w(p1i) + w(pij’) + w(pjk) < w(p) contradiction! 6
Shortest-Path Representation
For each vertex v  V:
t x
• d[v] = δ(s, v): a shortest-path estimate 3
6
9
– Initially, d[v]=∞ 3
4
2 1
– Reduces as algorithms progress s 0
2 7
3
• [v] = predecessor of v on a shortest 5
5 11
6
path from s y z
– If no predecessor, [v] = NIL
–  induces a tree—shortest-path tree
• Shortest paths & shortest path trees are
not unique
7
Initialization
Alg.: INITIALIZE-SINGLE-SOURCE(V, s)
1. for each v  V
2. do d[v] ← 
3. [v] ← NIL
4. d[s] ← 0

• All the shortest-paths algorithms start with


INITIALIZE-SINGLE-SOURCE

8
Relaxation
• Relaxing an edge (u, v) = testing whether we
can improve the shortest path to v found so far
by going through u
If d[v] > d[u] + w(u, v)
we can improve the shortest path to v
 update d[v] and [v]
s s
u v u v
5
2
9
2 After relaxation:
5 6
d[v]  d[u] + w(u, v)
RELAX(u, v, w) RELAX(u, v, w)

u v u v
2 2
5 7 5 6
9
RELAX(u, v, w)
1. if d[v] > d[u] + w(u, v)
2. then d[v] ← d[u] + w(u, v)
3. [v] ← u

• All the single-source shortest-paths algorithms


– start by calling INIT-SINGLE-SOURCE
– then relax edges
• The algorithms differ in the order and how
many times they relax each edge

10
Dijkstra’s Algorithm
• Single-source shortest path problem:
– No negative-weight edges: w(u, v) > 0  (u, v)  E
• Maintains two sets of vertices:
– S = vertices whose final shortest-path weights have
already been determined
– Q = vertices in V – S: min-priority queue
• Keys in Q are estimates of shortest-path weights (d[v])

• Repeatedly select a vertex u  V – S, with the


minimum shortest-path estimate d[v]
11
Dijkstra (G, w, s)
t 1 x
1. INITIALIZE-SINGLE-SOURCE(V, s)  
10 9
2. S ←  s 0
2 3 4 6

3. Q ← V[G] 5 7
 
4. while Q   y
2
z

do u ← EXTRACT-MIN(Q)
t x
5. 
10
1

6. S ← S  {u} 10 9
2 3 4 6
s 0
7. for each vertex v  Adj[u] 5 7

8. do RELAX(u, v, w) 
5 2

y z

12
Example
t 1 x t 1 x
8
10 
14 8 13
14
10 9 10 9
2 3 4 6 2 3 4 6
s 0 s 0
5 7 5 7
5 
7 5
2
7
2
y z y z

t x t 1 x
1
8 13
9 8 9
10 9 10 9

2 4 2 3 4 6
s 0 3 6 s 0

7 5 7
5
5 7 5 7
2 2
y z y z
13
Dijkstra's Shortest Path Algorithm
• Find shortest path from s to t.

24
2 3
9

s
18
14
2 6
6
30 4 19
11
15 5
5
6
20 16

t
7 44

14
Dijkstra's Shortest Path Algorithm
S={ }
PQ = { s, 2, 3, 4, 5, 6, 7, t }



24
2 3
0 9

s
18
14  2 6
6 
30  4 19
11
15 5
5
6
20 16

t
7 44
distance
15 label  
Dijkstra's Shortest Path Algorithm
S={ }
PQ = { s, 2, 3, 4, 5, 6, 7, t }

delmin


24
2 3
0 9

s
18
14  2 6
6 
30  4 19
11
15 5
5
6
20 16

t
7 44
distance
16 label  
Dijkstra's Shortest Path Algorithm
S={s}
PQ = { 2, 3, 4, 5, 6, 7, t }
decrease key



X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
30  4 19
11
15 5
5
6
20 16

t
7 44
distance
17 label  15
X 
Dijkstra's Shortest Path Algorithm
S={s}
PQ = { 2, 3, 4, 5, 6, 7, t }

delmin


X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
30  4 19
11
15 5
5
6
20 16

t
7 44
distance
18 label  15
X 
Dijkstra's Shortest Path Algorithm
S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }



X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
30  4 19
11
15 5
5
6
20 16

t
7 44

19  15
X 
Dijkstra's Shortest Path Algorithm
S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }
decrease key

 33
X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
30  4 19
11
15 5
5
6
20 16

t
7 44

20  15
X 
Dijkstra's Shortest Path Algorithm
S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }

 33
X

X 9
24
2 3
0 9
delmin
s
18
14 
X 14
2 6
6 
30  4 19
11
15 5
5
6
20 16

t
7 44

21  15
X 
Dijkstra's Shortest Path Algorithm
S = { s, 2, 6 }
PQ = { 3, 4, 5, 7, t }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
44
30 
X 4 19
11
15 5
5
6
20 16

t
7 44

22  15
X 
Dijkstra's Shortest Path Algorithm
S = { s, 2, 6, 7 }
PQ = { 3, 4, 5, t }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
44
30 
X 4 19
11
15 5
5
6
20 16

t
7 44

23  15
X delmin 
Dijkstra's Shortest Path Algorithm
S = { s, 2, 6, 7 }
PQ = { 3, 4, 5, t }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
X 35
44
30 
X 4 19
11
15 5
5
6
20 16

t
7 44

24  15
X 
59 X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 6, 7 } delmin
PQ = { 3, 4, 5, t }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
X 35
44
30 
X 4 19
11
15 5
5
6
20 16

t
7 44

25  15
X 
59 X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 6, 7 }
PQ = { 4, 5, t }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
X 35
44 X 34
30 
X 4 19
11
15 5
5
6
20 16

t
7 44

26  15
X 51 59
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 6, 7 }
PQ = { 4, 5, t }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
X 35
44 X 34
30 
X 4 19
11
15 5
5
6
20 16
delmin

t
7 44

27  15
X 51 59
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 5, 6, 7 }
PQ = { 4, t }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
45 X
X 35
44 X 34
30 
X 4 19
11
15 5
5
6
20 16

t
7 44

28  15
X 50 51
X 59 
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 5, 6, 7 }
PQ = { 4, t }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
45 X
X 35
44 X 34
30 
X 4 19
11
15 5 delmin
5
6
20 16

t
7 44

29  15
X 50 51
X 59 
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7 }
PQ = { t }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
45 X
X 35
44 X 34
30 
X 4 19
11
15 5
5
6
20 16

t
7 44

30  15
X 50 51
X 59 
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7 }
PQ = { t }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
45 X
X 35
44 X 34
30 
X 4 19
11
15 5
5
6
20 16

t
7 44

31  15
X
delmin 50 51
X 59 
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7, t }
PQ = { }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
45 X
X 35
44 X 34
30 
X 4 19
11
15 5
5
6
20 16

t
7 44

32  15
X 50 51
X 59 
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7, t }
PQ = { }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
45 X
X 35
44 X 34
30 
X 4 19
11
15 5
5
6
20 16

t
7 44

33  15
X 50 51
X 59 
X X
Dijkstra’s Pseudo Code
• Graph G, weight function w, root s

relaxing
edges
Dijkstra (G, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s) (V)
2. S ← 
3. Q ← V[G] O(V) build min-heap
4. while Q   Executed O(V) times
5. do u ← EXTRACT-MIN(Q) O(lgV)
6. S ← S  {u}
7. for each vertex v  Adj[u]
8. do RELAX(u, v, w) O(E) times; O(lgV)
Running time: O(VlgV + ElgV) = O(ElgV)
35
Dijkstra’s Running Time

• Extract-Min executed |V| time


• Decrease-Key executed |E| time
• Time = |V| TExtract-Min + |E| TDecrease-Key
• T depends on different Q implementations

Q T(Extract T(Decrease- Total


-Min) Key)
array O(V) O(1) O(V 2)
binary heap O(lg V) O(lg V) O(E lg V)
Fibonacci heap O(lg V) O(1) (amort.) O(V lgV + E)
Applications of Dijkstra's Algorithm
One particularly relevant this week:
epidemiology

Prof. Lauren Meyers (MIT, Biology


Dept.) uses networks to model the
spread of infectious diseases and
design prevention and response
strategies.

Vertices represent individuals, and


edges their possible contacts. It is
useful to calculate how a particular
individual is connected to others.

Knowing the shortest path lengths


to other individuals can be a relevant
indicator of the potential of a
particular individual to infect others.
Question
• Prove that, if there exists negative edge, dijkstra’s
shortest path algorithm may fail to find the shortest path
• In Dijkstra's algorithm, once a vertex is marked as
"closed" (and out of the open set) - the algorithm
found the shortest path to it, and will never have to
develop this node again - it assumes the path developed
to this path is the shortest.
• But with negative weights
it might not be true.
For example: Dijkstra from
A will first develop C,
and will later fail to find
A->B->C 38
Question
• Print the shortest path for dijkstra’s algorithm

• void printD() {
• int i;
• for (i = 1; i <= n; ++i)
• printf("%10d", i);
• printf("\n");
• for (i = 1; i <= n; ++i) {
• printf("%10ld", d[i]);
• }
• printf("\n");
• }
39

You might also like