AdvancedAbout 30 min

    Floyd-Warshall Algorithm

    Read the full article on Substack

    6 min read.

    Find shortest paths between ALL pairs of vertices. Works with negative weights (but no negative cycles). Simple triple-nested loop.

    Think of it this way: Imagine finding shortcuts between cities. For each intermediate city K, you check: 'Is going through K faster than the direct route?' You do this for EVERY pair of cities, considering EVERY possible intermediate stop.

    317241ABCD
    ABCDABCD037∞∞01∞2∞04∞1∞0

    Start with direct edge weights; ∞ where there is no edge.

    1/21

    Check your understanding

    Answer from memory before you scroll back up. Pulling an idea out of your head is what makes it stay there.

    All pairs, one vertex at a time

    Sequence

    Order how Floyd-Warshall fills the distance matrix.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    1. 1Drop a step here
    2. 2Drop a step here
    3. 3Drop a step here
    4. 4Drop a step here
    5. 5Drop a step here
    5 pieces left to place

    Quiz

    20 questions
    Question 1 of 20
    Score: 0

    What is the recurrence relation in Floyd-Warshall?

    Done with this lesson?

    Sign in free to keep your progress, streak and quiz scores.

    Keep going