COMP2521 Week 6 - Dijkstra's Algorithm
COMP2521 Week 6 - Dijkstra's Algorithm
23T3
Algorithm
Pseudocode
Example
Path Finding
Vertex Set
COMP2521 23T3
Analysis
Dijkstra’s Algorithm
Other
Algorithms
shortest path
dijkstra’s algorithm
COMP2521
23T3 Shortest Path
Algorithm
Pseudocode
Pseudocode
Example
Path Finding
Vertex Set
Variations on shortest path problem:
Analysis
• Source-target shortest path
Other
Algorithms • Shortest path from source vertex s to target vertex t
Appendix
• Single-source shortest path
• Shortest path from source vertex s to all other vertices
• All-pairs shortest path
• Shortest path between all pairs of source and target vertices
COMP2521
23T3 Shortest Path
Algorithm
Pseudocode
Example
1 2
2 2
0 1
5
COMP2521
23T3 Dijkstra’s Algorithm
Algorithm
Pseudocode
Example
Path Finding
Vertex Set
Analysis
Other
Algorithms Dijkstra’s algorithm
Appendix is used to find the shortest path
in a weighted graph with non-negative weights
COMP2521
23T3 Data Structures
Algorithm
Edge relaxation
Pseudocode
Example
Path Finding
Data structures used in Dijkstra’s algorithm:
Vertex Set • Distance array (dist)
Analysis • To keep track of shortest currently known distance to each vertex
Other
Algorithms
• Predecessor array (pred)
Appendix
• Same purpose as in BFS/DFS
• To keep track of the predecessor of each vertex on the shortest currently
known path to that vertex
• Used to construct the shortest path
• Set of vertices
• Stores unexplored vertices
COMP2521
23T3 Algorithm
Algorithm
Edge relaxation
Pseudocode
COMP2521
23T3 Edge Relaxation
Algorithm
Edge relaxation
Pseudocode
Example
Path Finding
Vertex Set
Other
Algorithms
Appendix
• dist[w] - length of shortest known path from s to w
dist[w]
s w
In edge relaxation, we take the shortest known path from s to v and extend it
using edge (v, w, weight) to create a new path from s to w.
dist[v] weight
s v w
COMP2521
23T3 Edge Relaxation
Algorithm
Edge relaxation
Analysis
Other
Algorithms • New path via v:
Appendix
dist[v] weight
s v w
Example i
... [v] ... [w]
Path Finding
dist[w] = 12 w
Vertex Set
dist ... 8 ... 12
Analysis
s
Other 3 ... ... ...
Algorithms dist[v] = 8 pred i
Appendix v
COMP2521
23T3 Edge Relaxation
Algorithm
Edge relaxation Before relaxation along (v, w, 3)
Pseudocode
Example i
... [v] ... [w]
Path Finding
dist[w] = 12 w
Vertex Set
dist ... 8 ... 12
Analysis
s
Other 3 ... ... ...
Algorithms dist[v] = 8 pred i
Appendix v
i
... [v] ... [w]
w
dist[w] = 11 dist ... 8 ... 11
s
3 ... ... ... v
dist[v] = 8 pred
v
COMP2521
23T3 Pseudocode
Algorithm
Pseudocode
Example
Other
create dist array, initialised to ∞
Algorithms create pred array, initialised to -1
Appendix create vSet containing all vertices of G
dist[src] = 0
while vSet is not empty:
find vertex v in vSet such that dist[v] is minimal
remove v from vSet
for each edge (v, w, weight) in G:
relax along (v, w weight)
COMP2521
23T3 Example
Algorithm
Pseudocode
Example
Path Finding
Dijkstra’s algorithm starting at 0
Vertex Set
5
Analysis
1 4
Other
Algorithms
14
Appendix 4 8
9 3
0 2 5
10
7 15
3
COMP2521
23T3 Example
Algorithm
dist 0 ∞ ∞ ∞ ∞ ∞
pred -1 -1 -1 -1 -1 -1
COMP2521
23T3 Example
Algorithm
dist 0 14 9 7 ∞ ∞
pred -1 0 0 0 -1 -1
COMP2521
23T3 Example
Algorithm
dist 0 14 9 7 ∞ 22
pred -1 0 0 0 -1 3
COMP2521
23T3 Example
Algorithm
dist 0 13 9 7 ∞ 12
pred -1 2 0 0 -1 2
COMP2521
23T3 Example
Algorithm
dist 0 13 9 7 20 12
pred -1 2 0 0 5 2
COMP2521
23T3 Example
Algorithm
dist 0 13 9 7 18 12
pred -1 2 0 0 1 2
COMP2521
23T3 Example
Algorithm
dist 0 13 9 7 18 12
pred -1 2 0 0 1 2
COMP2521
23T3 Example
Algorithm
dist 0 13 9 7 18 12
pred -1 2 0 0 1 2
COMP2521
23T3 Path Finding
Algorithm
Pseudocode
Example
Path Finding
Example
Vertex Set
Analysis
Other The shortest path from the source vertex to any other vertex
Algorithms
Appendix
can be constructed by tracing backwards through the predecessor array
(like for BFS)
COMP2521
23T3 Path Finding
Algorithm
Pseudocode
Example: Shortest path from 0 to 4
0 4
Example
Path Finding
Example
Vertex Set 5
1 4
Analysis
Other 14
Algorithms 4 8
Appendix
9 3
0 2 5
10
7 15
3
Pseudocode
Example: Shortest path from 0 to 4
0 4
Example
Path Finding
Example
Vertex Set 5
1 4
Analysis
Other 14
Algorithms 4 8
Appendix
9 3
0 2 5
10
7 15
3
COMP2521
23T3 Path Finding
Algorithm
Pseudocode
Example: Shortest path from 0 to 4
0 1 −→ 4
Example
Path Finding
Example
Vertex Set 5
1 4
Analysis
Other 14
Algorithms 4 8
Appendix
9 3
0 2 5
10
7 15
3
Pseudocode
Example: Shortest path from 0 to 4
0 1 −→ 4
Example
Path Finding
Example
Vertex Set 5
1 4
Analysis
Other 14
Algorithms 4 8
Appendix
9 3
0 2 5
10
7 15
3
COMP2521
23T3 Path Finding
Algorithm
Pseudocode
Example: Shortest path from 0 to 4
0 2 −→ 1 −→ 4
Example
Path Finding
Example
Vertex Set 5
1 4
Analysis
Other 14
Algorithms 4 8
Appendix
9 3
0 2 5
10
7 15
3
Pseudocode
Example: Shortest path from 0 to 4
0 2 −→ 1 −→ 4
Example
Path Finding
Example
Vertex Set 5
1 4
Analysis
Other 14
Algorithms 4 8
Appendix
9 3
0 2 5
10
7 15
3
COMP2521
23T3 Path Finding
Algorithm
Pseudocode
Example: Shortest path from 0 to 4
0 −→ 2 −→ 1 −→ 4
Example
Path Finding
Example
Vertex Set 5
1 4
Analysis
Other 14
Algorithms 4 8
Appendix
9 3
0 2 5
10
7 15
3
Pseudocode
Example
Path Finding
Example
Vertex Set
Analysis
How to find shortest path between two other vertices
Other
Algorithms (neither of which are the source vertex)?
Appendix
Generally, you will need to rerun Dijkstra’s algorithm from one of these
vertices.
COMP2521
23T3 Vertex Set
Algorithm
Pseudocode
Example
Path Finding
Vertex Set
Analysis
How to implement the vSet?
Other
Different methods...
Algorithms
3. Priority queue
COMP2521
23T3 Vertex Set
Visited array implementation
Algorithm
Pseudocode
Example
Path Finding
Vertex Set
COMP2521
23T3 Vertex Set
Array/list of vertices implementation
Algorithm
Pseudocode
Example
Path Finding
Vertex Set
Analysis
Other
Array/list of vertices implementation:
Algorithms
• Store all vertices in an array/linked list
Appendix
• After exploring vertex v, remove v from array/linked list
• At the start of each iteration, find vertex in array/list such that dist[v] is
minimal ⇒ O(V )
COMP2521
23T3 Vertex Set
Priority queue implementation
Algorithm
Pseudocode
Example
COMP2521
23T3 Analysis
Correctness
Algorithm
Pseudocode
Example
Path Finding
Other
Algorithms
1 For all explored nodes s, dist[s] is shortest distance from source to s
Appendix 2 For all unexplored nodes t, dist[t] is shortest distance from source to t
via explored nodes only
Pseudocode
Example
Path Finding
Vertex Set
Analysis
Correctness Base case:
Time complexity
Other
• Start of first iteration
Algorithms • 1 holds, as there are no explored nodes
Appendix
• 2 holds, because
• dist[source] = 0
• For all other nodes t, dist[t] = ∞
COMP2521
23T3 Analysis
Correctness
Algorithm
Vertex Set
Analysis
Correctness
Time complexity
Other
Algorithms
Appendix
src
COMP2521
23T3 Analysis
Correctness
Algorithm
Vertex Set
• Let s be an unexplored node with minimum distance
Analysis
Correctness
Time complexity
Other
Algorithms
Appendix
src s
COMP2521
23T3 Analysis
Correctness
Algorithm
Vertex Set
• Let s be an unexplored node with minimum distance
Analysis • We claim that dist[s] is the shortest distance from source to s
Correctness
Time complexity
Other
Algorithms
Appendix
src s
COMP2521
23T3 Analysis
Correctness
Algorithm
Vertex Set
• Let s be an unexplored node with minimum distance
Analysis • We claim that dist[s] is the shortest distance from source to s
• If there is a shorter path to s via explored nodes only, then dist[s] would
Correctness
Time complexity
Other have been updated when exploring the predecessor of s on that path
Algorithms
Appendix
src s
COMP2521
23T3 Analysis
Correctness
Algorithm
Vertex Set
• Let s be an unexplored node with minimum distance
Analysis • We claim that dist[s] is the shortest distance from source to s
• If there is a shorter path to s via explored nodes only, then dist[s] would
Correctness
Time complexity
Other have been updated when exploring the predecessor of s on that path
Algorithms
• If there is a shorter path to s via an unexplored node u, then dist[u] <
dist[s], which is a contradiction, since s has minimum distance out of all
Appendix
unexplored nodes
src s
COMP2521
23T3 Analysis
Correctness
Algorithm
Vertex Set
Analysis
Correctness
Time complexity
Other
Algorithms
Appendix
src s
COMP2521
23T3 Analysis
Correctness
Algorithm
Vertex Set
• After exploring s:
Analysis
Correctness
Time complexity
Other
Algorithms
Appendix
src s
COMP2521
23T3 Analysis
Correctness
Algorithm
Vertex Set
• After exploring s:
Analysis
• 1 still holds for s, since dist[s] is not updated while exploring s
Correctness • Same for all other explored nodes
Time complexity
Other
Algorithms
Appendix
src s
COMP2521
23T3 Analysis
Correctness
Algorithm
Vertex Set
• After exploring s:
Analysis
• 1 still holds for s, since dist[s] is not updated while exploring s
Correctness • Same for all other explored nodes
Time complexity
Other
• 2 still holds for all unexplored nodes t, since:
Algorithms
Appendix
src s
COMP2521
23T3 Analysis
Correctness
Algorithm
Vertex Set
• After exploring s:
Analysis
• 1 still holds for s, since dist[s] is not updated while exploring s
Correctness • Same for all other explored nodes
Time complexity
Other
• 2 still holds for all unexplored nodes t, since:
Algorithms • If there is a shorter path to t via s then we would have updated dist[t] while
Appendix exploring s
src s
COMP2521
23T3 Analysis
Correctness
Algorithm
Vertex Set
• After exploring s:
Analysis
• 1 still holds for s, since dist[s] is not updated while exploring s
Correctness • Same for all other explored nodes
Time complexity
Other
• 2 still holds for all unexplored nodes t, since:
Algorithms • If there is a shorter path to t via s then we would have updated dist[t] while
Appendix exploring s
• Otherwise, we would not have updated dist[t] and it would remain as it is
src s
COMP2521
23T3 Analysis
Time complexity
Algorithm
Pseudocode
Example
Other
Algorithms • Every iteration, algorithm must find vertex v in vSet with minimum
Appendix
distance - time complexity depends on vSet implementation
• Boolean array ⇒ O(V ) per iteration
⇒ overall cost = O(E + V 2 ) = O(V 2 )
• Array/list of vertices ⇒ O(V ) per iteration
⇒ overall cost = O(E + V 2 ) = O(V 2 )
• Priority queue ⇒ O(log V ) per iteration
⇒ overall cost = O(E + V log V )
COMP2521
23T3 Other Shortest Path Algorithms
Algorithm
Pseudocode
Example
Path Finding
Vertex Set
Analysis
• Floyd-Warshall Algorithm
Other
• All-pairs shortest path
Algorithms • Works for graphs with negative weights
Appendix
• Bellman-Ford Algorithm
• Single-source shortest path
• Works for graphs with negative weights
• Can detect negative cycles
COMP2521
23T3 Feedback
Algorithm
Pseudocode
Example
https://forms.office.com/r/aPF09YHZ3X
Path Finding
Vertex Set
Analysis
Other
Algorithms
Appendix
COMP2521
23T3
Algorithm
Pseudocode
Example
Path Finding
Vertex Set
Appendix
Analysis
Other
Algorithms
Appendix
Example
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 ∞ ∞ ∞ ∞ ∞
pred -1 -1 -1 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 ∞ ∞ ∞ ∞ ∞
pred -1 -1 -1 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 ∞ ∞ ∞ ∞ ∞
pred -1 -1 -1 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 ∞ ∞ ∞ ∞ ∞
pred -1 -1 -1 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 ∞ ∞ ∞ ∞ ∞
pred -1 -1 -1 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 ∞ ∞ ∞ ∞ ∞
pred -1 -1 -1 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 ∞ ∞ ∞ ∞ ∞
pred -1 -1 -1 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 ∞ ∞ ∞ ∞
pred -1 0 -1 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 14 ∞ ∞ ∞ ∞
pred -1 0 -1 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 ∞ ∞ ∞ ∞
pred -1 0 -1 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 ∞ ∞ ∞ ∞
pred -1 0 -1 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 ∞ ∞ ∞ ∞
pred -1 0 -1 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 ∞ ∞ ∞
pred -1 0 0 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 ∞ ∞ ∞
pred -1 0 0 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 ∞ ∞ ∞
pred -1 0 0 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 ∞ ∞ ∞
pred -1 0 0 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 ∞ ∞ ∞
pred -1 0 0 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ ∞
pred -1 0 0 0 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ ∞
pred -1 0 0 0 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ ∞
pred -1 0 0 0 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ ∞
pred -1 0 0 0 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ ∞
pred -1 0 0 0 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ ∞
pred -1 0 0 0 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ ∞
pred -1 0 0 0 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ ∞
pred -1 0 0 0 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ ∞
pred -1 0 0 0 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ ∞
pred -1 0 0 0 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ ∞
pred -1 0 0 0 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ ∞
pred -1 0 0 0 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ ∞
pred -1 0 0 0 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ ∞
pred -1 0 0 0 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ 22
pred -1 0 0 0 -1 3
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ 22
pred -1 0 0 0 -1 3
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ 22
pred -1 0 0 0 -1 3
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ 22
pred -1 0 0 0 -1 3
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ 22
pred -1 0 0 0 -1 3
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ 22
pred -1 0 0 0 -1 3
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ 22
pred -1 0 0 0 -1 3
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ 22
pred -1 0 0 0 -1 3
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 14 9 7 ∞ 22
pred -1 0 0 0 -1 3
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 ∞ 22
pred -1 2 0 0 -1 3
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 ∞ 22
pred -1 2 0 0 -1 3
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 ∞ 22
pred -1 2 0 0 -1 3
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 ∞ 22
pred -1 2 0 0 -1 3
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 ∞ 22
pred -1 2 0 0 -1 3
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 ∞ 22
pred -1 2 0 0 -1 3
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 ∞ 12
pred -1 2 0 0 -1 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 ∞ 12
pred -1 2 0 0 -1 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 ∞ 12
pred -1 2 0 0 -1 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 ∞ 12
pred -1 2 0 0 -1 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 ∞ 12
pred -1 2 0 0 -1 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 ∞ 12
pred -1 2 0 0 -1 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 ∞ 12
pred -1 2 0 0 -1 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 ∞ 12
pred -1 2 0 0 -1 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 ∞ 12
pred -1 2 0 0 -1 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 ∞ 12
pred -1 2 0 0 -1 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 20 12
pred -1 2 0 0 5 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 20 12
pred -1 2 0 0 5 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 20 12
pred -1 2 0 0 5 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 20 12
pred -1 2 0 0 5 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 20 12
pred -1 2 0 0 5 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 20 12
pred -1 2 0 0 5 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 20 12
pred -1 2 0 0 5 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 20 12
pred -1 2 0 0 5 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 20 12
pred -1 2 0 0 5 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 20 12
pred -1 2 0 0 5 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 18 12
pred -1 2 0 0 1 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 18 12
pred -1 2 0 0 1 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 18 12
pred -1 2 0 0 1 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 18 12
pred -1 2 0 0 1 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 18 12
pred -1 2 0 0 1 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 18 12
pred -1 2 0 0 1 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 18 12
pred -1 2 0 0 1 2
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm
Path Finding
5
Vertex Set 1 4
Analysis
14 while vSet is not empty:
Other
Algorithms
4 8 find vertex v in vSet such that
dist[v] is minimal
Appendix 9 3
Example
0 2 5 and remove it from vSet
3
[0] [1] [2] [3] [4] [5]
dist 0 13 9 7 18 12
pred -1 2 0 0 1 2