MyraMath
DisjointSet


Source: tests/multifrontal/symbolic/DisjointSet.cpp

1 // ========================================================================= //
2 // This file is part of MyraMath, copyright (c) 2014-2019 by Ryan A Chilton //
3 // and distributed by MyraCore, LLC. See LICENSE.txt for license terms. //
4 // ========================================================================= //
5 
11 // Containers.
12 #include <myramath/multifrontal/symbolic/detail/DisjointSet.h>
13 
14 // Reporting.
15 #include <tests/myratest.h>
17 #include <map>
18 #include <set>
19 
20 using namespace myra;
21 
22 // Makes a DisjointSet of 100 int's, then merges them according to a mod (%) operation.
23 ADD_TEST("DisjointSet","[symbolic]")
24  {
25  int N = 100;
26  int K = 13;
27  DisjointSet set(N);
28  for (int n = 0; n < N; ++n)
29  set.merge(n, n%K);
30  // Should be K roots at the end.
31  REQUIRE(set.roots() == K);
32  // Examine resulting sets.
33  using myra_stlprint::operator<<;
34  std::map<int,std::set<int> > output;
35  for (int n = 0; n < N; ++n)
36  output[set.root(n)].insert(n);
37  for (auto i = output.begin(); i != output.end(); ++i)
38  myra::out() << "output[" << i->first << "] = " << i->second << std::endl;
39  // Check equivalence between all (i,j) pairs in [0,N)
40  bool equivalence = true;
41  for (int i = 0; i < N; ++i)
42  for (int j = 0; j < N; ++j)
43  if ( (i%K) == (j%K) )
44  if ( set.root(i) != set.root(j) )
45  equivalence = false;
46  REQUIRE(equivalence);
47  }
48 
Definition: syntax.dox:1
Routines for printing the contents of various std::container&#39;s to a std::ostream using operator <<...


Results: [PASS]

output[87] = [ 9 22 35 48 61 74 87 ]
output[88] = [ 10 23 36 49 62 75 88 ]
output[89] = [ 11 24 37 50 63 76 89 ]
output[90] = [ 12 25 38 51 64 77 90 ]
output[91] = [ 0 13 26 39 52 65 78 91 ]
output[92] = [ 1 14 27 40 53 66 79 92 ]
output[93] = [ 2 15 28 41 54 67 80 93 ]
output[94] = [ 3 16 29 42 55 68 81 94 ]
output[95] = [ 4 17 30 43 56 69 82 95 ]
output[96] = [ 5 18 31 44 57 70 83 96 ]
output[97] = [ 6 19 32 45 58 71 84 97 ]
output[98] = [ 7 20 33 46 59 72 85 98 ]
output[99] = [ 8 21 34 47 60 73 86 99 ]


Go back to Summary of /test programs.