test/all_pairs_shortest_path_test.cc
author deba
Wed, 22 Feb 2006 18:26:56 +0000
changeset 1979 c2992fd74dad
parent 1763 49045f2d28d4
child 2242 16523135943d
permissions -rw-r--r--
Mergeing extendermerge branch
Changes:
the extender system
resize for static size graph
UGraphExtender => UndirectGraphExtender
UGraphExtenders with changed meaning
Some UGraphExtender /SubUGraphExtenders, DirectUGraphExtender/
GridGraph => GridUGraph
radix sort to ansi compatible
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
  srand(time(0));
deba@1711
    40
  typedef SmartGraph Graph;
deba@1711
    41
  typedef Graph::Node Node;
deba@1711
    42
  typedef Graph::Edge Edge;
deba@1711
    43
  typedef Graph::NodeIt NodeIt;
deba@1711
    44
  typedef Graph::EdgeIt EdgeIt;
deba@1711
    45
deba@1711
    46
  typedef Graph::EdgeMap<double> LengthMap;
deba@1711
    47
  typedef Graph::NodeMap<double> DistMap;
deba@1711
    48
deba@1711
    49
  const int n = argc > 1 ? atoi(argv[1]) : 20;
deba@1732
    50
  const int e = argc > 2 ? atoi(argv[2]) : (int)(n * log((double)n));
deba@1711
    51
  const double m = argc > 3 ? (double)atoi(argv[3]) : 100.0;
deba@1711
    52
deba@1711
    53
  Graph graph;
deba@1711
    54
  LengthMap length(graph);
deba@1711
    55
  vector<Node> nodes;
deba@1711
    56
  
deba@1711
    57
  DistMap shift(graph);
deba@1711
    58
  for (int i = 0; i < n; ++i) {
deba@1711
    59
    Node node = graph.addNode();
deba@1711
    60
    nodes.push_back(node);
deba@1711
    61
    shift[node] = m * (double)rand() / (RAND_MAX + 1.0);
deba@1711
    62
  }
deba@1711
    63
deba@1711
    64
  for (int i = 0; i < e; ++i) {
deba@1711
    65
    int s = (int)(n * (double)rand() / (RAND_MAX + 1.0));
deba@1711
    66
    int t = (int)(n * (double)rand() / (RAND_MAX + 1.0));
deba@1711
    67
    double c = m * (double)rand() / (RAND_MAX + 1.0);
deba@1711
    68
    Edge edge = graph.addEdge(nodes[s], nodes[t]);
deba@1711
    69
    length[edge] = c - shift[nodes[s]] + shift[nodes[t]];
deba@1711
    70
  }
deba@1711
    71
deba@1711
    72
  Johnson<Graph, LengthMap> johnson(graph, length);
deba@1711
    73
  {
deba@1711
    74
    Timer timer;
deba@1711
    75
    johnson.run();
deba@1711
    76
    cout << "Johnson: " << timer << endl;
deba@1711
    77
  }
deba@1711
    78
deba@1745
    79
  typedef FibHeap<Node, double, Graph::NodeMap<int> > DoubleFibHeap;
deba@1745
    80
  Johnson<Graph, LengthMap>::DefStandardHeap<DoubleFibHeap>
deba@1745
    81
    ::Create fibJohnson(graph, length);
deba@1745
    82
  {
deba@1745
    83
    Timer timer;
deba@1745
    84
    fibJohnson.run();
deba@1745
    85
    cout << "Johnson with fibonacci heap: " << timer << endl;
deba@1745
    86
  }
deba@1745
    87
deba@1711
    88
  FloydWarshall<Graph, LengthMap> floyd(graph, length);
deba@1711
    89
  {
deba@1711
    90
    Timer timer;
deba@1711
    91
    floyd.run();
deba@1711
    92
    cout << "FloydWarshall: " << timer << endl;
deba@1711
    93
  }    
deba@1711
    94
deba@1711
    95
  for (NodeIt it(graph); it != INVALID; ++it) {
deba@1711
    96
    for (NodeIt jt(graph); jt != INVALID; ++jt) {
deba@1732
    97
      check(johnson.connected(it, jt) == floyd.connected(it, jt),
deba@1732
    98
	    "Wrong connection in all pairs shortest path");
deba@1745
    99
      check(johnson.connected(it, jt) == fibJohnson.connected(it, jt),
deba@1745
   100
	    "Wrong connection in all pairs shortest path");
deba@1711
   101
      if (johnson.connected(it, jt)) {
deba@1732
   102
	check(johnson.dist(it, jt) == floyd.dist(it, jt),
deba@1732
   103
	      "Wrong distance in all pairs shortest path");
deba@1745
   104
	check(johnson.dist(it, jt) == fibJohnson.dist(it, jt),
deba@1745
   105
	      "Wrong distance in all pairs shortest path");
deba@1711
   106
	if (it != jt) {
deba@1732
   107
 	  check(johnson.dist(it, jt) == 
deba@1732
   108
		johnson.dist(it, johnson.predNode(it, jt)) +
deba@1763
   109
		length[johnson.predEdge(it, jt)],
deba@1732
   110
		"Wrong edge in all pairs shortest path");
deba@1745
   111
 	  check(fibJohnson.dist(it, jt) == 
deba@1745
   112
		fibJohnson.dist(it, fibJohnson.predNode(it, jt)) +
deba@1763
   113
		length[fibJohnson.predEdge(it, jt)],
deba@1745
   114
		"Wrong edge in all pairs shortest path");
deba@1732
   115
	  check(floyd.dist(it, jt) == 
deba@1732
   116
		floyd.dist(it, floyd.predNode(it, jt)) +
deba@1763
   117
		length[floyd.predEdge(it, jt)],
deba@1732
   118
		"Wrong edge in all pairs shortest path");
deba@1711
   119
	}
deba@1711
   120
      }
deba@1711
   121
    }
deba@1711
   122
  }
deba@1711
   123
deba@1711
   124
  return 0;
deba@1711
   125
}