📖 Scenario: You are working as a city planner. You have a map of cities connected by roads with distances. You want to find the shortest distance between every pair of cities to plan efficient travel routes.
🎯 Goal: Build a program using the Floyd Warshall algorithm to find the shortest distances between all pairs of cities.
📋 What You'll Learn
Create a 2D vector called dist representing the distance matrix between cities
Add a variable V for the number of cities
Implement the Floyd Warshall algorithm using three nested for loops with variables k, i, and j
Print the final shortest distance matrix after applying the algorithm
💡 Why This Matters
🌍 Real World
City planners and map services use shortest path algorithms to find the best routes between locations.
💼 Career
Understanding Floyd Warshall helps in roles involving route optimization, network analysis, and graph algorithms.
Progress0 / 4 steps
1
Create the distance matrix
Create an integer variable V and set it to 4. Then create a 2D vector of integers called dist with these exact values: {{0, 5, 999, 10}, {999, 0, 3, 999}, {999, 999, 0, 1}, {999, 999, 999, 0}}.
DSA C++
Hint
Use int V = 4; to set the number of cities. Use std::vector> dist = { ... }; to create the matrix.
2
Set the number of vertices
Create an integer variable called V and set it to 4 if not already done. This variable represents the number of cities.
DSA C++
Hint
Make sure the variable V is set to 4 to represent the number of cities.
3
Implement the Floyd Warshall algorithm
Use three nested for loops with variables k, i, and j each running from 0 to V-1. Inside the innermost loop, update dist[i][j] to the minimum of dist[i][j] and dist[i][k] + dist[k][j].
DSA C++
Hint
Use three nested loops with k, i, and j. Update dist[i][j] if a shorter path is found through k.
4
Print the shortest distance matrix
Use two nested for loops with variables i and j each running from 0 to V-1. Inside the inner loop, print dist[i][j] followed by a space. After the inner loop, print a newline.
DSA C++
Hint
Print the matrix row by row. Use std::cout << dist[i][j] << " " inside the inner loop and std::cout << std::endl; after the inner loop.
Practice
(1/5)
1. What is the main purpose of the Floyd Warshall algorithm in graph theory?
easy
A. To find shortest paths between all pairs of vertices in a weighted graph
B. To find the minimum spanning tree of a graph
C. To detect cycles in a directed graph
D. To perform depth-first search traversal
Solution
Step 1: Understand Floyd Warshall algorithm goal
The Floyd Warshall algorithm is designed to find shortest paths between every pair of vertices in a weighted graph, including negative weights but no negative cycles.
Step 2: Compare with other graph algorithms
Minimum spanning tree algorithms find a tree connecting all vertices with minimum total edge weight, cycle detection algorithms find cycles, and DFS is a traversal method. These are different from Floyd Warshall's goal.
Final Answer:
To find shortest paths between all pairs of vertices in a weighted graph -> Option A
Quick Check:
Floyd Warshall = all pairs shortest paths [OK]
Hint: Floyd Warshall finds shortest paths between all vertex pairs [OK]
Common Mistakes:
Confusing Floyd Warshall with minimum spanning tree algorithms
Thinking it only finds shortest path from one source
Assuming it detects cycles instead of paths
2. Which of the following is the correct way to initialize the distance matrix dist for Floyd Warshall in C++ for a graph with n vertices and adjacency matrix graph?
easy
A. for (int i = 0; i < n; i++) dist[i] = graph[i];
B. for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) dist[i][j] = graph[i][j];
C. dist = graph;
D. dist = new int[n][n];
Solution
Step 1: Understand distance matrix initialization
We need to copy each element from the adjacency matrix graph to dist element-wise because dist is a 2D array and direct assignment is not allowed in C++.
Step 2: Analyze each option
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) dist[i][j] = graph[i][j]; uses nested loops to copy each element correctly. for (int i = 0; i < n; i++) dist[i] = graph[i]; tries to assign a row pointer which is invalid for 2D arrays. dist = graph; tries direct assignment which is invalid for arrays. dist = new int[n][n]; only allocates memory but does not initialize values.
Final Answer:
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) dist[i][j] = graph[i][j]; -> Option B
Quick Check:
Element-wise copy needed = for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) dist[i][j] = graph[i][j]; [OK]
Hint: Use nested loops to copy 2D arrays element-wise [OK]
Common Mistakes:
Trying to assign 2D arrays directly
Copying only row pointers instead of elements
Not initializing distance matrix before algorithm
3. What will be the output distance matrix after running Floyd Warshall on the following graph with 3 vertices and adjacency matrix: