alpar@906: /* -*- C++ -*- alpar@921: * src/test/dijkstra_heap_test.cc - Part of LEMON, a generic C++ optimization library alpar@906: * alpar@1164: * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@906: * (Egervary Combinatorial Optimization Research Group, EGRES). alpar@906: * alpar@906: * Permission to use, modify and distribute this software is granted alpar@906: * provided that this copyright notice appears in all copies. For alpar@906: * precise terms see the accompanying LICENSE file. alpar@906: * alpar@906: * This software is provided "AS IS" with no warranty of any kind, alpar@906: * express or implied, and with no claim as to its suitability for any alpar@906: * purpose. alpar@906: * alpar@906: */ alpar@906: jacint@384: //Tests dijsktra.h with two heap implementations: jacint@384: //the default binary heap of bin_heap.h, and the jacint@384: //Fibonacci heap of fib_heap.h. jacint@384: jacint@384: //The input is a graph in standard dimacs format from the standard input (like alpar@921: //in /lemon_loc/testfiles/dimacs). It runs dijkstra.h on this graph with both jacint@422: //heaps, checking two postconditions: jacint@384: jacint@384: //- if the edges e=uv of the shortest path tree reported by dijkstra.h have jacint@384: //dist(v)-dist(u)=length(e) jacint@384: jacint@384: // - if all edges e=uv with u reachable from the root have jacint@384: //dist(v)-dist(u)>=length(e) jacint@384: #include jacint@384: #include jacint@384: alpar@921: #include deba@1194: deba@1194: #include deba@1194: #include deba@1194: deba@1194: alpar@921: #include alpar@921: #include alpar@921: #include deba@1194: #include deba@1194: alpar@921: #include alpar@921: #include deba@1194: #include jacint@384: alpar@921: using namespace lemon; jacint@384: deba@1194: typedef ListGraph Graph; deba@1194: deba@1194: typedef Graph::Edge Edge; deba@1194: typedef Graph::Node Node; deba@1194: typedef Graph::EdgeIt EdgeIt; deba@1194: typedef Graph::NodeIt NodeIt; deba@1194: typedef Graph::EdgeMap LengthMap; deba@1194: deba@1194: deba@1194: struct FibTraits : public DijkstraDefaultTraits { deba@1194: typedef FibHeap > Heap; deba@1194: }; deba@1194: deba@1194: struct RadixTraits : public DijkstraDefaultTraits { deba@1194: typedef RadixHeap > Heap; deba@1194: }; deba@1194: deba@1194: deba@1194: int main(int argc, const char* argv[]) { jacint@384: jacint@384: deba@1194: Graph graph; deba@1194: Node s, t; deba@1194: LengthMap cap(graph); deba@1194: readDimacs(std::cin, graph, cap, s, t); deba@1194: Timer ts; jacint@384: deba@1194: GraphWriter writer(std::cout, graph); deba@1194: deba@1194: IdMap nodeIdMap(graph); deba@1194: writer.addNodeMap("id", nodeIdMap); deba@1194: deba@1194: IdMap edgeIdMap(graph); deba@1194: writer.addEdgeMap("id", edgeIdMap); deba@1194: deba@1194: writer.addEdgeMap("cap", cap); deba@1194: deba@1194: writer.run(); jacint@384: jacint@384: std::cout << jacint@384: "\n Testing dijkstra.h with binary heap implementation bin_heap.h," jacint@384: < deba@1194: dijkstra_test(graph, cap); jacint@384: ts.reset(); deba@1194: dijkstra_test.run(s); jacint@384: std::cout << "elapsed time: " << ts << std::endl; deba@1194: jacint@384: int error1=0; jacint@384: int error2=0; jacint@384: deba@1194: for(EdgeIt e(graph); e!=INVALID; ++e) { deba@1194: Node u=graph.source(e); deba@1194: Node v=graph.target(e); jacint@384: if ( dijkstra_test.dist(v) - dijkstra_test.dist(u) > cap[e] ) jacint@384: if ( dijkstra_test.reached(u) ) { alpar@986: std::cout<<"Error! dist(target)-dist(source)- edge_length= " jacint@384: < deba@1194: dijkstra_test2(graph, cap); jacint@384: ts.reset(); jacint@384: dijkstra_test2.run(s); jacint@384: std::cout << "elapsed time: " << ts << std::endl; jacint@384: jacint@384: error1=0; jacint@384: error2=0; jacint@384: deba@1194: for(EdgeIt e(graph); e!=INVALID; ++e) { deba@1194: Node u=graph.source(e); deba@1194: Node v=graph.target(e); jacint@384: if ( dijkstra_test2.dist(v) - dijkstra_test2.dist(u) > cap[e] ) jacint@384: if ( dijkstra_test2.reached(u) ) { alpar@986: std::cout<<"Error! dist(target)-dist(source)- edge_length= " jacint@384: < deba@1194: dijkstra_test3(graph, cap); deba@1194: ts.reset(); deba@1194: dijkstra_test3.run(s); deba@1194: std::cout << "elapsed time: " << ts << std::endl; deba@1194: deba@1194: deba@1194: error1=0; deba@1194: error2=0; deba@1194: deba@1194: for(EdgeIt e(graph); e!=INVALID; ++e) { deba@1194: Node u=graph.source(e); deba@1194: Node v=graph.target(e); deba@1194: if ( dijkstra_test3.dist(v) - dijkstra_test3.dist(u) > cap[e] ) deba@1194: if ( dijkstra_test3.reached(u) ) { deba@1194: std::cout<<"Error! dist(target)-dist(source)- edge_length= " deba@1194: <