Lesson 28 +30 XP

Floyd-Warshall & Topological Sorting (Beyond Syllabus)

Floyd-Warshall & Topological Sorting (Beyond Syllabus)

🧭 Beyond syllabus. The syllabus asks for "shortest path algorithms"; Dijkstra is the core one. Floyd-Warshall (all pairs) and topological sorting appear in the notes' practice list and in many university papers as extensions.

Part A-Floyd-Warshall: All-Pairs Shortest Paths

Dijkstra finds distances from one source. Floyd-Warshall finds the shortest distance between every pair of vertices, works with negative edges (but no negative cycles), and is a classic example of dynamic programming.

Idea: Dk[i][j] = the shortest path from i to j that may pass only through intermediate vertices 1…k.

Dk[i][j] = min(Dk-1[i][j], Dk-1[i][k] + Dk-1[k][j])

C
void floydWarshall(int n, int d[][MAX]) {
    for (int k = 0; k < n; k++)              /* allow vertex k as a stop-over */
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                if (d[i][k] + d[k][j] < d[i][j])
                    d[i][j] = d[i][k] + d[k][j];
}

Three nested loops → O(V3) time, O(V2) space. (Use a large number for ∞ and guard against overflow.)

Worked Example (4 vertices, directed)

Edges: 1→2 (3), 1→4 (7), 2→1 (8), 2→3 (2), 3→1 (5), 3→4 (1), 4→1 (2).

TEXT
D0 (direct edges)      D1 (via 1)            D2 (via 1, 2)
  0  3  ∞  7             0  3  ∞  7            0  3  5  7
  8  0  2  ∞             8  0  2 15            8  0  2 15
  5  ∞  0  1             5  8  0  1            5  8  0  1
  2  ∞  ∞  0             2  5  ∞  0            2  5  7  0

D3 (via 1, 2, 3)       D4 (via all) = answer
  0  3  5  6             0  3  5  6
  7  0  2  3             5  0  2  3
  5  8  0  1             3  6  0  1
  2  5  7  0             2  5  7  0

Example: in D4, 2 → 1 costs 5 via 2 → 3 → 4 → 1 (2 + 1 + 2), better than the direct edge of 8.

Part B-Topological Sorting

A topological order of a directed acyclic graph (DAG) lists the vertices so that for every edge u → v, u comes before v. It exists only if the graph has no cycle.

Uses: course prerequisites, build systems (compile dependencies first), task scheduling, spreadsheet formula evaluation.

Kahn's Algorithm (BFS on in-degrees)

  1. Compute the in-degree of every vertex.
  2. Put all vertices with in-degree 0 in a queue.
  3. Repeat: remove a vertex u, append it to the output, and decrease the in-degree of each neighbour; any neighbour that reaches 0 joins the queue.
  4. If the output has fewer than V vertices, the graph has a cycle.

Example: edges A→C, A→D, B→D, B→E, C→F, D→F, E→F.

StepQueue (sorted)OutputIn-degree changes
startA, B-in(C)=1, in(D)=2, in(E)=1, in(F)=3
1B, CAC → 0, D → 1
2C, D, EA BD → 0, E → 0
3D, EA B CF → 2
4EA B C DF → 1
5FA B C D EF → 0
6-A B C D E F

DFS Method

Run DFS; when a vertex finishes (all its descendants are done), push it on a stack. Popping the stack gives a topological order. Both methods run in O(V + E).

📝 Practice Questions

  • Q1. Apply Floyd-Warshall to the 4-vertex graph above and show D0 to D4.
  • Q2. What is a topological sort? Find one for the DAG A→C, A→D, B→D, B→E, C→F, D→F, E→F using Kahn's algorithm.
  • Q3. Compare Dijkstra and Floyd-Warshall (problem solved, negative edges, complexity).