Floyd-Warshall All-Pairs Problem

Floyd-Warshall All-Pairs Problem — ExecCode Hard DSA Practice

Solve the Floyd-Warshall All-Pairs problem on ExecCode. Free online hard DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Given a directed weighted graph, compute shortest-path distances between every pair of nodes using Floyd-Warshall. Return the n×n distance matrix (use Infinity for unreachable pairs).

Examples

Input n = 5, edges = [[0, 1, 3], [0, 3, 7], [1, 2, 1], [1, 3, 5], [2, 3, 2], [3, 4, 4], [2, 4, 9]]; Output [[0, 3, 4, 6, 10], [null, 0, 1, 3, 7], [null, null, 0, 2, 6], [null, null, null, 0, 4], [null, null, null, null, 0]]. Input n = 3, edges = [[0, 1, 2], [1, 2, 5], [0, 2, 10]]; Output [[0, 2, 7], [null, 0, 5], [null, null, 0]]

Constraints

1 ≤ n ≤ 400 0 ≤ edges.length ≤ n*(n-1) No negative cycles assumed for this demo

Practice Floyd-Warshall All-Pairs free on ExecCode. Browse DSA problems, topic map, and placement guides.