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