test/all_pairs_shortest_path_test.cc
author deba
Mon, 30 Oct 2006 12:07:52 +0000
changeset 2269 fb1c634fff29
parent 2242 16523135943d
child 2335 27aa03cd3121
permissions -rw-r--r--
Bug fix for removing heap Item from template parameter list
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
 */
alpar@1956
    18
deba@1711
    19
#include <iostream>
deba@1711
    20
#include <vector>
deba@1711
    21
deba@1711
    22
#include <cmath>
deba@1711
    23
#include <cstdlib>
deba@1711
    24
deba@1711
    25
#include <lemon/smart_graph.h>
deba@1711
    26
#include <lemon/dijkstra.h>
deba@1711
    27
#include <lemon/floyd_warshall.h>
deba@1711
    28
#include <lemon/johnson.h>
deba@1711
    29
deba@1745
    30
#include <lemon/fib_heap.h>
deba@1745
    31
deba@1711
    32
#include <lemon/time_measure.h>
deba@1732
    33
#include "test_tools.h"
deba@1711
    34
deba@1711
    35
using namespace lemon;
deba@1711
    36
using namespace std;
deba@1711
    37
deba@1711
    38
int main(int argc, const char *argv[]) {
deba@1711
    39
  typedef SmartGraph Graph;
deba@1711
    40
  typedef Graph::Node Node;
deba@1711
    41
  typedef Graph::Edge Edge;
deba@1711
    42
  typedef Graph::NodeIt NodeIt;
deba@1711
    43
  typedef Graph::EdgeIt EdgeIt;
deba@1711
    44
deba@2242
    45
  typedef Graph::EdgeMap<int> LengthMap;
deba@2242
    46
  typedef Graph::NodeMap<int> DistMap;
deba@1711
    47
deba@1711
    48
  const int n = argc > 1 ? atoi(argv[1]) : 20;
deba@1732
    49
  const int e = argc > 2 ? atoi(argv[2]) : (int)(n * log((double)n));
deba@2242
    50
  const int m = argc > 3 ? atoi(argv[3]) : 100;
deba@1711
    51
deba@1711
    52
  Graph graph;
deba@1711
    53
  LengthMap length(graph);
deba@1711
    54
  vector<Node> nodes;
deba@1711
    55
  
deba@1711
    56
  DistMap shift(graph);
deba@1711
    57
  for (int i = 0; i < n; ++i) {
deba@1711
    58
    Node node = graph.addNode();
deba@1711
    59
    nodes.push_back(node);
deba@2242
    60
    shift[node] = rnd[m];
deba@1711
    61
  }
deba@1711
    62
deba@1711
    63
  for (int i = 0; i < e; ++i) {
deba@2242
    64
    int s = rnd[n];
deba@2242
    65
    int t = rnd[n];
deba@1711
    66
    Edge edge = graph.addEdge(nodes[s], nodes[t]);
deba@2242
    67
    length[edge] = rnd[m] - shift[nodes[s]] + shift[nodes[t]];
deba@1711
    68
  }
deba@1711
    69
deba@1711
    70
  Johnson<Graph, LengthMap> johnson(graph, length);
deba@1711
    71
  {
deba@1711
    72
    Timer timer;
deba@1711
    73
    johnson.run();
deba@1711
    74
    cout << "Johnson: " << timer << endl;
deba@1711
    75
  }
deba@1711
    76
deba@2269
    77
  typedef FibHeap<int, Graph::NodeMap<int> > IntFibHeap;
deba@2242
    78
  Johnson<Graph, LengthMap>::DefStandardHeap<IntFibHeap>
deba@1745
    79
    ::Create fibJohnson(graph, length);
deba@1745
    80
  {
deba@1745
    81
    Timer timer;
deba@1745
    82
    fibJohnson.run();
deba@1745
    83
    cout << "Johnson with fibonacci heap: " << timer << endl;
deba@1745
    84
  }
deba@1745
    85
deba@1711
    86
  FloydWarshall<Graph, LengthMap> floyd(graph, length);
deba@1711
    87
  {
deba@1711
    88
    Timer timer;
deba@1711
    89
    floyd.run();
deba@1711
    90
    cout << "FloydWarshall: " << timer << endl;
deba@1711
    91
  }    
deba@1711
    92
deba@1711
    93
  for (NodeIt it(graph); it != INVALID; ++it) {
deba@1711
    94
    for (NodeIt jt(graph); jt != INVALID; ++jt) {
deba@1732
    95
      check(johnson.connected(it, jt) == floyd.connected(it, jt),
deba@1732
    96
	    "Wrong connection in all pairs shortest path");
deba@1745
    97
      check(johnson.connected(it, jt) == fibJohnson.connected(it, jt),
deba@1745
    98
	    "Wrong connection in all pairs shortest path");
deba@1711
    99
      if (johnson.connected(it, jt)) {
deba@1732
   100
	check(johnson.dist(it, jt) == floyd.dist(it, jt),
deba@1732
   101
	      "Wrong distance in all pairs shortest path");
deba@1745
   102
	check(johnson.dist(it, jt) == fibJohnson.dist(it, jt),
deba@1745
   103
	      "Wrong distance in all pairs shortest path");
deba@1711
   104
	if (it != jt) {
deba@1732
   105
 	  check(johnson.dist(it, jt) == 
deba@1732
   106
		johnson.dist(it, johnson.predNode(it, jt)) +
deba@1763
   107
		length[johnson.predEdge(it, jt)],
deba@1732
   108
		"Wrong edge in all pairs shortest path");
deba@1745
   109
 	  check(fibJohnson.dist(it, jt) == 
deba@1745
   110
		fibJohnson.dist(it, fibJohnson.predNode(it, jt)) +
deba@1763
   111
		length[fibJohnson.predEdge(it, jt)],
deba@1745
   112
		"Wrong edge in all pairs shortest path");
deba@1732
   113
	  check(floyd.dist(it, jt) == 
deba@1732
   114
		floyd.dist(it, floyd.predNode(it, jt)) +
deba@1763
   115
		length[floyd.predEdge(it, jt)],
deba@1732
   116
		"Wrong edge in all pairs shortest path");
deba@1711
   117
	}
deba@1711
   118
      }
deba@1711
   119
    }
deba@1711
   120
  }
deba@1711
   121
deba@1711
   122
  return 0;
deba@1711
   123
}