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
20 ///\brief Test cases for Dijkstra algorithm.
22 #include <lemon/concepts/digraph.h>
23 #include <lemon/smart_graph.h>
24 #include <lemon/list_graph.h>
25 #include <lemon/graph_utils.h>
26 #include <lemon/dijkstra.h>
27 #include <lemon/path.h>
29 #include "test_tools.h"
31 using namespace lemon;
33 void checkDijkstraCompile()
36 typedef concepts::Digraph Digraph;
37 typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
38 typedef Dijkstra<Digraph, LengthMap> DType;
47 // DType::PredNodeMap pn(G);
50 DType dijkstra_test(G,length);
54 l = dijkstra_test.dist(n);
55 e = dijkstra_test.predArc(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);
62 Path<Digraph> pp = dijkstra_test.path(n);
65 void checkDijkstraFunctionCompile()
68 typedef concepts::Digraph Digraph;
69 typedef Digraph::Arc Arc;
70 typedef Digraph::Node Node;
71 typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
74 dijkstra(g,LengthMap(),Node()).run();
75 dijkstra(g,LengthMap()).source(Node()).run();
76 dijkstra(g,LengthMap())
77 .predMap(concepts::WriteMap<Node,Arc>())
78 .distMap(concepts::WriteMap<Node,VType>())
82 template <class Digraph>
83 void checkDijkstra() {
84 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
85 typedef typename Digraph::template ArcMap<int> LengthMap;
90 PetStruct<Digraph> ps = addPetersen(G, 5);
92 for(int i=0;i<5;i++) {
93 length[ps.outcir[i]]=4;
94 length[ps.incir[i]]=1;
95 length[ps.chords[i]]=10;
100 Dijkstra<Digraph, LengthMap>
101 dijkstra_test(G, length);
102 dijkstra_test.run(s);
104 check(dijkstra_test.dist(t)==13,"Dijkstra found a wrong path.");
106 Path<Digraph> p = dijkstra_test.path(t);
107 check(p.length()==4,"getPath() found a wrong path.");
108 check(checkPath(G, p),"path() found a wrong path.");
109 check(pathSource(G, p) == s,"path() found a wrong path.");
110 check(pathTarget(G, p) == t,"path() found a wrong path.");
112 for(ArcIt e(G); e!=INVALID; ++e) {
115 check( !dijkstra_test.reached(u) || (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]),
116 "dist(target)-dist(source)-arc_length= " << dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]);
119 for(NodeIt v(G); v!=INVALID; ++v){
120 check(dijkstra_test.reached(v),"Each node should be reached.");
121 if ( dijkstra_test.predArc(v)!=INVALID ) {
122 Arc e=dijkstra_test.predArc(v);
124 check(u==dijkstra_test.predNode(v),"Wrong tree.");
125 check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == length[e],
126 "Wrong distance! Difference: " << std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]));
131 NullMap<Node,Arc> myPredMap;
132 dijkstra(G,length).predMap(myPredMap).run(s);
137 checkDijkstra<ListDigraph>();
138 checkDijkstra<SmartDigraph>();