src/test/dijkstra_test.cc
author alpar
Thu, 31 Mar 2005 13:31:39 +0000
changeset 1283 fc20371677b9
parent 1220 20b26ee5812b
child 1359 1581f961cfaa
permissions -rw-r--r--
getPath() added to Bfs/Dfs/Dijkstra.
     1 /* -*- C++ -*-
     2  * src/test/dijkstra_test.cc - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2005 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 
    17 #include "test_tools.h"
    18 #include <lemon/smart_graph.h>
    19 #include <lemon/dijkstra.h>
    20 #include <lemon/path.h>
    21 #include <lemon/maps.h>
    22 #include <lemon/concept/graph.h>
    23 #include <lemon/concept/maps.h>
    24 using namespace lemon;
    25 
    26 const int PET_SIZE =5;
    27 
    28 
    29 void check_Dijkstra_BinHeap_Compile() 
    30 {
    31   typedef int VType;
    32   typedef concept::StaticGraph Graph;
    33 
    34   typedef Graph::Edge Edge;
    35   typedef Graph::Node Node;
    36   typedef Graph::EdgeIt EdgeIt;
    37   typedef Graph::NodeIt NodeIt;
    38   typedef concept::ReadMap<Edge,VType> LengthMap;
    39  
    40   typedef Dijkstra<Graph, LengthMap> DType;
    41   
    42   Graph G;
    43   Node n;
    44   Edge e;
    45   VType l;
    46   bool b;
    47   DType::DistMap d(G);
    48   DType::PredMap p(G);
    49   //  DType::PredNodeMap pn(G);
    50   LengthMap cap;
    51 
    52   DType dijkstra_test(G,cap);
    53 
    54   dijkstra_test.run(n);
    55 
    56   l  = dijkstra_test.dist(n);
    57   e  = dijkstra_test.pred(n);
    58   n  = dijkstra_test.predNode(n);
    59   d  = dijkstra_test.distMap();
    60   p  = dijkstra_test.predMap();
    61   //  pn = dijkstra_test.predNodeMap();
    62   b  = dijkstra_test.reached(n);
    63 
    64   DirPath<Graph> pp(G);
    65   dijkstra_test.getPath(pp,n);
    66 }
    67 
    68 void check_Dijkstra_Function_Compile() 
    69 {
    70   typedef int VType;
    71   typedef concept::StaticGraph Graph;
    72 
    73   typedef Graph::Edge Edge;
    74   typedef Graph::Node Node;
    75   typedef Graph::EdgeIt EdgeIt;
    76   typedef Graph::NodeIt NodeIt;
    77   typedef concept::ReadMap<Edge,VType> LengthMap;
    78    
    79   dijkstra(Graph(),LengthMap(),Node()).run();
    80   dijkstra(Graph(),LengthMap()).source(Node()).run();
    81   dijkstra(Graph(),LengthMap())
    82     .predMap(concept::WriteMap<Node,Edge>())
    83     .distMap(concept::WriteMap<Node,VType>())
    84     .run(Node());
    85   
    86 }
    87 
    88 
    89 int main()
    90 {
    91     
    92   typedef SmartGraph Graph;
    93 
    94   typedef Graph::Edge Edge;
    95   typedef Graph::Node Node;
    96   typedef Graph::EdgeIt EdgeIt;
    97   typedef Graph::NodeIt NodeIt;
    98   typedef Graph::EdgeMap<int> LengthMap;
    99 
   100   Graph G;
   101   Node s, t;
   102   LengthMap cap(G);
   103   PetStruct<Graph> ps = addPetersen(G,PET_SIZE);
   104    
   105   for(int i=0;i<PET_SIZE;i++) {
   106     cap[ps.outcir[i]]=4;
   107     cap[ps.incir[i]]=1;
   108     cap[ps.chords[i]]=10;
   109   }
   110   s=ps.outer[0];
   111   t=ps.inner[1];
   112   
   113   Dijkstra<Graph, LengthMap> 
   114 	dijkstra_test(G, cap);
   115   dijkstra_test.run(s);
   116   
   117   check(dijkstra_test.dist(t)==13,"Dijkstra found a wrong path.");
   118 
   119 
   120   DirPath<Graph> p(G);
   121   check(dijkstra_test.getPath(p,t),"getPath() failed to set the path.");
   122   check(p.length()==4,"getPath() found a wrong path.");
   123   
   124 
   125   for(EdgeIt e(G); e!=INVALID; ++e) {
   126     Node u=G.source(e);
   127     Node v=G.target(e);
   128     check( !dijkstra_test.reached(u) ||
   129 	   (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= cap[e]),
   130 	   "dist(target)-dist(source)- edge_length= " 
   131 	   << dijkstra_test.dist(v) - dijkstra_test.dist(u) 
   132 	   - cap[e]);
   133   }
   134 
   135   ///\bug This works only for integer lengths
   136   for(NodeIt v(G); v!=INVALID; ++v){
   137     check(dijkstra_test.reached(v),"Each node should be reached.");
   138     if ( dijkstra_test.pred(v)!=INVALID ) {
   139       Edge e=dijkstra_test.pred(v);
   140       Node u=G.source(e);
   141       check(u==dijkstra_test.predNode(v),"Wrong tree.");
   142       check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == cap[e],
   143 	    "Wrong distance! Difference: " 
   144 	    << std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u) 
   145 			    - cap[e]));
   146     }
   147   }
   148 
   149   
   150   {
   151     NullMap<Node,Edge> myPredMap;
   152     dijkstra(G,cap).predMap(myPredMap).run(s);
   153   }
   154   return 0;
   155 }