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>
23 #include <lemon/dijkstra.h>
24 #include <lemon/path.h>
26 #include "graph_test.h"
27 #include "test_tools.h"
29 using namespace lemon;
52 void checkDijkstraCompile()
55 typedef concepts::Digraph Digraph;
56 typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
57 typedef Dijkstra<Digraph, LengthMap> DType;
68 DType dijkstra_test(G,length);
72 l = dijkstra_test.dist(n);
73 e = dijkstra_test.predArc(n);
74 n = dijkstra_test.predNode(n);
75 d = dijkstra_test.distMap();
76 p = dijkstra_test.predMap();
77 b = dijkstra_test.reached(n);
79 Path<Digraph> pp = dijkstra_test.path(n);
82 void checkDijkstraFunctionCompile()
85 typedef concepts::Digraph Digraph;
86 typedef Digraph::Arc Arc;
87 typedef Digraph::Node Node;
88 typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
92 dijkstra(g,LengthMap()).run(Node());
93 b=dijkstra(g,LengthMap()).run(Node(),Node());
94 dijkstra(g,LengthMap())
95 .predMap(concepts::ReadWriteMap<Node,Arc>())
96 .distMap(concepts::ReadWriteMap<Node,VType>())
97 .processedMap(concepts::WriteMap<Node,bool>())
99 b=dijkstra(g,LengthMap())
100 .predMap(concepts::ReadWriteMap<Node,Arc>())
101 .distMap(concepts::ReadWriteMap<Node,VType>())
102 .processedMap(concepts::WriteMap<Node,bool>())
103 .path(concepts::Path<Digraph>())
108 template <class Digraph>
109 void checkDijkstra() {
110 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
111 typedef typename Digraph::template ArcMap<int> LengthMap;
117 std::istringstream input(test_lgf);
118 digraphReader(input, G).
119 arcMap("length", length).
124 Dijkstra<Digraph, LengthMap>
125 dijkstra_test(G, length);
126 dijkstra_test.run(s);
128 check(dijkstra_test.dist(t)==3,"Dijkstra found a wrong path.");
130 Path<Digraph> p = dijkstra_test.path(t);
131 check(p.length()==3,"path() found a wrong path.");
132 check(checkPath(G, p),"path() found a wrong path.");
133 check(pathSource(G, p) == s,"path() found a wrong path.");
134 check(pathTarget(G, p) == t,"path() found a wrong path.");
136 for(ArcIt e(G); e!=INVALID; ++e) {
139 check( !dijkstra_test.reached(u) ||
140 (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]),
141 "Wrong output. dist(target)-dist(source)-arc_length=" <<
142 dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]);
145 for(NodeIt v(G); v!=INVALID; ++v) {
146 if (dijkstra_test.reached(v)) {
147 check(v==s || dijkstra_test.predArc(v)!=INVALID, "Wrong tree.");
148 if (dijkstra_test.predArc(v)!=INVALID ) {
149 Arc e=dijkstra_test.predArc(v);
151 check(u==dijkstra_test.predNode(v),"Wrong tree.");
152 check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == length[e],
153 "Wrong distance! Difference: " <<
154 std::abs(dijkstra_test.dist(v)-dijkstra_test.dist(u)-length[e]));
160 NullMap<Node,Arc> myPredMap;
161 dijkstra(G,length).predMap(myPredMap).run(s);
166 checkDijkstra<ListDigraph>();
167 checkDijkstra<SmartDigraph>();