P2P Paths With Djikstra's Algorithm
P2P Paths With Djikstra's Algorithm
Learning objectives
After studying this chapter, you should be able to:
■ apply Dijkstra’s algorithm to a network
■ trace back through a network to find a route corresponding to a shortest path
■ apply Dijkstra’s algorithm to a network with multiple start points
■ understand the situations where Dijkstra’s algorithm fails.
2.1 Introduction
When you plan a journey, there are different factors you might
consider:
● do you want to go the shortest distance?
● do you want to take the minimum time?
● do you want to minimise the cost?
Autoroute is a computerised route planner. You enter the start and
finish points of a journey and the program works out the different
routes, depending on the criterion that is to be minimised.
Another innovation used today is satellite navigation systems.
These keep a motorist in constant contact with a central computer.
The central computer monitors traffic flow and updates the
motorist on the optimum route from his or her current position.
These systems are invaluable for haulage and coach companies.
How do these systems work?
The basic principle is to find the shortest route from one part of
the diagram to another, in this case from A to F.
B 11 D
8 11 9
A F
7
10 8
C 9 E
24 Shortest path problem (Dijkstra’s algorithm)
100 1 12
B B
6 12
185
110 2
L L
The numbers on the first diagram show the distances, in miles,
between the towns.
There are two possible routes: Royton direct to London, which is
185 miles, or Royton to Birmingham and then Birmingham to
London, a total distance of 210 miles.
The right-hand diagram shows the times of the same journeys.
The time taken on the direct route from Royton to London is
612 hours. The time from Royton to Birmingham is 112 hours and
Birmingham to London 2 hours. Hence we have a triangle with
sides of length 112, 2 and 612! The right-hand diagram is an
impossible triangle.
This is acceptable because the diagram shows a network
representing a real-life situation and not a scale drawing.
4 6 14
3 9 3
8 D 10 G 15
A J
1 4 5 5
5 8
B
C 11 F 11 I 4
Solution 8D
Step 1 Label A as 0. A
0
Step 2 Box this number.
Step 3 Label values of 4 at B, 8 at D and 5 at C. 5
Step 4 Box the 4 at B. C
Step 5 From B, the connected vertices are D and E. The distances
at these vertices are 7 at D (4 3) and 16 at E (4 12).
26 Shortest path problem (Dijkstra’s algorithm)
8 7
A D
0
5
C
8 7 6
A D
0
5 16
F
C
8 7 6
A G
0 D 16
5 C F
16 10
Shortest path problem (Dijkstra’s algorithm) 27
8 7 6 G
A 16 15
0 D
5 C I
F 16 10 21
B 16 15 E 25
4 H
8 7 6 G
A 16 15
0 D
5 C I
F 16 10 21
8 7 6 G
A J 30
0 D 16 15
5 C I
F 16 10 21 20
8 7 6 G
A J 30
0 D 16 15
5 C I
F 16 10 21 20
8 7 6 G
A J
0 D 16 15 30 28
5 C I
F 16 10 21 20
Shortest path problem (Dijkstra’s algorithm) 29
Step 7 The final vertex, in this case J, is not boxed. The boxed How do we retrace the route
number at J is the shortest distance. The route that corresponds to this shortest
corresponding to this distance of 28 is ACDFGIJ, but this network? This problem will be
is not immediately obvious from the network. dealt with later in the chapter.
5 9 13
8
A H
5 6
6 11
C 6 E 7 G
Solution
The fully labelled diagram below shows the values, both Any answer to an exam question
temporary and permanent, at each vertex. must have exactly the same
amount of detail as shown here.
B 5 D 12 11 F 19 18
A H 31 30
0
C 6 E 13 12 G 20 19
EXERCISE 2A
1 Use Dijkstra’s algorithm on the networks below to find the
shortest distance from A to H.
(a) B 7 E (b) B 14 D
5
8 5 5 7 12
11
5 C 10 F 8 E
A H A H
10
10
7 11 6 5
7 G
8
D 4 G C 6 F
30 Shortest path problem (Dijkstra’s algorithm)
15 14 9
8
Manchester
Rochdale Stockport
25 20
10 14 17 13
Oldham 23 Ashton
3 5 7
Royton 3 Aston
12
3
4