COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/test/dijkstra_test.cc @ 1148:1eea022c7a16

Last change on this file since 1148:1eea022c7a16 was 1148:1eea022c7a16, checked in by Alpar Juttner, 19 years ago

Test the new dijkstra features.

File size: 3.0 KB
RevLine 
[906]1/* -*- C++ -*-
[921]2 * src/test/dijkstra_test.cc - Part of LEMON, a generic C++ optimization library
[906]3 *
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
6 *
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
10 *
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
13 * purpose.
14 *
15 */
16
[578]17#include "test_tools.h"
[921]18#include <lemon/smart_graph.h>
19#include <lemon/dijkstra.h>
[1148]20#include <lemon/maps.h>
[959]21#include <lemon/concept/graph.h>
22#include <lemon/concept/maps.h>
[921]23using namespace lemon;
[568]24
25const int PET_SIZE =5;
26
[570]27
[793]28void check_Dijkstra_BinHeap_Compile()
[570]29{
30  typedef int VType;
[959]31  typedef concept::StaticGraph Graph;
[570]32
33  typedef Graph::Edge Edge;
34  typedef Graph::Node Node;
35  typedef Graph::EdgeIt EdgeIt;
36  typedef Graph::NodeIt NodeIt;
[959]37  typedef concept::ReadMap<Edge,VType> LengthMap;
[570]38 
39  typedef Dijkstra<Graph, LengthMap> DType;
40 
41  Graph G;
42  Node n;
43  Edge e;
44  VType l;
45  bool b;
46  DType::DistMap d(G);
47  DType::PredMap p(G);
[1148]48  //  DType::PredNodeMap pn(G);
[793]49  LengthMap cap;
[570]50
51  DType dijkstra_test(G,cap);
52
53  dijkstra_test.run(n);
54
55  l  = dijkstra_test.dist(n);
56  e  = dijkstra_test.pred(n);
57  n  = dijkstra_test.predNode(n);
58  d  = dijkstra_test.distMap();
59  p  = dijkstra_test.predMap();
[1148]60  //  pn = dijkstra_test.predNodeMap();
[570]61  b  = dijkstra_test.reached(n);
[1148]62 
[570]63}
64
[568]65int main()
66{
67   
68  typedef SmartGraph Graph;
69
70  typedef Graph::Edge Edge;
71  typedef Graph::Node Node;
72  typedef Graph::EdgeIt EdgeIt;
73  typedef Graph::NodeIt NodeIt;
74  typedef Graph::EdgeMap<int> LengthMap;
75
76  Graph G;
77  Node s, t;
78  LengthMap cap(G);
79  PetStruct<Graph> ps = addPetersen(G,PET_SIZE);
[578]80   
[568]81  for(int i=0;i<PET_SIZE;i++) {
82    cap[ps.outcir[i]]=4;
83    cap[ps.incir[i]]=1;
84    cap[ps.chords[i]]=10;
85  }
86  s=ps.outer[0];
87  t=ps.inner[1];
88 
89  Dijkstra<Graph, LengthMap>
90        dijkstra_test(G, cap);
91  dijkstra_test.run(s);
92 
93  check(dijkstra_test.dist(t)==13,"Dijkstra found a wrong path.");
[585]94
95
[776]96  for(EdgeIt e(G); e!=INVALID; ++e) {
[986]97    Node u=G.source(e);
98    Node v=G.target(e);
[585]99    check( !dijkstra_test.reached(u) ||
100           (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= cap[e]),
[986]101           "dist(target)-dist(source)- edge_length= "
[585]102           << dijkstra_test.dist(v) - dijkstra_test.dist(u)
103           - cap[e]);
104  }
105
106  ///\bug This works only for integer lengths
[780]107  for(NodeIt v(G); v!=INVALID; ++v){
108    check(dijkstra_test.reached(v),"Each node should be reached.");
109    if ( dijkstra_test.pred(v)!=INVALID ) {
[585]110      Edge e=dijkstra_test.pred(v);
[986]111      Node u=G.source(e);
[780]112      check(u==dijkstra_test.predNode(v),"Wrong tree.");
[585]113      check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == cap[e],
[780]114            "Wrong distance! Difference: "
[585]115            << std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u)
116                            - cap[e]));
117    }
[780]118  }
[1148]119
120 
121  {
122    NullMap<Node,Node> myPredNodeMap;
123    dijkstra(G,cap).predNodeMap(myPredNodeMap).run(s);
124  }
125  return 0;
[568]126}
Note: See TracBrowser for help on using the repository browser.