COIN-OR::LEMON - Graph Library

source: lemon-main/test/dijkstra_test.cc @ 241:92f046dd7f6c

Last change on this file since 241:92f046dd7f6c was 228:b6732e0d38c5, checked in by Balazs Dezso <deba@…>, 16 years ago

Reworking graph testing

  • The graph tests check more graph functionality.
  • The petersen graph is too regular, therefore special graphs are used.
  • The graph_test.h contains just general tools to test graphs.
File size: 4.1 KB
Line 
1/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 *
3 * This file is a part of LEMON, a generic C++ optimization library.
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/lgf_reader.h>
23
24#include <lemon/dijkstra.h>
25#include <lemon/path.h>
26
27#include "graph_test.h"
28#include "test_tools.h"
29
30using namespace lemon;
31
32char test_lgf[] =
33  "@nodes\n"
34  "label\n"
35  "0\n"
36  "1\n"
37  "2\n"
38  "3\n"
39  "4\n"
40  "@arcs\n"
41  "     label length\n"
42  "0 1  0     1\n"
43  "1 2  1     1\n"
44  "2 3  2     1\n"
45  "0 3  4     5\n"
46  "0 3  5     10\n"
47  "0 3  6     7\n"
48  "4 2  7     1\n"
49  "@attributes\n"
50  "source 0\n"
51  "target 3\n";
52
53void checkDijkstraCompile()
54{
55  typedef int VType;
56  typedef concepts::Digraph Digraph;
57  typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
58  typedef Dijkstra<Digraph, LengthMap> DType;
59
60  Digraph G;
61  Digraph::Node n;
62  Digraph::Arc e;
63  VType l;
64  bool b;
65  DType::DistMap d(G);
66  DType::PredMap p(G);
67  //  DType::PredNodeMap pn(G);
68  LengthMap length;
69
70  DType dijkstra_test(G,length);
71
72  dijkstra_test.run(n);
73
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);
81
82  Path<Digraph> pp = dijkstra_test.path(n);
83}
84
85void checkDijkstraFunctionCompile()
86{
87  typedef int VType;
88  typedef concepts::Digraph Digraph;
89  typedef Digraph::Arc Arc;
90  typedef Digraph::Node Node;
91  typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
92
93  Digraph g;
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>())
99    .run(Node());
100}
101
102template <class Digraph>
103void checkDijkstra() {
104  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
105  typedef typename Digraph::template ArcMap<int> LengthMap;
106
107  Digraph G;
108  Node s, t;
109  LengthMap length(G);
110
111  std::istringstream input(test_lgf);
112  digraphReader(input, G).
113    arcMap("length", length).
114    node("source", s).
115    node("target", t).
116    run();
117
118  Dijkstra<Digraph, LengthMap>
119        dijkstra_test(G, length);
120  dijkstra_test.run(s);
121
122  check(dijkstra_test.dist(t)==3,"Dijkstra found a wrong path.");
123
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.");
129
130  for(ArcIt e(G); e!=INVALID; ++e) {
131    Node u=G.source(e);
132    Node v=G.target(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]);
137  }
138
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);
144        Node u=G.source(e);
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]));
149      }
150    }
151  }
152
153  {
154    NullMap<Node,Arc> myPredMap;
155    dijkstra(G,length).predMap(myPredMap).run(s);
156  }
157}
158
159int main() {
160  checkDijkstra<ListDigraph>();
161  checkDijkstra<SmartDigraph>();
162  return 0;
163}
Note: See TracBrowser for help on using the repository browser.