test/unionfind_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Sun, 30 Mar 2008 14:27:59 +0200
changeset 114 c837d1e449dc
parent 103 b68a7e348e00
child 171 02f4d5d9bfd7
permissions -rw-r--r--
Minor fix in concepts/maps.h
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@105
    21
#include <lemon/list_graph.h>
alpar@103
    22
#include <lemon/maps.h>
alpar@103
    23
#include <lemon/unionfind.h>
alpar@103
    24
#include "test_tools.h"
alpar@103
    25
alpar@103
    26
using namespace lemon;
alpar@103
    27
using namespace std;
alpar@103
    28
alpar@105
    29
typedef UnionFindEnum<ListGraph::NodeMap<int> > UFE;
alpar@103
    30
alpar@103
    31
int main() {
alpar@105
    32
  ListGraph g;
alpar@105
    33
  ListGraph::NodeMap<int> base(g);
alpar@103
    34
  UFE U(base);
alpar@105
    35
  vector<ListGraph::Node> n;
alpar@105
    36
  
alpar@105
    37
  for(int i=0;i<20;i++) n.push_back(g.addNode());
alpar@103
    38
alpar@105
    39
  U.insert(n[1]);
alpar@105
    40
  U.insert(n[2]);
alpar@103
    41
alpar@105
    42
  check(U.join(n[1],n[2]) != -1,"Test failed.");
alpar@103
    43
alpar@105
    44
  U.insert(n[3]);
alpar@105
    45
  U.insert(n[4]);
alpar@105
    46
  U.insert(n[5]);
alpar@105
    47
  U.insert(n[6]);
alpar@105
    48
  U.insert(n[7]);
alpar@103
    49
alpar@103
    50
alpar@105
    51
  check(U.join(n[1],n[4]) != -1,"Test failed.");
alpar@105
    52
  check(U.join(n[2],n[4]) == -1,"Test failed.");
alpar@105
    53
  check(U.join(n[3],n[5]) != -1,"Test failed.");
alpar@103
    54
alpar@103
    55
alpar@105
    56
  U.insert(n[8],U.find(n[5]));
alpar@103
    57
alpar@103
    58
alpar@105
    59
  check(U.size(U.find(n[4])) == 3,"Test failed.");
alpar@105
    60
  check(U.size(U.find(n[5])) == 3,"Test failed.");
alpar@105
    61
  check(U.size(U.find(n[6])) == 1,"Test failed.");
alpar@105
    62
  check(U.size(U.find(n[2])) == 3,"Test failed.");
alpar@103
    63
alpar@103
    64
alpar@105
    65
  U.insert(n[9]);
alpar@105
    66
  U.insert(n[10],U.find(n[9]));
alpar@103
    67
alpar@103
    68
alpar@105
    69
  check(U.join(n[8],n[10]) != -1,"Test failed.");
alpar@103
    70
alpar@103
    71
alpar@105
    72
  check(U.size(U.find(n[4])) == 3,"Test failed.");
alpar@105
    73
  check(U.size(U.find(n[9])) == 5,"Test failed.");
alpar@103
    74
alpar@105
    75
  check(U.size(U.find(n[8])) == 5,"Test failed.");
alpar@103
    76
alpar@105
    77
  U.erase(n[9]);
alpar@105
    78
  U.erase(n[1]);
alpar@103
    79
alpar@105
    80
  check(U.size(U.find(n[10])) == 4,"Test failed.");
alpar@105
    81
  check(U.size(U.find(n[2])) == 2,"Test failed.");
alpar@103
    82
alpar@105
    83
  U.erase(n[6]);
alpar@105
    84
  U.split(U.find(n[8]));
alpar@103
    85
alpar@103
    86
alpar@105
    87
  check(U.size(U.find(n[4])) == 2,"Test failed.");
alpar@105
    88
  check(U.size(U.find(n[3])) == 1,"Test failed.");
alpar@105
    89
  check(U.size(U.find(n[2])) == 2,"Test failed.");
alpar@103
    90
alpar@103
    91
alpar@105
    92
  check(U.join(n[3],n[4]) != -1,"Test failed.");
alpar@105
    93
  check(U.join(n[2],n[4]) == -1,"Test failed.");
alpar@103
    94
alpar@103
    95
alpar@105
    96
  check(U.size(U.find(n[4])) == 3,"Test failed.");
alpar@105
    97
  check(U.size(U.find(n[3])) == 3,"Test failed.");
alpar@105
    98
  check(U.size(U.find(n[2])) == 3,"Test failed.");
alpar@103
    99
alpar@105
   100
  U.eraseClass(U.find(n[4]));
alpar@105
   101
  U.eraseClass(U.find(n[7]));
alpar@103
   102
alpar@103
   103
  return 0;
alpar@103
   104
}