1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
19 #include <lemon/concepts/digraph.h>
20 #include <lemon/smart_graph.h>
21 #include <lemon/list_graph.h>
22 #include <lemon/lgf_reader.h>
24 #include <lemon/dijkstra.h>
25 #include <lemon/path.h>
27 #include "graph_test.h"
28 #include "test_tools.h"
30 using namespace lemon;
53 void checkDijkstraCompile()
56 typedef concepts::Digraph Digraph;
57 typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
58 typedef Dijkstra<Digraph, LengthMap> DType;
67 // DType::PredNodeMap pn(G);
70 DType dijkstra_test(G,length);
74 l = dijkstra_test.dist(n);
75 e = dijkstra_test.predArc(n);
76 n = dijkstra_test.predNode(n);
77 d = dijkstra_test.distMap();
78 p = dijkstra_test.predMap();
79 // pn = dijkstra_test.predNodeMap();
80 b = dijkstra_test.reached(n);
82 Path<Digraph> pp = dijkstra_test.path(n);
85 void checkDijkstraFunctionCompile()
88 typedef concepts::Digraph Digraph;
89 typedef Digraph::Arc Arc;
90 typedef Digraph::Node Node;
91 typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
94 dijkstra(g,LengthMap(),Node()).run();
95 dijkstra(g,LengthMap()).source(Node()).run();
96 dijkstra(g,LengthMap())
97 .predMap(concepts::WriteMap<Node,Arc>())
98 .distMap(concepts::WriteMap<Node,VType>())
102 template <class Digraph>
103 void checkDijkstra() {
104 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
105 typedef typename Digraph::template ArcMap<int> LengthMap;
111 std::istringstream input(test_lgf);
112 digraphReader(input, G).
113 arcMap("length", length).
118 Dijkstra<Digraph, LengthMap>
119 dijkstra_test(G, length);
120 dijkstra_test.run(s);
122 check(dijkstra_test.dist(t)==3,"Dijkstra found a wrong path.");
124 Path<Digraph> p = dijkstra_test.path(t);
125 check(p.length()==3,"getPath() found a wrong path.");
126 check(checkPath(G, p),"path() found a wrong path.");
127 check(pathSource(G, p) == s,"path() found a wrong path.");
128 check(pathTarget(G, p) == t,"path() found a wrong path.");
130 for(ArcIt e(G); e!=INVALID; ++e) {
133 check( !dijkstra_test.reached(u) ||
134 (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]),
135 "dist(target)-dist(source)-arc_length= " <<
136 dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]);
139 for(NodeIt v(G); v!=INVALID; ++v) {
140 if (dijkstra_test.reached(v)) {
141 check(v==s || dijkstra_test.predArc(v)!=INVALID, "Wrong tree.");
142 if (dijkstra_test.predArc(v)!=INVALID ) {
143 Arc e=dijkstra_test.predArc(v);
145 check(u==dijkstra_test.predNode(v),"Wrong tree.");
146 check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == length[e],
147 "Wrong distance! Difference: " <<
148 std::abs(dijkstra_test.dist(v)-dijkstra_test.dist(u)-length[e]));
154 NullMap<Node,Arc> myPredMap;
155 dijkstra(G,length).predMap(myPredMap).run(s);
160 checkDijkstra<ListDigraph>();
161 checkDijkstra<SmartDigraph>();