0
0
Data Structures Theoryknowledge~20 mins

Floyd-Warshall algorithm in Data Structures Theory - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Floyd-Warshall Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the core idea of Floyd-Warshall

What is the main purpose of the Floyd-Warshall algorithm in graph theory?

ATo detect cycles in a directed graph
BTo find the minimum spanning tree of a graph
CTo find the shortest paths between all pairs of vertices in a weighted graph
DTo perform a depth-first search on a graph
Attempts:
2 left
💡 Hint

Think about what kind of paths the algorithm calculates for every pair of points.

📋 Factual
intermediate
2:00remaining
Time complexity of Floyd-Warshall

What is the time complexity of the Floyd-Warshall algorithm for a graph with n vertices?

AO(n^3)
BO(n^2)
CO(n log n)
DO(2^n)
Attempts:
2 left
💡 Hint

Consider the three nested loops used in the algorithm.

🔍 Analysis
advanced
2:00remaining
Detecting negative cycles with Floyd-Warshall

After running the Floyd-Warshall algorithm, how can you detect if the graph contains a negative weight cycle?

ACheck if any diagonal element in the distance matrix is negative
BCheck if any distance value is zero
CCheck if the sum of all distances is positive
DCheck if any edge weight is negative
Attempts:
2 left
💡 Hint

Think about what the diagonal elements represent in the distance matrix.

🚀 Application
advanced
2:00remaining
Result of Floyd-Warshall on a specific graph

Given a graph with 3 vertices and edges: 0→1 (weight 4), 1→2 (weight -2), 2→0 (weight 3), what is the shortest distance from vertex 0 to vertex 2 after running Floyd-Warshall?

A1
B5
C-2
D2
Attempts:
2 left
💡 Hint

Consider the paths from 0 to 2 and their total weights.

Reasoning
expert
2:00remaining
Why Floyd-Warshall works with negative edges but not negative cycles

Why can the Floyd-Warshall algorithm handle graphs with negative edge weights but fails if there is a negative weight cycle?

ABecause negative edges are ignored by the algorithm
BBecause negative cycles cause distances to decrease indefinitely, making shortest paths undefined
CBecause the algorithm only works on unweighted graphs
DBecause negative cycles increase the total path cost
Attempts:
2 left
💡 Hint

Think about what happens if you keep going around a negative cycle.