Steply

Shortest path from A to C: AB 3, BC 4, AC 9

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 C.

from A to C: AB 3, BC 4, AC 9

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, C = 9 = 9 ✗

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 9 is 9.

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

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

0
3A
9A

4 Round 2

The smallest open number is B: 3.

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

C: 3 plus 4 is 7, less than 9.

Change it.

from B: C: 3 + 4 = 7 < 9

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

–
3A
7B

5 Round 3

The smallest open number is C: 7.

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

6 The route back

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

C from B, B from A

That is the route.

The shortest path is A, B, C, length 7.

= A, B, C: length 7

7 Check

Add the lengths along the route: 3 + 4 = 7.

Right!

3 + 4 = 7

The route with the fewest edges, A, C, is 9: longer.

Right!

fewest edges A, C: 9 = 9 > 7

Let's look back.

First: draw the graph.

Then: start the table.

Then: round 1.

Then: round 2.

Then: round 3.

Then: the route back.

Last: check.

Answer
= A, B, C: length 7

Your turn

Try this one yourself, then check:

from A to C: AB 3, BC 3, AC 8
Show the answer
A, B, C, length 6

More from Shortest path

The whole lesson: Shortest path →