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