Graphs

Problem shape

Advanced
A
B
C
D
E
Core idea

Try every node as an intermediate stop between every pair of nodes.

Step 1 / 5
Problem shape
Current lesson

Floyd-Warshall Lab

Graphs Advanced

Floyd-Warshall

Welcome to the Floyd-Warshall algorithm lab. Try every node as an intermediate stop between every pair of nodes. Use it when: Use for all-pairs shortest paths on small dense graphs. The time complexity is O(V^3), and the space complexity is O(V^2). The pseudocode is: for k from 0 to V - 1 for i from 0 to V - 1 for j from 0 to V - 1 dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

O(V^3)

Space

O(V^2)

Pseudocode

for k from 0 to V - 1
  for i from 0 to V - 1
    for j from 0 to V - 1
      dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
Open deeper structure lesson
1.0x