COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/johanna/unionfind_test.cc @ 474:229a16b5fd0f

Last change on this file since 474:229a16b5fd0f was 462:0ab31578af67, checked in by beckerjc, 20 years ago

Doc for the union-find structures.

File size: 3.3 KB
Line 
1#include <iostream>
2
3#include <maps.h>
4#include <unionfind.h>
5
6using namespace hugo;
7using namespace std;
8
9bool passed = true;
10
11void check(bool rc) {
12  passed = passed && rc;
13  if(!rc) {
14    cout << "Test failed!" << endl;
15  }
16}
17
18template <typename T>
19class BaseMap : public StdMap<int,T> {};
20
21typedef UnionFindEnum<int, BaseMap> UFE;
22
23void print(UFE const &ufe) {
24  UFE::ClassIt cit;
25  cout << "printing the classes of the structure:" << endl;
26  int i = 1;
27  for (ufe.first(cit); ufe.valid(cit); ufe.next(cit)) {
28    cout << "  " << i << " (" << cit << "):" << flush;
29    UFE::ItemIt iit;
30    for (ufe.first(iit, cit); ufe.valid(iit); ufe.next(iit)) {
31      cout << " " << iit << flush;
32    }
33    cout << endl;
34    i++;
35  }
36  cout << "done" << endl;
37}
38
39
40int main() {
41  UFE::MapType base;
42  UFE U(base);
43
44  print(U);
45
46  cout << "Inserting 1..." << endl;
47  U.insert(1);
48  print(U);
49 
50  cout << "Inserting 2..." << endl;
51  U.insert(2);
52  print(U);
53 
54  cout << "Joining 1 and 2..." << endl;
55  check(U.join(1,2));
56  print(U);
57
58  cout << "Inserting 3, 4, 5, 6, 7..." << endl;
59  U.insert(3);
60  U.insert(4);
61  U.insert(5);
62  U.insert(6);
63  U.insert(7);
64  print (U);
65
66  cout << "Joining 1 - 4, 2 - 4 and 3 - 5 ..." << endl;
67  check(U.join(1,4));
68  check(!U.join(2,4));
69  check(U.join(3,5));
70  print(U);
71
72  cout << "size of the class of 4: " << U.size(4) << endl;
73  check(U.size(4) == 3);
74  cout << "size of the class of 5: " << U.size(5) << endl;
75  check(U.size(5) == 2);
76  cout << "size of the class of 6: " << U.size(6) << endl;
77  check(U.size(6) == 1);
78  cout << "size of the class of 2: " << U.size(2) << endl;
79  check(U.size(2) == 3);
80
81  cout <<"erase 1: " << endl;
82  U.erase(1);
83  print(U);
84
85  cout << "size of the class of 4: " << U.size(4) << endl;
86  check(U.size(4) == 2);
87  cout << "size of the class of 2: " << U.size(2) << endl;
88  check(U.size(2) == 2);
89
90
91  cout <<"erase 1: " << endl;
92  U.erase(1);
93  print(U);
94
95  cout <<"erase 6: " << endl;
96  U.erase(6);
97  print(U);
98
99  cout << "split the class of 5: " << endl;
100  U.split(5);
101  print(U);
102
103
104  cout << "size of the class of 4: " << U.size(4) << endl;
105  check(U.size(4) == 2);
106  cout << "size of the class of 3: " << U.size(3) << endl;
107  check(U.size(3) == 1);
108  cout << "size of the class of 2: " << U.size(2) << endl;
109  check(U.size(2) == 2);
110
111
112  cout << "Joining 3 - 4 and 2 - 4 ..." << endl;
113  check(U.join(3,4));
114  check(!U.join(2,4));
115  print(U);
116
117
118  cout << "size of the class of 4: " << U.size(4) << endl;
119  check(U.size(4) == 3);
120  cout << "size of the class of 3: " << U.size(3) << endl;
121  check(U.size(3) == 3);
122  cout << "size of the class of 2: " << U.size(2) << endl;
123  check(U.size(2) == 3);
124
125  cout << "makeRep(4)" << endl;
126  U.makeRep(4);
127  print(U);
128  cout << "makeRep(3)" << endl;
129  U.makeRep(3);
130  print(U);
131  cout << "makeRep(2)" << endl;
132  U.makeRep(2);
133  print(U);
134
135  cout << "size of the class of 4: " << U.size(4) << endl;
136  check(U.size(4) == 3);
137  cout << "size of the class of 3: " << U.size(3) << endl;
138  check(U.size(3) == 3);
139  cout << "size of the class of 2: " << U.size(2) << endl;
140  check(U.size(2) == 3);
141
142
143  cout << "eraseClass 4 ..." << endl;
144  U.eraseClass(4);
145  print(U);
146
147  cout << "eraseClass 7 ..." << endl;
148  U.eraseClass(7);
149  print(U);
150
151  cout << "done" << endl;
152
153  cout << (passed ? "All tests passed." : "Some of the tests failed!!!")
154       << endl;
155
156  return passed ? 0 : 1;
157}
Note: See TracBrowser for help on using the repository browser.