Debian Codegraph @

avogadro/core/ringperceiver.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "ringperceiver.h"
7
8 #include "molecule.h"
9
10 #include <algorithm>
11 #include <cstring>
12 #include <iterator>
13 #include <limits>
14 #include <set>
15 #include <vector>
16
17 namespace Avogadro::Core {
18
19 namespace {
20
21 // === DistanceMatrix ====================================================== //
22 class DistanceMatrix
23 {
24 public:
25 // swap function marked friend so it can only be found via Argument Dependent
26 // Lookup (ADL)
27 friend void swap(DistanceMatrix& first, DistanceMatrix& second)
28 {
29 // Enable ADL for the swap
30 using std::swap;
31
32 swap(first.m_size, second.m_size);
33 swap(first.m_values, second.m_values);
34 }
35
36 // construction and destruction
37 DistanceMatrix(size_t size);
38 ~DistanceMatrix();
39
40 DistanceMatrix(const DistanceMatrix& other);
41
42 // intentional pass-by-value to leverage previous copy ctor
43 DistanceMatrix& operator=(DistanceMatrix other);
44
45 // operators
46 size_t operator()(size_t i, size_t j) const;
47 size_t& operator()(size_t i, size_t j);
48
49 private:
50 size_t m_size;
51 size_t* m_values;
52 };
53
54 DistanceMatrix::DistanceMatrix(size_t size)
55 : m_size(size), m_values(new size_t[size * size])
56 {
57 memset(m_values, 0, size * size * sizeof(size_t));
58 }
59
60 [[maybe_unused]] DistanceMatrix::DistanceMatrix(const DistanceMatrix& other)
61 : m_size(other.m_size),
62 m_values(other.m_size ? new size_t[other.m_size * other.m_size] : nullptr)
63 {
64 if (m_values)
65 std::copy(other.m_values, other.m_values + (other.m_size * other.m_size),
66 m_values);
67 }
68
69 [[maybe_unused]] DistanceMatrix& DistanceMatrix::operator=(DistanceMatrix other)
70 {
71 // will use friend swap function via Argument Dependent Lookup
72 swap(*this, other);
73
74 return *this;
75 }
76
77 DistanceMatrix::~DistanceMatrix()
78 {
79 delete[] m_values;
80 }
81
82 size_t& DistanceMatrix::operator()(size_t i, size_t j)
83 {
84 return m_values[i * m_size + j];
85 }
86
87 // === PidMatrix =========================================================== //
88 // The PidMatrix class implements a path-included distance matrix.
89 class PidMatrix
90 {
91 public:
92 // construction and destruction
93 PidMatrix(size_t size);
94 ~PidMatrix();
95
96 // paths
97 std::vector<std::vector<size_t>>& paths(size_t i, size_t j);
98 void addPaths(size_t i, size_t j,
99 const std::vector<std::vector<size_t>>& paths);
100 std::vector<std::vector<size_t>> splice(size_t i, size_t j, size_t k);
101
102 // operators
103 std::vector<std::vector<size_t>>& operator()(size_t i, size_t j);
104
105 private:
106 size_t m_size;
107 std::vector<std::vector<size_t>>* m_values;
108 };
109
110 // --- Construction and Destruction ---------------------------------------- //
111 PidMatrix::PidMatrix(size_t size)
112 : m_size(size), m_values(new std::vector<std::vector<size_t>>[size * size])
113 {
114 }
115
116 PidMatrix::~PidMatrix()
117 {
118 delete[] m_values;
119 }
120
121 // --- Paths --------------------------------------------------------------- //
122 std::vector<std::vector<size_t>>& PidMatrix::paths(size_t i, size_t j)
123 {
124 return m_values[i * m_size + j];
125 }
126
127 void PidMatrix::addPaths(size_t i, size_t j,
128 const std::vector<std::vector<size_t>>& p)
129 {
130 std::vector<std::vector<size_t>>& current = m_values[i * m_size + j];
131 current.insert(current.end(), p.begin(), p.end());
132 }
133
134 std::vector<std::vector<size_t>>& PidMatrix::operator()(size_t i, size_t j)
135 {
136 return paths(i, j);
137 }
138
139 std::vector<std::vector<size_t>> PidMatrix::splice(size_t i, size_t j, size_t k)
140 {
141 std::vector<std::vector<size_t>> splicedPaths;
142
143 std::vector<std::vector<size_t>> ijPaths = paths(i, j);
144 std::vector<std::vector<size_t>> jkPaths = paths(j, k);
145
146 if (ijPaths.empty() && jkPaths.empty()) {
147 std::vector<size_t> path;
148 path.push_back(j);
149 splicedPaths.push_back(path);
150 } else if (ijPaths.empty()) {
151 for (auto& jkPath : jkPaths) {
152 std::vector<size_t> path;
153 path.push_back(j);
154 path.insert(path.end(), jkPath.begin(), jkPath.end());
155 splicedPaths.push_back(path);
156 }
157 } else if (jkPaths.empty()) {
158 for (auto path : ijPaths) {
159 path.push_back(j);
160 splicedPaths.push_back(path);
161 }
162 } else {
163 for (auto path : ijPaths) {
164 for (auto& jkPath : jkPaths) {
165 path.push_back(j);
166 path.insert(path.end(), jkPath.begin(), jkPath.end());
167 splicedPaths.push_back(path);
168 }
169 }
170 }
171
172 return splicedPaths;
173 }
174
175 // === RingCandidate ======================================================= //
176 class RingCandidate
177 {
178 public:
179 // construction and destruction
180 RingCandidate(size_t n, size_t s, size_t e);
181
182 // properties
183 size_t size() const;
184 size_t start() const;
185 size_t end() const;
186
187 // static methods
188 static bool compareSize(const RingCandidate& a, const RingCandidate& b);
189
190 private:
191 size_t m_size;
192 size_t m_start;
193 size_t m_end;
194 };
195
196 // --- Construction and Destruction ---------------------------------------- //
197 RingCandidate::RingCandidate(size_t n, size_t s, size_t e)
198 : m_size(n), m_start(s), m_end(e)
199 {
200 }
201
202 // --- Properties ---------------------------------------------------------- //
203 size_t RingCandidate::size() const
204 {
205 return m_size;
206 }
207
208 size_t RingCandidate::start() const
209 {
210 return m_start;
211 }
212
213 size_t RingCandidate::end() const
214 {
215 return m_end;
216 }
217
218 // --- Static Methods ------------------------------------------------------ //
219 bool RingCandidate::compareSize(const RingCandidate& a, const RingCandidate& b)
220 {
221 return a.size() < b.size();
222 }
223
224 // === Sssr ================================================================ //
225 class Sssr
226 {
227 public:
228 // construction and destruction
229 Sssr() = default;
230 ~Sssr() = default;
231
232 // properties
233 size_t size() const;
234 bool isEmpty() const;
235
236 // rings
237 const std::vector<std::vector<size_t>>& rings() const;
238 void append(const std::vector<size_t>& ring);
239 bool isValid(const std::vector<size_t>& ring) const;
240 bool isUnique(const std::vector<size_t>& ring) const;
241
242 private:
243 std::vector<std::vector<size_t>> m_rings;
244 };
245
246 // --- Properties ---------------------------------------------------------- //
247 size_t Sssr::size() const
248 {
249 return m_rings.size();
250 }
251
252 bool Sssr::isEmpty() const
253 {
254 return m_rings.empty();
255 }
256
257 // --- Rings --------------------------------------------------------------- //
258 const std::vector<std::vector<size_t>>& Sssr::rings() const
259 {
260 return m_rings;
261 }
262
263 void Sssr::append(const std::vector<size_t>& ring)
264 {
265 m_rings.push_back(ring);
266 }
267
268 bool Sssr::isValid(const std::vector<size_t>& ring) const
269 {
270 // Check for any duplicate atoms.
271 for (size_t i = 0; i < ring.size(); ++i)
272 for (size_t j = i + 1; j < ring.size(); ++j)
273 if (ring[i] == ring[j])
274 return false;
275
276 return true;
277 }
278
279 bool Sssr::isUnique(const std::vector<size_t>& path) const
280 {
281 // Must be unique if sssr is empty.
282 if (isEmpty())
283 return true;
284
285 // Check if a ring with the same atoms is already in the sssr.
286 std::set<size_t> pathSet;
287 pathSet.insert(path.begin(), path.end());
288
289 for (const auto& ring : m_rings) {
290 std::set<size_t> ringSet;
291 ringSet.insert(ring.begin(), ring.end());
292
293 std::vector<size_t> sortedRing(ring.begin(), ring.end());
294 std::sort(sortedRing.begin(), sortedRing.end());
295
296 std::set<size_t> intersection;
297 std::set_intersection(pathSet.begin(), pathSet.end(), ringSet.begin(),
298 ringSet.end(),
299 std::inserter(intersection, intersection.begin()));
300
301 if (intersection.size() == ring.size())
302 return false;
303 }
304
305 // Build the set of bonds in the path.
306 std::set<std::pair<size_t, size_t>> pathBonds;
307 for (size_t i = 0; i < path.size() - 1; i++) {
308 pathBonds.insert(std::make_pair(std::min(path[i], path[i + 1]),
309 std::max(path[i], path[i + 1])));
310 }
311
312 pathBonds.insert(std::make_pair(std::min(path.front(), path.back()),
313 std::max(path.front(), path.back())));
314
315 // Remove bonds from path bonds that are already in a smaller ring.
316 for (const auto& ring : m_rings) {
317 if (ring.size() >= path.size())
318 continue;
319
320 for (size_t i = 0; i < ring.size(); i++) {
321 pathBonds.erase(std::make_pair(std::min(ring[i], ring[i + 1]),
322 std::max(ring[i], ring[i + 1])));
323 }
324
325 pathBonds.erase(std::make_pair(std::min(ring.front(), ring.back()),
326 std::max(ring.front(), ring.back())));
327 }
328
329 // Check if any other ring contains the same bonds.
330 for (const auto& ring : m_rings) {
331 std::set<std::pair<size_t, size_t>> ringBonds;
332
333 // Add ring bonds.
334 for (size_t i = 0; i < ring.size() - 1; i++) {
335 ringBonds.insert(std::make_pair(std::min(ring[i], ring[i + 1]),
336 std::max(ring[i], ring[i + 1])));
337 }
338
339 // Add closure bond.
340 ringBonds.insert(std::make_pair(std::min(ring.front(), ring.back()),
341 std::max(ring.front(), ring.back())));
342
343 // Check intersection.
344 std::set<std::pair<size_t, size_t>> intersection;
345 std::set_intersection(pathBonds.begin(), pathBonds.end(), ringBonds.begin(),
346 ringBonds.end(),
347 std::inserter(intersection, intersection.begin()));
348
349 if (intersection.size() == pathBonds.size())
350 return false;
351 }
352
353 return true;
354 }
355
356 std::vector<std::vector<size_t>> perceiveRings(const Graph& graph)
357 {
358 size_t n = graph.size();
359
360 if (graph.edgeCount() < graph.vertexCount())
361 return std::vector<std::vector<size_t>>();
362 size_t ringCount = graph.edgeCount() - graph.vertexCount() + 1;
363 if (ringCount == 0)
364 return std::vector<std::vector<size_t>>();
365
366 // Algorithm 1 - create the distance and pid matrices.
367 DistanceMatrix D(n);
368 PidMatrix P(n);
369 PidMatrix Pt(n);
370
371 for (size_t i = 0; i < n; ++i) {
372 for (size_t j = 0; j < n; ++j) {
373 if (i == j)
374 D(i, j) = 0;
375 else if (graph.containsEdge(i, j))
376 D(i, j) = 1;
377 else
378 D(i, j) = std::numeric_limits<size_t>::max() / 2; // ~ infinity
379 }
380 }
381
382 for (size_t k = 0; k < n; ++k) {
383 for (size_t i = 0; i < n; ++i) {
384 for (size_t j = 0; j < n; ++j) {
385 if (i == j || i == k || k == j)
386 continue;
387
388 if (D(i, j) > D(i, k) + D(k, j)) {
389 if (D(i, j) == D(i, k) + D(k, j) + 1)
390 Pt(i, j) = P(i, j);
391 else
392 Pt(i, j).clear();
393
394 D(i, j) = D(i, k) + D(k, j);
395 P(i, j) = P.splice(i, k, j);
396 } else if (D(i, j) == D(i, k) + D(k, j)) {
397 P.addPaths(i, j, P.splice(i, k, j));
398 } else if (D(i, j) == D(i, k) + D(k, j) - 1) {
399 Pt.addPaths(i, j, P.splice(i, k, j));
400 }
401 }
402 }
403 }
404
405 // Algorithm 2 - create the ring candidate set.
406 std::vector<RingCandidate> candidates;
407 for (size_t i = 0; i < n; i++) {
408 for (size_t j = i + 1; j < n; j++) {
409 if (P(i, j).size() == 1 && Pt(i, j).size() == 0) {
410 continue;
411 } else {
412 size_t size;
413
414 if (P(i, j).size() > 1)
415 size = 2 * D(i, j);
416 else
417 size = 2 * D(i, j) + 1;
418
419 if (size > 2)
420 candidates.emplace_back(size, i, j);
421 }
422 }
423 }
424
425 // Sort the candidates.
426 std::sort(candidates.begin(), candidates.end(), RingCandidate::compareSize);
427
428 // Algorithm 3 - find sssr from the ring candidate set.
429 Sssr sssr;
430
431 for (auto& candidate : candidates) {
432 // odd sized ring
433 if (candidate.size() & 1) {
434 for (size_t i = 0; i < Pt(candidate.start(), candidate.end()).size();
435 ++i) {
436 std::vector<size_t> ring;
437 ring.push_back(candidate.start());
438 std::vector<size_t>& path = Pt(candidate.start(), candidate.end())[i];
439 ring.insert(ring.end(), path.begin(), path.end());
440 ring.push_back(candidate.end());
441 if (!P(candidate.end(), candidate.start()).empty()) {
442 path = P(candidate.end(), candidate.start())[0];
443 ring.insert(ring.end(), path.begin(), path.end());
444 }
445
446 // Check if ring is valid and unique.
447 if (sssr.isValid(ring) && sssr.isUnique(ring)) {
448 sssr.append(ring);
449 break;
450 }
451 }
452 }
453
454 // Even sized ring.
455 else {
456 for (size_t i = 0; i < P(candidate.start(), candidate.end()).size() - 1;
457 ++i) {
458 std::vector<size_t> ring;
459 ring.push_back(candidate.start());
460 std::vector<size_t>& path = P(candidate.start(), candidate.end())[i];
461 ring.insert(ring.end(), path.begin(), path.end());
462 ring.push_back(candidate.end());
463 path = P(candidate.end(), candidate.start())[i + 1];
464 ring.insert(ring.end(), path.begin(), path.end());
465
466 // check if ring is valid and unique
467 if (sssr.isValid(ring) && sssr.isUnique(ring)) {
468 sssr.append(ring);
469 break;
470 }
471 }
472 }
473
474 if (sssr.size() == ringCount)
475 break;
476 }
477
478 return sssr.rings();
479 }
480
481 } // end anonymous namespace
482
483 RingPerceiver::RingPerceiver(const Molecule* m)
484 : m_ringsPerceived(false), m_molecule(m)
485 {
486 }
487
488 void RingPerceiver::setMolecule(const Molecule* m)
489 {
490 m_molecule = m;
491 m_ringsPerceived = false;
492 }
493
494 const Molecule* RingPerceiver::molecule() const
495 {
496 return m_molecule;
497 }
498
499 std::vector<std::vector<size_t>>& RingPerceiver::rings()
500 {
501 if (!m_ringsPerceived) {
502 if (m_molecule)
503 m_rings = perceiveRings(m_molecule->graph());
504 else
505 m_rings.clear();
506
507 m_ringsPerceived = true;
508 }
509
510 return m_rings;
511 }
512
513 } // namespace Avogadro::Core