[906] | 1 | /* -*- C++ -*- |
---|
[1435] | 2 | * test/dijkstra_test.cc - Part of LEMON, a generic C++ optimization library |
---|
[906] | 3 | * |
---|
[1164] | 4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[906] | 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> |
---|
[1283] | 20 | #include <lemon/path.h> |
---|
[1148] | 21 | #include <lemon/maps.h> |
---|
[959] | 22 | #include <lemon/concept/graph.h> |
---|
| 23 | #include <lemon/concept/maps.h> |
---|
[921] | 24 | using namespace lemon; |
---|
[568] | 25 | |
---|
| 26 | const int PET_SIZE =5; |
---|
| 27 | |
---|
[570] | 28 | |
---|
[793] | 29 | void check_Dijkstra_BinHeap_Compile() |
---|
[570] | 30 | { |
---|
| 31 | typedef int VType; |
---|
[959] | 32 | typedef concept::StaticGraph Graph; |
---|
[570] | 33 | |
---|
| 34 | typedef Graph::Edge Edge; |
---|
| 35 | typedef Graph::Node Node; |
---|
| 36 | typedef Graph::EdgeIt EdgeIt; |
---|
| 37 | typedef Graph::NodeIt NodeIt; |
---|
[959] | 38 | typedef concept::ReadMap<Edge,VType> LengthMap; |
---|
[570] | 39 | |
---|
| 40 | typedef Dijkstra<Graph, LengthMap> DType; |
---|
| 41 | |
---|
| 42 | Graph G; |
---|
| 43 | Node n; |
---|
| 44 | Edge e; |
---|
| 45 | VType l; |
---|
| 46 | bool b; |
---|
| 47 | DType::DistMap d(G); |
---|
| 48 | DType::PredMap p(G); |
---|
[1148] | 49 | // DType::PredNodeMap pn(G); |
---|
[793] | 50 | LengthMap cap; |
---|
[570] | 51 | |
---|
| 52 | DType dijkstra_test(G,cap); |
---|
| 53 | |
---|
| 54 | dijkstra_test.run(n); |
---|
| 55 | |
---|
| 56 | l = dijkstra_test.dist(n); |
---|
| 57 | e = dijkstra_test.pred(n); |
---|
| 58 | n = dijkstra_test.predNode(n); |
---|
| 59 | d = dijkstra_test.distMap(); |
---|
| 60 | p = dijkstra_test.predMap(); |
---|
[1148] | 61 | // pn = dijkstra_test.predNodeMap(); |
---|
[570] | 62 | b = dijkstra_test.reached(n); |
---|
[1283] | 63 | |
---|
| 64 | DirPath<Graph> pp(G); |
---|
| 65 | dijkstra_test.getPath(pp,n); |
---|
[570] | 66 | } |
---|
| 67 | |
---|
[1220] | 68 | void check_Dijkstra_Function_Compile() |
---|
| 69 | { |
---|
| 70 | typedef int VType; |
---|
| 71 | typedef concept::StaticGraph Graph; |
---|
| 72 | |
---|
| 73 | typedef Graph::Edge Edge; |
---|
| 74 | typedef Graph::Node Node; |
---|
| 75 | typedef Graph::EdgeIt EdgeIt; |
---|
| 76 | typedef Graph::NodeIt NodeIt; |
---|
| 77 | typedef concept::ReadMap<Edge,VType> LengthMap; |
---|
| 78 | |
---|
| 79 | dijkstra(Graph(),LengthMap(),Node()).run(); |
---|
| 80 | dijkstra(Graph(),LengthMap()).source(Node()).run(); |
---|
| 81 | dijkstra(Graph(),LengthMap()) |
---|
| 82 | .predMap(concept::WriteMap<Node,Edge>()) |
---|
| 83 | .distMap(concept::WriteMap<Node,VType>()) |
---|
| 84 | .run(Node()); |
---|
| 85 | |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | |
---|
[568] | 89 | int main() |
---|
| 90 | { |
---|
| 91 | |
---|
| 92 | typedef SmartGraph Graph; |
---|
| 93 | |
---|
| 94 | typedef Graph::Edge Edge; |
---|
| 95 | typedef Graph::Node Node; |
---|
| 96 | typedef Graph::EdgeIt EdgeIt; |
---|
| 97 | typedef Graph::NodeIt NodeIt; |
---|
| 98 | typedef Graph::EdgeMap<int> LengthMap; |
---|
| 99 | |
---|
| 100 | Graph G; |
---|
| 101 | Node s, t; |
---|
| 102 | LengthMap cap(G); |
---|
| 103 | PetStruct<Graph> ps = addPetersen(G,PET_SIZE); |
---|
[578] | 104 | |
---|
[568] | 105 | for(int i=0;i<PET_SIZE;i++) { |
---|
| 106 | cap[ps.outcir[i]]=4; |
---|
| 107 | cap[ps.incir[i]]=1; |
---|
| 108 | cap[ps.chords[i]]=10; |
---|
| 109 | } |
---|
| 110 | s=ps.outer[0]; |
---|
| 111 | t=ps.inner[1]; |
---|
| 112 | |
---|
| 113 | Dijkstra<Graph, LengthMap> |
---|
| 114 | dijkstra_test(G, cap); |
---|
| 115 | dijkstra_test.run(s); |
---|
| 116 | |
---|
| 117 | check(dijkstra_test.dist(t)==13,"Dijkstra found a wrong path."); |
---|
[585] | 118 | |
---|
| 119 | |
---|
[1283] | 120 | DirPath<Graph> p(G); |
---|
| 121 | check(dijkstra_test.getPath(p,t),"getPath() failed to set the path."); |
---|
| 122 | check(p.length()==4,"getPath() found a wrong path."); |
---|
| 123 | |
---|
| 124 | |
---|
[776] | 125 | for(EdgeIt e(G); e!=INVALID; ++e) { |
---|
[986] | 126 | Node u=G.source(e); |
---|
| 127 | Node v=G.target(e); |
---|
[585] | 128 | check( !dijkstra_test.reached(u) || |
---|
| 129 | (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= cap[e]), |
---|
[986] | 130 | "dist(target)-dist(source)- edge_length= " |
---|
[585] | 131 | << dijkstra_test.dist(v) - dijkstra_test.dist(u) |
---|
| 132 | - cap[e]); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | ///\bug This works only for integer lengths |
---|
[780] | 136 | for(NodeIt v(G); v!=INVALID; ++v){ |
---|
| 137 | check(dijkstra_test.reached(v),"Each node should be reached."); |
---|
| 138 | if ( dijkstra_test.pred(v)!=INVALID ) { |
---|
[585] | 139 | Edge e=dijkstra_test.pred(v); |
---|
[986] | 140 | Node u=G.source(e); |
---|
[780] | 141 | check(u==dijkstra_test.predNode(v),"Wrong tree."); |
---|
[585] | 142 | check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == cap[e], |
---|
[780] | 143 | "Wrong distance! Difference: " |
---|
[585] | 144 | << std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u) |
---|
| 145 | - cap[e])); |
---|
| 146 | } |
---|
[780] | 147 | } |
---|
[1148] | 148 | |
---|
| 149 | |
---|
| 150 | { |
---|
[1218] | 151 | NullMap<Node,Edge> myPredMap; |
---|
| 152 | dijkstra(G,cap).predMap(myPredMap).run(s); |
---|
[1148] | 153 | } |
---|
| 154 | return 0; |
---|
[568] | 155 | } |
---|