Steply

Shortest path from A to D: AB 1, BC 1, CD 1, AD 5

Step by step, with the reason for every step · Shortest path · Discrete Math

Step by step

The question

Question: find the shortest path from A to D.

from A to D: AB 1, BC 1, CD 1, AD 5

1 Draw the graph

The numbers on the edges are lengths: how far it is along each edge.

2 Start the table

A table: a column for each vertex.

Each cell holds how far it is from A so far, and, small, the vertex we came from.

A is 0 from itself.

0

The others are not reached yet: ∞, as far as can be.

∞
∞
∞

Careful!

Fewer edges is not the same as shorter.

We must add the lengths.

Careful: not A, D = 5 = 5 ✗

3 Round 1

The smallest open number is A: 0.

Nothing can beat it any more, so A is final.

B: 0 plus 1 is 1.

D: 0 plus 5 is 5.

from A: B: 0 + 1 = 1, D: 0 + 5 = 5

The next row: the new numbers, the rest copied down.

0
1A
∞
5A

4 Round 2

The smallest open number is B: 1.

Nothing can beat it any more, so B is final.

C: 1 plus 1 is 2.

from B: C: 1 + 1 = 2

The next row: the new numbers, the rest copied down.

–
1A
2B
5A

5 Round 3

The smallest open number is C: 2.

Nothing can beat it any more, so C is final.

D: 2 plus 1 is 3, less than 5.

Change it.

from C: D: 2 + 1 = 3 < 5

The next row: the new numbers, the rest copied down.

–
–
2B
3C

6 Round 4

The smallest open number is D: 3.

Nothing can beat it any more, so D is final.

7 The route back

Go back with the small letters: D from C, C from B, B from A.

D from C, C from B, B from A

That is the route.

The shortest path is A, B, C, D, length 3.

= A, B, C, D: length 3

8 Check

Add the lengths along the route: 1 + 1 + 1 = 3.

Right!

1 + 1 + 1 = 3

The route with the fewest edges, A, D, is 5: longer.

Right!

fewest edges A, D: 5 = 5 > 3

Let's look back.

First: draw the graph.

Then: start the table.

Then: round 1.

Then: round 2.

Then: round 3.

Then: round 4.

Then: the route back.

Last: check.

Answer
= A, B, C, D: length 3

Your turn

Try this one yourself, then check:

from A to D: AB 2, BC 2, CD 1, AD 4
Show the answer
A, D, length 4

More from Shortest path

The whole lesson: Shortest path →