[209] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
[170] | 2 | * |
---|
[209] | 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
[170] | 4 | * |
---|
| 5 | * Copyright (C) 2003-2008 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #include <lemon/concepts/digraph.h> |
---|
| 20 | #include <lemon/smart_graph.h> |
---|
| 21 | #include <lemon/list_graph.h> |
---|
| 22 | #include <lemon/dijkstra.h> |
---|
| 23 | #include <lemon/path.h> |
---|
| 24 | |
---|
[171] | 25 | #include "graph_test.h" |
---|
[170] | 26 | #include "test_tools.h" |
---|
| 27 | |
---|
| 28 | using namespace lemon; |
---|
| 29 | |
---|
[209] | 30 | void checkDijkstraCompile() |
---|
[170] | 31 | { |
---|
| 32 | typedef int VType; |
---|
| 33 | typedef concepts::Digraph Digraph; |
---|
| 34 | typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap; |
---|
| 35 | typedef Dijkstra<Digraph, LengthMap> DType; |
---|
[209] | 36 | |
---|
[170] | 37 | Digraph G; |
---|
| 38 | Digraph::Node n; |
---|
| 39 | Digraph::Arc e; |
---|
| 40 | VType l; |
---|
| 41 | bool b; |
---|
| 42 | DType::DistMap d(G); |
---|
| 43 | DType::PredMap p(G); |
---|
| 44 | // DType::PredNodeMap pn(G); |
---|
| 45 | LengthMap length; |
---|
| 46 | |
---|
| 47 | DType dijkstra_test(G,length); |
---|
| 48 | |
---|
| 49 | dijkstra_test.run(n); |
---|
| 50 | |
---|
| 51 | l = dijkstra_test.dist(n); |
---|
| 52 | e = dijkstra_test.predArc(n); |
---|
| 53 | n = dijkstra_test.predNode(n); |
---|
| 54 | d = dijkstra_test.distMap(); |
---|
| 55 | p = dijkstra_test.predMap(); |
---|
| 56 | // pn = dijkstra_test.predNodeMap(); |
---|
| 57 | b = dijkstra_test.reached(n); |
---|
| 58 | |
---|
| 59 | Path<Digraph> pp = dijkstra_test.path(n); |
---|
| 60 | } |
---|
| 61 | |
---|
[209] | 62 | void checkDijkstraFunctionCompile() |
---|
[170] | 63 | { |
---|
| 64 | typedef int VType; |
---|
| 65 | typedef concepts::Digraph Digraph; |
---|
| 66 | typedef Digraph::Arc Arc; |
---|
| 67 | typedef Digraph::Node Node; |
---|
| 68 | typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap; |
---|
[209] | 69 | |
---|
[170] | 70 | Digraph g; |
---|
| 71 | dijkstra(g,LengthMap(),Node()).run(); |
---|
| 72 | dijkstra(g,LengthMap()).source(Node()).run(); |
---|
| 73 | dijkstra(g,LengthMap()) |
---|
| 74 | .predMap(concepts::WriteMap<Node,Arc>()) |
---|
| 75 | .distMap(concepts::WriteMap<Node,VType>()) |
---|
| 76 | .run(Node()); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | template <class Digraph> |
---|
[209] | 80 | void checkDijkstra() { |
---|
[170] | 81 | TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
---|
| 82 | typedef typename Digraph::template ArcMap<int> LengthMap; |
---|
| 83 | |
---|
| 84 | Digraph G; |
---|
| 85 | Node s, t; |
---|
| 86 | LengthMap length(G); |
---|
| 87 | PetStruct<Digraph> ps = addPetersen(G, 5); |
---|
[209] | 88 | |
---|
[170] | 89 | for(int i=0;i<5;i++) { |
---|
| 90 | length[ps.outcir[i]]=4; |
---|
| 91 | length[ps.incir[i]]=1; |
---|
| 92 | length[ps.chords[i]]=10; |
---|
| 93 | } |
---|
| 94 | s=ps.outer[0]; |
---|
| 95 | t=ps.inner[1]; |
---|
[209] | 96 | |
---|
| 97 | Dijkstra<Digraph, LengthMap> |
---|
| 98 | dijkstra_test(G, length); |
---|
[170] | 99 | dijkstra_test.run(s); |
---|
[209] | 100 | |
---|
[170] | 101 | check(dijkstra_test.dist(t)==13,"Dijkstra found a wrong path."); |
---|
| 102 | |
---|
| 103 | Path<Digraph> p = dijkstra_test.path(t); |
---|
| 104 | check(p.length()==4,"getPath() found a wrong path."); |
---|
| 105 | check(checkPath(G, p),"path() found a wrong path."); |
---|
| 106 | check(pathSource(G, p) == s,"path() found a wrong path."); |
---|
| 107 | check(pathTarget(G, p) == t,"path() found a wrong path."); |
---|
[209] | 108 | |
---|
[170] | 109 | for(ArcIt e(G); e!=INVALID; ++e) { |
---|
| 110 | Node u=G.source(e); |
---|
| 111 | Node v=G.target(e); |
---|
[210] | 112 | check( !dijkstra_test.reached(u) || |
---|
| 113 | (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]), |
---|
| 114 | "dist(target)-dist(source)-arc_length= " << |
---|
| 115 | dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]); |
---|
[170] | 116 | } |
---|
| 117 | |
---|
| 118 | for(NodeIt v(G); v!=INVALID; ++v){ |
---|
| 119 | check(dijkstra_test.reached(v),"Each node should be reached."); |
---|
| 120 | if ( dijkstra_test.predArc(v)!=INVALID ) { |
---|
| 121 | Arc e=dijkstra_test.predArc(v); |
---|
| 122 | Node u=G.source(e); |
---|
| 123 | check(u==dijkstra_test.predNode(v),"Wrong tree."); |
---|
| 124 | check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == length[e], |
---|
[210] | 125 | "Wrong distance! Difference: " << |
---|
| 126 | std::abs(dijkstra_test.dist(v)-dijkstra_test.dist(u)-length[e])); |
---|
[170] | 127 | } |
---|
| 128 | } |
---|
[209] | 129 | |
---|
[170] | 130 | { |
---|
| 131 | NullMap<Node,Arc> myPredMap; |
---|
| 132 | dijkstra(G,length).predMap(myPredMap).run(s); |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | int main() { |
---|
| 137 | checkDijkstra<ListDigraph>(); |
---|
| 138 | checkDijkstra<SmartDigraph>(); |
---|
| 139 | return 0; |
---|
| 140 | } |
---|