Professional Documents
Culture Documents
Tuesday 8 May
or
Friday 11 May
or
Monday 14 May
Outline
Graph storage
Breadth-first search
Dijkstra's Algorithm
Exercise in Class - BFS Solution
Breadth-First Tree
0
0 1 A
A B
1 1
B D
2 1 2
2 2
C D E
E C
What if we look at cost, not number of edges?
Breadth-First Tree
A
25 B
A 25 8
10
8 B D
11 15
10 24
24 47
C D E
E C
Breadth-First Tree
0
0 1 A
A B
1 1
B D
2 1 2
2 2
C D E
E C
1) Is W definitely reachable from S (is there definitely a path from S to W) ?
1) Yes, because there’s a path to V that we found earlier, and there’s an edge from V to W
2) No, because at each step BFS processes the node with the shortest distance from S
(that hasn’t been done before, i.e. whose adjacent nodes like W weren’t seen before)
1-6
V Positive-Weighted graph:
Which queue node do we choose next?
How do we update costs ?
1-9
Copyright © 2010 Pearson Education, publishing as Addison-Wesley. All rights reserved
Example: Compute the shortest path from A to C
∞ 15
∞ Adjacency List
A B
A→ B → D
10 B→E
80 25 C →A
11
D→B→C
∞ ∞ ∞ E→D
24 47
C D E
Iteration A B C D E
0 ∞ ∞ ∞ ∞ ∞
0
15
∞ Adjacency List
A B
A→ B → D
10 B→E
80 25 C →A
11
D→B→C
∞ ∞ ∞ E→D
24 47
C D E
Vertex A B C D E
A 0A ∞ ∞ ∞ ∞
Numbers in the array show the cost of the shortest path to that node so far.
The letter next to the cost shows the node we reached it from.
Now add nodes adjacent to A. i.e. (A → B) and (A → D)
Vertex A B C D E
Red
block A 0A ∞ ∞ ∞ ∞
means
node
taken A 0 15 ∞ 80 ∞
A A A
off
queue
Now select the node closest to A (i.e. minimum distance), which is node B
Check other reachable vertices from “current vertex”, B
Vertex A B C D E
A 0A ∞ ∞ ∞ ∞
A 0A 15A ∞ 80A ∞
15
B A ∞ 80A 25B
Now select the minimum distance, that is node E
Check other reachable vertices from “current vertex”, E
Vertex A B C D E
A 0A ∞ ∞ ∞ ∞
A 0A 15A ∞ 80A ∞
15
B A ∞ 80A 25B
Continue the process...E is new min-node, update cost for node D
0 15 Adjacency List
15
A B
A→ B → D
10 B→E
80 25 C →A
11
25 D→B→C
∞ 24
80
47 E→D
C D E
Vertex A B C D E
A 0A ∞ ∞ ∞ ∞
A 0A 15A ∞ 80A ∞
B 15
A
∞ 80A 25B
E ∞ 72E 25B
If the newly computed cost is < than stored cost, update (else don’t)
Continue the process...D is new min-node, update cost for node C
0 15 Adjacency List
15
A B
A→ B → D
10 B→E
80 25 C →A
11
72 25 D→B→C
96 E→D
24 47
C D E
Vertex A B C D E
A 0A ∞ ∞ ∞ ∞
A 0A 15A ∞ 80A ∞
B 15A ∞ 80A 25B
E ∞ 72E 25B
D 96D 72E
If the newly computed cost is < than stored cost, update (else don’t)
What is the cost of the minimum weighted path from A to C? 96
What is this path? Backwards it is D, E, B, A so path is ABEDC.
from A to C is 96
0 15 Adjacency List
15
A B
A→ B → D
10 B→E
80 25 C →A
11
72 25 D→B→C
96 E→D
24 47
C D E
Vertex A B C D E
A 0A ∞ ∞ ∞ ∞
A 0A 15A ∞ 80A ∞
B 15A ∞ 80A 25B
E ∞ 72E 25B
D 96D 72E
C 96D
See notes below the slides that
Exercise - In Class follow as well.
V0 2 V1
4 10
1 3
V2 2 V3 2 V4
8 4
5 6
V5 V6
1
Graph
In the slides that follow, green, pink and blue arrows are only to help you understand.
Green arrows point to nodes on the priority queue at each stage.
Pink and blue arrows show how costs/distances get changed.
If you use this method of showing your working, instead of the table method, you must
also show the node via which that node was reached, e.g. in brackets next to the cost.
Why did the chicken cross Dijkstra said it was the
the road? shortest path
What did Usain Bolt say to Dijkstra? My runtime is faster than yours.