0% found this document useful (0 votes)
19 views

COMP2521 Week 6 - Dijkstra's Algorithm

This document describes Dijkstra's algorithm for finding the shortest paths between vertices in a weighted graph. It discusses the key data structures used, including a distance array and predecessor array. It also explains the edge relaxation process, where if a shorter path to a vertex is found, the distance and predecessor for that vertex are updated. Pseudocode is provided that implements Dijkstra's algorithm using these concepts to iteratively find the single-source shortest paths from a starting vertex to all other vertices in the graph.

Uploaded by

Hysan Dax
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

COMP2521 Week 6 - Dijkstra's Algorithm

This document describes Dijkstra's algorithm for finding the shortest paths between vertices in a weighted graph. It discusses the key data structures used, including a distance array and predecessor array. It also explains the edge relaxation process, where if a shorter path to a vertex is found, the distance and predecessor for that vertex are updated. Pseudocode is provided that implements Dijkstra's algorithm using these concepts to iteratively find the single-source shortest paths from a starting vertex to all other vertices in the graph.

Uploaded by

Hysan Dax
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

COMP2521

23T3

Algorithm

Pseudocode

Example

Path Finding

Vertex Set
COMP2521 23T3
Analysis
Dijkstra’s Algorithm
Other
Algorithms

Appendix Kevin Luxa


[email protected]

shortest path
dijkstra’s algorithm

COMP2521
23T3 Shortest Path
Algorithm

Pseudocode

Example In a weighted graph…


Path Finding

Vertex Set A path is a sequence of edges


Analysis connected end-to-end
Other
Algorithms
(v0 , v1 , w1 ), (v1 , v2 , w2 ), … , (vm−1 , vm , wm )
Appendix
weight1 weight2 weightm

v0 v1 v2 vm−1 vm
The cost of a path is
the sum of edge weights along the path

The shortest path between two vertices s and t is


the path from s to t with minimum cost
COMP2521
23T3 Shortest Path
Algorithm

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

Path Finding In a weighted graph,


Vertex Set the shortest path between two vertices
Analysis is not necessarily the path with the least number of edges
Other
Algorithms
5 1
Appendix
1
3
4 2

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

Example 1 Create and initialise data structures


Path Finding • Initialise distance array to infinity
Vertex Set • In C, can use INT_MAX (from <limits.h>)
Analysis • Initialise predecessor array to -1
Other • Initialise set of vertices to contain all vertices
Algorithms

Appendix 2 Set distance of starting vertex to 0


3 While set of vertices is not empty:
1 Remove vertex from vertex set with smallest distance in distance array
• Let this vertex be v
2 Explore v - that is, for each edge v–w:
• Check if using this edge gives a shorter path to w
• If so, update w’s distance and predecessor - this is called edge relaxation

COMP2521
23T3 Edge Relaxation
Algorithm
Edge relaxation

Pseudocode

Example

Path Finding

Vertex Set

Analysis During Dijkstra’s algorithm, the dist and pred arrays:


Other
Algorithms • contain data about the shortest path discovered so far
Appendix • need to be updated if a shorter path to some vertex is found
• this is done via edge relaxation
COMP2521
23T3 Edge Relaxation
Algorithm
Edge relaxation Suppose we are considering edge (v, w, weight).
Pseudocode

Example We have the following data:


Path Finding
• dist[v] - length of shortest known path from s to v
Vertex Set
dist[v]
s v
Analysis

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

Pseudocode Now we have two paths from s to w:


Example
• Shortest known path:
Path Finding
dist[w]
s w
Vertex Set

Analysis

Other
Algorithms • New path via v:
Appendix
dist[v] weight
s v w

If the new path is shorter, then we update dist[w] and pred[w].

if dist[v] + weight < dist[w]:


dist[w] = dist[v] + weight
pred[w] = 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

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

After relaxation along (v, w, 3)

i
... [v] ... [w]
w
dist[w] = 11 dist ... 8 ... 11
s
3 ... ... ... v
dist[v] = 8 pred
v
COMP2521
23T3 Pseudocode
Algorithm

