test/heap_test.h
author deba
Wed, 01 Mar 2006 10:25:30 +0000
changeset 1991 d7442141d9ef
parent 1763 49045f2d28d4
child 2229 4dbb6dd2dd4b
permissions -rw-r--r--
The graph adadptors can be alteration observed.
In most cases it uses the adapted graph alteration notifiers.
Only special case is now the UndirGraphAdaptor, where
we have to proxy the signals from the graph.

The SubBidirGraphAdaptor is removed, because it doest not
gives more feature than the EdgeSubGraphAdaptor<UndirGraphAdaptor<Graph>>.

The ResGraphAdaptor is based on this composition.
alpar@1956
     1
/* -*- C++ -*-
alpar@1956
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1956
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@1956
     8
 *
alpar@1956
     9
 * Permission to use, modify and distribute this software is granted
alpar@1956
    10
 * provided that this copyright notice appears in all copies. For
alpar@1956
    11
 * precise terms see the accompanying LICENSE file.
alpar@1956
    12
 *
alpar@1956
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@1956
    14
 * express or implied, and with no claim as to its suitability for any
alpar@1956
    15
 * purpose.
alpar@1956
    16
 *
alpar@1956
    17
 */
deba@1206
    18
deba@1187
    19
#include <vector>
deba@1187
    20
#include <algorithm>
deba@1187
    21
deba@1187
    22
#include <lemon/dijkstra.h>
deba@1187
    23
deba@1187
    24
class IntIntMap : public std::vector<int> {
deba@1187
    25
public:
deba@1187
    26
  typedef std::vector<int> Parent;
deba@1187
    27
deba@1187
    28
  IntIntMap() : Parent() {}
deba@1187
    29
  IntIntMap(int n) : Parent(n) {}
deba@1187
    30
  IntIntMap(int n, int v) : Parent(n, v) {}
deba@1187
    31
deba@1187
    32
  void set(int key, int value) {
deba@1187
    33
    Parent::operator[](key) = value;
deba@1187
    34
  }
deba@1187
    35
};
deba@1187
    36
deba@1187
    37
deba@1187
    38
template <typename _Heap>
deba@1187
    39
void heapSortTest(int n) {
deba@1187
    40
  typedef _Heap Heap;
deba@1187
    41
  IntIntMap map(n, -1);
deba@1187
    42
deba@1187
    43
  Heap heap(map);
deba@1187
    44
  
deba@1187
    45
  std::vector<int> v(n);
deba@1187
    46
deba@1187
    47
  for (int i = 0; i < n; ++i) {
deba@1187
    48
    v[i] = rand() % 1000;
deba@1187
    49
    heap.push(i, v[i]);
deba@1187
    50
  }
deba@1187
    51
  std::sort(v.begin(), v.end());
deba@1187
    52
  for (int i = 0; i < n; ++i) {
deba@1187
    53
    check(v[i] == heap.prio() ,"Wrong order in heap sort.");
deba@1187
    54
    heap.pop();
deba@1187
    55
  }
deba@1187
    56
}
deba@1187
    57
deba@1206
    58
template <typename _Heap>
deba@1206
    59
void heapIncreaseTest(int n) {
deba@1206
    60
  typedef _Heap Heap;
deba@1206
    61
  IntIntMap map(n, -1);
deba@1206
    62
deba@1206
    63
  Heap heap(map);
deba@1206
    64
  
deba@1206
    65
  std::vector<int> v(n);
deba@1206
    66
deba@1206
    67
  for (int i = 0; i < n; ++i) {
deba@1206
    68
    v[i] = rand() % 1000;
deba@1206
    69
    heap.push(i, v[i]);
deba@1206
    70
  }
deba@1206
    71
  for (int i = 0; i < n; ++i) {
deba@1206
    72
    v[i] += rand() % 1000;
deba@1206
    73
    heap.increase(i, v[i]);
deba@1206
    74
  }
deba@1206
    75
  std::sort(v.begin(), v.end());
deba@1206
    76
  for (int i = 0; i < n; ++i) {
deba@1206
    77
    check(v[i] == heap.prio() ,"Wrong order in heap increase test.");
deba@1206
    78
    heap.pop();
deba@1206
    79
  }
deba@1206
    80
}
deba@1206
    81
deba@1206
    82
deba@1206
    83
deba@1187
    84
template <typename _Graph, typename _LengthMap, typename _Heap>
deba@1187
    85
void dijkstraHeapTest(_Graph& graph, _LengthMap& length,
deba@1187
    86
		      typename _Graph::Node& start) {
deba@1187
    87
deba@1187
    88
  typedef _Heap Heap;
deba@1187
    89
  typedef _Graph Graph;
deba@1187
    90
  typedef _LengthMap LengthMap;
deba@1187
    91
deba@1187
    92
  typedef typename Graph::Node Node;
deba@1187
    93
  typedef typename Graph::Edge Edge;
deba@1187
    94
  typedef typename Graph::NodeIt NodeIt;
deba@1187
    95
  typedef typename Graph::EdgeIt EdgeIt;
deba@1187
    96
deba@1745
    97
  typename Dijkstra<Graph, LengthMap>::template DefStandardHeap<Heap>::
deba@1728
    98
    Create dijkstra(graph, length);
deba@1187
    99
deba@1187
   100
  dijkstra.run(start);
deba@1187
   101
deba@1187
   102
  for(EdgeIt e(graph); e!=INVALID; ++e) {
deba@1187
   103
    Node u=graph.source(e); 
deba@1187
   104
    Node v=graph.target(e);
deba@1187
   105
    if (dijkstra.reached(u)) {
deba@1187
   106
      check( dijkstra.dist(v) - dijkstra.dist(u) <= length[e],
deba@1728
   107
      	     "Error in a shortest path tree edge!");
deba@1187
   108
    }
deba@1187
   109
  }
deba@1187
   110
deba@1187
   111
  for(NodeIt v(graph); v!=INVALID; ++v) {
deba@1763
   112
    if ( dijkstra.reached(v) && dijkstra.predEdge(v) != INVALID ) {
deba@1763
   113
      Edge e=dijkstra.predEdge(v);
deba@1187
   114
      Node u=graph.source(e);
deba@1187
   115
      check( dijkstra.dist(v) - dijkstra .dist(u) == length[e],
deba@1187
   116
	     "Error in a shortest path tree edge!");
deba@1187
   117
    }
deba@1187
   118
  }
deba@1187
   119
deba@1187
   120
}