Photo AI

Last Updated Sep 27, 2025

Floyd's Algorithm Simplified Revision Notes

Revision notes with simplified explanations to understand Floyd's Algorithm quickly and effectively.

user avatar
user avatar
user avatar
user avatar
user avatar

363+ students studying

10.4.2 Floyd's Algorithm

Floyd's algorithm, also known as the Floyd-Warshall algorithm, is a method for finding the shortest paths between all pairs of nodes in a weighted graph. It is particularly effective for dense graphs and works for both directed and undirected graphs, provided there are no negative weight cycles.

How Floyd's Algorithm Works

Key Concepts

  • The algorithm iteratively improves estimates of the shortest paths by considering each node as an intermediate step.
  • It updates a distance matrix and (optionally) a route matrix to track the shortest distances and routes between pairs of nodes.

Step-by-Step Process

  1. Initialisation:
  • Start with the adjacency matrix of the graph. If there is no direct connection between two nodes, the distance is set to ∞ (infinity), except for the diagonal entries (distance from a node to itself), which are 0.
  • Optionally, initialise a route matrix where the entry R[i][j]R[i][j]R[i][j]R[i][j] stores the node used to travel directly from ii to jj
  1. Iterative Updates:
  • Use each node kk (in turn) as an intermediate node to check if it provides a shorter path between any two other nodes ii and jj.
  • Update the distance matrix using the formula:
d[i][j]=min(d[i][j],d[i][k]+d[k][j])d[i][j] = \min(d[i][j], d[i][k] + d[k][j])
  • Update the route matrix to reflect the intermediate node kk if the shorter path is found.
  1. Repeat for All Nodes:
  • Perform these updates iteratively for k=1k=1 to nn, where nn is the number of nodes in the graph.
  1. Termination:
  • The algorithm completes when all possible intermediate nodes have been considered.

Worked Example

infoNote

Problem

Find the shortest paths between all pairs of nodes in the graph below:

Node PairWeight
A → B3
A → C
A → D7
B → A8
B → C2
B → D
C → A5
C → B
C → D1
D → A2
D → B
D → C

Initialisation Create the initial distance matrix D0D_0 and route matrix R0R_0:


Distance Matrix D0D_0:

ABCDA037B802C501D20\begin{array}{c|cccc} & A & B & C & D \\ \hline A & 0 & 3 & ∞ & 7 \\ B & 8 & 0 & 2 & ∞ \\ C & 5 & ∞ & 0 & 1 \\ D & 2 & ∞ & ∞ & 0 \\ \end{array}

Route Matrix R0R_0:

ABCDABDBACCADDA\begin{array}{c|cccc} & A & B & C & D \\ \hline A & - & B & - & D \\ B & A & - & C & - \\ C & A & - & - & D \\ D & A & - & - & - \\ \end{array}

First Iteration (k=Ak = A) Use AA as the intermediate node to update distances:

d[i][j]=min(d[i][j],d[i][A]+d[A][j])d[i][j] = \min(d[i][j], d[i][A] + d[A][j])

Updated Distance Matrix D1D_1:

ABCDA037B80215C5801D250\begin{array}{c|cccc} & A & B & C & D \\ \hline A & 0 & 3 & ∞ & 7 \\ B & 8 & 0 & 2 & 15 \\ C & 5 & 8 & 0 & 1 \\ D & 2 & 5 & ∞ & 0 \\ \end{array}

Updated Route Matrix R1R_1:

ABCDABDBACACAADDAB\begin{array}{c|cccc} & A & B & C & D \\ \hline A & - & B & - & D \\ B & A & - & C & A \\ C & A & A & - & D \\ D & A & B & - & - \\ \end{array}

Second Iteration (k=Bk = B) Use BB as the intermediate node to update distances:


Updated Distance Matrix D2D_2:

ABCDA0357B80215C5801D2570\begin{array}{c|cccc} & A & B & C & D \\ \hline A & 0 & 3 & 5 & 7 \\ B & 8 & 0 & 2 & 15 \\ C & 5 & 8 & 0 & 1 \\ D & 2 & 5 & 7 & 0 \\ \end{array}

Updated Route Matrix R2R_2:

ABCDABBDBACACAADDABB\begin{array}{c|cccc} & A & B & C & D \\ \hline A & - & B & B & D \\ B & A & - & C & A \\ C & A & A & - & D \\ D & A & B & B & - \\ \end{array}

Third Iteration (k=Ck = C)

Repeat using CC as the intermediate node.


Fourth Iteration (k=Dk = D)

Finally, use DD as the intermediate node.


Final Result

After all iterations, the final distance matrix and route matrix give the shortest paths between all pairs of nodes.

Note Summary

infoNote

Common Mistakes

  1. Incorrect Initialisation Forgetting to set d[i][i]=0d[i][i] = 0 and all non-edges to .

  2. Order of Iterations Not completing the updates row by row as required.

  3. Missing Updates Failing to check all pairs i,ji, j when using kk as the intermediate node.

  4. Route Matrix Errors Forgetting to update the route matrix when a shorter path is found.

  5. Negative Cycles Using the algorithm on graphs with negative weight cycles, which causes incorrect results.

infoNote

Key Formulas/Theorems

  1. Distance Update Rule
d[i][j]=min(d[i][j],d[i][k]+d[k][j])d[i][j] = \min(d[i][j], d[i][k] + d[k][j])
  1. Route Matrix Update If a shorter path is found, update R[i][j]R[i][j] to reflect the intermediate node kk.

  2. Termination Criterion The algorithm completes after nn iterations, where nn is the number of nodes.

Books

Only available for registered users.

Sign up now to view the full note, or log in if you already have an account!

500K+ Students Use These Powerful Tools to Master Floyd's Algorithm

Enhance your understanding with flashcards, quizzes, and exams—designed to help you grasp key concepts, reinforce learning, and master any topic with confidence!

30 flashcards

Flashcards on Floyd's Algorithm

Revise key concepts with interactive flashcards.

Try Further Maths Decision Maths 1 Flashcards

3 quizzes

Quizzes on Floyd's Algorithm

Test your knowledge with fun and engaging quizzes.

Try Further Maths Decision Maths 1 Quizzes

29 questions

Exam questions on Floyd's Algorithm

Boost your confidence with real exam questions.

Try Further Maths Decision Maths 1 Questions

27 exams created

Exam Builder on Floyd's Algorithm

Create custom exams across topics for better practice!

Try Further Maths Decision Maths 1 exam builder

50 papers

Past Papers on Floyd's Algorithm

Practice past papers to reinforce exam experience.

Try Further Maths Decision Maths 1 Past Papers

Other Revision Notes related to Floyd's Algorithm you should explore

Discover More Revision Notes Related to Floyd's Algorithm to Deepen Your Understanding and Improve Your Mastery

96%

114 rated

Shortest Path Algorithms

Dijkstra's Algorithm

user avatar
user avatar
user avatar
user avatar
user avatar

446+ studying

196KViews

96%

114 rated

Shortest Path Algorithms

Comparing Dijkstra's & Floyd's Algorithms

user avatar
user avatar
user avatar
user avatar
user avatar

441+ studying

196KViews
Load more notes

Join 500,000+ A-Level students using SimpleStudy...

Join Thousands of A-Level Students Using SimpleStudy to Learn Smarter, Stay Organized, and Boost Their Grades with Confidence!

97% of Students

Report Improved Results

98% of Students

Recommend to friends

500,000+

Students Supported

50 Million+

Questions answered