| 1 | /* | 
|---|
| 2 | * DepthFirstSearchAnalysis.hpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Feb 16, 2011 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #ifndef DEPTHFIRSTSEARCHANALYSIS_HPP_ | 
|---|
| 9 | #define DEPTHFIRSTSEARCHANALYSIS_HPP_ | 
|---|
| 10 |  | 
|---|
| 11 | // include config.h | 
|---|
| 12 | #ifdef HAVE_CONFIG_H | 
|---|
| 13 | #include <config.h> | 
|---|
| 14 | #endif | 
|---|
| 15 |  | 
|---|
| 16 | #include <deque> | 
|---|
| 17 |  | 
|---|
| 18 | #include "ConnectedSubgraph.hpp" | 
|---|
| 19 |  | 
|---|
| 20 | class atom; | 
|---|
| 21 | class bond; | 
|---|
| 22 | class MoleculeLeafClass; | 
|---|
| 23 | class molecule; | 
|---|
| 24 |  | 
|---|
| 25 | class DepthFirstSearchAnalysis | 
|---|
| 26 | { | 
|---|
| 27 | public: | 
|---|
| 28 | /** Constructor of DepthFirstSearchAnalysis. | 
|---|
| 29 | */ | 
|---|
| 30 | DepthFirstSearchAnalysis(); | 
|---|
| 31 |  | 
|---|
| 32 | /** Destructor of DepthFirstSearchAnalysis. | 
|---|
| 33 | */ | 
|---|
| 34 | ~DepthFirstSearchAnalysis(); | 
|---|
| 35 |  | 
|---|
| 36 | /** Performs a Depth-First search on all atoms. | 
|---|
| 37 | * Marks bonds in molecule as cyclic, bridge, ... and atoms as | 
|---|
| 38 | * articulations points, ... | 
|---|
| 39 | * We use the algorithm from [Even, Graph Algorithms, p.62]. | 
|---|
| 40 | * \param *&BackEdgeStack NULL pointer to std::deque<bond *> with all the found back edges, allocated and filled on return | 
|---|
| 41 | * \return list of each disconnected subgraph as an individual molecule class structure | 
|---|
| 42 | */ | 
|---|
| 43 | void operator()(); | 
|---|
| 44 |  | 
|---|
| 45 | /** Scans through all bonds and set bond::Cyclic to true where atom::LowpointNr of both ends is equal: LP criterion. | 
|---|
| 46 | * \note Hydrogen bonds can never by cyclic, thus no check for that | 
|---|
| 47 | * \return number of cyclic bonds | 
|---|
| 48 | */ | 
|---|
| 49 | unsigned int CyclicBondAnalysis() const; | 
|---|
| 50 |  | 
|---|
| 51 | /** Removes all molecules and assign anew with known ListOfConnectedSubgraphs. | 
|---|
| 52 | * | 
|---|
| 53 | */ | 
|---|
| 54 | void UpdateMoleculeStructure() const; | 
|---|
| 55 |  | 
|---|
| 56 | /** Creates a chained list of all present molecules. | 
|---|
| 57 | * | 
|---|
| 58 | * \warning returns allocated chained list as reference, has to be free'd item by item through the list | 
|---|
| 59 | * | 
|---|
| 60 | * @return start node of chained molecules list | 
|---|
| 61 | */ | 
|---|
| 62 | MoleculeLeafClass *getMoleculeStructure() const; | 
|---|
| 63 |  | 
|---|
| 64 | /** Picks from a global stack with all back edges the ones in the fragment. | 
|---|
| 65 | * | 
|---|
| 66 | * Reference is the internal BackEdgeStack. | 
|---|
| 67 | * | 
|---|
| 68 | * \param **ListOfLocalAtoms array of father atom::nr to local atom::nr (reverse of atom::father) | 
|---|
| 69 | * \param *LocalStack stack to be filled | 
|---|
| 70 | * \return true - everything ok, false - ReferenceStack was empty | 
|---|
| 71 | */ | 
|---|
| 72 | bool PickLocalBackEdges(atom **ListOfLocalAtoms, std::deque<bond *> *&LocalStack) const; | 
|---|
| 73 |  | 
|---|
| 74 | /** Getter for BackEdgeStack. | 
|---|
| 75 | * | 
|---|
| 76 | * @return const reference to BackEdgeStack | 
|---|
| 77 | */ | 
|---|
| 78 | const std::deque<bond *>& getBackEdgeStack() const; | 
|---|
| 79 |  | 
|---|
| 80 | private: | 
|---|
| 81 | /** Resets GraphNodeInfo::ComponentNr and GraphNodeInfo::GraphrNr. | 
|---|
| 82 | * | 
|---|
| 83 | */ | 
|---|
| 84 | void Init(); | 
|---|
| 85 |  | 
|---|
| 86 | /** Returns next unused bond for this atom \a *vertex or NULL of none exists. | 
|---|
| 87 | * \param *vertex atom to regard | 
|---|
| 88 | * \return bond class or NULL | 
|---|
| 89 | */ | 
|---|
| 90 | bond * FindNextUnused(atom *vertex) const; | 
|---|
| 91 |  | 
|---|
| 92 | /** Resets bond::Used flag of all bonds in this molecule. | 
|---|
| 93 | * \return true - success, false - -failure | 
|---|
| 94 | */ | 
|---|
| 95 | void ResetAllBondsToUnused() const; | 
|---|
| 96 |  | 
|---|
| 97 | /** Sets the next component number. | 
|---|
| 98 | * This is O(N) as the number of bonds per atom is bound. | 
|---|
| 99 | * \param *vertex atom whose next atom::*ComponentNr is to be set | 
|---|
| 100 | * \param nr number to use | 
|---|
| 101 | */ | 
|---|
| 102 | void SetNextComponentNumber(atom *vertex, int nr) const; | 
|---|
| 103 |  | 
|---|
| 104 | /** Sets atom::GraphNr and atom::LowpointNr to CurrentGraphNr. | 
|---|
| 105 | * \param *Walker current node | 
|---|
| 106 | */ | 
|---|
| 107 | void SetWalkersGraphNr(atom *&Walker); | 
|---|
| 108 |  | 
|---|
| 109 | /** During DFS goes along unvisited bond and touches other atom. | 
|---|
| 110 | * Sets bond::type, if | 
|---|
| 111 | *  -# BackEdge: set atom::LowpointNr and push on \a BackEdgeStack | 
|---|
| 112 | *  -# TreeEgde: set atom::Ancestor and continue with Walker along this edge | 
|---|
| 113 | * Continue until molecule::FindNextUnused() finds no more unused bonds. | 
|---|
| 114 | * \param *&Walker current node | 
|---|
| 115 | * \param *&Binder current edge | 
|---|
| 116 | */ | 
|---|
| 117 | void ProbeAlongUnusedBond(atom *&Walker, bond *&Binder); | 
|---|
| 118 |  | 
|---|
| 119 | /** Checks whether we have a new component. | 
|---|
| 120 | * if atom::LowpointNr of \a *&Walker is greater than atom::GraphNr of its atom::Ancestor, we have a new component. | 
|---|
| 121 | * Meaning that if we touch upon a node who suddenly has a smaller atom::LowpointNr than its ancestor, then we | 
|---|
| 122 | * have a found a new branch in the graph tree. | 
|---|
| 123 | * \param *&Walker current node | 
|---|
| 124 | * \param &LeafWalker contains reference to destination subgraph | 
|---|
| 125 | */ | 
|---|
| 126 | void CheckForaNewComponent(atom *&Walker, ConnectedSubgraph &LeafWalker); | 
|---|
| 127 |  | 
|---|
| 128 | /** Cleans the root stack when we have found a component. | 
|---|
| 129 | * If we are not BackStepping, then we clear the root stack by putting everything into a | 
|---|
| 130 | * component down till we meet Root. | 
|---|
| 131 | * \param *&Walker current node | 
|---|
| 132 | * \param *&Binder current edge | 
|---|
| 133 | * \param &LeafWalker contains reference to destination subgraph | 
|---|
| 134 | */ | 
|---|
| 135 | void CleanRootStackDownTillWalker(atom *&Walker, bond *&Binder, ConnectedSubgraph &LeafWalker); | 
|---|
| 136 |  | 
|---|
| 137 | /** Output graph information per atom. | 
|---|
| 138 | */ | 
|---|
| 139 | void OutputGraphInfoPerAtom() const; | 
|---|
| 140 |  | 
|---|
| 141 | /** Output graph information per bond. | 
|---|
| 142 | */ | 
|---|
| 143 | void OutputGraphInfoPerBond() const; | 
|---|
| 144 |  | 
|---|
| 145 | std::deque<atom *> AtomStack; | 
|---|
| 146 | std::deque<bond *> BackEdgeStack; | 
|---|
| 147 | int CurrentGraphNr; | 
|---|
| 148 | int ComponentNumber; | 
|---|
| 149 | atom *Root; | 
|---|
| 150 | bool BackStepping; | 
|---|
| 151 |  | 
|---|
| 152 | typedef std::list< ConnectedSubgraph > ConnectedSubgraphList; | 
|---|
| 153 |  | 
|---|
| 154 | ConnectedSubgraphList ListOfConnectedSubgraphs; | 
|---|
| 155 | }; | 
|---|
| 156 |  | 
|---|
| 157 | #endif /* DEPTHFIRSTSEARCHANALYSIS_HPP_ */ | 
|---|