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/dijkstra.h>
23 #include <lemon/path.h>
25 #include "graph_test.h"
26 #include "test_tools.h"
28 using namespace lemon;
30 void checkDijkstraCompile()
33 typedef concepts::Digraph Digraph;
34 typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
35 typedef Dijkstra<Digraph, LengthMap> DType;
44 // DType::PredNodeMap pn(G);
47 DType dijkstra_test(G,length);
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);
59 Path<Digraph> pp = dijkstra_test.path(n);
62 void checkDijkstraFunctionCompile()
65 typedef concepts::Digraph Digraph;
66 typedef Digraph::Arc Arc;
67 typedef Digraph::Node Node;
68 typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
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>())
79 template <class Digraph>
80 void checkDijkstra() {
81 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
82 typedef typename Digraph::template ArcMap<int> LengthMap;
87 PetStruct<Digraph> ps = addPetersen(G, 5);
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;
97 Dijkstra<Digraph, LengthMap>
98 dijkstra_test(G, length);
101 check(dijkstra_test.dist(t)==13,"Dijkstra found a wrong path.");
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.");
109 for(ArcIt e(G); e!=INVALID; ++e) {
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]);
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);
123 check(u==dijkstra_test.predNode(v),"Wrong tree.");
124 check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == length[e],
125 "Wrong distance! Difference: " <<
126 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>();