0% found this document useful (0 votes)
154 views8 pages

P2P Paths With Djikstra's Algorithm

The document describes Dijkstra's algorithm for finding the shortest path through a network. It provides an example network from point A to J and step-by-step works through applying Dijkstra's algorithm to find the shortest distance of 21. Key aspects of Dijkstra's algorithm include labeling the starting point with a distance of 0, then exploring connected points and updating distances if a shorter path is found until the destination point is reached.

Uploaded by

Nouman Afzal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
154 views8 pages

P2P Paths With Djikstra's Algorithm

The document describes Dijkstra's algorithm for finding the shortest path through a network. It provides an example network from point A to J and step-by-step works through applying Dijkstra's algorithm to find the shortest distance of 21. Key aspects of Dijkstra's algorithm include labeling the starting point with a distance of 0, then exploring connected points and updating distances if a shorter path is found until the destination point is reached.

Uploaded by

Nouman Afzal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CHAPTER 2

Shortest path problem


(Dijkstra’s algorithm) 2

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)

There are four routes from A to F:


ABDF, length 28; ABEF, length 27; ACDF, length 27;
ACEF, length 26
The shortest route is ACEF.
In this example there are only four possibilities to consider, but if
the network were more complex then this method, called a
complete enumeration, would become impractical. This chapter
uses an algorithm to find the shortest path.

2.2 Triangles in networks


Consider a real-life situation in which we wish to travel from
Royton to London.
The diagrams below show roads connecting Royton, Birmingham
and London. R R

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.

Real-life problems may not obey the triangle inequality.


Shortest path problem (Dijkstra’s algorithm) 25

2.3 Dijkstra’s algorithm


In 1959, Edsger Dijkstra invented an algorithm for finding the
shortest path through a network. The following is a simple set of
instructions that enables students to follow the algorithm:
Step 1 Label the start vertex as 0. 2
Step 2 Box this number (permanent label). Sometimes edges of networks
Step 3 Label each vertex that is connected to the start have arrows that show you
vertex with its distance (temporary label). which directions you must go
from one vertex to the next.
Step 4 Box the smallest number. These are called directed
networks. To apply Dijkstra’s
Step 5 From this vertex, consider the distance to each
algorithm to a directed network,
connected vertex.
at each stage only consider edges
Step 6 If a distance is less than a distance already at this that lead from the vertex.
vertex, cross out this distance and write in the new
distance. If there was no distance at the vertex,
write down the new distance.
Step 7 Repeat from step 4 until the destination vertex is
boxed.
If a vertex is boxed then you do
not write down a new
Note: When a vertex is boxed you do not reconsider it. You need temporary value. This would be a
to show all temporary labels together with their crossings out. complete enumeration.

Worked example 2.1


Find the shortest distance from A to J on the network below.
B 12 E 10 H

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)

Step 6 As the distance at D is 7, lower than the 8 currently at D,


cross out the 8.
4
B 16 E

8 7
A D
0

5
C

Step 4 Box the smallest number, which is the 5 at C.


Step 5 From C, the connected vertices are D and F. The distances
at these vertices are 6 at D (5  1) and 16 at F (5  11).
Step 6 As the distance at D is 6, lower than the 7 currently at D,
cross out the 7.
B 16
4 E

8 7 6
A D
0

5 16
F
C

Step 4 Box the smallest number, which is the 6 at D.


Step 5 From D, the connected vertices are E, F and G. The
distances at these vertices are 15 at E (6  9), 16 at
G (6  10) and 10 at F (6  4).
Step 6 As the distance at E is 15, lower than the 16 currently at
E, cross out the 16. As the distance at F is 10, lower than
the 16 currently at F, cross out the 16.
B 16 15
4 E

8 7 6
A G
0 D 16

5 C F
16 10
Shortest path problem (Dijkstra’s algorithm) 27

Step 4 Box the smallest number, which is the 10 at F.


Step 5 From F, the connected vertices are G and I. The distances
at these vertices are 15 at G (10 + 5) and 21 at I (11 + 10).
Step 6 As the distance at G is 15, lower than the 16 currently at
G, cross out the 16. 2
B 16 15
4 E

8 7 6 G
A 16 15
0 D

5 C I
F 16 10 21

Step 4 Box the smallest number, which the 15 at either E or G


(it doesn’t matter which you chose).
Step 5 From E, the connected vertices are H and G. The
distances at these vertices are 21 at G (15  6) and 25 at
H (15  10). Do not write down the value of 21 at G as
this is greater than the number already there.
Step 6 There are no improvements, so there is no crossing out.

B 16 15 E 25
4 H

8 7 6 G
A 16 15
0 D

5 C I
F 16 10 21

Step 4 Box the smallest number, which is the 15 at G.


Step 5 From G, the connected vertices are H, I and J. The
distances at these vertices are 18 at H (15  3),
20 at I (15  5) and 30 at J (15  15).
28 Shortest path problem (Dijkstra’s algorithm)

Step 6 As the distance at H is 18, lower than the 25 currently at


H, cross out the 25. As the distance at I is 20, lower than
the 21 currently at I, cross out the 21.
B 16 15 E 25 18
4 H

8 7 6 G
A J 30
0 D 16 15

5 C I
F 16 10 21 20

Step 4 Box the smallest number, which is the 18 at H.


Step 5 From H, the connected vertex is J. The distance at this
vertex is 32 (18  14). Do not write down the value of 32
at J as this is greater than the 30 already there.
Step 6 There are no improvements, so there is no crossing out.
B 16 15 E H
4 25 18

8 7 6 G
A J 30
0 D 16 15

5 C I
F 16 10 21 20

Step 4 Box the smallest number, which is the 20 at I.


Step 5 From I, the connected vertex is J. The distance at this
vertex is 28 (20  8).
Step 6 As the distance at J is 28, lower than the 30 currently at
J, cross out the 30.
B 16 15 E H
4 25 18

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.

Worked example 2.2 2


Find the shortest distance from A to H on the network below.
B 7 D 8 F

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)

2 The diagram below shows roads connecting towns near to


Rochdale. The numbers on each arc represent the time, in
minutes, required to travel along each road. Peter is delivering
books from his base at Rochdale to Stockport. Use Dijkstra’s
algorithm to find the minimum time for Peter’s journey.
Bury 17 Trafford

15 14 9
8

Manchester
Rochdale Stockport
25 20

10 14 17 13

Oldham 23 Ashton

3 The diagram below shows roads connecting villages near to


Royton. The numbers on each arc represent the distance, in
miles, along each road. Leon lives in Royton and works in
Ashton. Use Dijkstra’s algorithm to find the minimum
distance for Leon’s journey to work.
Chadderton 6 Dukinfield

3 5 7

Royton 3 Aston

12
3
4

Shaw 3 Lees 6 Mossley

2.4 Multiple start points


How do we cope with a situation in which instead of having a
single starting point and a single end point there are multiple
start points and a single end point? For example, in the Monte
Carlo rally cars start from a number of different countries but all
end up at the same finishing point. Dijkstra’s algorithm gives a
method of finding the shortest distance, as in the previous
example from A to J, but it is an identical problem to find the
shortest distance from J to A.

You might also like