test/all_pairs_shortest_path_test.cc
author deba
Tue, 22 Jul 2008 11:29:57 +0000
changeset 2617 5222a3c470ed
parent 2553 bfced05fa852
permissions -rw-r--r--
Backport bug fix for Id handling from hg changeset [e67acd83a9ca]
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@2553
     5
 * Copyright (C) 2003-2008
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
alpar@2569
    22
#include <lemon/math.h>
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@2335
    32
#include <lemon/path.h>
deba@2335
    33
deba@1711
    34
#include <lemon/time_measure.h>
deba@1732
    35
#include "test_tools.h"
deba@1711
    36
deba@1711
    37
using namespace lemon;
deba@1711
    38
using namespace std;
deba@1711
    39
deba@1711
    40
int main(int argc, const char *argv[]) {
deba@1711
    41
  typedef SmartGraph Graph;
deba@1711
    42
  typedef Graph::Node Node;
deba@1711
    43
  typedef Graph::Edge Edge;
deba@1711
    44
  typedef Graph::NodeIt NodeIt;
deba@1711
    45
  typedef Graph::EdgeIt EdgeIt;
deba@1711
    46
deba@2242
    47
  typedef Graph::EdgeMap<int> LengthMap;
deba@2242
    48
  typedef Graph::NodeMap<int> DistMap;
deba@1711
    49
deba@1711
    50
  const int n = argc > 1 ? atoi(argv[1]) : 20;
deba@2386
    51
  const int e = argc > 2 ? atoi(argv[2]) : int(n * log(double(n)));
deba@2242
    52
  const int m = argc > 3 ? atoi(argv[3]) : 100;
deba@1711
    53
deba@1711
    54
  Graph graph;
deba@1711
    55
  LengthMap length(graph);
deba@1711
    56
  vector<Node> nodes;
deba@1711
    57
  
deba@1711
    58
  DistMap shift(graph);
deba@1711
    59
  for (int i = 0; i < n; ++i) {
deba@1711
    60
    Node node = graph.addNode();
deba@1711
    61
    nodes.push_back(node);
deba@2242
    62
    shift[node] = rnd[m];
deba@1711
    63
  }
deba@1711
    64
deba@1711
    65
  for (int i = 0; i < e; ++i) {
deba@2242
    66
    int s = rnd[n];
deba@2242
    67
    int t = rnd[n];
deba@1711
    68
    Edge edge = graph.addEdge(nodes[s], nodes[t]);
deba@2242
    69
    length[edge] = rnd[m] - 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@2269
    79
  typedef FibHeap<int, Graph::NodeMap<int> > IntFibHeap;
deba@2242
    80
  Johnson<Graph, LengthMap>::DefStandardHeap<IntFibHeap>
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@2335
    95
  bool checked_path = false;
deba@2335
    96
deba@1711
    97
  for (NodeIt it(graph); it != INVALID; ++it) {
deba@1711
    98
    for (NodeIt jt(graph); jt != INVALID; ++jt) {
deba@1732
    99
      check(johnson.connected(it, jt) == floyd.connected(it, jt),
deba@1732
   100
	    "Wrong connection in all pairs shortest path");
deba@1745
   101
      check(johnson.connected(it, jt) == fibJohnson.connected(it, jt),
deba@1745
   102
	    "Wrong connection in all pairs shortest path");
deba@1711
   103
      if (johnson.connected(it, jt)) {
deba@2335
   104
        if (it != jt && !checked_path) {
deba@2335
   105
          {
deba@2335
   106
            Path<Graph> path = johnson.path(it, jt);
deba@2335
   107
            check(checkPath(graph, path), "Wrong path.");
deba@2335
   108
            check(pathSource(graph, path) == it, "Wrong path.");
deba@2335
   109
            check(pathTarget(graph, path) == jt, "Wrong path.");
deba@2335
   110
          }
deba@2335
   111
          {
deba@2335
   112
            Path<Graph> path = floyd.path(it, jt);
deba@2335
   113
            check(checkPath(graph, path), "Wrong path.");
deba@2335
   114
            check(pathSource(graph, path) == it, "Wrong path.");
deba@2335
   115
            check(pathTarget(graph, path) == jt, "Wrong path.");
deba@2335
   116
          }
deba@2335
   117
          checked_path = true;
deba@2335
   118
          std::cout << "Path checked" << std::endl;
deba@2335
   119
        }
deba@1732
   120
	check(johnson.dist(it, jt) == floyd.dist(it, jt),
deba@1732
   121
	      "Wrong distance in all pairs shortest path");
deba@1745
   122
	check(johnson.dist(it, jt) == fibJohnson.dist(it, jt),
deba@1745
   123
	      "Wrong distance in all pairs shortest path");
deba@1711
   124
	if (it != jt) {
deba@1732
   125
 	  check(johnson.dist(it, jt) == 
deba@1732
   126
		johnson.dist(it, johnson.predNode(it, jt)) +
deba@1763
   127
		length[johnson.predEdge(it, jt)],
deba@1732
   128
		"Wrong edge in all pairs shortest path");
deba@1745
   129
 	  check(fibJohnson.dist(it, jt) == 
deba@1745
   130
		fibJohnson.dist(it, fibJohnson.predNode(it, jt)) +
deba@1763
   131
		length[fibJohnson.predEdge(it, jt)],
deba@1745
   132
		"Wrong edge in all pairs shortest path");
deba@1732
   133
	  check(floyd.dist(it, jt) == 
deba@1732
   134
		floyd.dist(it, floyd.predNode(it, jt)) +
deba@1763
   135
		length[floyd.predEdge(it, jt)],
deba@1732
   136
		"Wrong edge in all pairs shortest path");
deba@1711
   137
	}
deba@1711
   138
      }
deba@1711
   139
    }
deba@1711
   140
  }
deba@1711
   141
deba@1711
   142
  return 0;
deba@1711
   143
}