D1 2.2 - Floyd's algorithm

D1 2.2 - Floyd's algorithm

You will construct directed distance and route tables, perform Floyd's iterations in the specified order, and read complete shortest routes from the final next-hop table.

All pairs of nodes, not one starting node

An initial table says the distance from DD to CC is ∞\infty, but the completed table says 1313. Has the algorithm invented a road? No: it has found an indirect route through roads already present.

You already know

Dijkstra gives shortest distances from one specified start. A directed arc can be travelled only in its arrow direction. A route's length is the sum of its arc weights.

Floyd finds shortest distances between every ordered pair of nodes in one sequence of tables. Four nodes give twelve ordered pairs with different endpoints. A route from AA to BB need not have the same distance as a route from BB to AA.

[DIAGRAM: asset_name: A directed network for Floyd's algorithm; asset_slug: 9fm0-d1-168-directed-network; caption: All six arcs are directed; use the arrow direction when constructing the initial table.; description: Four nodes A, B, C and D with directed arcs A to B weight4, A to C weight12, B to C weight3, B to D weight10, C to D weight2 and D to A weight6; the diagonal crossing has no node.]
All six arcs are directed

Distance table and route table

In a distance table, row ii, column jj gives the current best distance from ii to jj. In the corresponding next-hop route table, that entry gives the first node to go to after leaving ii on the recorded route to jj.

The initial distance table records direct arcs only. An ∞\infty entry means no direct arc is available in that direction; later iterations may replace it by a finite indirect distance. If it remains ∞\infty in the final table, the destination cannot be reached from that start.

Key idea

Read rows as origins and columns as destinations. Floyd's distance table records lengths; its route table records next nodes, not distances or complete routes.

Separate “no direct arc” from “no route at all”.

Constructing the initial tables

Use the stated node order for both rows and columns. Copy each directed arc into its own row-to-column position; do not automatically put the same weight in the reverse position.

Initial table conventions

The initial distance entry is the direct arc weight, or ∞\infty if there is no direct arc. Use a dash on the diagonal, interpreted as distance 00 in calculations.

In Pearson's initial route table, each row is the list of column names: A,B,C,DA,B,C,D here. An entry corresponding to an infinite distance is a placeholder, not evidence that a route exists.

With a finite direct arc, the first node after leaving the origin is simply its destination. That explains the repeated column names in the route table. The unused diagonal route entries can retain their own node names.

Not in the formula booklet: learn it.

Worked example 1: direction matters

Question. Construct the initial distance and route tables, in order A,B,C,DA,B,C,D, for

A→B:4,A→C:12,B→C:3,B→D:10,C→D:2,D→A:6A\to B:4,\quad A\to C:12,\quad B\to C:3,\quad B\to D:10,\quad C\to D:2,\quad D\to A:6

Plan. Enter only the six direct arcs, fill missing directed connections with ∞\infty, then write the initial next-hop table.

The distance table is

D(0)=ABCDA−412∞B∞−310C∞∞−2D6∞∞−\mathbf D^{(0)}= \begin{array}{c|rrrr} &A&B&C&D\\ \hline A&-&4&12&\infty\\ B&\infty&-&3&10\\ C&\infty&\infty&-&2\\ D&6&\infty&\infty&- \end{array}

The route table is

R(0)=ABCDAABCDBABCDCABCDDABCD\mathbf R^{(0)}= \begin{array}{c|cccc} &A&B&C&D\\ \hline A&A&B&C&D\\ B&A&B&C&D\\ C&A&B&C&D\\ D&A&B&C&D \end{array}

Why is row BB, column AA infinite despite A→BA\to B existing? The road has only the direction AA to BB; reversing it would invent an arc.

Answer. The two initial tables are displayed above.

Check. Exactly six off-diagonal distance entries are finite, matching the six directed arcs. The table is not symmetric.

Worked example 2 changes only D→AD\to A, from weight 66 to weight 99. Its reverse entry is still unavailable.

Worked example 2: changing one directed weight

Question. Construct the initial tables for the same network when D→AD\to A has weight 99.

Plan. Change only row DD, column AA of the distance table; retain the next-hop initialisation.

D(0)=ABCDA−412∞B∞−310C∞∞−2D9∞∞−\mathbf D^{(0)}= \begin{array}{c|rrrr} &A&B&C&D\\ \hline A&-&4&12&\infty\\ B&\infty&-&3&10\\ C&\infty&\infty&-&2\\ D&9&\infty&\infty&- \end{array} R(0)=ABCDAABCDBABCDCABCDDABCD\mathbf R^{(0)}= \begin{array}{c|cccc} &A&B&C&D\\ \hline A&A&B&C&D\\ B&A&B&C&D\\ C&A&B&C&D\\ D&A&B&C&D \end{array}

Why does the route table not change? The direct DD-to-AA route still has AA as its next node; only its cost changed.

Answer. The distance entry dDAd_{DA} is 99, while dADd_{AD} remains ∞\infty; the initial route table is unchanged.

Check. The six directed connections are the same. A weight change has neither added a road nor reversed one.

A question may supply the tables rather than the network. Read its symbols and node order before starting; a matrix of direct connections and a completed matrix of shortest distances serve different purposes.

Watch out

Wrong: mirror a one-way arc across the diagonal, or put zero for a missing road. Right: row-to-column direction controls the entry, and an unavailable direct arc is ∞\infty.

Change the same directed weight again.

Completing the iterations

Unless directed otherwise, use AA as the first pivot, then BB, then CC, then DD. At each iteration, the pivot's row and column stay unchanged; use them to test every other origin-destination pair.

Floyd's distance and next-hop updates

At the iteration using pivot node kk, compare the current route ii to jj with going via kk:

dijnew=min⁡ ⁣(dijold, dikold+dkjold)d_{ij}^{\mathrm{new}}= \min\!\left(d_{ij}^{\mathrm{old}},\ d_{ik}^{\mathrm{old}}+d_{kj}^{\mathrm{old}}\right)

If the candidate is strictly smaller, replace the distance and copy

rijnew=rikoldr_{ij}^{\mathrm{new}}=r_{ik}^{\mathrm{old}}

Otherwise retain both entries. Treat a diagonal dash as 00, and ∞\infty plus a finite number as ∞\infty.

Not in the formula booklet: learn it.

Why copy rikr_{ik}, not simply write kk? The first leg from ii to kk may already be an indirect route. Its first step is the first step of the new complete route too. On an equal distance, retaining the old route entry keeps one consistent shortest route.

[DIAGRAM: asset_name: Testing an entry using a pivot row and column; asset_slug: 9fm0-d1-168-pivot-update; caption: At the B iteration, D to C improves from 18 to 13, but its next hop remains A.; description: The distance table after the A iteration has row B and column B shaded; the calculation D to B10 plus B to C3 gives13 instead of18, with next-hop copying from row D column B equal to A.]
At the B iteration, D to C improves from 18 to 13, but its next hop remains A.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account