source: src/Graph/DepthFirstSearchAnalysis.cpp

Candidate_v1.6.1
Last change on this file was 9eb71b3, checked in by Frederik Heber <frederik.heber@…>, 8 years ago

Commented out MemDebug include and Memory::ignore.

  • MemDebug clashes with various allocation operators that use a specific placement in memory. It is so far not possible to wrap new/delete fully. Hence, we stop this effort which so far has forced us to put ever more includes (with clashes) into MemDebug and thereby bloat compilation time.
  • MemDebug does not add that much usefulness which is not also provided by valgrind.
  • Property mode set to 100644
File size: 17.5 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
6 *
7 *
8 * This file is part of MoleCuilder.
9 *
10 * MoleCuilder is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * MoleCuilder is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24/*
25 * DepthFirstSearchAnalysis.cpp
26 *
27 * Created on: Feb 16, 2011
28 * Author: heber
29 */
30
31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
36//#include "CodePatterns/MemDebug.hpp"
37
38#include "DepthFirstSearchAnalysis.hpp"
39
40#include <algorithm>
41#include <functional>
42
43#include "Atom/atom.hpp"
44#include "Bond/bond.hpp"
45#include "CodePatterns/Assert.hpp"
46#include "CodePatterns/Info.hpp"
47#include "CodePatterns/Log.hpp"
48#include "CodePatterns/Verbose.hpp"
49#include "Descriptors/AtomDescriptor.hpp"
50#include "Descriptors/MoleculeDescriptor.hpp"
51#include "Graph/ListOfLocalAtoms.hpp"
52#include "molecule.hpp"
53#include "MoleculeLeafClass.hpp"
54#include "World.hpp"
55
56DepthFirstSearchAnalysis::DepthFirstSearchAnalysis() :
57 CurrentGraphNr(0),
58 ComponentNumber(0),
59 BackStepping(false)
60{
61 ResetAllBondsToUnused();
62}
63
64DepthFirstSearchAnalysis::~DepthFirstSearchAnalysis()
65{}
66
67void DepthFirstSearchAnalysis::Init()
68{
69 CurrentGraphNr = 0;
70 ComponentNumber = 0;
71 BackStepping = false;
72 std::for_each(World::getInstance().getAtomIter(),World::getInstance().atomEnd(),
73 std::mem_fun(&atom::resetGraphNr));
74 std::for_each(World::getInstance().getAtomIter(),World::getInstance().atomEnd(),
75 std::mem_fun(&atom::InitComponentNr));
76}
77
78
79bond::ptr DepthFirstSearchAnalysis::FindNextUnused(atom *vertex) const
80{
81 const BondList& ListOfBonds = vertex->getListOfBonds();
82 for (BondList::const_iterator Runner = ListOfBonds.begin();
83 Runner != ListOfBonds.end();
84 ++Runner)
85 if ((*Runner)->IsUsed() == GraphEdge::white)
86 return ((*Runner));
87 return bond::ptr();
88}
89
90
91void DepthFirstSearchAnalysis::ResetAllBondsToUnused() const
92{
93 World::AtomComposite allatoms = World::getInstance().getAllAtoms();
94 for(World::AtomComposite::const_iterator AtomRunner = allatoms.begin();
95 AtomRunner != allatoms.end();
96 ++AtomRunner) {
97 const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
98 for(BondList::const_iterator BondRunner = ListOfBonds.begin();
99 BondRunner != ListOfBonds.end();
100 ++BondRunner)
101 if ((*BondRunner)->leftatom == *AtomRunner)
102 (*BondRunner)->ResetUsed();
103 }
104}
105
106void DepthFirstSearchAnalysis::SetNextComponentNumber(atom *vertex, int nr) const
107{
108 size_t i = 0;
109 ASSERT(vertex != NULL,
110 "DepthFirstSearchAnalysis::SetNextComponentNumber() - Given vertex is NULL!");
111 const BondList& ListOfBonds = vertex->getListOfBonds();
112 for (; i < ListOfBonds.size(); i++) {
113 if (vertex->ComponentNr[i] == -1) { // check if not yet used
114 vertex->ComponentNr[i] = nr;
115 break;
116 } else if (vertex->ComponentNr[i] == nr) // if number is already present, don't add another time
117 break; // breaking here will not cause error!
118 }
119 ASSERT(i < ListOfBonds.size(),
120 "DepthFirstSearchAnalysis::SetNextComponentNumber() - All Component entries are already occupied!");
121}
122
123
124bool DepthFirstSearchAnalysis::PickLocalBackEdges(const ListOfLocalAtoms_t &ListOfLocalAtoms, std::deque<bond::ptr > *&LocalStack) const
125{
126 bool status = true;
127 if (BackEdgeStack.empty()) {
128 ELOG(1, "Reference BackEdgeStack is empty!");
129 return false;
130 }
131 std::deque<bond::ptr > MyBackEdgeStack = BackEdgeStack;
132
133 do { // go through all bonds and push local ones
134 const bond::ptr &Binder = MyBackEdgeStack.front(); // loop the stack for next item
135 MyBackEdgeStack.pop_front();
136 LOG(3, "INFO: Current candidate edge " << *Binder << ".");
137 const ListOfLocalAtoms_t::const_iterator leftiter = ListOfLocalAtoms.find(Binder->leftatom->getNr());
138 ASSERT( leftiter != ListOfLocalAtoms.end(),
139 "DepthFirstSearchAnalysis::PickLocalBackEdges() - could not find atom id "
140 +toString(Binder->leftatom->getNr())+" in ListOfLocalAtoms.");
141 atom * const Walker = leftiter->second; // get one atom in the reference molecule
142 if (Walker != NULL) { // if this Walker exists in the subgraph ...
143 const BondList& ListOfBonds = Walker->getListOfBonds();
144 for (BondList::const_iterator Runner = ListOfBonds.begin();
145 Runner != ListOfBonds.end();
146 ++Runner) {
147 atom * const OtherAtom = (*Runner)->GetOtherAtom(Walker);
148 const ListOfLocalAtoms_t::const_iterator rightiter = ListOfLocalAtoms.find((*Runner)->rightatom->getNr());
149 if (OtherAtom == rightiter->second) { // found the bond
150 LocalStack->push_front((*Runner));
151 LOG(3, "INFO: Found local edge " << *(*Runner) << ".");
152 break;
153 }
154 }
155 }
156 } while (!MyBackEdgeStack.empty());
157
158 return status;
159}
160
161
162
163void DepthFirstSearchAnalysis::OutputGraphInfoPerAtom() const
164{
165 LOG(1, "Final graph info for each atom is:");
166 World::ConstAtomComposite allatoms = const_cast<const World &>(World::getInstance()).
167 getAllAtoms();
168 for_each(allatoms.begin(),allatoms.end(),mem_fun(&atom::OutputGraphInfo));
169}
170
171
172void DepthFirstSearchAnalysis::OutputGraphInfoPerBond() const
173{
174 LOG(1, "Final graph info for each bond is:");
175 World::ConstAtomComposite allatoms = const_cast<const World &>(World::getInstance()).
176 getAllAtoms();
177 for(World::ConstAtomComposite::const_iterator AtomRunner = allatoms.begin();
178 AtomRunner != allatoms.end();
179 ++AtomRunner) {
180 const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
181 for(BondList::const_iterator BondRunner = ListOfBonds.begin();
182 BondRunner != ListOfBonds.end();
183 ++BondRunner)
184 if ((*BondRunner)->leftatom == *AtomRunner) {
185 const bond::ptr Binder = *BondRunner;
186 if (DoLog(2)) {
187 std::stringstream output;
188 output << ((Binder->Type == GraphEdge::TreeEdge) ? "TreeEdge " : "BackEdge ") << *Binder << ": <";
189 output << ((Binder->leftatom->SeparationVertex) ? "SP," : "") << "L" << Binder->leftatom->LowpointNr << " G" << Binder->leftatom->GraphNr << " Comp.";
190 Binder->leftatom->OutputComponentNumber(&output);
191 output << " === ";
192 output << ((Binder->rightatom->SeparationVertex) ? "SP," : "") << "L" << Binder->rightatom->LowpointNr << " G" << Binder->rightatom->GraphNr << " Comp.";
193 Binder->rightatom->OutputComponentNumber(&output);
194 output << ">.";
195 LOG(2, output.str());
196 }
197 if (Binder->Cyclic) // cyclic ??
198 LOG(3, "Lowpoint at each side are equal: CYCLIC!");
199 }
200 }
201}
202
203
204unsigned int DepthFirstSearchAnalysis::CyclicBondAnalysis() const
205{
206 unsigned int NoCyclicBonds = 0;
207 World::ConstAtomComposite allatoms = const_cast<const World &>(World::getInstance()).
208 getAllAtoms();
209 for(World::ConstAtomComposite::const_iterator AtomRunner = allatoms.begin();
210 AtomRunner != allatoms.end();
211 ++AtomRunner) {
212 const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
213 for(BondList::const_iterator BondRunner = ListOfBonds.begin();
214 BondRunner != ListOfBonds.end();
215 ++BondRunner)
216 if ((*BondRunner)->leftatom == *AtomRunner)
217 if ((*BondRunner)->rightatom->LowpointNr == (*BondRunner)->leftatom->LowpointNr) { // cyclic ??
218 (*BondRunner)->Cyclic = true;
219 NoCyclicBonds++;
220 }
221 }
222 return NoCyclicBonds;
223}
224
225
226void DepthFirstSearchAnalysis::SetWalkersGraphNr(atom *&Walker)
227{
228 if (!BackStepping) { // if we don't just return from (8)
229 Walker->GraphNr = CurrentGraphNr;
230 Walker->LowpointNr = CurrentGraphNr;
231 LOG(1, "Setting Walker[" << Walker->getName() << "]'s number to " << Walker->GraphNr << " with Lowpoint " << Walker->LowpointNr << ".");
232 AtomStack.push_front(Walker);
233 CurrentGraphNr++;
234 }
235}
236
237
238void DepthFirstSearchAnalysis::ProbeAlongUnusedBond(atom *&Walker, bond::ptr &Binder)
239{
240 atom *OtherAtom = NULL;
241
242 do { // (3) if Walker has no unused egdes, go to (5)
243 BackStepping = false; // reset backstepping flag for (8)
244 if (Binder == NULL) // if we don't just return from (11), Binder is already set to next unused
245 Binder = FindNextUnused(Walker);
246 if (Binder == NULL)
247 break;
248 LOG(2, "Current Unused Bond is " << *Binder << ".");
249 // (4) Mark Binder used, ...
250 Binder->MarkUsed(GraphEdge::black);
251 OtherAtom = Binder->GetOtherAtom(Walker);
252 LOG(2, "(4) OtherAtom is " << OtherAtom->getName() << ".");
253 if (OtherAtom->GraphNr != -1) {
254 // (4a) ... if "other" atom has been visited (GraphNr != 0), set lowpoint to minimum of both, go to (3)
255 Binder->Type = GraphEdge::BackEdge;
256 BackEdgeStack.push_front(Binder);
257 Walker->LowpointNr = (Walker->LowpointNr < OtherAtom->GraphNr) ? Walker->LowpointNr : OtherAtom->GraphNr;
258 LOG(3, "(4a) Visited: Setting Lowpoint of Walker[" << Walker->getName() << "] to " << Walker->LowpointNr << ".");
259 } else {
260 // (4b) ... otherwise set OtherAtom as Ancestor of Walker and Walker as OtherAtom, go to (2)
261 Binder->Type = GraphEdge::TreeEdge;
262 OtherAtom->Ancestor = Walker;
263 Walker = OtherAtom;
264 LOG(3, "(4b) Not Visited: OtherAtom[" << OtherAtom->getName() << "]'s Ancestor is now " << OtherAtom->Ancestor->getName() << ", Walker is OtherAtom " << OtherAtom->getName() << ".");
265 break;
266 }
267 Binder.reset();
268 } while (1); // (3)
269}
270
271
272void DepthFirstSearchAnalysis::CheckForaNewComponent(atom *&Walker, ConnectedSubgraph &Subgraph)
273{
274 atom *OtherAtom = NULL;
275
276 // (5) if Ancestor of Walker is ...
277 LOG(1, "(5) Number of Walker[" << Walker->getName() << "]'s Ancestor[" << Walker->Ancestor->getName() << "] is " << Walker->Ancestor->GraphNr << ".");
278
279 if (Walker->Ancestor->GraphNr != Root->GraphNr) {
280 // (6) (Ancestor of Walker is not Root)
281 if (Walker->LowpointNr < Walker->Ancestor->GraphNr) {
282 // (6a) set Ancestor's Lowpoint number to minimum of of its Ancestor and itself, go to Step(8)
283 Walker->Ancestor->LowpointNr = (Walker->Ancestor->LowpointNr < Walker->LowpointNr) ? Walker->Ancestor->LowpointNr : Walker->LowpointNr;
284 LOG(2, "(6) Setting Walker[" << Walker->getName() << "]'s Ancestor[" << Walker->Ancestor->getName() << "]'s Lowpoint to " << Walker->Ancestor->LowpointNr << ".");
285 } else {
286 // (7) (Ancestor of Walker is a separating vertex, remove all from stack till Walker (including), these and Ancestor form a component
287 Walker->Ancestor->SeparationVertex = true;
288 LOG(2, "(7) Walker[" << Walker->getName() << "]'s Ancestor[" << Walker->Ancestor->getName() << "]'s is a separating vertex, creating component.");
289 SetNextComponentNumber(Walker->Ancestor, ComponentNumber);
290 LOG(3, "(7) Walker[" << Walker->getName() << "]'s Ancestor's Compont is " << ComponentNumber << ".");
291 SetNextComponentNumber(Walker, ComponentNumber);
292 LOG(3, "(7) Walker[" << Walker->getName() << "]'s Compont is " << ComponentNumber << ".");
293 do {
294 ASSERT(!AtomStack.empty(), "DepthFirstSearchAnalysis_CheckForaNewComponent() - AtomStack is empty!");
295 OtherAtom = AtomStack.front();
296 AtomStack.pop_front();
297 Subgraph.push_back(OtherAtom);
298 SetNextComponentNumber(OtherAtom, ComponentNumber);
299 LOG(3, "(7) Other[" << OtherAtom->getName() << "]'s Compont is " << ComponentNumber << ".");
300 } while (OtherAtom != Walker);
301 ComponentNumber++;
302 }
303 // (8) Walker becomes its Ancestor, go to (3)
304 LOG(2, "(8) Walker[" << Walker->getName() << "] is now its Ancestor " << Walker->Ancestor->getName() << ", backstepping. ");
305 Walker = Walker->Ancestor;
306 BackStepping = true;
307 }
308}
309
310
311void DepthFirstSearchAnalysis::CleanRootStackDownTillWalker(atom *&Walker, bond::ptr &Binder, ConnectedSubgraph &Subgraph)
312{
313 atom *OtherAtom = NULL;
314
315 if (!BackStepping) { // coming from (8) want to go to (3)
316 // (9) remove all from stack till Walker (including), these and Root form a component
317 //AtomStack.Output(out);
318 SetNextComponentNumber(Root, ComponentNumber);
319 LOG(3, "(9) Root[" << Root->getName() << "]'s Component is " << ComponentNumber << ".");
320 SetNextComponentNumber(Walker, ComponentNumber);
321 LOG(3, "(9) Walker[" << Walker->getName() << "]'s Component is " << ComponentNumber << ".");
322 do {
323 ASSERT(!AtomStack.empty(), "DepthFirstSearchAnalysis::CleanRootStackDownTillWalker() - AtomStack is empty!");
324 OtherAtom = AtomStack.front();
325 AtomStack.pop_front();
326 Subgraph.push_back(OtherAtom);
327 SetNextComponentNumber(OtherAtom, ComponentNumber);
328 LOG(3, "(7) Other[" << OtherAtom->getName() << "]'s Component is " << ComponentNumber << ".");
329 } while (OtherAtom != Walker);
330 ComponentNumber++;
331
332 // (11) Root is separation vertex, set Walker to Root and go to (4)
333 Walker = Root;
334 Binder = FindNextUnused(Walker);
335 if (Binder != NULL) { // Root is separation vertex
336 LOG(1, "(10) Walker is Root[" << Root->getName() << "], next Unused Bond is " << *Binder << ".");
337 LOG(1, "(11) Root is a separation vertex.");
338 Walker->SeparationVertex = true;
339 } else {
340 LOG(1, "(10) Walker is Root[" << Root->getName() << "], no next Unused Bond.");
341 }
342 }
343}
344
345
346const std::deque<bond::ptr >& DepthFirstSearchAnalysis::getBackEdgeStack() const
347{
348 return BackEdgeStack;
349}
350
351
352void DepthFirstSearchAnalysis::operator()()
353{
354 Info FunctionInfo("DepthFirstSearchAnalysis");
355 ListOfConnectedSubgraphs.clear();
356 int OldGraphNr = 0;
357 atom *Walker = NULL;
358 bond::ptr Binder;
359
360 if (World::getInstance().numAtoms() == 0)
361 return;
362
363 Init();
364
365 LOG(0, "STATUS: Start walking the bond graph.");
366 for(World::AtomIterator iter = World::getInstance().getAtomIter();
367 iter != World::getInstance().atomEnd();) { // don't advance, is done at the end
368 Root = *iter;
369 // (1) mark all edges unused, empty stack, set atom->GraphNr = -1 for all
370 AtomStack.clear();
371
372 // put into new subgraph molecule and add this to list of subgraphs
373 ConnectedSubgraph CurrentSubgraph;
374 CurrentSubgraph.push_back(Root);
375
376 OldGraphNr = CurrentGraphNr;
377 Walker = Root;
378 do { // (10)
379 do { // (2) set number and Lowpoint of Atom to i, increase i, push current atom
380 SetWalkersGraphNr(Walker);
381
382 ProbeAlongUnusedBond(Walker, Binder);
383
384 if (Binder == NULL) {
385 LOG(2, "No more Unused Bonds.");
386 break;
387 } else
388 Binder.reset();
389 } while (1); // (2)
390
391 // if we came from backstepping, yet there were no more unused bonds, we end up here with no Ancestor, because Walker is Root! Then we are finished!
392 if ((Walker == Root) && (Binder == NULL))
393 break;
394
395 CheckForaNewComponent( Walker, CurrentSubgraph);
396
397 CleanRootStackDownTillWalker(Walker, Binder, CurrentSubgraph);
398
399 } while ((BackStepping) || (Binder != NULL)); // (10) halt only if Root has no unused edges
400
401 ListOfConnectedSubgraphs.push_back(CurrentSubgraph);
402 // From OldGraphNr to CurrentGraphNr ranges an disconnected subgraph
403 std::stringstream output;
404 output << CurrentSubgraph;
405 LOG(1, "INFO: Disconnected subgraph ranges from " << OldGraphNr << " to "
406 << CurrentGraphNr-1 << ": " << output.str());
407
408 // step on to next root
409 while (iter != World::getInstance().atomEnd()) {
410 if ((*iter)->GraphNr != -1) { // if already discovered, step on
411 iter++;
412 } else {
413 LOG(1,"Current next subgraph root candidate is " << (*iter)->getName()
414 << " with GraphNr " << (*iter)->GraphNr << ".");
415 break;
416 }
417 }
418 }
419 LOG(0, "STATUS: Done walking the bond graph.");
420
421 // set cyclic bond criterium on "same LP" basis
422 CyclicBondAnalysis();
423
424 OutputGraphInfoPerAtom();
425
426 OutputGraphInfoPerBond();
427}
428
429void DepthFirstSearchAnalysis::UpdateMoleculeStructure() const
430{
431 // remove all of World's molecules
432 for (World::MoleculeIterator iter = World::getInstance().getMoleculeIter();
433 World::getInstance().getMoleculeIter() != World::getInstance().moleculeEnd();
434 iter = World::getInstance().getMoleculeIter()) {
435 World::getInstance().destroyMolecule(*iter);
436 }
437 // instantiate new molecules
438 molecule *newmol = NULL;
439 for (ConnectedSubgraphList::const_iterator iter = ListOfConnectedSubgraphs.begin();
440 iter != ListOfConnectedSubgraphs.end();
441 ++iter) {
442 newmol = (*iter).getMolecule();
443 if (DoLog(2)) {
444 LOG(2, "STATUS: Creating new molecule:");
445 std::stringstream output;
446 newmol->Output(&output);
447 std::stringstream outstream(output.str());
448 std::string line;
449 while (getline(outstream, line)) {
450 LOG(2, "\t"+line);
451 }
452 }
453 }
454}
455
456MoleculeLeafClass *DepthFirstSearchAnalysis::getMoleculeStructure() const
457{
458 MoleculeLeafClass *Subgraphs = new MoleculeLeafClass(NULL);
459 MoleculeLeafClass *MolecularWalker = Subgraphs;
460 for (World::MoleculeIterator iter = World::getInstance().getMoleculeIter();
461 iter != World::getInstance().moleculeEnd();
462 ++iter) {
463 // TODO: Remove the insertion into molecule when saving does not depend on them anymore. Also, remove molecule.hpp include
464 MolecularWalker = new MoleculeLeafClass(MolecularWalker);
465 MolecularWalker->Leaf = (*iter);
466 }
467 return Subgraphs;
468}
469
Note: See TracBrowser for help on using the repository browser.