test/unionfind_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 29 Feb 2008 11:01:39 +0000
changeset 103 b68a7e348e00
child 105 e4948ef6a4ca
permissions -rw-r--r--
Port kruskal() and UnionFind from svn -r3468

The class type interface of Kruskal has not been ported yet
alpar@103
     1
/* -*- C++ -*-
alpar@103
     2
 *
alpar@103
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@103
     4
 *
alpar@103
     5
 * Copyright (C) 2003-2008
alpar@103
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@103
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@103
     8
 *
alpar@103
     9
 * Permission to use, modify and distribute this software is granted
alpar@103
    10
 * provided that this copyright notice appears in all copies. For
alpar@103
    11
 * precise terms see the accompanying LICENSE file.
alpar@103
    12
 *
alpar@103
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@103
    14
 * express or implied, and with no claim as to its suitability for any
alpar@103
    15
 * purpose.
alpar@103
    16
 *
alpar@103
    17
 */
alpar@103
    18
alpar@103
    19
#include <iostream>
alpar@103
    20
alpar@103
    21
#include <lemon/maps.h>
alpar@103
    22
#include <lemon/unionfind.h>
alpar@103
    23
#include "test_tools.h"
alpar@103
    24
alpar@103
    25
using namespace lemon;
alpar@103
    26
using namespace std;
alpar@103
    27
alpar@103
    28
typedef UnionFindEnum<StdMap<int, int> > UFE;
alpar@103
    29
alpar@103
    30
void print(UFE const &ufe) {
alpar@103
    31
  cout << "Print the classes of the structure:" << endl;
alpar@103
    32
  int i = 1;
alpar@103
    33
  for (UFE::ClassIt cit(ufe); cit != INVALID; ++cit) {
alpar@103
    34
    cout << "  " << i << " (" << cit << "):" << flush;
alpar@103
    35
    for (UFE::ItemIt iit(ufe, cit); iit != INVALID; ++iit) {
alpar@103
    36
      cout << " " << iit << flush;
alpar@103
    37
    }
alpar@103
    38
    cout << endl;
alpar@103
    39
    i++;
alpar@103
    40
  }
alpar@103
    41
  cout << "done" << endl;
alpar@103
    42
}
alpar@103
    43
alpar@103
    44
alpar@103
    45
int main() {
alpar@103
    46
  StdMap<int, int> base;
alpar@103
    47
  UFE U(base);
alpar@103
    48
alpar@103
    49
  U.insert(1);
alpar@103
    50
  U.insert(2);
alpar@103
    51
alpar@103
    52
  check(U.join(1,2) != -1,"Test failed.");
alpar@103
    53
alpar@103
    54
  U.insert(3);
alpar@103
    55
  U.insert(4);
alpar@103
    56
  U.insert(5);
alpar@103
    57
  U.insert(6);
alpar@103
    58
  U.insert(7);
alpar@103
    59
alpar@103
    60
alpar@103
    61
  check(U.join(1,4) != -1,"Test failed.");
alpar@103
    62
  check(U.join(2,4) == -1,"Test failed.");
alpar@103
    63
  check(U.join(3,5) != -1,"Test failed.");
alpar@103
    64
alpar@103
    65
alpar@103
    66
  U.insert(8,U.find(5));
alpar@103
    67
alpar@103
    68
alpar@103
    69
  check(U.size(U.find(4)) == 3,"Test failed.");
alpar@103
    70
  check(U.size(U.find(5)) == 3,"Test failed.");
alpar@103
    71
  check(U.size(U.find(6)) == 1,"Test failed.");
alpar@103
    72
  check(U.size(U.find(2)) == 3,"Test failed.");
alpar@103
    73
alpar@103
    74
alpar@103
    75
  U.insert(9);
alpar@103
    76
  U.insert(10,U.find(9));
alpar@103
    77
alpar@103
    78
alpar@103
    79
  check(U.join(8,10) != -1,"Test failed.");
alpar@103
    80
alpar@103
    81
alpar@103
    82
  check(U.size(U.find(4)) == 3,"Test failed.");
alpar@103
    83
  check(U.size(U.find(9)) == 5,"Test failed.");
alpar@103
    84
alpar@103
    85
  check(U.size(U.find(8)) == 5,"Test failed.");
alpar@103
    86
alpar@103
    87
  U.erase(9);
alpar@103
    88
  U.erase(1);
alpar@103
    89
alpar@103
    90
  check(U.size(U.find(10)) == 4,"Test failed.");
alpar@103
    91
  check(U.size(U.find(2)) == 2,"Test failed.");
alpar@103
    92
alpar@103
    93
  U.erase(6);
alpar@103
    94
  U.split(U.find(8));
alpar@103
    95
alpar@103
    96
alpar@103
    97
  check(U.size(U.find(4)) == 2,"Test failed.");
alpar@103
    98
  check(U.size(U.find(3)) == 1,"Test failed.");
alpar@103
    99
  check(U.size(U.find(2)) == 2,"Test failed.");
alpar@103
   100
alpar@103
   101
alpar@103
   102
  check(U.join(3,4) != -1,"Test failed.");
alpar@103
   103
  check(U.join(2,4) == -1,"Test failed.");
alpar@103
   104
alpar@103
   105
alpar@103
   106
  check(U.size(U.find(4)) == 3,"Test failed.");
alpar@103
   107
  check(U.size(U.find(3)) == 3,"Test failed.");
alpar@103
   108
  check(U.size(U.find(2)) == 3,"Test failed.");
alpar@103
   109
alpar@103
   110
  U.eraseClass(U.find(4));
alpar@103
   111
  U.eraseClass(U.find(7));
alpar@103
   112
alpar@103
   113
  return 0;
alpar@103
   114
}