You are on page 1of 24

First semester 2-5pm prac test:

Tuesday 8 May
or
Friday 11 May
or
Monday 14 May
Outline

 Graph storage

 Adjacency matrix or adjacency lists

 Unweighted Shortest Path

 Breadth-first search

 Positive-Weighted Shortest Path

 Dijkstra's Algorithm
Exercise in Class - BFS Solution

What if we look at cost, not number of edges?

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

What is the weighted path length of A  B  E ?


Is this the cheapest path from A to E ?
The path with the fewest edges may not be the cheapest path!
Cheapest path is a (slightly) different problem!
Exercise in Class - BFS Solution

Is Breadth First Search algorithm correct?

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) ?

2) Couldn’t there be a shorter path that goes S  - - -  N  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 ?

bfs(v): // shortest paths from v


set each vertex cost to -9999
set each vertex cost to 999999999
v.cost = 0
v.cost = 0
initialize queue with v
initialize queue with v
while queue not empty DO
while queue not empty DO
a = front of queue
a = cheapest in queue
for each edge a  w
for each each edge a  w
if w.cost = -9999
c = a.cost + (weight of aw)
c = a.cost + 1
if c < w.cost
w.cost = c
w.cost = c
add w to queue
add w to queue
remove a from queue
remove a from queue
1-8
// pop the node with the shortest distance

// look at each edge leaving that node

// get the destination node that edge goes to

// update & Q it if this route better

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 ∞ ∞ ∞ ∞ ∞

See notes below next few slides for explanations.


Example: Compute the shortest path from A to C

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)

Example: Compute the shortest path from A to C


Adjacency List
0 15
15 B A→ B → D
A
B→E
10 C →A
80 25 D→B→C
11
E→D
∞ 80 ∞
24 47
C D E

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

Example: Compute the shortest path from A to C


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 ∞
15
B A ∞ 80A 25B
Now select the minimum distance, that is node E
Check other reachable vertices from “current vertex”, E

Example: Compute the shortest path from A to C


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 ∞
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.

Consider the graph shown below, compute the positive-


weighted shortest path from V0 to V5

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.

You might also like