00001 #include "util/exception.hh"
00002 #include <climits>
00003 #include <vector>
00004
00005 #define MAX_DIST (INT_MAX / 2)
00006
00007
00008
00009 using namespace std;
00010
00011
00012 void floyd_warshall(const std::vector<std::vector<bool> >& edges, std::vector<std::vector<int> >& dist)
00013 {
00014 UTIL_THROW_IF2(edges.size() != edges.front().size(), "Error");
00015 dist.clear();
00016 dist.resize(edges.size(), std::vector<int>(edges.size(), 0));
00017
00018 size_t num_edges = edges.size();
00019
00020 for (size_t i=0; i<num_edges; ++i) {
00021 for (size_t j=0; j<num_edges; ++j) {
00022 if (edges[i][j])
00023 dist[i][j] = 1;
00024 else
00025 dist[i][j] = MAX_DIST;
00026 if (i == j) dist[i][j] = MAX_DIST;
00027 }
00028 }
00029
00030 for (size_t k=0; k<num_edges; ++k)
00031 for (size_t i=0; i<num_edges; ++i)
00032 for (size_t j=0; j<num_edges; ++j)
00033 if (dist[i][j] > (dist[i][k] + dist[k][j]))
00034 dist[i][j] = dist[i][k] + dist[k][j];
00035 }
00036