test/graph_utils_test.cc
author deba
Fri, 14 Apr 2006 18:07:33 +0000
changeset 2051 08652c1763f6
parent 1772 dd1e0c442fe0
child 2236 9f329faa4aee
permissions -rw-r--r--
MaxWeightedBipartiteMatching
MinCostMaxBipartiteMatching

Both algorithms are based on successive shortest
path algorithm with dijkstra shortest path
finding
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
 */
klao@946
    18
klao@946
    19
#include <iostream>
klao@946
    20
#include <vector>
klao@946
    21
klao@977
    22
#include <lemon/graph_utils.h>
klao@977
    23
klao@946
    24
#include <lemon/list_graph.h>
klao@946
    25
#include <lemon/smart_graph.h>
klao@946
    26
#include <lemon/full_graph.h>
klao@946
    27
klao@946
    28
#include "test_tools.h"
klao@946
    29
#include "graph_utils_test.h"
klao@946
    30
klao@946
    31
klao@946
    32
using namespace lemon;
klao@946
    33
alpar@1459
    34
template<class Graph>
alpar@1459
    35
void checkSnapDeg() 
alpar@1459
    36
{
alpar@1459
    37
  Graph g;
alpar@1459
    38
  typename Graph::Node n1=g.addNode();
alpar@1459
    39
  typename Graph::Node n2=g.addNode();
alpar@1459
    40
   
alpar@1459
    41
  InDegMap<Graph> ind(g);
alpar@1459
    42
 
alpar@1459
    43
  g.addEdge(n1,n2);
alpar@1459
    44
  
alpar@1772
    45
  typename Graph::Snapshot snap(g);
alpar@1459
    46
  
alpar@1459
    47
  OutDegMap<Graph> outd(g);
alpar@1459
    48
  
alpar@1459
    49
  check(ind[n1]==0 && ind[n2]==1, "Wrong InDegMap value.");
alpar@1459
    50
  check(outd[n1]==1 && outd[n2]==0, "Wrong OutDegMap value.");
alpar@1459
    51
alpar@1459
    52
  g.addEdge(n1,n2);
alpar@1459
    53
  g.addEdge(n2,n1);
alpar@1459
    54
 
alpar@1459
    55
  check(ind[n1]==1 && ind[n2]==2, "Wrong InDegMap value.");
alpar@1459
    56
  check(outd[n1]==2 && outd[n2]==1, "Wrong OutDegMap value.");
alpar@1459
    57
alpar@1459
    58
  snap.restore();
alpar@1459
    59
alpar@1459
    60
  check(ind[n1]==0 && ind[n2]==1, "Wrong InDegMap value.");
alpar@1459
    61
  check(outd[n1]==1 && outd[n2]==0, "Wrong OutDegMap value.");
alpar@1459
    62
  
alpar@1459
    63
}
klao@946
    64
klao@946
    65
int main() {
klao@946
    66
  ///\file
klao@946
    67
  { // checking list graph
klao@946
    68
    checkGraphCounters<ListGraph>();
deba@1568
    69
    checkFindEdge<ListGraph>();
klao@946
    70
  }
klao@946
    71
  { // checking smart graph
klao@946
    72
    checkGraphCounters<SmartGraph>();
deba@1568
    73
    checkFindEdge<SmartGraph>();
klao@946
    74
  }
klao@977
    75
  {
klao@977
    76
    int num = 5;
klao@977
    77
    FullGraph fg(num);
klao@977
    78
    check(countNodes(fg) == num, "FullGraph: wrong node number.");
deba@1568
    79
    check(countEdges(fg) == num*num, "FullGraph: wrong edge number.");
deba@1568
    80
    for (FullGraph::NodeIt src(fg); src != INVALID; ++src) {
deba@1568
    81
      for (FullGraph::NodeIt trg(fg); trg != INVALID; ++trg) {
deba@1568
    82
	ConEdgeIt<FullGraph> con(fg, src, trg);
deba@1568
    83
	check(con != INVALID, "There is no connecting edge.");
deba@1568
    84
	check(fg.source(con) == src, "Wrong source.");
deba@1568
    85
	check(fg.target(con) == trg, "Wrong target.");
deba@1568
    86
	check(++con == INVALID, "There is more connecting edge.");
deba@1568
    87
      }
deba@1568
    88
    }
klao@977
    89
  }
klao@946
    90
alpar@1772
    91
  //check In/OutDegMap (and Snapshot feature)
alpar@1459
    92
alpar@1459
    93
  checkSnapDeg<ListGraph>();
alpar@1459
    94
  checkSnapDeg<SmartGraph>();
alpar@1459
    95
  
deba@1728
    96
  {
deba@1728
    97
    const int nodeNum = 10;
deba@1728
    98
    const int edgeNum = 100;
deba@1728
    99
    ListGraph graph;
deba@1728
   100
    InDegMap<ListGraph> inDeg(graph);
deba@1729
   101
    OutDegMap<ListGraph> outDeg(graph);
deba@1728
   102
    std::vector<ListGraph::Node> nodes(nodeNum);
deba@1728
   103
    for (int i = 0; i < nodeNum; ++i) {
deba@1728
   104
      nodes[i] = graph.addNode();
deba@1728
   105
    }
deba@1728
   106
    std::vector<ListGraph::Edge> edges(edgeNum);
deba@1728
   107
    for (int i = 0; i < edgeNum; ++i) {
deba@1728
   108
      edges[i] = 
deba@1728
   109
	graph.addEdge(nodes[urandom(nodeNum)], nodes[urandom(nodeNum)]);
deba@1728
   110
    }
deba@1728
   111
    for (int i = 0; i < nodeNum; ++i) {
deba@1728
   112
      check(inDeg[nodes[i]] == countInEdges(graph, nodes[i]), 
deba@1728
   113
	    "Wrong in degree map");
deba@1728
   114
    }
deba@1728
   115
    for (int i = 0; i < nodeNum; ++i) {
deba@1729
   116
      check(outDeg[nodes[i]] == countOutEdges(graph, nodes[i]), 
deba@1728
   117
	    "Wrong in degree map");
deba@1728
   118
    }
deba@1728
   119
  }
alpar@1459
   120
alpar@1459
   121
  ///Everything is OK
klao@946
   122
  std::cout << __FILE__ ": All tests passed.\n";
klao@946
   123
klao@946
   124
  return 0;
klao@946
   125
}