diff --git a/Graph_Algorithms/src/Floyd_Warshall.cpp b/Graph_Algorithms/src/Floyd_Warshall.cpp new file mode 100644 index 0000000..25251e5 --- /dev/null +++ b/Graph_Algorithms/src/Floyd_Warshall.cpp @@ -0,0 +1,63 @@ +// Floyd Warshall Algorithm +// A space efficient algorithm to find shortest distance in space complexity of (O(1)) +// Time complexity - O(v^3) + +#include +using namespace std; + +// Function which finds the shortest distance +void shortest_distance(vector>&matrix) +{ + // Making first loop as intermediate between two vertices + for(int k=0;k (matrix[i][k]+matrix[k][j])) || matrix[i][j]==-1)) + matrix[i][j]=matrix[i][k]+matrix[k][j]; + } + } + + } + } +} + +int main() +{ + int v; + cin>>v; + vector>matrix(v,vector(v,-1)); + for(int i=0;i>matrix[i][j]; + } + } + shortest_distance(matrix); + + // Printing the updated matrix + for(int i=0;i