test/unionfind_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Fri, 03 Apr 2009 18:59:15 +0200
changeset 608 6ac5d9ae1d3d
parent 209 765619b7cbb2
permissions -rw-r--r--
Support real types + numerical stability fix in NS (#254)

- Real types are supported by appropriate inicialization.
- A feature of the XTI spanning tree structure is removed to ensure
numerical stability (could cause problems using integer types).
The node potentials are updated always on the lower subtree,
in order to prevent overflow problems.
The former method isn't notably faster during to our tests.
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     5  * Copyright (C) 2003-2009
     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 <lemon/list_graph.h>
    20 #include <lemon/maps.h>
    21 #include <lemon/unionfind.h>
    22 #include "test_tools.h"
    23 
    24 using namespace lemon;
    25 using namespace std;
    26 
    27 typedef UnionFindEnum<ListGraph::NodeMap<int> > UFE;
    28 
    29 int main() {
    30   ListGraph g;
    31   ListGraph::NodeMap<int> base(g);
    32   UFE U(base);
    33   vector<ListGraph::Node> n;
    34 
    35   for(int i=0;i<20;i++) n.push_back(g.addNode());
    36 
    37   U.insert(n[1]);
    38   U.insert(n[2]);
    39 
    40   check(U.join(n[1],n[2]) != -1, "Something is wrong with UnionFindEnum");
    41 
    42   U.insert(n[3]);
    43   U.insert(n[4]);
    44   U.insert(n[5]);
    45   U.insert(n[6]);
    46   U.insert(n[7]);
    47 
    48 
    49   check(U.join(n[1],n[4]) != -1, "Something is wrong with UnionFindEnum");
    50   check(U.join(n[2],n[4]) == -1, "Something is wrong with UnionFindEnum");
    51   check(U.join(n[3],n[5]) != -1, "Something is wrong with UnionFindEnum");
    52 
    53 
    54   U.insert(n[8],U.find(n[5]));
    55 
    56 
    57   check(U.size(U.find(n[4])) == 3, "Something is wrong with UnionFindEnum");
    58   check(U.size(U.find(n[5])) == 3, "Something is wrong with UnionFindEnum");
    59   check(U.size(U.find(n[6])) == 1, "Something is wrong with UnionFindEnum");
    60   check(U.size(U.find(n[2])) == 3, "Something is wrong with UnionFindEnum");
    61 
    62 
    63   U.insert(n[9]);
    64   U.insert(n[10],U.find(n[9]));
    65 
    66 
    67   check(U.join(n[8],n[10])  != -1, "Something is wrong with UnionFindEnum");
    68 
    69 
    70   check(U.size(U.find(n[4])) == 3, "Something is wrong with UnionFindEnum");
    71   check(U.size(U.find(n[9])) == 5, "Something is wrong with UnionFindEnum");
    72 
    73   check(U.size(U.find(n[8])) == 5, "Something is wrong with UnionFindEnum");
    74 
    75   U.erase(n[9]);
    76   U.erase(n[1]);
    77 
    78   check(U.size(U.find(n[10])) == 4, "Something is wrong with UnionFindEnum");
    79   check(U.size(U.find(n[2]))  == 2, "Something is wrong with UnionFindEnum");
    80 
    81   U.erase(n[6]);
    82   U.split(U.find(n[8]));
    83 
    84 
    85   check(U.size(U.find(n[4])) == 2, "Something is wrong with UnionFindEnum");
    86   check(U.size(U.find(n[3])) == 1, "Something is wrong with UnionFindEnum");
    87   check(U.size(U.find(n[2])) == 2, "Something is wrong with UnionFindEnum");
    88 
    89 
    90   check(U.join(n[3],n[4]) != -1, "Something is wrong with UnionFindEnum");
    91   check(U.join(n[2],n[4]) == -1, "Something is wrong with UnionFindEnum");
    92 
    93 
    94   check(U.size(U.find(n[4])) == 3, "Something is wrong with UnionFindEnum");
    95   check(U.size(U.find(n[3])) == 3, "Something is wrong with UnionFindEnum");
    96   check(U.size(U.find(n[2])) == 3, "Something is wrong with UnionFindEnum");
    97 
    98   U.eraseClass(U.find(n[4]));
    99   U.eraseClass(U.find(n[7]));
   100 
   101   return 0;
   102 }