Floyd Warshall | Practice | GeeksforGeeks

The problem is to find shortest distances between every pair of vertices in a given edge weighted directed Graph. The Graph is represented as adjancency matrix, and the matrix denotes the weight of the edegs (if it exists) else -1. Do it in-place.
 
Example 1:
Input: matrix = {{0,25},{-1,0}}
Output: {{0,25},{-1,0}}
Explanation: The shortest distance between
every pair is already given(if it exists).

Example 2:
Input: matrix = {{0,1,43},{1,0,6},{-1,-1,0}}
Output: {{0,1,7},{1,0,6},{-1,-1,0}}
Explanation: We can reach 3 from 1 as 1->2->3
and the cost will be 1+6=7 which is less than 
43.

 
Your Task:
You don't need to read, return or print anything. Your task is to complete the function shortest_distance() which takes the matrix as input parameter and modify the distances for every pair in-place.
 
Expected Time Complexity: O(n3)
Expected Space Compelxity: O(1)
 
Constraints:
1 <= n <= 100
+2
khatritaukir1 month ago
class Solution {
  public:
	void shortest_distance(vector<vector<int>>&g){
	    int n = g.size();
	    for(int k=0;k<n;k++) {
	        for(int i=0;i<n;i++) {
	            for(int j=0;j<n;j++) {
	                if(i==k || j==k || g[i][k]==-1 || g[k][j] == -1) continue;
	                g[i][j] = min(g[i][j]==-1 ? INT_MAX: g[i][j], g[i][k] + g[k][j]);
	            }
	        }
	    }
	}
};
0
tvaishnavi10111 month ago
class Solution { public:void shortest_distance(vector<vector<int>>&a){    // Code here    int n=a.size();
    for(int k=0;k<n;k++){         for(int i=0;i<n;i++){        for(int j=0;j<n;j++){            if(i==k || j==k || i==j || a[k][j]==-1 || a[i][k]==-1 ){                continue;            }            if(a[i][j]==-1){                a[i][j]=a[i][k]+a[k][j];            }            else           a[i][j]=min(a[i][j],a[i][k]+a[k][j]);        }    }    }        }};
-1
mohdhammadsiddiqui1 month ago
This is the shortest solution you could find on GFG (2 Line condition):-
 
class Solution { public:void shortest_distance(vector<vector<int>>&matrix){    int n=matrix.size();   for(int k=0;k<n;k++){       for(int row=0;row<n;row++){           for(int col=0;col<n;col++){               if(row==k ||col==k || row==k ||row==col||matrix[row][k]==-1 || matrix[k][col]==-1) continue;              matrix[row][col]= matrix[row][col]==-1?matrix[row][col]=matrix[row][k]+matrix[k][col]:min(matrix[row][col],matrix[row][k]+matrix[k][col]);           }       }    }   }};
 
-1
mohdhammadsiddiqui1 month ago
2 Line condition Soution using 3 loops-
class Solution { public:void shortest_distance(vector<vector<int>>&matrix){    int n=matrix.size();   for(int k=0;k<n;k++){       for(int row=0;row<n;row++){           for(int col=0;col<n;col++){               if(row==k ||col==k || row==k ||row==col|| (matrix[row][k]==-1 && matrix[k][col]==-1) || matrix[row][k]==-1 || matrix[k][col]==-1) continue;              matrix[row][col]= matrix[row][col]==-1?matrix[row][col]=matrix[row][k]+matrix[k][col]:min(matrix[row][col],matrix[row][k]+matrix[k][col]);                        }       }    }   }};
 
-1
mohdhammadsiddiqui1 month ago
 
class Solution { public:void shortest_distance(vector<vector<int>>&matrix){    int n=matrix.size();   for(int k=0;k<n;k++){       for(int row=0;row<n;row++){           for(int col=0;col<n;col++){               if(row==k ||col==k || row==k ||row==col|| matrix[row][k]==-1 || matrix[k][col]==-1) continue;               if(matrix[row][k]==-1 && matrix[k][col]==-1) continue;               if(matrix[row][col]==-1)  matrix[row][col]=matrix[row][k]+matrix[k][col];               else matrix[row][col]=min(matrix[row][col],matrix[row][k]+matrix[k][col]);                        }       }    }   }};
 
+1
decostarsharma1132 months ago
DESCRIPTION OF EXAMPLES IS BAD
Just because I was not able to understand the example I looked the solution .
0
sanjay raz2 months ago
class Solution {
  shortest_distance(matrix){
    for(let k = 0; k < matrix.length; k++) {
        for(let i = 0; i < matrix.length; i++) {
            for(let j = 0; j < matrix.length; j++) {
                const ij = matrix[i][j] === -1 ? Infinity : matrix[i][j];
                const ik = matrix[i][k] === -1 ? Infinity : matrix[i][k];
                const kj = matrix[k][j] === -1 ? Infinity : matrix[k][j];
                
                const min = Math.min(ij, ik + kj);
                
                matrix[i][j] = min === Infinity ? -1 : min;
            }
        }
    }
    
    return matrix;
  }
}
+1
aloksinghbais022 months ago
C++ solution having time complexity as O(N^3+2*(N^2)) and auxiliary space complexity as O(1) is as follows :- 
 
Execution Time :- 0.3 / 1.5 sec
 
void shortest_distance(vector<vector<int>>&matrix){    int n = matrix.size();        for(int i = 0; i < n; i++){        for(int j = 0; j < n; j++){            if(matrix[i][j] == -1)                matrix[i][j] = INT_MAX;        }    }        for(int k = 0; k < n; k++){        for(int i = 0; i < n; i++){            for(int j = 0; j < n; j++){                if(matrix[i][k] == INT_MAX || matrix[k][j] == INT_MAX){                    continue;                }                else{                    matrix[i][j] = min(matrix[i][j],matrix[i][k] + matrix[k][j]);                }            }        }    }        for(int i = 0; i < n; i++){        for(int j = 0; j < n; j++){            if(matrix[i][j] == INT_MAX)                matrix[i][j] = -1;        }    }}
0
puranjanprithu2 months ago
Formula :
matrix[i][j] = min(matrix[i][j],matrix[i][k]+matrix[k][j])
 
 
for k in range(n):
     for i in range(n):
         for j in range(n):
             if matrix[i][k]!=-1 and matrix[k][j]!=-1 :
                 if matrix[i][j]==-1:
                     matrix[i][j]=matrix[i][k]+matrix[k][j]
                 else:
                     matrix[i][j]=min(matrix[i][j],matrix[i][k]+matrix[k][j])
+2
sanketbhagat2 months ago
SIMPLE JAVA SOLUTION
class Solution{
    public void shortest_distance(int[][] adj){
        // Code here 
        int n = adj.length;
        for(int k=0;k<n;k++){
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    if(adj[i][k]==-1 || adj[k][j]==-1) continue;
                    if(adj[i][j]==-1) adj[i][j] = adj[i][k]+adj[k][j];
                    else adj[i][j] = Math.min(adj[i][j],adj[i][k]+adj[k][j]);
                }
            }
        }
    }
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.