test/all_pairs_shortest_path_test.cc
author hegyi
Fri, 06 Jan 2006 16:07:08 +0000
changeset 1884 9c061834b33b
parent 1745 d356e54bdafc
child 1956 a055123339d5
permissions -rw-r--r--
In algorithm window maps can be selected and reated through MapSelector widget.
deba@1711
     1
#include <iostream>
deba@1711
     2
#include <vector>
deba@1711
     3
deba@1711
     4
#include <cmath>
deba@1711
     5
#include <cstdlib>
deba@1711
     6
deba@1711
     7
#include <lemon/smart_graph.h>
deba@1711
     8
#include <lemon/dijkstra.h>
deba@1711
     9
#include <lemon/floyd_warshall.h>
deba@1711
    10
#include <lemon/johnson.h>
deba@1711
    11
deba@1745
    12
#include <lemon/fib_heap.h>
deba@1745
    13
deba@1711
    14
#include <lemon/time_measure.h>
deba@1732
    15
#include "test_tools.h"
deba@1711
    16
deba@1711
    17
using namespace lemon;
deba@1711
    18
using namespace std;
deba@1711
    19
deba@1711
    20
int main(int argc, const char *argv[]) {
deba@1711
    21
  srand(time(0));
deba@1711
    22
  typedef SmartGraph Graph;
deba@1711
    23
  typedef Graph::Node Node;
deba@1711
    24
  typedef Graph::Edge Edge;
deba@1711
    25
  typedef Graph::NodeIt NodeIt;
deba@1711
    26
  typedef Graph::EdgeIt EdgeIt;
deba@1711
    27
deba@1711
    28
  typedef Graph::EdgeMap<double> LengthMap;
deba@1711
    29
  typedef Graph::NodeMap<double> DistMap;
deba@1711
    30
deba@1711
    31
  const int n = argc > 1 ? atoi(argv[1]) : 20;
deba@1732
    32
  const int e = argc > 2 ? atoi(argv[2]) : (int)(n * log((double)n));
deba@1711
    33
  const double m = argc > 3 ? (double)atoi(argv[3]) : 100.0;
deba@1711
    34
deba@1711
    35
  Graph graph;
deba@1711
    36
  LengthMap length(graph);
deba@1711
    37
  vector<Node> nodes;
deba@1711
    38
  
deba@1711
    39
  DistMap shift(graph);
deba@1711
    40
  for (int i = 0; i < n; ++i) {
deba@1711
    41
    Node node = graph.addNode();
deba@1711
    42
    nodes.push_back(node);
deba@1711
    43
    shift[node] = m * (double)rand() / (RAND_MAX + 1.0);
deba@1711
    44
  }
deba@1711
    45
deba@1711
    46
  for (int i = 0; i < e; ++i) {
deba@1711
    47
    int s = (int)(n * (double)rand() / (RAND_MAX + 1.0));
deba@1711
    48
    int t = (int)(n * (double)rand() / (RAND_MAX + 1.0));
deba@1711
    49
    double c = m * (double)rand() / (RAND_MAX + 1.0);
deba@1711
    50
    Edge edge = graph.addEdge(nodes[s], nodes[t]);
deba@1711
    51
    length[edge] = c - shift[nodes[s]] + shift[nodes[t]];
deba@1711
    52
  }
deba@1711
    53
deba@1711
    54
  Johnson<Graph, LengthMap> johnson(graph, length);
deba@1711
    55
  {
deba@1711
    56
    Timer timer;
deba@1711
    57
    johnson.run();
deba@1711
    58
    cout << "Johnson: " << timer << endl;
deba@1711
    59
  }
deba@1711
    60
deba@1745
    61
  typedef FibHeap<Node, double, Graph::NodeMap<int> > DoubleFibHeap;
deba@1745
    62
  Johnson<Graph, LengthMap>::DefStandardHeap<DoubleFibHeap>
deba@1745
    63
    ::Create fibJohnson(graph, length);
deba@1745
    64
  {
deba@1745
    65
    Timer timer;
deba@1745
    66
    fibJohnson.run();
deba@1745
    67
    cout << "Johnson with fibonacci heap: " << timer << endl;
deba@1745
    68
  }
deba@1745
    69
deba@1711
    70
  FloydWarshall<Graph, LengthMap> floyd(graph, length);
deba@1711
    71
  {
deba@1711
    72
    Timer timer;
deba@1711
    73
    floyd.run();
deba@1711
    74
    cout << "FloydWarshall: " << timer << endl;
deba@1711
    75
  }    
deba@1711
    76
deba@1711
    77
  for (NodeIt it(graph); it != INVALID; ++it) {
deba@1711
    78
    for (NodeIt jt(graph); jt != INVALID; ++jt) {
deba@1732
    79
      check(johnson.connected(it, jt) == floyd.connected(it, jt),
deba@1732
    80
	    "Wrong connection in all pairs shortest path");
deba@1745
    81
      check(johnson.connected(it, jt) == fibJohnson.connected(it, jt),
deba@1745
    82
	    "Wrong connection in all pairs shortest path");
deba@1711
    83
      if (johnson.connected(it, jt)) {
deba@1732
    84
	check(johnson.dist(it, jt) == floyd.dist(it, jt),
deba@1732
    85
	      "Wrong distance in all pairs shortest path");
deba@1745
    86
	check(johnson.dist(it, jt) == fibJohnson.dist(it, jt),
deba@1745
    87
	      "Wrong distance in all pairs shortest path");
deba@1711
    88
	if (it != jt) {
deba@1732
    89
 	  check(johnson.dist(it, jt) == 
deba@1732
    90
		johnson.dist(it, johnson.predNode(it, jt)) +
deba@1763
    91
		length[johnson.predEdge(it, jt)],
deba@1732
    92
		"Wrong edge in all pairs shortest path");
deba@1745
    93
 	  check(fibJohnson.dist(it, jt) == 
deba@1745
    94
		fibJohnson.dist(it, fibJohnson.predNode(it, jt)) +
deba@1763
    95
		length[fibJohnson.predEdge(it, jt)],
deba@1745
    96
		"Wrong edge in all pairs shortest path");
deba@1732
    97
	  check(floyd.dist(it, jt) == 
deba@1732
    98
		floyd.dist(it, floyd.predNode(it, jt)) +
deba@1763
    99
		length[floyd.predEdge(it, jt)],
deba@1732
   100
		"Wrong edge in all pairs shortest path");
deba@1711
   101
	}
deba@1711
   102
      }
deba@1711
   103
    }
deba@1711
   104
  }
deba@1711
   105
deba@1711
   106
  return 0;
deba@1711
   107
}