Steply

Shortest path from A to D: AB 3, AC 1, BC 1, BD 2, CD 6

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 3, AC 1, BC 1, BD 2, CD 6

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, B, D = 3 + 2 = 5 ✗

3 Round 1

The smallest open number is A: 0.

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

B: 0 plus 3 is 3.

C: 0 plus 1 is 1.

from A: B: 0 + 3 = 3, C: 0 + 1 = 1

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

0
3A
1A
∞

4 Round 2

The smallest open number is C: 1.

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

B: 1 plus 1 is 2, less than 3.

Change it.

D: 1 plus 6 is 7.

from C: B: 1 + 1 = 2 < 3, D: 1 + 6 = 7

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

–
2C
1A
7C

5 Round 3

The smallest open number is B: 2.

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

D: 2 plus 2 is 4, less than 7.

Change it.

from B: D: 2 + 2 = 4 < 7

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

–
2C
–
4B

6 Round 4

The smallest open number is D: 4.

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

7 The route back

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

D from B, B from C, C from A

That is the route.

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

= A, C, B, D: length 4

8 Check

Add the lengths along the route: 1 + 1 + 2 = 4.

Right!

1 + 1 + 2 = 4

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

Right!

fewest edges A, B, D: 3 + 2 = 5 > 4

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, C, B, D: length 4

Your turn

Try this one yourself, then check:

from A to D: AB 2, AC 2, BC 1, BD 2, CD 5
Show the answer
A, B, D, length 4

More from Shortest path

The whole lesson: Shortest path →