📖 Scenario: You are working with a network of cities connected by roads. Each road has a travel cost. You want to find the shortest travel cost between every pair of cities.
🎯 Goal: Build a program that uses the Floyd Warshall algorithm to find the shortest paths between all pairs of cities in a given network.
📋 What You'll Learn
Create a 2D array called graph representing the travel costs between 4 cities.
Create an integer variable V for the number of cities.
Implement the Floyd Warshall algorithm using nested loops with variables k, i, and j.
Print the final shortest path matrix after running the algorithm.
💡 Why This Matters
🌍 Real World
Finding shortest travel costs between all pairs of cities helps in planning efficient routes for delivery, travel, or communication networks.
💼 Career
Understanding Floyd Warshall algorithm is useful for software engineers working on network routing, map services, and optimization problems.
Progress0 / 4 steps
1
Create the graph matrix
Create a 2D integer array called graph with 4 rows and 4 columns. Initialize it with these exact values: { {0, 5, 999, 10}, {999, 0, 3, 999}, {999, 999, 0, 1}, {999, 999, 999, 0} }. Also create an integer variable V and set it to 4.
DSA C
Hint
Use a 2D array to represent the graph. Use 999 to represent no direct path.
2
Set up the distance matrix
Create a 2D integer array called dist with 4 rows and 4 columns. Copy the values from graph into dist using nested for loops with variables i and j.
DSA C
Hint
Use two nested loops to copy each element from graph to dist.
3
Implement the Floyd Warshall algorithm
Use three nested for loops with variables k, i, and j to update dist[i][j] with the minimum of its current value and the sum of dist[i][k] and dist[k][j].
DSA C
Hint
Use three nested loops to check if going through city k gives a shorter path from i to j.
4
Print the shortest path matrix
Use nested for loops with variables i and j to print the dist matrix. Print each value followed by a space. Print a new line after each row. If the value is 999, print INF instead.
DSA C
Hint
Print each row of the dist matrix. Replace 999 with INF to show no path.
Practice
(1/5)
1. What is the main purpose of the Floyd Warshall algorithm in graph theory?
easy
A. To detect cycles in a graph
B. To find the minimum spanning tree of a graph
C. To find the shortest paths between all pairs of vertices in a weighted graph
D. To perform a depth-first search traversal
Solution
Step 1: Understand Floyd Warshall's goal
The algorithm computes shortest paths between every pair of vertices in a weighted graph.
Step 2: Compare with other graph algorithms
Minimum spanning tree, cycle detection, and DFS serve different purposes than Floyd Warshall.
Final Answer:
To find the shortest paths between all pairs of vertices in a weighted graph -> Option C
Quick Check:
Floyd Warshall = All pairs shortest paths [OK]
Hint: Remember: Floyd Warshall finds shortest paths for all vertex pairs [OK]
Common Mistakes:
Confusing with Dijkstra's single-source shortest path
Thinking it finds minimum spanning tree
Assuming it detects cycles
2. Which of the following is the correct way to initialize the distance matrix dist for Floyd Warshall algorithm in C for a graph with V vertices?
easy
A. for (int i = 0; i < V; i++) for (int j = 0; j < V; j++) dist[i][j] = INF;
B. for (int i = 0; i < V; i++) dist[i] = 0;
C. for (int i = 0; i < V; i++) dist[i][i] = INF;
D. for (int i = 0; i < V; i++) for (int j = 0; j < V; j++) dist[i][j] = (i == j) ? 0 : INF;
Solution
Step 1: Understand initialization requirements
Distance from a vertex to itself is 0; others are set to infinity (INF) initially.
Step 2: Check each option
for (int i = 0; i < V; i++) for (int j = 0; j < V; j++) dist[i][j] = (i == j) ? 0 : INF; correctly sets 0 for same vertices and INF for others; others do not initialize properly.
Final Answer:
for (int i = 0; i < V; i++) for (int j = 0; j < V; j++) dist[i][j] = (i == j) ? 0 : INF; -> Option D