Shortest Path Dijkstra 1
Shortest Path Dijkstra 1
A A
450
60 190
C unweighted C weighted
graph graph
200 130
D D
E E
Example
Source node-A
4
Destination node-F
A B 9
5
5
8 E F
6 2
3
C 2
D
10
Steps:
⮚Find the shortest path between two vertices in a graph
⮚Calculate all vertices from the graph
6
Example Cont.
4
A B 9
5
5 8 E 2 F
6 3
C D
2
10
Path Weight Result
A-B-E-F 4+9+2 15
A-B-D-E-F 4+8+3+2 17 Finally the minimum
A-B-C-D-E-F 4+6+2+3+2 17 shortest path,
A-B-C-D-B-E-F 4+6+2+8+9+2 31
A-C-D-E-F 5+2+3+2 12 A-D-E-F = 10
A-C-F 5+10 15
A-D-E-F 5+3+2 10
A-D-B-E-F 5+8+9+2 24
A-C-D-B-E-F 5+2+8+9+2 26
7
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)
5
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
1
6 1
y z
7
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
5 3
• π[v] = predecessor of v on a shortest 5
1
6 1
path from s y z
9
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
10
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
11
Dijkstra’s Algorithm
• Single-source shortest path problem:
– No negative-weight edges: w(u, v) > 0 ∀ (u, v) ∈ E
3. Q ← V[G] 5 7
∞ ∞
4. while Q ≠ ∅ y
2
z
t x
5. do u ← EXTRACT-MIN(Q) 1
∞
1
∞
0 9
10
6. S ← S ∪ {u}
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
13
Example
t 1 x t 1 x
1 1 11
8 ∞ 8
0 4 43
10 9 10 9
2 3 4 6 2 3 4 6
s 0 s 0
5 7 5 7
5 7
∞ 5 7
2 2
y z y z
t x t 1 x
1
1 8 9
8 9
3 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
14
Another Example
Another Example
Another Example
Another Example
Another Example
Another Example
Another Example
Another Example
Another Example
Exercise 1
3 3 5
∞ ∞
Initialize
3 3 5
∞ ∞
4
Choose Minimum Temporary Label
2 ∞
2 4 4
2 2
0
1 2 6 ∞
1 3
4 2
3 3 5
4 ∞
Update Step
6
2 ∞
2 4 4
2 2
0
1 2 6 ∞
1 3
4 2
3 3 5
4 ∞
3 4
The predecessor of
node 3 is now node 2
Choose Minimum Temporary Label
2 6
2 4 4
2 2
0
1 2 6 ∞
1 3
4 2
3 3 5
3 4
Update
2 6
2 4 4
2 2
0
1 2 6 ∞
1 3
4 2
3 3 5
3 4
3 3 5
3 4
Update
2 6
2 4 4
2 2
0
6
1 2 6 ∞
1 3
4 2
3 3 5
3 4
3 3 5
3 4
Update
2 6
2 4 4
2 2
0
1 2 6 6
1 3
4 2
3 3 5
3 4
3 3 5
3 4
3 3 5
3 4
relaxing
edges
DIJKSTRA'S ALGORITHM - WHY USE IT?
6. S ← S ∪ {u}
7. for each vertex v ∈ Adj[u] O(E) times
8. do RELAX(u, v, w) (total) O(ElgV)
9. Update Q (DECREASE_KEY) O(lgV)
43
Question
• How to solve ACM534 – Frogger?
44