📖 Scenario: You are working as a city planner. You have a map of 4 cities connected by roads with different travel times. You want to find the shortest travel time between every pair of cities.
🎯 Goal: Build a program that uses the Floyd Warshall algorithm to find the shortest travel times between all pairs of cities.
📋 What You'll Learn
Create a 2D array called dist with the initial travel times between cities
Create an integer variable n for the number of cities
Use three nested for loops with variables k, i, and j to update shortest paths
Print the final dist matrix showing shortest travel times
💡 Why This Matters
🌍 Real World
City planners and GPS navigation systems use shortest path algorithms to find the quickest routes between locations.
💼 Career
Understanding Floyd Warshall helps in roles involving network optimization, route planning, and graph algorithms.
Progress0 / 4 steps
1
Create the initial distance matrix
Create a 2D integer array called dist with these exact values representing travel times between 4 cities: dist = new int[,] { {0, 5, 999, 10}, {999, 0, 3, 999}, {999, 999, 0, 1}, {999, 999, 999, 0} }; Use 999 to represent no direct road between cities.
DSA C#
Hint
Use int[,] to create a 2D array and fill it with the exact values given.
2
Set the number of cities
Create an integer variable called n and set it to 4 to represent the number of cities.
DSA C#
Hint
Just create a variable n and assign it the value 4.
3
Implement the Floyd Warshall algorithm
Use three nested for loops with variables k, i, and j from 0 to n - 1 to update dist[i, j] with the minimum of dist[i, j] and dist[i, k] + dist[k, j].
DSA C#
Hint
Use three loops and update the distance if a shorter path is found through city k.
4
Print the shortest distance matrix
Use two nested for loops with variables i and j from 0 to n - 1 to print the dist[i, j] values separated by spaces. Print a new line after each row. Replace 999 with INF when printing.
DSA C#
Hint
Print each row on a new line. Replace 999 with INF for better readability.
Practice
(1/5)
1. What is the main purpose of the Floyd Warshall algorithm in graph theory?
easy
A. To perform a depth-first search traversal
B. To find the minimum spanning tree of a graph
C. To detect cycles in a graph
D. To find the shortest paths between all pairs of vertices in a weighted graph
Solution
Step 1: Understand Floyd Warshall's goal
The Floyd Warshall algorithm calculates shortest paths between every pair of vertices in a weighted graph, including negative weights but no negative cycles.
Step 2: Compare with other algorithms
Minimum spanning tree algorithms find a tree connecting all vertices with minimum total weight, cycle detection finds loops, and DFS is a traversal method, none of which match Floyd Warshall's purpose.
Final Answer:
To find the shortest paths between all pairs of vertices in a weighted graph -> Option D
Quick Check:
Floyd Warshall = All pairs shortest paths [OK]
Hint: Floyd Warshall finds shortest paths between all vertex pairs [OK]
Common Mistakes:
Confusing with minimum spanning tree algorithms
Thinking it only finds shortest path from one source
Assuming it works with negative cycles
2. Which of the following is the correct way to initialize the distance matrix dist for Floyd Warshall in C# when given an adjacency matrix graph of size V?
easy
A. for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { dist[i, j] = graph[i, j]; } }
B. for (int i = 0; i < V; i++) { dist[i, i] = 0; } for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (i != j) dist[i, j] = int.MaxValue; } }
C. for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { dist[i, j] = 0; } }
D. for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { dist[i, j] = -1; } }"
Solution
Step 1: Understand initialization for Floyd Warshall
The distance matrix dist should start as a copy of the adjacency matrix graph because it holds initial edge weights or infinity where no edge exists.
Step 2: Check each option
for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { dist[i, j] = graph[i, j]; } } copies graph to dist, which is correct. for (int i = 0; i < V; i++) { dist[i, i] = 0; } for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (i != j) dist[i, j] = int.MaxValue; } } sets 0 for diagonal and max value elsewhere but does not copy actual edges. Options C and D incorrectly set all distances to 0 or -1.
Final Answer:
for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { dist[i, j] = graph[i, j]; } } -> Option A
Quick Check:
Initialize dist as graph copy [OK]
Hint: Copy graph matrix to dist for Floyd Warshall start [OK]
Common Mistakes:
Setting all distances to zero
Not copying actual edge weights
Using -1 instead of infinity for no edge
3. Consider the following C# code snippet implementing Floyd Warshall on a graph with 3 vertices and adjacency matrix:
int V = 3;
int[,] graph = {
{0, 5, int.MaxValue},
{50, 0, 15},
{30, int.MaxValue, 0}
};
int[,] dist = new int[V, V];
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
dist[i, j] = graph[i, j];
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] != int.MaxValue && dist[k, j] != int.MaxValue && dist[i, k] + dist[k, j] < dist[i, j]) {
dist[i, j] = dist[i, k] + dist[k, j];
}
}
}
}
// Print dist matrix
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
Console.Write(dist[i, j] == int.MaxValue ? "INF " : dist[i, j] + " ");
}
Console.WriteLine();
}
What is the printed output?
medium
A. 0 5 20
50 0 15
30 INF 0
B. 0 5 20
45 0 15
30 35 0
C. 0 5 INF
50 0 15
30 INF 0
D. 0 5 15
50 0 15
30 35 0
Solution
Step 1: Initialize dist matrix
dist starts as:
0 5 INF
50 0 15
30 INF 0
Step 2: Apply Floyd Warshall updates
Check paths through each vertex k:
- k=0: Update dist[1,2] via 1->0->2: 50 + INF no update; dist[2,1] via 2->0->1: 30 + 5 = 35 < INF, update dist[2,1] = 35
- k=1: Update dist[0,2] via 0->1->2: 5 + 15 = 20 < INF, update dist[0,2] = 20
- k=2: No further improvements
Final dist:
0 5 20
45 0 15
30 35 0