test/unionfind_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Tue, 24 Mar 2009 00:18:25 +0100
changeset 651 8c3112a66878
parent 209 765619b7cbb2
permissions -rw-r--r--
Use XTI implementation instead of ATI in NetworkSimplex (#234)

XTI (eXtended Threaded Index) is an imporved version of the widely
known ATI (Augmented Threaded Index) method for storing and updating
the spanning tree structure in Network Simplex algorithms.

In the ATI data structure three indices are stored for each node:
predecessor, thread and depth. In the XTI data structure depth is
replaced by the number of successors and the last successor
(according to the thread index).
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@103
     2
 *
alpar@209
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@103
     4
 *
alpar@463
     5
 * Copyright (C) 2003-2009
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@105
    19
#include <lemon/list_graph.h>
alpar@103
    20
#include <lemon/maps.h>
alpar@103
    21
#include <lemon/unionfind.h>
alpar@103
    22
#include "test_tools.h"
alpar@103
    23
alpar@103
    24
using namespace lemon;
alpar@103
    25
using namespace std;
alpar@103
    26
alpar@105
    27
typedef UnionFindEnum<ListGraph::NodeMap<int> > UFE;
alpar@103
    28
alpar@103
    29
int main() {
alpar@105
    30
  ListGraph g;
alpar@105
    31
  ListGraph::NodeMap<int> base(g);
alpar@103
    32
  UFE U(base);
alpar@105
    33
  vector<ListGraph::Node> n;
alpar@209
    34
alpar@105
    35
  for(int i=0;i<20;i++) n.push_back(g.addNode());
alpar@103
    36
alpar@105
    37
  U.insert(n[1]);
alpar@105
    38
  U.insert(n[2]);
alpar@103
    39
kpeter@171
    40
  check(U.join(n[1],n[2]) != -1, "Something is wrong with UnionFindEnum");
alpar@103
    41
alpar@105
    42
  U.insert(n[3]);
alpar@105
    43
  U.insert(n[4]);
alpar@105
    44
  U.insert(n[5]);
alpar@105
    45
  U.insert(n[6]);
alpar@105
    46
  U.insert(n[7]);
alpar@103
    47
alpar@103
    48
kpeter@171
    49
  check(U.join(n[1],n[4]) != -1, "Something is wrong with UnionFindEnum");
kpeter@171
    50
  check(U.join(n[2],n[4]) == -1, "Something is wrong with UnionFindEnum");
kpeter@171
    51
  check(U.join(n[3],n[5]) != -1, "Something is wrong with UnionFindEnum");
alpar@103
    52
alpar@103
    53
alpar@105
    54
  U.insert(n[8],U.find(n[5]));
alpar@103
    55
alpar@103
    56
kpeter@171
    57
  check(U.size(U.find(n[4])) == 3, "Something is wrong with UnionFindEnum");
kpeter@171
    58
  check(U.size(U.find(n[5])) == 3, "Something is wrong with UnionFindEnum");
kpeter@171
    59
  check(U.size(U.find(n[6])) == 1, "Something is wrong with UnionFindEnum");
kpeter@171
    60
  check(U.size(U.find(n[2])) == 3, "Something is wrong with UnionFindEnum");
alpar@103
    61
alpar@103
    62
alpar@105
    63
  U.insert(n[9]);
alpar@105
    64
  U.insert(n[10],U.find(n[9]));
alpar@103
    65
alpar@103
    66
kpeter@171
    67
  check(U.join(n[8],n[10])  != -1, "Something is wrong with UnionFindEnum");
alpar@103
    68
alpar@103
    69
kpeter@171
    70
  check(U.size(U.find(n[4])) == 3, "Something is wrong with UnionFindEnum");
kpeter@171
    71
  check(U.size(U.find(n[9])) == 5, "Something is wrong with UnionFindEnum");
alpar@103
    72
kpeter@171
    73
  check(U.size(U.find(n[8])) == 5, "Something is wrong with UnionFindEnum");
alpar@103
    74
alpar@105
    75
  U.erase(n[9]);
alpar@105
    76
  U.erase(n[1]);
alpar@103
    77
kpeter@171
    78
  check(U.size(U.find(n[10])) == 4, "Something is wrong with UnionFindEnum");
kpeter@171
    79
  check(U.size(U.find(n[2]))  == 2, "Something is wrong with UnionFindEnum");
alpar@103
    80
alpar@105
    81
  U.erase(n[6]);
alpar@105
    82
  U.split(U.find(n[8]));
alpar@103
    83
alpar@103
    84
kpeter@171
    85
  check(U.size(U.find(n[4])) == 2, "Something is wrong with UnionFindEnum");
kpeter@171
    86
  check(U.size(U.find(n[3])) == 1, "Something is wrong with UnionFindEnum");
kpeter@171
    87
  check(U.size(U.find(n[2])) == 2, "Something is wrong with UnionFindEnum");
alpar@103
    88
alpar@103
    89
kpeter@171
    90
  check(U.join(n[3],n[4]) != -1, "Something is wrong with UnionFindEnum");
kpeter@171
    91
  check(U.join(n[2],n[4]) == -1, "Something is wrong with UnionFindEnum");
alpar@103
    92
alpar@103
    93
kpeter@171
    94
  check(U.size(U.find(n[4])) == 3, "Something is wrong with UnionFindEnum");
kpeter@171
    95
  check(U.size(U.find(n[3])) == 3, "Something is wrong with UnionFindEnum");
kpeter@171
    96
  check(U.size(U.find(n[2])) == 3, "Something is wrong with UnionFindEnum");
alpar@103
    97
alpar@105
    98
  U.eraseClass(U.find(n[4]));
alpar@105
    99
  U.eraseClass(U.find(n[7]));
alpar@103
   100
alpar@103
   101
  return 0;
alpar@103
   102
}