Leaving Certificate Applied Mathematics2.8 Shortest paths and Dijkstra's algorithm

All books

2.8 Shortest paths and Dijkstra's algorithm — Practise with help

Warm-up: remember

  1. A network has 7 vertices. How many edges has a minimum spanning tree of it?
  2. On the same network, how many edges could a shortest path have?
Answers and feedback
  1. 7−1=67 - 1 = 6 edges.
  2. Anywhere from 1 (if the two vertices are joined) to 6 (if the route visits every vertex). A shortest path has no fixed length — that is the difference from a spanning tree.

Practise with help

1. A network has edges PQ=3PQ = 3, PR=6PR = 6, QR=4QR = 4. Find the shortest distance from PP to RR using Dijkstra's algorithm.

Hint

There are two routes: direct, and through QQ.

Next step

Direct: PR=6PR = 6. Through QQ: PQ+QR=3+4PQ + QR = 3 + 4.

Full working
Settled Distance Updates
PP 0 Q→3Q \to 3; R→6R \to 6
QQ 3 R→min⁡(6,3+4)=6R \to \min(6, 3+4) = 6
RR 6 done

The shortest distance is 6, and here the direct edge is as good as the detour. Both routes cost 6, so there are two shortest paths: P→RP \to R and P→Q→RP \to Q \to R.

2. A network has edges AB=5AB = 5, AC=1AC = 1, CB=2CB = 2, BD=3BD = 3, CD=10CD = 10. Find the shortest distance from AA to DD.

Hint

Settle AA, then the nearest unsettled vertex.

Next step

From AA: B→5B \to 5, C→1C \to 1. Settle CC next.

Full working
Settled Distance Updates
AA 0 B→5B \to 5; C→1C \to 1
CC 1 B→min⁡(5,1+2)=3B \to \min(5, 1+2) = 3; D→1+10=11D \to 1+10 = 11
BB 3 D→min⁡(11,3+3)=6D \to \min(11, 3+3) = 6
DD 6 done

The shortest distance is 6, along A→C→B→DA \to C \to B \to D: 1+2+3=61 + 2 + 3 = 6. The direct route A→B→DA \to B \to D costs 5+3=85 + 3 = 8, and going through CC is cheaper.

3. In that question, at the moment AA is settled, BB's tentative distance is 5. Why does it change to 3?

Hint

A tentative distance is a guess, and a guess can improve.

Next step

Settling CC at distance 1 opens a new route to BB: through CC.

Full working

When AA is settled, the only known route to BB is the direct edge AB=5AB = 5, so BB's tentative distance is 5. But 5 is a guess, not a fact — BB is not settled yet.

When CC is settled at distance 1, the algorithm looks at CC's neighbours. The route A→C→BA \to C \to B costs 1+2=31 + 2 = 3, which is less than 5, so the tentative distance is replaced by 3.

That is the point of keeping tentative distances: the algorithm improves its guesses as it learns more, and only fixes a distance when it settles the vertex.

4. A student runs Dijkstra's algorithm and settles a vertex at distance 7, then later finds a route to it of total 6. What has gone wrong?

Hint

Can a settled distance ever be improved?

Next step

No — that is the guarantee the algorithm rests on.

Full working

A settled distance can never be improved, so a route of total 6 found after settling a vertex at 7 means the algorithm was not run correctly. Something has gone wrong in one of two ways:

  • a tentative distance was not updated when a neighbour was settled, so the vertex was settled at 7 when its true distance was already 6; or
  • the wrong vertex was settled — the smallest tentative distance was not chosen, so a vertex was settled before its distance was final.

Both are the same mistake in different clothes: settling something before it was the smallest unsettled distance.

5. On the network AB=4AB = 4, AC=2AC = 2, BC=1BC = 1, BD=5BD = 5, CD=8CD = 8, CE=10CE = 10, DE=2DE = 2, DF=6DF = 6, EF=3EF = 3, find the shortest distance from AA to EE.

Hint

Run the same table as Example 1 and stop when EE is settled.

Next step

EE is updated twice: to 12 via CC, then to 10 via DD.

Full working

From Example 1 the settled distances are A=0A = 0, C=2C = 2, B=3B = 3, D=8D = 8, E=10E = 10, F=13F = 13.

The shortest distance from AA to EE is 10, along A→C→B→D→EA \to C \to B \to D \to E: 2+1+5+2=102 + 1 + 5 + 2 = 10.

The direct-looking route A→C→EA \to C \to E costs 2+10=122 + 10 = 12, so the longer way round is cheaper. This is why the algorithm is needed — you cannot tell by looking.

6. Explain why Dijkstra's algorithm cannot be used on a network with a negative edge weight.

Hint

The algorithm settles the nearest vertex because going further always costs more.

Next step

With a negative weight, is going further still more expensive?

Full working

The algorithm settles the nearest unsettled vertex because any other route to it would have to pass through an unsettled vertex whose distance is already at least as large, and every extra edge adds more.

A negative edge breaks that. A route could go to a vertex that looks far away, take a negative edge, and arrive back cheaper than the vertex the algorithm already settled. The settled distance would then be wrong, and the whole guarantee fails.

Every weight in this course is a distance, cost or time, so every weight is positive, and the algorithm is safe to use.

A question from Section 2.7

7. The minimum spanning tree of this network weighs 13, and the shortest path from AA to FF also weighs 13. A student concludes they are the same thing. Explain the mistake.

Hint

What does each answer consist of?

Next step

One is a set of edges reaching every vertex; the other is a route between two.

Full working

The two answers happen to share the same total here, but they are different objects. The minimum spanning tree is a set of 5 edges reaching every vertex. The shortest path is a single route from AA to FF.

They also answer different questions: "connect everything as cheaply as possible" and "get from here to there as cheaply as possible". A route that is cheapest between two vertices need not be part of the cheapest way to connect everything, and the cheapest way to connect everything is not a route at all.

The equality of the two totals is a coincidence of this network. Change DFDF from 6 to 4 and the tree changes while the shortest path does not, or vice versa. Never use one answer for the other question.

8. A network has 6 vertices. Dijkstra's algorithm from AA settles them in the order A,C,B,D,E,FA, C, B, D, E, F with distances 0,2,3,8,10,130, 2, 3, 8, 10, 13. Write down the shortest path from AA to FF, and explain how you found it.

Hint

Trace back from FF: which settled vertex updated it last?

Next step

F=13=10+3F = 13 = 10 + 3, and E=10E = 10.

Full working

Trace back from FF:

  • 13=10+313 = 10 + 3, and the edge EFEF has weight 3, so FF came from EE;
  • 10=8+210 = 8 + 2, and DE=2DE = 2, so EE came from DD;
  • 8=3+58 = 3 + 5, and BD=5BD = 5, so DD came from BB;
  • 3=2+13 = 2 + 1, and BC=1BC = 1, so BB came from CC;
  • 2=0+22 = 0 + 2, and AC=2AC = 2, so CC came from AA.

The shortest path is A→C→B→D→E→FA \to C \to B \to D \to E \to F. To check it, add the weights along the route: 2+1+5+2+3=132 + 1 + 5 + 2 + 3 = 13, which matches the settled distance of FF.

Settings

Colour scheme

Light or dark pages. Your colour and layout stay the same.

Colour

The paper, ink and lettering. Your layout stays the same.

Quiet colours
Bold colours
Layout

How pages are arranged. Your colours stay the same.

Reading offline

Keep this book on this device and read it offline.