#include #include #include #include #include #include #include #include #include using namespace lemon; using namespace std; int main(int argc, const char *argv[]) { srand(time(0)); typedef SmartGraph Graph; typedef Graph::Node Node; typedef Graph::Edge Edge; typedef Graph::NodeIt NodeIt; typedef Graph::EdgeIt EdgeIt; typedef Graph::EdgeMap LengthMap; typedef Graph::NodeMap DistMap; const int n = argc > 1 ? atoi(argv[1]) : 20; const int e = argc > 2 ? atoi(argv[2]) : (int)(n * log(n)); const double m = argc > 3 ? (double)atoi(argv[3]) : 100.0; Graph graph; LengthMap length(graph); vector nodes; DistMap shift(graph); for (int i = 0; i < n; ++i) { Node node = graph.addNode(); nodes.push_back(node); shift[node] = m * (double)rand() / (RAND_MAX + 1.0); } for (int i = 0; i < e; ++i) { int s = (int)(n * (double)rand() / (RAND_MAX + 1.0)); int t = (int)(n * (double)rand() / (RAND_MAX + 1.0)); double c = m * (double)rand() / (RAND_MAX + 1.0); Edge edge = graph.addEdge(nodes[s], nodes[t]); length[edge] = c - shift[nodes[s]] + shift[nodes[t]]; } Johnson johnson(graph, length); { Timer timer; johnson.run(); cout << "Johnson: " << timer << endl; } FloydWarshall floyd(graph, length); { Timer timer; floyd.run(); cout << "FloydWarshall: " << timer << endl; } for (NodeIt it(graph); it != INVALID; ++it) { for (NodeIt jt(graph); jt != INVALID; ++jt) { assert(johnson.connected(it, jt) == floyd.connected(it, jt)); if (johnson.connected(it, jt)) { assert(johnson.dist(it, jt) == floyd.dist(it, jt)); if (it != jt) { assert(johnson.dist(it, jt) == johnson.dist(it, johnson.predNode(it, jt)) + length[johnson.pred(it, jt)]); assert(floyd.dist(it, jt) == floyd.dist(it, floyd.predNode(it, jt)) + length[floyd.pred(it, jt)]); } } } } return 0; }