Floyd Warshall Algorithm

Given a matrix of integers A of size N x N, where A[i][j] represents the weight of directed edge
from i to j (i —> j).

If i == j, A[i][j] = 0, and if there is no directed edge from vertex i to vertex j,
A[i][j] = -1.

Return a matrix B of size N x N where B[i][j] = shortest path from vertex i to vertex j.

If there is no possible path from vertex i to vertex j , B[i][j] = -1

Note: Rows are numbered from top to bottom and columns are numbered from left to right.



Input Format

The first and only argument given is the integer matrix A.

Output Format

Return a matrix B of size N x N where B[i][j] = shortest path from vertex i to vertex j
If there is no possible path from vertex i to vertex j, B[i][j] = -1.

Constraints

1 <= N <= 200
1 <= A[i][j] <= 1000000

For Example

Input 1: 
A = [   [0 , 50 , 39]
        [-1 , 0 , 1]
        [-1 , 10 , 0]   ] 
              
Output 1: 
    [   [0 , 49 , 39 ] 
        [-1 , 0 , -1 ] 
        [-1 , 10 , 0 ]  ]
 
Explanation 1: 
    Shortest Path from 1 to 2 would be 1 ---> 3 ---> 2 and not directly from 1 to 2, 
    All remaining distances remains same. 
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Floyd Warshall Algorithm on Interview Code Editor
Sign Up
to access hints and editorial solutions for Floyd Warshall Algorithm

Discussion


Loading...
Click here to start solving coding interview questions