Pseudocode

Example

Path Finding dijkstraSSSP(G, src):


Vertex Set Inputs: graph G, source vertex src
Analysis

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

Pseudocode (p)Initialisation (p)


Example
5
Path Finding 1 4
Vertex Set
14 while vSet is not empty:
Analysis 4 8 find vertex v in vSet such that
Other dist[v] is minimal
Algorithms 9 3
0 2 5 and remove it from vSet
Appendix

for each edge (v, w, weight) in G:


10 relax along (v, w, weight)
7 15
3
[0] [1] [2] [3] [4] [5]

dist 0 ∞ ∞ ∞ ∞ ∞

pred -1 -1 -1 -1 -1 -1

COMP2521
23T3 Example
Algorithm

Pseudocode (p)After first iteration (v = 0) (p)


Example
5
Path Finding 1 4
Vertex Set
14 while vSet is not empty:
Analysis 4 8 find vertex v in vSet such that
Other dist[v] is minimal
Algorithms 9 3
0 2 5 and remove it from vSet
Appendix

for each edge (v, w, weight) in G:


10 relax along (v, w, weight)
7 15
3
[0] [1] [2] [3] [4] [5]

dist 0 14 9 7 ∞ ∞

pred -1 0 0 0 -1 -1
COMP2521
23T3 Example
Algorithm

Pseudocode (p)After second iteration (v = 3) (p)


Example
5
Path Finding 1 4
Vertex Set
14 while vSet is not empty:
Analysis 4 8 find vertex v in vSet such that
Other dist[v] is minimal
Algorithms 9 3
0 2 5 and remove it from vSet
Appendix

for each edge (v, w, weight) in G:


10 relax along (v, w, weight)
7 15
3
[0] [1] [2] [3] [4] [5]

dist 0 14 9 7 ∞ 22

pred -1 0 0 0 -1 3

COMP2521
23T3 Example
Algorithm

Pseudocode (p)After third iteration (v = 2) (p)


Example
5
Path Finding 1 4
Vertex Set
14 while vSet is not empty:
Analysis 4 8 find vertex v in vSet such that
Other dist[v] is minimal
Algorithms 9 3
0 2 5 and remove it from vSet
Appendix

for each edge (v, w, weight) in G:


10 relax along (v, w, weight)
7 15
3
[0] [1] [2] [3] [4] [5]

dist 0 13 9 7 ∞ 12

pred -1 2 0 0 -1 2
COMP2521
23T3 Example
Algorithm

Pseudocode (p)After fourth iteration (v = 5) (p)


Example
5
Path Finding 1 4
Vertex Set
14 while vSet is not empty:
Analysis 4 8 find vertex v in vSet such that
Other dist[v] is minimal
Algorithms 9 3
0 2 5 and remove it from vSet
Appendix

for each edge (v, w, weight) in G:


10 relax along (v, w, weight)
7 15
3
[0] [1] [2] [3] [4] [5]

dist 0 13 9 7 20 12

pred -1 2 0 0 5 2

COMP2521
23T3 Example
Algorithm

Pseudocode (p)After fifth iteration (v = 1) (p)


Example
5
Path Finding 1 4
Vertex Set
14 while vSet is not empty:
Analysis 4 8 find vertex v in vSet such that
Other dist[v] is minimal
Algorithms 9 3
0 2 5 and remove it from vSet
Appendix

for each edge (v, w, weight) in G:


10 relax along (v, w, weight)
7 15
3
[0] [1] [2] [3] [4] [5]

dist 0 13 9 7 18 12

pred -1 2 0 0 1 2
COMP2521
23T3 Example
Algorithm

Pseudocode (p)After sixth iteration (v = 4) (p)


Example
5
Path Finding 1 4
Vertex Set
14 while vSet is not empty:
Analysis 4 8 find vertex v in vSet such that
Other dist[v] is minimal
Algorithms 9 3
0 2 5 and remove it from vSet
Appendix

for each edge (v, w, weight) in G:


