🌍 Floyd-Warshall Algorithm – Comprehensive Study Guide

The **Floyd-Warshall Algorithm** is a **dynamic programming technique** that finds the **shortest path between all pairs of vertices** in a graph.

⚑ Why Use Floyd-Warshall?

  • βœ… **Computes shortest paths between all node pairs.**
  • πŸ“‘ **Works for graphs with negative weights (but no negative cycles).**
  • πŸš€ **Used in network routing and GPS navigation.**
  • πŸ” **Can detect negative weight cycles efficiently.**

πŸ“Œ How Does Floyd-Warshall Work?

The algorithm **iteratively improves** the shortest paths by considering intermediate vertices between each pair of nodes.

πŸ“Œ Step-by-Step Execution

  1. βœ… **Initialize** the distance matrix with given edge weights.
  2. πŸ“Œ Use **three nested loops** to iteratively update the shortest distances.
  3. πŸ” If `dist[i][j] > dist[i][k] + dist[k][j]`, update the shortest path.
  4. 🚨 If `dist[i][i] < 0` for any vertex, a **negative cycle** exists.

πŸ› οΈ Floyd-Warshall Algorithm Implementation

πŸ”„ **1. Floyd-Warshall Algorithm (O(VΒ³))**


                #include 
                using namespace std;
            
                const int INF = 1e9; // Large number to represent infinity
            
                void floydWarshall(vector> &dist, int V) {
                    for (int k = 0; k < V; k++) {
                        for (int i = 0; i < V; i++) {
                            for (int j = 0; j < V; j++) {
                                if (dist[i][k] < INF && dist[k][j] < INF) {
                                    dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
                                }
                            }
                        }
                    }
                }
            
                int main() {
                    int V = 4;
                    vector> dist = {
                        {0, 3, INF, 5},
                        {2, 0, INF, 4},
                        {INF, 1, 0, INF},
                        {INF, INF, 2, 0}
                    };
            
                    floydWarshall(dist, V);
            
                    cout << "Shortest Distance Matrix:\n";
                    for (auto &row : dist) {
                        for (int val : row)
                            cout << (val == INF ? "INF" : to_string(val)) << " ";
                        cout << endl;
                    }
                    return 0;
                }
                

🚨 Handling Negative Weight Cycles

The Floyd-Warshall algorithm detects **negative weight cycles** by checking the diagonal of the distance matrix.


                for (int i = 0; i < V; i++) {
                    if (dist[i][i] < 0) {
                        cout << "Negative Cycle Detected!\n";
                    }
                }
                

βš–οΈ Floyd-Warshall vs Dijkstra vs Bellman-Ford

Feature Floyd-Warshall Dijkstra Bellman-Ford
Time Complexity O(VΒ³) O(V + E log V) O(VE)
Handles Negative Weights? βœ… Yes (No negative cycles) ❌ No βœ… Yes
Shortest Path for All Pairs Single Source Single Source
Use Cases Network Routing, Graph Theory Navigation, AI Pathfinding Currency Exchange, Road Networks

⏳ Time Complexity Analysis

  • βœ… **Best Case:** O(VΒ³) – Fully connected graph.
  • ⚑ **Worst Case:** O(VΒ³) – Sparse graph.

🌍 Real-World Applications of Floyd-Warshall

  • πŸ“‘ **Network Routing Protocols** – Finding shortest routes between all nodes.
  • πŸ“Š **Social Network Analysis** – Measuring distances between users.
  • πŸ”— **AI & Game Development** – Pathfinding in multi-agent systems.
  • πŸš— **GPS & Traffic Navigation Systems** – Optimal route calculations.

πŸ”— Next Topics