diff --git a/C++/Floyd-Warshall/question_output.txt b/C++/Floyd-Warshall.cpp similarity index 51% rename from C++/Floyd-Warshall/question_output.txt rename to C++/Floyd-Warshall.cpp index e26ddb9..e536d26 100644 --- a/C++/Floyd-Warshall/question_output.txt +++ b/C++/Floyd-Warshall.cpp @@ -1,3 +1,4 @@ +/* QUESTION -- Floyd Warshall Algorithm @@ -27,4 +28,67 @@ OUTPUT -- EXPLANATION -- In this example, the final matrix stores the shortest distances between all pairs of nodes. For instance, matrix[0][3] = 6 indicates that the shortest distance from node 0 to node 3 is 6. Similarly, matrix[2][1] = 8 shows the shortest distance from node 2 to node 1. -In general, matrix[i][j] is storing the shortest distance from node i to j. \ No newline at end of file +In general, matrix[i][j] is storing the shortest distance from node i to j. +*/ + +#include +using namespace std; + +class Solver { +public: + void calculateShortestDistance(vector>& m) { + int size = m.size(); + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + if (m[i][j] == -1) { + m[i][j] = 1e9; + } + if (i == j) m[i][j] = 0; + } + } + + for (int k = 0; k < size; k++) { + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + m[i][j] = min(m[i][j], m[i][k] + m[k][j]); + } + } + } + + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + if (m[i][j] == 1e9) { + m[i][j] = -1; + } + } + } + } +}; + +int main() { + int n; + cout << "Enter the size of the matrix (n x n): "; + cin >> n; + + vector> m(n, vector(n)); + + cout << "Enter the matrix (use -1 for no direct path):\n"; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + cin >> m[i][j]; + } + } + + Solver obj; + obj.calculateShortestDistance(m); + + cout << "The shortest distances between each pair of vertices are:\n"; + for (auto& row : m) { + for (auto& cell : row) { + cout << cell << " "; + } + cout << endl; + } + + return 0; +} diff --git a/C++/Floyd-Warshall/FloydWarshall.cpp b/C++/Floyd-Warshall/FloydWarshall.cpp deleted file mode 100644 index 55ac196..0000000 --- a/C++/Floyd-Warshall/FloydWarshall.cpp +++ /dev/null @@ -1,61 +0,0 @@ -#include -using namespace std; - -class Solver { -public: - void calculateShortestDistance(vector>& m) { - int size = m.size(); - for (int i = 0; i < size; i++) { - for (int j = 0; j < size; j++) { - if (m[i][j] == -1) { - m[i][j] = 1e9; - } - if (i == j) m[i][j] = 0; - } - } - - for (int k = 0; k < size; k++) { - for (int i = 0; i < size; i++) { - for (int j = 0; j < size; j++) { - m[i][j] = min(m[i][j], m[i][k] + m[k][j]); - } - } - } - - for (int i = 0; i < size; i++) { - for (int j = 0; j < size; j++) { - if (m[i][j] == 1e9) { - m[i][j] = -1; - } - } - } - } -}; - -int main() { - int n; - cout << "Enter the size of the matrix (n x n): "; - cin >> n; - - vector> m(n, vector(n)); - - cout << "Enter the matrix (use -1 for no direct path):\n"; - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { - cin >> m[i][j]; - } - } - - Solver obj; - obj.calculateShortestDistance(m); - - cout << "The shortest distances between each pair of vertices are:\n"; - for (auto& row : m) { - for (auto& cell : row) { - cout << cell << " "; - } - cout << endl; - } - - return 0; -}