10 relax along (v, w, weight)
7 15
3
[0] [1] [2] [3] [4] [5]

dist 0 13 9 7 18 12

pred -1 2 0 0 1 2

COMP2521
23T3 Example
Algorithm

Pseudocode (p)Done (p)


Example
5
Path Finding 1 4
Vertex Set
14 while vSet is not empty:
Analysis 4 8 find vertex v in vSet such that
Other dist[v] is minimal
Algorithms 9 3
0 2 5 remove v from vSet
Appendix

for each edge (v, w, weight) in G:


10 relax along (v, w, weight)
7 15
3
[0] [1] [2] [3] [4] [5]

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

[0] [1] [2] [3] [4] [5]


pred -1 2 0 0 1 2
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

[0] [1] [2] [3] [4] [5]


pred -1 2 0 0 1 2

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

[0] [1] [2] [3] [4] [5]


pred -1 2 0 0 1 2
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

[0] [1] [2] [3] [4] [5]


pred -1 2 0 0 1 2

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

[0] [1] [2] [3] [4] [5]


pred -1 2 0 0 1 2
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

[0] [1] [2] [3] [4] [5]


pred -1 2 0 0 1 2

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

[0] [1] [2] [3] [4] [5]


pred -1 2 0 0 1 2
COMP2521
23T3 Path Finding
Algorithm

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

Appendix 1. Visited array

2. Explicit array/list of vertices

3. Priority queue
COMP2521
23T3 Vertex Set
Visited array implementation
Algorithm

Pseudocode

Example

Path Finding

Vertex Set

Analysis Visited array implementation:


Other
Algorithms • Similar to visited array in BFS/DFS
Appendix • Array of V booleans, initialised to false
• After exploring vertex v, set visited[v] to true
• At the start of each iteration, find vertex v such that visited[v] is false
and dist[v] is minimal ⇒ O(V )

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

Path Finding Priority queue implementation:


Vertex Set • A priority queue is an ADT…
Analysis • where each item has a priority
Other • with two main operations:
Algorithms
• Insert: insert item with priority
Appendix
• Delete: remove item with highest priority
• Use priority queue to store vertices, use distance to vertex as priority
(smaller distance = higher priority)
• A good priority queue implementation has O(log n) insert and delete

Priority queues will be discussed next week.

COMP2521
23T3 Analysis
Correctness
Algorithm

Pseudocode

Example

Path Finding

Vertex Set Proof by induction.


Analysis

Aim is to prove that before and after each iteration:


Correctness
Time complexity

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

Ultimately, all nodes are explored, so by 1 :


• For all nodes v, dist[v] is the shortest distance from source to v
COMP2521
23T3 Analysis
Correctness
Algorithm

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

Pseudocode Induction step:


Example
• Assume that 1 and 2 hold at the start of an iteration
Path Finding

Vertex Set

Analysis
Correctness
Time complexity

Other
Algorithms

Appendix

src
COMP2521
23T3 Analysis
Correctness
Algorithm

Pseudocode Induction step:


Example
• Assume that 1 and 2 hold at the start of an iteration
Path Finding

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

Pseudocode Induction step:


Example
• Assume that 1 and 2 hold at the start of an iteration
Path Finding

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

Pseudocode Induction step:


Example
• Assume that 1 and 2 hold at the start of an iteration
Path Finding

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

Pseudocode Induction step:


Example
• Assume that 1 and 2 hold at the start of an iteration
Path Finding

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

Pseudocode Induction step (continued):


Example
• dist[s] is the shortest distance from source to s
Path Finding

Vertex Set

Analysis
Correctness
Time complexity

Other
Algorithms

Appendix

src s

COMP2521
23T3 Analysis
Correctness
Algorithm

Pseudocode Induction step (continued):


Example
• dist[s] is the shortest distance from source to s
Path Finding

Vertex Set
• After exploring s:
Analysis
Correctness
Time complexity

Other
Algorithms

Appendix

src s
COMP2521
23T3 Analysis
Correctness
Algorithm

Pseudocode Induction step (continued):


