permlib  0.2.9
Library for permutation computations
type_recognition_heuristic.h
1 // ---------------------------------------------------------------------------
2 //
3 // This file is part of PermLib.
4 //
5 // Copyright (c) 2009-2012 Thomas Rehn <thomas@carmen76.de>
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 // 1. Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // 2. Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 // 3. The name of the author may not be used to endorse or promote products
17 // derived from this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // ---------------------------------------------------------------------------
31 
32 
33 #ifndef TYPERECOGNITIONHEURISTIC_H_
34 #define TYPERECOGNITIONHEURISTIC_H_
35 
36 #include <list>
37 #include <vector>
38 
39 #include <boost/iterator/counting_iterator.hpp>
40 #include <boost/pending/disjoint_sets.hpp>
41 
42 namespace permlib {
43 
45 
49 template <class PERM>
51 public:
56  SymmetricGroupRecognitionHeuristic(unsigned int n, bool storeUnusedGenerators = false);
57 
59 
63  bool addGenerator(const PERM& p);
65 
68  void symmetricGroupOrbits(std::vector<std::list<dom_int> >& orbits);
69 
71  const std::list<PERM>& unusedGenerators() { return m_unusedGenerators; }
72 private:
73  bool addUnusedGenerator(const PERM& p);
74 
75  unsigned int m_n;
76  bool m_storeUnusedGenerators;
77  std::list<PERM> m_unusedGenerators;
78 
79  std::vector<dom_int> m_rank;
80  std::vector<dom_int> m_parent;
81  boost::disjoint_sets<dom_int*,dom_int*> m_components;
82 
83 };
84 
85 //
86 // ---- IMPLEMENTATION
87 //
88 
89 template <class PERM>
91  : m_n(n), m_storeUnusedGenerators(storeUnusedGenerators), m_rank(n), m_parent(n), m_components(&m_rank[0], &m_parent[0])
92 {
93  for (dom_int i = 0; i < n; ++i)
94  m_components.make_set(i);
95 }
96 
97 
98 template <class PERM>
100  BOOST_ASSERT( p.size() == m_n );
101 
102  dom_int cycle[2];
103  unsigned int cycleIndex = 0;
104 
105  for (unsigned int i = 0; i < p.size(); ++i) {
106  if (p.at(i) != i) {
107  if (cycleIndex >= 2)
108  return addUnusedGenerator(p);
109 
110  cycle[cycleIndex++] = i;
111  }
112  }
113 
114  if (cycleIndex == 0)
115  // discard identity
116  return true;
117 
118  BOOST_ASSERT(cycleIndex == 2);
119 
120  m_components.union_set(cycle[0], cycle[1]);
121  return true;
122 }
123 
124 
125 template <class PERM>
126 void SymmetricGroupRecognitionHeuristic<PERM>::symmetricGroupOrbits(std::vector<std::list<dom_int> >& orbits) {
127  m_components.compress_sets(boost::counting_iterator<dom_int>(0), boost::counting_iterator<dom_int>(m_n));
128 
129  std::vector<unsigned int> componentSizes(m_n);
130  for (unsigned int i = 0; i < m_n; ++i) {
131  ++componentSizes[ m_components.find_set(i) ];
132  }
133 
134  std::vector<int> nonTrivialComponentMap(m_n, -1);
135  unsigned int nonTrivialIndex = 0;
136  for (unsigned int i = 0; i < m_n; ++i) {
137  if (componentSizes[i] > 1)
138  nonTrivialComponentMap[i] = nonTrivialIndex++;
139  }
140 
141  orbits.clear();
142  orbits.resize(nonTrivialIndex);
143 
144  for (unsigned int i = 0; i < m_n; ++i) {
145  unsigned int componentIndex = m_components.find_set(i);
146  if (nonTrivialComponentMap[componentIndex] >= 0)
147  orbits[ nonTrivialComponentMap[componentIndex] ].push_back(i);
148  }
149 }
150 
151 
152 template <class PERM>
154  m_unusedGenerators.push_back(p);
155  return false;
156 }
157 
158 } // end NS
159 
160 #endif // -- TYPERECOGNITIONHEURISTIC_H_
bool addGenerator(const PERM &p)
adds a group generator for recognition
Definition: type_recognition_heuristic.h:99
SymmetricGroupRecognitionHeuristic(unsigned int n, bool storeUnusedGenerators=false)
Definition: type_recognition_heuristic.h:90
const std::list< PERM > & unusedGenerators()
returns the list of all group generators that were not used for group recognition ...
Definition: type_recognition_heuristic.h:71
void symmetricGroupOrbits(std::vector< std::list< dom_int > > &orbits)
computes the orbits of recognized non-trivial symmetric subgroups
Definition: type_recognition_heuristic.h:126
Definition: abstract_bsgs.h:49
Fast recognition of symmetric group subgroups.
Definition: type_recognition_heuristic.h:50