[906] | 1 | /* -*- C++ -*- |
---|
[921] | 2 | * src/test/dijkstra_test.cc - Part of LEMON, a generic C++ optimization library |
---|
[906] | 3 | * |
---|
| 4 | * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 5 | * (Egervary Combinatorial Optimization Research Group, EGRES). |
---|
| 6 | * |
---|
| 7 | * Permission to use, modify and distribute this software is granted |
---|
| 8 | * provided that this copyright notice appears in all copies. For |
---|
| 9 | * precise terms see the accompanying LICENSE file. |
---|
| 10 | * |
---|
| 11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 12 | * express or implied, and with no claim as to its suitability for any |
---|
| 13 | * purpose. |
---|
| 14 | * |
---|
| 15 | */ |
---|
| 16 | |
---|
[578] | 17 | #include "test_tools.h" |
---|
[921] | 18 | #include <lemon/smart_graph.h> |
---|
| 19 | #include <lemon/dijkstra.h> |
---|
[959] | 20 | #include <lemon/concept/graph.h> |
---|
| 21 | #include <lemon/concept/maps.h> |
---|
[921] | 22 | using namespace lemon; |
---|
[568] | 23 | |
---|
| 24 | const int PET_SIZE =5; |
---|
| 25 | |
---|
[570] | 26 | |
---|
[793] | 27 | void check_Dijkstra_BinHeap_Compile() |
---|
[570] | 28 | { |
---|
| 29 | typedef int VType; |
---|
[959] | 30 | typedef concept::StaticGraph Graph; |
---|
[570] | 31 | |
---|
| 32 | typedef Graph::Edge Edge; |
---|
| 33 | typedef Graph::Node Node; |
---|
| 34 | typedef Graph::EdgeIt EdgeIt; |
---|
| 35 | typedef Graph::NodeIt NodeIt; |
---|
[959] | 36 | typedef concept::ReadMap<Edge,VType> LengthMap; |
---|
[570] | 37 | |
---|
| 38 | typedef Dijkstra<Graph, LengthMap> DType; |
---|
| 39 | |
---|
| 40 | Graph G; |
---|
| 41 | Node n; |
---|
| 42 | Edge e; |
---|
| 43 | VType l; |
---|
| 44 | bool b; |
---|
| 45 | DType::DistMap d(G); |
---|
| 46 | DType::PredMap p(G); |
---|
| 47 | DType::PredNodeMap pn(G); |
---|
[793] | 48 | LengthMap cap; |
---|
[570] | 49 | |
---|
| 50 | DType dijkstra_test(G,cap); |
---|
| 51 | |
---|
| 52 | dijkstra_test.run(n); |
---|
| 53 | |
---|
| 54 | l = dijkstra_test.dist(n); |
---|
| 55 | e = dijkstra_test.pred(n); |
---|
| 56 | n = dijkstra_test.predNode(n); |
---|
| 57 | d = dijkstra_test.distMap(); |
---|
| 58 | p = dijkstra_test.predMap(); |
---|
| 59 | pn = dijkstra_test.predNodeMap(); |
---|
| 60 | b = dijkstra_test.reached(n); |
---|
| 61 | |
---|
| 62 | } |
---|
| 63 | |
---|
[568] | 64 | int main() |
---|
| 65 | { |
---|
| 66 | |
---|
| 67 | typedef SmartGraph Graph; |
---|
| 68 | |
---|
| 69 | typedef Graph::Edge Edge; |
---|
| 70 | typedef Graph::Node Node; |
---|
| 71 | typedef Graph::EdgeIt EdgeIt; |
---|
| 72 | typedef Graph::NodeIt NodeIt; |
---|
| 73 | typedef Graph::EdgeMap<int> LengthMap; |
---|
| 74 | |
---|
| 75 | Graph G; |
---|
| 76 | Node s, t; |
---|
| 77 | LengthMap cap(G); |
---|
| 78 | PetStruct<Graph> ps = addPetersen(G,PET_SIZE); |
---|
[578] | 79 | |
---|
[568] | 80 | for(int i=0;i<PET_SIZE;i++) { |
---|
| 81 | cap[ps.outcir[i]]=4; |
---|
| 82 | cap[ps.incir[i]]=1; |
---|
| 83 | cap[ps.chords[i]]=10; |
---|
| 84 | } |
---|
| 85 | s=ps.outer[0]; |
---|
| 86 | t=ps.inner[1]; |
---|
| 87 | |
---|
| 88 | Dijkstra<Graph, LengthMap> |
---|
| 89 | dijkstra_test(G, cap); |
---|
| 90 | dijkstra_test.run(s); |
---|
| 91 | |
---|
| 92 | check(dijkstra_test.dist(t)==13,"Dijkstra found a wrong path."); |
---|
[585] | 93 | |
---|
| 94 | |
---|
[776] | 95 | for(EdgeIt e(G); e!=INVALID; ++e) { |
---|
[986] | 96 | Node u=G.source(e); |
---|
| 97 | Node v=G.target(e); |
---|
[585] | 98 | check( !dijkstra_test.reached(u) || |
---|
| 99 | (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= cap[e]), |
---|
[986] | 100 | "dist(target)-dist(source)- edge_length= " |
---|
[585] | 101 | << dijkstra_test.dist(v) - dijkstra_test.dist(u) |
---|
| 102 | - cap[e]); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | ///\bug This works only for integer lengths |
---|
[780] | 106 | for(NodeIt v(G); v!=INVALID; ++v){ |
---|
| 107 | check(dijkstra_test.reached(v),"Each node should be reached."); |
---|
| 108 | if ( dijkstra_test.pred(v)!=INVALID ) { |
---|
[585] | 109 | Edge e=dijkstra_test.pred(v); |
---|
[986] | 110 | Node u=G.source(e); |
---|
[780] | 111 | check(u==dijkstra_test.predNode(v),"Wrong tree."); |
---|
[585] | 112 | check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == cap[e], |
---|
[780] | 113 | "Wrong distance! Difference: " |
---|
[585] | 114 | << std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u) |
---|
| 115 | - cap[e])); |
---|
| 116 | } |
---|
[780] | 117 | } |
---|
[568] | 118 | } |
---|
[780] | 119 | |
---|