Top Banner
2 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. 8 10 7 11 9 11 9 8 C E F A B D CHAPTER 2 Shortest path problem (Dijkstra’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.
21

CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

May 14, 2018

Download

Documents

ngotu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

2

2.1 IntroductionWhen you plan a journey, there are different factors you mightconsider:

● 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 andfinish points of a journey and the program works out the differentroutes, 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 themotorist 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 ofthe diagram to another, in this case from A to F.

8

107

11

9

11 9

8

C E

FA

B D

C H A P T E R 2

Shortest path problem(Dijkstra’s algorithm)

Learning objectivesAfter 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.

Page 2: CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

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 ifthe network were more complex then this method, called acomplete enumeration, would become impractical. This chapteruses an algorithm to find the shortest path.

2.2 Triangles in networksConsider a real-life situation in which we wish to travel fromRoyton to London.

The diagrams below show roads connecting Royton, Birminghamand London.

The numbers on the first diagram show the distances, in miles,between the towns.

There are two possible routes: Royton direct to London, which is185 miles, or Royton to Birmingham and then Birmingham toLondon, 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 6�

12� hours. The time from Royton to Birmingham is 1�

12� hours and

Birmingham to London 2 hours. Hence we have a triangle withsides of length 1�

12�, 2 and 6�

12�! The right-hand diagram is an

impossible triangle.

This is acceptable because the diagram shows a networkrepresenting a real-life situation and not a scale drawing.

Real-life problems may not obey the triangle inequality.

B

100 1

110

185

L

R

B

L

R

12

2

6 12

Page 3: CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

Shortest path problem (Dijkstra’s algorithm) 25

2

2.3 Dijkstra’s algorithmIn 1959, Edsger Dijkstra invented an algorithm for finding theshortest path through a network. The following is a simple set ofinstructions that enables students to follow the algorithm:

Note: When a vertex is boxed you do not reconsider it. You needto show all temporary labels together with their crossings out.

Worked example 2.1Find the shortest distance from A to J on the network below.

SolutionStep 1 Label A as 0.

Step 2 Box this number.

Step 3 Label values of 4 at B, 8 at D and 5 at C.

Step 4 Box the 4 at B.

Step 5 From B, the connected vertices are D and E. The distancesat these vertices are 7 at D (4 � 3) and 16 at E (4 � 12).

A J

B E

108 15

H

C F11 11

12 10

I

D

4

51 4 5 5

1439

63

8

G

Step 1 Label the start vertex as 0.

Step 2 Box this number (permanent label).

Step 3 Label each vertex that is connected to the startvertex with its distance (temporary label).

Step 4 Box the smallest number.

Step 5 From this vertex, consider the distance to eachconnected vertex.

Step 6 If a distance is less than a distance already at thisvertex, cross out this distance and write in the newdistance. If there was no distance at the vertex,write down the new distance.

Step 7 Repeat from step 4 until the destination vertex isboxed. If a vertex is boxed then you do

not write down a newtemporary value. This would be acomplete enumeration.

A

B

8

C

D

0

4

5

Sometimes edges of networkshave arrows that show youwhich directions you must gofrom one vertex to the next.These are called directednetworks. To apply Dijkstra’salgorithm to a directed network,at each stage only consider edgesthat lead from the vertex.

Page 4: CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

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

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

Step 5 From C, the connected vertices are D and F. The distancesat 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.

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

Step 5 From D, the connected vertices are E, F and G. Thedistances 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 atE, cross out the 16. As the distance at F is 10, lower thanthe 16 currently at F, cross out the 16.

A

B

C

D0

4

5

6G

16

E

16 10F

8

16 15

7

A

B

C

D0

4

5

16 E

16F

8 7 6

A

B

8 7

C

D0

4

5

16 E

26 Shortest path problem (Dijkstra’s algorithm)

Page 5: CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

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

Step 5 From F, the connected vertices are G and I. The distancesat 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 atG, cross out the 16.

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. Thedistances at these vertices are 21 at G (15 � 6) and 25 atH (15 � 10). Do not write down the value of 21 at G asthis is greater than the number already there.

Step 6 There are no improvements, so there is no crossing out.

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

Step 5 From G, the connected vertices are H, I and J. Thedistances at these vertices are 18 at H (15 � 3), 20 at I (15 � 5) and 30 at J (15 � 15).

A

B

C

D0

4

5

6 G

E

16 2110FI

8

16 15

16 157

H25

A

B

C

D0

4

5

6 G

E

16 2110FI

8

16 15

16 157

Shortest path problem (Dijkstra’s algorithm) 27

2

Page 6: CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

Step 6 As the distance at H is 18, lower than the 25 currently atH, cross out the 25. As the distance at I is 20, lower thanthe 21 currently at I, cross out the 21.

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

Step 5 From H, the connected vertex is J. The distance at thisvertex is 32 (18 � 14). Do not write down the value of 32at J as this is greater than the 30 already there.

Step 6 There are no improvements, so there is no crossing out.

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

Step 5 From I, the connected vertex is J. The distance at thisvertex is 28 (20 � 8).

Step 6 As the distance at J is 28, lower than the 30 currently atJ, cross out the 30.

A

B

C

D0

4

5

6 G

E

16 10 20FI

8

21

2830

16 1525 18

16 15

7

H

J

A

B

C

D0

4

5

6 G

E

16 10FI

8

21 20

16 1525 18

16 15

7

H

J 30

A

B

C

D0

4

5

6 G

E

16 10FI

8

16 15

16 15 25 18

21 20

J 307

H

28 Shortest path problem (Dijkstra’s algorithm)

Page 7: CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

Step 7 The final vertex, in this case J, is not boxed. The boxednumber at J is the shortest distance. The routecorresponding to this distance of 28 is ACDFGIJ, but thisis not immediately obvious from the network.

Worked example 2.2Find the shortest distance from A to H on the network below.

SolutionThe fully labelled diagram below shows the values, bothtemporary and permanent, at each vertex.

EXERCISE 2A1 Use Dijkstra’s algorithm on the networks below to find the

shortest distance from A to H.

(a) (b)

A H

B D F

C E G

12 115

0

19 18

31 30

20 1913 126

A H

B D F

C E6 7

7 8

G

5

65 6

1398

11

Shortest path problem (Dijkstra’s algorithm) 29

2

How do we retrace the routethat corresponds to this shortestnetwork? This problem will bedealt with later in the chapter.

Any answer to an exam questionmust have exactly the sameamount of detail as shown here.

B E

D 4

A H10 85

118 5

7 117

5

C F

7

G

B D

C 6

A H

5 12

1010

8

6 5

7

E

14

F

G

Page 8: CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

2 The diagram below shows roads connecting towns near toRochdale. The numbers on each arc represent the time, inminutes, required to travel along each road. Peter is deliveringbooks from his base at Rochdale to Stockport. Use Dijkstra’salgorithm to find the minimum time for Peter’s journey.

3 The diagram below shows roads connecting villages near toRoyton. The numbers on each arc represent the distance, inmiles, along each road. Leon lives in Royton and works inAshton. Use Dijkstra’s algorithm to find the minimumdistance for Leon’s journey to work.

2.4 Multiple start pointsHow do we cope with a situation in which instead of having asingle starting point and a single end point there are multiplestart points and a single end point? For example, in the MonteCarlo rally cars start from a number of different countries but allend up at the same finishing point. Dijkstra’s algorithm gives amethod of finding the shortest distance, as in the previousexample from A to J, but it is an identical problem to find theshortest distance from J to A.

3 7

34

12

3

5

6Chadderton Dukinfield

LeesShaw 63 Mossley

AstonRoyton

Bury 17

915

10

148

14 17 13

2025

Trafford

StockportRochdaleManchester

Oldham 23 Ashton

30 Shortest path problem (Dijkstra’s algorithm)

Page 9: CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

Worked example 2.3Three boys walk to school, J, from their homes at A, B and C. The diagram shows the network ofroads near their homes and school. The numbers on each arc represent the distance, in metres, along each road. Use Dijkstra’s algorithm from J to find which boy lives nearest to the school.

SolutionIf we were to apply Dijkstra’s algorithm in the normal way, theworkings would be difficult to follow. There would be temporaryand permanent labels from each of the three starting points.Working backwards from J, and applying the standardalgorithm, the worked solution below is obtained.

The boy living in house A lives closest to the school.

EXERCISE 2B1 Use Dijkstra’s algorithm, starting from H, on the networks

below to find which of the vertices A or B is nearer to H.(a) (b)

HE

IF

DJ

G

A

B

C600

590

580

390400

480

250

300

0

280

490500

360370

Shortest path problem (Dijkstra’s algorithm) 31

2

A

BD

G

E

C

F5

2

9

8

2

46

H

8

7

A C E G

B 7 4 10

5 6 6

4

6

73

10

D F H

If there are multiple start points, then we apply Dijkstra’salgorithm from the end point until we have reached each ofthe starting points. In this way we can find the shortest route.

HE

250250

10090 300

280110

150

120130

IF 80240

110

190

DJ

G

90A

B

C

Page 10: CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

2.5 LimitationsWhilst Dijkstra’s algorithm gives an exact method of finding theshortest distance connecting two points, it doesn’t work if any ofthe edges have a negative value. How can you have an edge ofnegative length? If we are talking about distances then obviouslysomething like this is impossible, but consider the followingsituation.

A catalogue delivery firm calculates the cost of delivering along avariety of routes. Each of the distances is represented by an actualcost. However, if whilst on a delivery a delivery van also makes acollection, then the value of making this collection en route maysave more money than it would normally cost by driving alongthis road. Hence you may have an edge that has a negative value.

If we accept that negative lengths are possible, then it willbecome immediately obvious that Dijkstra’s algorithm won’twork because using the algorithm we don’t revisit anywhere thathas already been boxed, yet if we come to a vertex then fromthere a negative length may produce a value that is shorter thanone that is already boxed. In such situations, where Dijkstra’salgorithm fails, the normal approach is to use dynamicprogramming (see D2).

Worked example 2.4The following network shows the cost, in pounds, of travellingalong a series of roads. Use Dijkstra’s algorithm to find theminimum cost of travelling from A to F.

B D

A F

C 9

7

3

�3

6

8 9

12

E

32 Shortest path problem (Dijkstra’s algorithm)

If we use Dijkstra’s algorithm on a network containing anedge that has a negative value it does not work.

Page 11: CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

SolutionWorking through the network in the standard way, the workeddiagram below is obtained.

At F, the figure of 16 is boxed and hence the question is finished.However, from E to F a value of �3 reduces the minimum cost ofgetting to F to 14!

2.6 Finding the routeWe can use Dijkstra’s algorithm to find the length of the shortestroute between two vertices in a network. However, if we want tofind the corresponding route, we need to record moreinformation as we apply the algorithm.

Worked example 2.5The network below was used in Worked example 1.

JAD G

C F I

B E12 10

3 649

5 41

5

143

5 8

11 11

H

8 10 15

B D

A F

C E

6

0

8 18 17

16

13

Shortest path problem (Dijkstra’s algorithm) 33

2

Instead of listing temporary values, we put a letter aftereach value, which indicates the preceding vertex on theroute. We find the route by backtracking through thenetwork from the finishing point.

Page 12: CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

Solution

Box A has a value of zero.At B, the distance from A is 4 so we now write 4A.At D, the distance from A is 8 so we write 8A and at C we write 5A.We then box the value of 4 at B, so 4A is now boxed.From B, the value at E becomes 16B. At D we get 7B.Box the smallest number, which is the value of 5A at C, so 5A isboxed at C.From there, there is a value of 16C at F and a value of 6C at D.We box the smallest value, which is 6C at D.At D there is 8A crossed out, 7B crossed out and 6C, which hasbeen boxed. This tells us that to get to D the smallest distance is6 and we came from vertex C.Working from the finishing point J we have a boxed value of 28Iso we now look at vertex I. Here the boxed value is 20G so wenow look at vertex G, and so on until we return to A. Hence theshortest path is ACDFGIJ, with length 28.

EXERCISE 2CRepeat Exercises 2A and 2B to find the routes that correspond tothe minimum distances.

JAD G

C F I

B E H

20G10D5A

0

4A 18G

21F

28I30G

15F16D

16C

15D16B

6C8A 7B

34 Shortest path problem (Dijkstra’s algorithm)

Page 13: CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

MIXED EXERCISE1

(a) Use Dijkstra’s algorithm on the above diagram to find theminimum time to travel from A to J, and state the route.

(b) A new road is to be constructed connecting B to G. Findthe time needed for travelling this section of road if theoverall minimum journey time to travel from A to J isreduced by 10 minutes. State the new route. [A]

2 The following network shows the time, in minutes, of trainjourneys between seven stations.

(a) Given that there is no time delay in passing through astation, use Dijkstra’s algorithm to find the shortest timeto travel from A to G.

(b) Find the shortest time to travel from A to G if in realityeach time the train passes through a station, excluding A and G, an extra 10 minutes is added to the journeytime. [A]

D

C

E

G

F

A

B 65

2520

60

35

30

30

50

357540

35

B E 108 I

J

H

FC 90

112

208

A D

G

96 20

78

5618

16

46

20

62

56

40

29

21

110

Shortest path problem (Dijkstra’s algorithm) 35

2

Page 14: CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

3 The following network shows two islands, each with 7 smalltowns. One road bridge connects the two islands. Valuesshown represent distances by road, in miles.

Use Dijkstra’s algorithm to find the shortest distance betweenA and N, stating the route. [A]

4 The following diagram shows main roads connecting placesnear to Manchester, where the values shown represent thedistances in miles. Mark lives in Rochdale and works inTrafford.

(a) Use Dijkstra’s algorithm to find the shortest distancefrom Rochdale to Trafford. Write down the correspondingroute.

(b) A new orbital motorway is built around Manchester as shown in the diagram below. The values shown represent the distances in miles.

Mark has access to the new motorway at points X and Y. Due to traffic conditions, he can drive at 20 mph on all main roads and 40 mph on the motorway.Find the minimum time for Mark to travel from Rochdale to Trafford and state the route he shouldtake. [A]

Bury (B)

Rochdale (R)

Oldham (O)

Manchester (M)

Ashton (A)5

3

6

7

9 12

6

513

8

1211

7

11

Sale (S)

Trafford (T)

Eccles (E)

B F I 6 L

J 4

7

H

8

1 2

M

KN

4

4

7

4

4

2

9

3

C 3

6

A

5

2 5

D

E G

1

1

7

51

3

36 Shortest path problem (Dijkstra’s algorithm)

22

T

Y

R 2.5

25

3

5

X

Page 15: CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

5 Every day, Mary thinks of a rumour to spread on her way toschool. The rumour is then spread from one person to another.The following network shows the route through which therumour spreads. The number on each arc represents the time,in minutes, for the rumour to spread from one person to another.

(a) Use Dijkstra’s algorithm to find the time taken for therumour to reach each person.

(b) List the route through which Brendan first hears the rumour.(c) On a particular day Pauline is not at school. Find, by

inspection, the extra time that elapses before Brendanfirst hears the rumour for that day. [A]

6 Three boys, John, Lee and Safraz, are to take part in a runningrace. They are each starting from a different point but they allmust finish at the same point N.

John starts from the point A, Lee from the point B and Safrazfrom the point C.

The following diagram shows the network of streets that theymay run along. The numbers on the arcs represent the time, inseconds, taken to run along each street.

(a) Working backwards from N, or otherwise, use Dijkstra’salgorithm to find the time taken for each of the threeboys to complete the course. Show all your working ateach vertex.

(b) Write down the route that each boy should take. [A]

D G J

1041 40 30

2525

40

50 10

25 22 10

1015 15

18

6020

8030

15 8 915 12 11

M

F I L O

BE H

KN Finish

A

C

Angela Rose Kevin

Ged

Liz

Hugo

Nicola

PaulineBrendanTerryMary

SteveCathJudith

12 21

3

8

7

19

69

33

412

977

4

5

6

7

2

14 3

13

Shortest path problem (Dijkstra’s algorithm) 37

2

Page 16: CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

7 A school is organising a short road race in which pupils have to start at A and use their own choice of route to reach J as quickly as possible. The diagram below shows the network of roads available and, for each road, the minimum completion time, in seconds, for a girl in the school.

Mia, the best girl runner in the school, is planning her strategy. Given that she is able to run each section in the minimum time, use Dijkstra’s algorithm to find the route she should take and her time for the race. [A]

8 An insurance salesman has to drive from his home at A to hishead office at L. The time, in minutes, for each section of thejourney is shown in the diagram below.

(a) Use Dijkstra’s algorithm to find the minimum time for thetotal journey and state the route the salesman should take.

(b) A new road is constructed joining D to F, as shown below.The journey time for this section of road is x minutes.Find an expression, in terms of x, for the minimum timefor the journey from A to L using the new road. [A]

C G

L

IFEB

AD H45

9

5

8

17

7

11

18

11

35

28

20

12

x

12

7

32

12

18

15

17

K

J

C G

L

IFEB

AD H45

9

5

8

17

7

11

18

11

35

28

20

12

12

7

32

12

18

15

17

K

J

38 Shortest path problem (Dijkstra’s algorithm)

D

A(Start)

J(Finish)

220

60210

120 100 80

12012080 70 150 65

50 150 70

200FC

90E I

B 130 130G H

Hint for Question 7: See marginnote on page 25.

Page 17: CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

9 The following diagram shows motorways and main roadsconnecting towns in Sicily. The numbers represent the timestaken, in minutes, to drive along each road. There are twoairports on the island, one at Catania (C) and one at Palermo(P).

Stella plans to fly to Catania and then drive to Agrigento.

(a) Find, by inspection, the minimum time for Stella todrive from Catania to Agrigento.

(b) Due to the volcano at Mount Etna erupting, Stella’sflight is diverted to Palermo.(i) Use Dijkstra’s algorithm to find the minimum time

to drive from Palermo to Agrigento.(ii) State the route that she should take.

(c) Stella drives at 50 km/h on main roads and 100 km/h onmotorways. Given that she keeps her driving time to aminimum, find the extra distance that she would havehad to drive if she had landed at Catania airport ratherthan at Palermo airport. [A]

10 The following diagram shows the lengths, in miles, of roads connecting 10 towns.(a) Use Kruskal’s algorithm, showing the

order in which you select the edges, to find the minimum spanning tree for the network. Draw your minimum spanning tree and state its length.

(b) (i) Use Dijkstra’s algorithm to find the shortest distance from A to J. State the route corresponding to this minimum distance.

(ii) A new road is built connecting F to I. The length ofthis road is x miles, where x is an integer. A shorterroute from A to J than that found in (b)(i) is nowavailable.Find the maximum value of x. [A]

24

170

Motorway

75

75

35

63

2090

5058

8095

55

MountEtna Catania (C)

Enna (E)

Termini (T)

Agrigento (A)

Corleone(Co)

Mazarro(M)

Palermo (P)

Lercara (L)

Main road

Shortest path problem (Dijkstra’s algorithm) 39

2

B

A JD

8 2

5 14 12

8

13 3 6 4

1

9

16 539 15

7

G

E H

C F I

Page 18: CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

11 A railway company is considering opening some new linesbetween seven towns A–G. The possible lines and the cost ofsetting them up (in millions of pounds) are shown in thefollowing network.

(a) Use Dijkstra’s algorithm to find the minimum cost ofopening lines from A to E. Show all your workings ateach vertex.

(b) From your workings in (a) write down the minimumcost of opening lines:(i) from A to B(ii) from A to F.

(c) Use Kruskal’s algorithm to find the minimum cost ofopening lines so that it is possible to travel between anytwo of the towns by rail, and state the lines whichshould be opened in order to achieve this minimum cost.

(d) The rail company wants to open some of the lines sothat it is possible to travel by rail starting at one town,finishing at another and passing through each of theother five towns exactly once. Which lines should therail company open in order to do this as cheaply aspossible? [A]

12 The diagram represents the roads joining 10 villages, labelled A to J. The numbers give distances in kilometres.(a) Use Dijkstra’s algorithm to find a

shortest route from A to J. Explain the method carefully, and show all your working. Give a shortest route and its length.

(b) A driver usually completes this journey driving at an average speed of 60 km/h. The local radio reports a serious accident at village E, and warns drivers of adelay of 10 minutes.Describe how to modify your approach to (a) to find thequickest route, explaining how to take account of thisinformation. What is the quickest route, and how longwill it take? [A]

AC

8 E

3020

11

40

20

20

10

5

48

35

B D

G F

40 Shortest path problem (Dijkstra’s algorithm)

D

B17

13

15 15

15

13

14

119

3

77

3

9

7

7

E

9

A J

C

G

F

I

H

Page 19: CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

13 The network shows the roads around the town of Kester (K)and the times, in minutes, needed to travel by car along those roads.

(a) A motorist wishes to travel from A to C along these roads in the minimum possible time. Use Dijkstra’s algorithm to find the route the motorist should use and the time that the journey will take. Show all your workings clearly.

(b) The four sections of ring road AB, BC, CD and DA each require the same amount of time, and next year there will be improvements to the ring road in order to reduce this time from 30 minutes to mminutes. This will enable the motorist to reduce the minimum time for a journey from A to C by 2 minutes. Find the value of m and state his new route. [A]

14 The network shows the distances in kilometres of variousroutes between points S, T, U, V, W, X, Y and Z.

Use Dijkstra’s algorithm to find the shortest path from S to Z.Show your working. [A]

W

S

X

T

105

Y

U12 6 8

4 15 7 Z

V

3 86 5 4

Shortest path problem (Dijkstra’s algorithm) 41

2

1 Real-life problems may not obey the triangle p24inequality.

2 Dijkstra’s algorithm enables the shortest path p25between two points to be found.

3 Dijkstra’s algorithm is equally valid when used p30backwards through a network.

4 Dijkstra’s algorithm fails if there are negative p32edges in a network.

5 Tracing a route through a network can be easily p33found if careful labelling of Dijkstra’s algorithm is used.

Key point summary

12

7

8

8

1110 20 20

30

30

30

30

5 29

28 8

B

D

A C

H

K

F

E G

Page 20: CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

42 Shortest path problem (Dijkstra’s algorithm)

1 Use Dijkstra’s algorithm to find the shortest distance from A Section 2.3to E in the networks below.

(a) (b)

2 Find the route corresponding to the shortest distance for Section 2.6question 1.

3 The network below has four vertices and five edges. Sections 2.3, 2.6

Given that the second shortest route from A to D is ABCD, find the range of values of x.

4 The network below has eight vertices and nine edges. Section 2.4

Find which of the vertices A, B or C is nearest to vertex H.

A

B

D F

E G

H5

8

7

5

6

8

8

11

6

C

A D

8 x

15

5

10

B C

A 9 6C E

B 5

5 3 41

D

D

A E

B

C

7

4 8

2

16 6

4

What to reviewTest yourself

Page 21: CHAPTER 2 Shortest path problem (Dijkstra’s algorithm) 2€¦ · Shortest path problem (Dijkstra’s algorithm) 25 2 2.3 Dijkstra’s algorithm In 1959, Edsger Dijkstra invented

Shortest path problem (Dijkstra’s algorithm) 43

2

ANSWERSTest yourself

1(a)

(b)

2(a)ACDE

(b)ABCDE

32�x�5

4A

B

DF

EG

H

C

1615

21

21

21

20

137

0

5

ACE

BD

981413

109 5

0

D

CAE

B

121110

76

0

65

4