Example
• dist[s] is the shortest distance from source to s
Path Finding

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

Pseudocode Induction step (continued):


Example
• dist[s] is the shortest distance from source to s
Path Finding

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

Pseudocode Induction step (continued):


Example
• dist[s] is the shortest distance from source to s
Path Finding

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

Pseudocode Induction step (continued):


Example
• dist[s] is the shortest distance from source to s
Path Finding

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

Path Finding Analysis:


Vertex Set • Each edge is considered once ⇒ O(E)
Analysis
Correctness
• Undirected edges are considered once in each direction
• Outer loop has V iterations
Time complexity

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

Pseudocode (p)Initialisation (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

3
[0] [1] [2] [3] [4] [5]

dist 0 ∞ ∞ ∞ ∞ ∞

pred -1 -1 -1 -1 -1 -1

COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm

Pseudocode (p)Remove 0 from vSet (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

3
[0] [1] [2] [3] [4] [5]

dist 0 ∞ ∞ ∞ ∞ ∞

pred -1 -1 -1 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm

Pseudocode (p)Explore 0 (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

3
[0] [1] [2] [3] [4] [5]

dist 0 ∞ ∞ ∞ ∞ ∞

pred -1 -1 -1 -1 -1 -1

COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm

Pseudocode (p)Relax along (0, 1, 14) (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

3
[0] [1] [2] [3] [4] [5]

dist 0 ∞ ∞ ∞ ∞ ∞

pred -1 -1 -1 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm

Pseudocode (p)Relax along (0, 1, 14) (p)


Example dist[0] + 14 = 14 < dist[1]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

3
[0] [1] [2] [3] [4] [5]

dist 0 ∞ ∞ ∞ ∞ ∞

pred -1 -1 -1 -1 -1 -1

COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm

Pseudocode (p)Relax along (0, 1, 14) (p)


Example dist[0] + 14 = 14 < dist[1]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

3
[0] [1] [2] [3] [4] [5]

dist 0 ∞ ∞ ∞ ∞ ∞

pred -1 -1 -1 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm

Pseudocode (p)Relax along (0, 1, 14) (p)


Example dist[0] + 14 = 14 < dist[1]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

3
[0] [1] [2] [3] [4] [5]

dist 0 ∞ ∞ ∞ ∞ ∞

pred -1 -1 -1 -1 -1 -1

COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm

Pseudocode (p)Relax along (0, 1, 14) (p)


Example dist[0] + 14 = 14 < dist[1]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

3
[0] [1] [2] [3] [4] [5]

dist 0 14 ∞ ∞ ∞ ∞

pred -1 0 -1 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm

Pseudocode (p)Relax along (0, 2, 9) (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

3
[0] [1] [2] [3] [4] [5]

dist 0 14 ∞ ∞ ∞ ∞

pred -1 0 -1 -1 -1 -1

COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm

Pseudocode (p)Relax along (0, 2, 9) (p)


Example dist[0] + 9 = 9 < dist[2]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

3
[0] [1] [2] [3] [4] [5]

dist 0 14 ∞ ∞ ∞ ∞

pred -1 0 -1 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm

Pseudocode (p)Relax along (0, 2, 9) (p)


Example dist[0] + 9 = 9 < dist[2]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

3
[0] [1] [2] [3] [4] [5]

dist 0 14 ∞ ∞ ∞ ∞

pred -1 0 -1 -1 -1 -1

COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm

Pseudocode (p)Relax along (0, 2, 9) (p)


Example dist[0] + 9 = 9 < dist[2]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

3
[0] [1] [2] [3] [4] [5]

dist 0 14 ∞ ∞ ∞ ∞

pred -1 0 -1 -1 -1 -1
COMP2521
23T3 Dijkstra’s Algorithm
Example
Algorithm

Pseudocode (p)Relax along (0, 2, 9) (p)


Example dist[0] + 9 = 9 < dist[2]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (0, 3, 7) (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (0, 3, 7) (p)


Example dist[0] + 7 = 7 < dist[3]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (0, 3, 7) (p)


Example dist[0] + 7 = 7 < dist[3]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (0, 3, 7) (p)


Example dist[0] + 7 = 7 < dist[3]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (0, 3, 7) (p)


Example dist[0] + 7 = 7 < dist[3]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Done with exploring 0 (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Remove 3 from vSet (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Explore 3 (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)No need to consider (3, 0, 7) (p)


Example (0 has already been explored)
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (3, 2, 10) (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (3, 2, 10) (p)


Example dist[3] + 10 = 17 ≮ dist[2]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (3, 2, 10) (p)


Example dist[3] + 10 = 17 ≮ dist[2]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (3, 2, 10) (p)


Example dist[3] + 10 = 17 ≮ dist[2]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (3, 2, 10) (p)


Example dist[3] + 10 = 17 ≮ dist[2]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (3, 5, 15) (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (3, 5, 15) (p)


Example dist[3] + 15 = 22 < dist[5]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (3, 5, 15) (p)


Example dist[3] + 15 = 22 < dist[5]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (3, 5, 15) (p)


Example dist[3] + 15 = 22 < dist[5]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (3, 5, 15) (p)


Example dist[3] + 15 = 22 < dist[5]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Done with exploring 3 (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Remove 2 from vSet (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Explore 2 (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)No need to consider (2, 0, 9) (p)


Example (0 has already been explored)
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (2, 1, 4) (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (2, 1, 4) (p)


Example dist[2] + 4 = 13 < dist[1]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (2, 1, 4) (p)


Example dist[2] + 4 = 13 < dist[1]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (2, 1, 4) (p)


Example dist[2] + 4 = 13 < dist[1]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (2, 1, 4) (p)


Example dist[2] + 4 = 13 < dist[1]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)No need to consider (2, 3, 10) (p)


Example (3 has already been explored)
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (2, 5, 3) (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (2, 5, 3) (p)


Example dist[2] + 3 = 12 < dist[5]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (2, 5, 3) (p)


Example dist[2] + 3 = 12 < dist[5]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (2, 5, 3) (p)


Example dist[2] + 3 = 12 < dist[5]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (2, 5, 3) (p)


Example dist[2] + 3 = 12 < dist[5]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Done with exploring 2 (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Remove 5 from vSet (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Explore 5 (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)No need to consider (5, 2, 3) (p)


Example (2 has already been explored)
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)No need to consider (5, 3, 15) (p)


Example (3 has already been explored)
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (5, 4, 8) (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (5, 4, 8) (p)


Example dist[5] + 8 = 20 < dist[4]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (5, 4, 8) (p)


Example dist[5] + 8 = 20 < dist[4]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (5, 4, 8) (p)


Example dist[5] + 8 = 20 < dist[4]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (5, 4, 8) (p)


Example dist[5] + 8 = 20 < dist[4]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Done with exploring 5 (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Remove 1 from vSet (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Explore 1 (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)No need to consider (1, 0, 14) (p)


Example (0 has already been explored)
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)No need to consider (1, 2, 4) (p)


Example (2 has already been explored)
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (1, 4, 5) (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (1, 4, 5) (p)


Example dist[1] + 5 = 18 < dist[4]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (1, 4, 5) (p)


Example dist[1] + 5 = 18 < dist[4]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (1, 4, 5) (p)


Example dist[1] + 5 = 18 < dist[4]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Relax along (1, 4, 5) (p)


Example dist[1] + 5 = 18 < dist[4]
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Done with exploring 1 (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Remove 4 from vSet (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Explore 4 (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)No need to consider (4, 1, 5) (p)


Example (1 has already been explored)
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)No need to consider (4, 5, 8) (p)


Example (5 has already been explored)
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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Done with exploring 4 (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

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

Pseudocode (p)Finished (p)


Example

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

for each edge (v, w, weight) in G:


10
7 15 relax along (v, w, weight)

3
[0] [1] [2] [3] [4] [5]

dist 0 13 9 7 18 12

pred -1 2 0 0 1 2

You might also like