test/unionfind_test.cc
author kpeter
Thu, 28 Feb 2008 02:54:27 +0000
changeset 2581 054566ac0934
parent 2505 1bb471764ab8
permissions -rw-r--r--
Query improvements in the min cost flow algorithms.

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