//
// main.cpp
// Floyd-Warshall
//
// Created by Himanshu on 12/11/22.
//
#include <iostream>
#include <vector>
#include <cmath>
#include <climits>
#define N 5
using namespace std;
void printMatrix(int G[][N]) {
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
if (G[i][j] == INT_MAX) {
cout<<"INF ";
} else {
cout<<G[i][j]<<" ";
}
}
cout<<endl;
}
}
//n = number of nodes in graph
void FloydWarshall (int P[][N]) {
for (int k=0; k<N; k++) {
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
if (P[i][k] == INT_MAX || P[k][j] == INT_MAX) {
P[i][j] = P[i][j];
} else {
P[i][j] = min(P[i][j], P[i][k] + P[k][j]);
}
}
}
cout<<endl<<"Path Matrix (P) after k = "<<(k+1)<<endl;
printMatrix (P);
}
}
int main() {
int G[N][N] = {{0, 3, 8, INT_MAX, -4},
{INT_MAX, 0, INT_MAX, 1, 7},
{INT_MAX, 4, 0, INT_MAX, INT_MAX},
{2, INT_MAX, -5, 0, INT_MAX},
{INT_MAX, INT_MAX, INT_MAX, 6, 0}};
cout<<"Graph G (Adjacency Matrix):"<<endl;
printMatrix(G);
cout<<endl<<"Floyd Warshall Algorithm:"<<endl;
FloydWarshall (G);
return 0;
}
Ly8KLy8gIG1haW4uY3BwCi8vICBGbG95ZC1XYXJzaGFsbAovLwovLyAgQ3JlYXRlZCBieSBIaW1hbnNodSBvbiAxMi8xMS8yMi4KLy8KCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8Y2xpbWl0cz4KI2RlZmluZSBOIDUKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnZvaWQgcHJpbnRNYXRyaXgoaW50IEdbXVtOXSkgewogICAgCiAgICBmb3IgKGludCBpPTA7IGk8TjsgaSsrKSB7CiAgICAgICAgZm9yIChpbnQgaj0wOyBqPE47IGorKykgewogICAgICAgICAgICBpZiAoR1tpXVtqXSA9PSBJTlRfTUFYKSB7CiAgICAgICAgICAgICAgICBjb3V0PDwiSU5GICI7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBjb3V0PDxHW2ldW2pdPDwiICI7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgY291dDw8ZW5kbDsKICAgIH0KfQoKLy9uID0gbnVtYmVyIG9mIG5vZGVzIGluIGdyYXBoCnZvaWQgRmxveWRXYXJzaGFsbCAoaW50IFBbXVtOXSkgewoKICAgIGZvciAoaW50IGs9MDsgazxOOyBrKyspIHsKICAgICAgICAKICAgICAgICBmb3IgKGludCBpPTA7IGk8TjsgaSsrKSB7CiAgICAgICAgICAgIGZvciAoaW50IGo9MDsgajxOOyBqKyspIHsKICAgICAgICAgICAgICAgIGlmIChQW2ldW2tdID09IElOVF9NQVggfHwgUFtrXVtqXSA9PSBJTlRfTUFYKSB7CiAgICAgICAgICAgICAgICAgICAgUFtpXVtqXSA9IFBbaV1bal07CiAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgIFBbaV1bal0gPSBtaW4oUFtpXVtqXSwgUFtpXVtrXSArIFBba11bal0pOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIAogICAgICAgIGNvdXQ8PGVuZGw8PCJQYXRoIE1hdHJpeCAoUCkgYWZ0ZXIgayA9ICI8PChrKzEpPDxlbmRsOwogICAgICAgIHByaW50TWF0cml4IChQKTsKICAgIH0KICAgIAp9CgoKaW50IG1haW4oKSB7CiAgICBpbnQgR1tOXVtOXSA9IHt7MCwgMywgOCwgSU5UX01BWCwgLTR9LAogICAgICAgICAgICAgICAgICAgICAgIHtJTlRfTUFYLCAwLCBJTlRfTUFYLCAxLCA3fSwKICAgICAgICAgICAgICAgICAgICAgICB7SU5UX01BWCwgNCwgMCwgSU5UX01BWCwgSU5UX01BWH0sCiAgICAgICAgICAgICAgICAgICAgICAgezIsIElOVF9NQVgsIC01LCAwLCBJTlRfTUFYfSwKICAgICAgICAgICAgICAgICAgICAgICB7SU5UX01BWCwgSU5UX01BWCwgSU5UX01BWCwgNiwgMH19OwoKICAgIAogICAgY291dDw8IkdyYXBoIEcgKEFkamFjZW5jeSBNYXRyaXgpOiI8PGVuZGw7CiAgICBwcmludE1hdHJpeChHKTsKICAgIAogICAgY291dDw8ZW5kbDw8IkZsb3lkIFdhcnNoYWxsIEFsZ29yaXRobToiPDxlbmRsOwogICAgRmxveWRXYXJzaGFsbCAoRyk7CiAgICAKICAgIAogICAgcmV0dXJuIDA7Cn0K