test/dijkstra_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 18 Jul 2008 16:47:27 +0100
changeset 226 4c9d85f5dc93
parent 210 81cfc04531e8
child 228 b6732e0d38c5
permissions -rw-r--r--
Merge
     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/dijkstra.h>
    23 #include <lemon/path.h>
    24 
    25 #include "graph_test.h"
    26 #include "test_tools.h"
    27 
    28 using namespace lemon;
    29 
    30 void checkDijkstraCompile()
    31 {
    32   typedef int VType;
    33   typedef concepts::Digraph Digraph;
    34   typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
    35   typedef Dijkstra<Digraph, LengthMap> DType;
    36 
    37   Digraph G;
    38   Digraph::Node n;
    39   Digraph::Arc e;
    40   VType l;
    41   bool b;
    42   DType::DistMap d(G);
    43   DType::PredMap p(G);
    44   //  DType::PredNodeMap pn(G);
    45   LengthMap length;
    46 
    47   DType dijkstra_test(G,length);
    48 
    49   dijkstra_test.run(n);
    50 
    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);
    58 
    59   Path<Digraph> pp = dijkstra_test.path(n);
    60 }
    61 
    62 void checkDijkstraFunctionCompile()
    63 {
    64   typedef int VType;
    65   typedef concepts::Digraph Digraph;
    66   typedef Digraph::Arc Arc;
    67   typedef Digraph::Node Node;
    68   typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
    69 
    70   Digraph g;
    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>())
    76     .run(Node());
    77 }
    78 
    79 template <class Digraph>
    80 void checkDijkstra() {
    81   TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
    82   typedef typename Digraph::template ArcMap<int> LengthMap;
    83 
    84   Digraph G;
    85   Node s, t;
    86   LengthMap length(G);
    87   PetStruct<Digraph> ps = addPetersen(G, 5);
    88 
    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;
    93   }
    94   s=ps.outer[0];
    95   t=ps.inner[1];
    96 
    97   Dijkstra<Digraph, LengthMap>
    98         dijkstra_test(G, length);
    99   dijkstra_test.run(s);
   100 
   101   check(dijkstra_test.dist(t)==13,"Dijkstra found a wrong path.");
   102 
   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.");
   108 
   109   for(ArcIt e(G); e!=INVALID; ++e) {
   110     Node u=G.source(e);
   111     Node v=G.target(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]);
   116   }
   117 
   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);
   122       Node u=G.source(e);
   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]));
   127     }
   128   }
   129 
   130   {
   131     NullMap<Node,Arc> myPredMap;
   132     dijkstra(G,length).predMap(myPredMap).run(s);
   133   }
   134 }
   135 
   136 int main() {
   137   checkDijkstra<ListDigraph>();
   138   checkDijkstra<SmartDigraph>();
   139   return 0;
   140 }