From 6b952a061028a1a7c2a037a6cc92cc9cdeffbc3b Mon Sep 17 00:00:00 2001 From: Abhishek-kumar82078 <63895917+Abhishek-kumar82078@users.noreply.github.com> Date: Sun, 17 Oct 2021 21:33:49 +0530 Subject: [PATCH] Added Floyd Warshall Algorithm in c++ Added an algorithm to find shortest distance --- Graph_Algorithms/src/Floyd_Warshall.cpp | 63 +++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Graph_Algorithms/src/Floyd_Warshall.cpp 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