Index: src/builder.cpp
===================================================================
--- src/builder.cpp	(revision de293ac530466215e29d973ad379a2e65165a40c)
+++ src/builder.cpp	(revision fc850d10e0fcb47d27b1b6baf17186a20debf1ea)
@@ -720,5 +720,5 @@
   int Z;
   int j, axis, count, faktor;
-  int MinimumRingSize = -1;
+  int *MinimumRingSize = NULL;
   enum ConfigStatus config_present = absent;
   MoleculeLeafClass *Subgraphs = NULL;
@@ -1140,4 +1140,5 @@
         }
         delete(Subgraphs);    // we don't need the list here, so free everything
+        delete[](MinimumRingSize);
         Subgraphs = NULL;
         end = clock();
Index: src/molecules.cpp
===================================================================
--- src/molecules.cpp	(revision de293ac530466215e29d973ad379a2e65165a40c)
+++ src/molecules.cpp	(revision fc850d10e0fcb47d27b1b6baf17186a20debf1ea)
@@ -1179,5 +1179,5 @@
 {
   int No = 0;
-  int MinimumRingSize;
+  int *MinimumRingSize = NULL;
   MoleculeLeafClass *Subgraphs = NULL;
   bond *Binder = first;
@@ -1190,4 +1190,5 @@
     }
     delete(Subgraphs);
+    delete[](MinimumRingSize);
   }
   while(Binder->next != last) {
@@ -1437,8 +1438,8 @@
  * \param *out output stream for debugging
  * \param ReturnStack true - return pointer to atom stack of separable components, false - return NULL
- * \param MinimumRingSize contains smallest ring size in molecular structure on return or -1 if no rings were found
+ * \param *&MinimumRingSize contains smallest ring size in molecular structure on return or -1 if no rings were found
  * \return list of each disconnected subgraph as an individual molecule class structure
  */
-MoleculeLeafClass * molecule::DepthFirstSearchAnalysis(ofstream *out, bool ReturnStack, int &MinimumRingSize)
+MoleculeLeafClass * molecule::DepthFirstSearchAnalysis(ofstream *out, bool ReturnStack, int *&MinimumRingSize)
 {
   class StackClass<atom *> *AtomStack;
@@ -1630,8 +1631,8 @@
  * \param *out output stream for debugging
  * \param *BackEdgeStack stack with all back edges found during DFS scan
- * \param MinimumRingSize contains smallest ring size in molecular structure on return or -1 if no rings were found, if set is maximum search distance
+ * \param *&MinimumRingSize contains smallest ring size in molecular structure on return or -1 if no rings were found, if set is maximum search distance
  * \todo BFS from the not-same-LP to find back to starting point of tributary cycle over more than one bond
  */
-void molecule::CyclicStructureAnalysis(ofstream *out, class StackClass<bond *> *  BackEdgeStack, int &MinimumRingSize)
+void molecule::CyclicStructureAnalysis(ofstream *out, class StackClass<bond *> *  BackEdgeStack, int *&MinimumRingSize)
 {
   atom **PredecessorList = (atom **) Malloc(sizeof(atom *)*AtomCount, "molecule::CyclicStructureAnalysis: **PredecessorList");
@@ -1642,5 +1643,5 @@
   atom *Walker = NULL, *OtherAtom = NULL, *Root = NULL;
   bond *Binder = NULL, *BackEdge = NULL;
-  int RingSize, NumCycles;
+  int RingSize, NumCycles, MinRingSize = -1;
 
   // initialise each vertex as white with no predecessor, empty queue, color Root lightgray
@@ -1650,4 +1651,8 @@
     ColorList[i] = white;
   }
+  MinimumRingSize = new int[AtomCount];
+  for(int i=0;i<AtomCount;i++)
+    MinimumRingSize[i] = AtomCount;
+
   
   *out << Verbose(1) << "Back edge list - ";
@@ -1655,6 +1660,4 @@
   
   *out << Verbose(1) << "Analysing cycles ... " << endl;
-  if ((MinimumRingSize <= 2) || (MinimumRingSize > AtomCount))
-    MinimumRingSize = AtomCount;
   NumCycles = 0;
   while (!BackEdgeStack->IsEmpty()) {
@@ -1670,5 +1673,5 @@
     //*out << Verbose(1) << "---------------------------------------------------------------------------------------------------------" << endl;
     OtherAtom = NULL;
-    while ((Walker != Root) && ((OtherAtom == NULL) || (ShortestPathList[OtherAtom->nr] < MinimumRingSize))) {  // look for Root
+    while ((Walker != Root) && ((OtherAtom == NULL) || (ShortestPathList[OtherAtom->nr] < MinimumRingSize[Walker->GetTrueFather()->nr]))) {  // look for Root
       Walker = BFSStack->PopFirst();
       //*out << Verbose(2) << "Current Walker is " << *Walker << ", we look for SP to Root " << *Root << "." << endl;
@@ -1684,5 +1687,5 @@
             ShortestPathList[OtherAtom->nr] = ShortestPathList[Walker->nr]+1;
             //*out << Verbose(2) << "Coloring OtherAtom " << OtherAtom->Name << " lightgray, its predecessor is " << Walker->Name << " and its Shortest Path is " << ShortestPathList[OtherAtom->nr] << " egde(s) long." << endl; 
-            if (ShortestPathList[OtherAtom->nr] < MinimumRingSize) { // Check for maximum distance
+            if (ShortestPathList[OtherAtom->nr] < MinimumRingSize[Walker->GetTrueFather()->nr]) { // Check for maximum distance
               //*out << Verbose(3) << "Putting OtherAtom into queue." << endl;
               BFSStack->Push(OtherAtom);
@@ -1711,8 +1714,15 @@
       }
       *out << Walker->Name << "  with a length of " << RingSize << "." << endl << endl;
-      if (RingSize < MinimumRingSize)
-        MinimumRingSize = RingSize;
+      // walk through all and set MinimumRingSize
+      Walker = Root;
+      while (Walker != BackEdge->rightatom) {
+        Walker = PredecessorList[Walker->nr];
+        if (RingSize < MinimumRingSize[Walker->GetTrueFather()->nr])
+          MinimumRingSize[Walker->GetTrueFather()->nr] = RingSize;
+      }
+      if ((RingSize < MinRingSize) || (MinRingSize == -1))
+        MinRingSize = RingSize;
     } else {
-      *out << Verbose(1) << "No ring with length equal to or smaller than " << MinimumRingSize << " found." << endl;
+      *out << Verbose(1) << "No ring containing " << *Root << " with length equal to or smaller than " << MinimumRingSize[Walker->GetTrueFather()->nr] << " found." << endl;
     }
     
@@ -1725,8 +1735,61 @@
     }
   }
+  // go over all atoms 
+  Root = start;
+  while(Root->next != end) {
+    Root = Root->next;
+    
+    if (MinimumRingSize[Root->GetTrueFather()->nr] == AtomCount) { // check whether MinimumRingSize is set, if not BFS to next where it is
+      ShortestPathList[Walker->nr] = 0;
+      BFSStack->ClearStack();  // start with empty BFS stack
+      BFSStack->Push(Walker);
+      TouchedStack->Push(Walker);
+      //*out << Verbose(1) << "---------------------------------------------------------------------------------------------------------" << endl;
+      OtherAtom = Walker;
+      while ((Walker != Root) && (OtherAtom != NULL)) {  // look for Root
+        Walker = BFSStack->PopFirst();
+        //*out << Verbose(2) << "Current Walker is " << *Walker << ", we look for SP to Root " << *Root << "." << endl;
+        for(int i=0;i<NumberOfBondsPerAtom[Walker->nr];i++) {
+          Binder = ListOfBondsPerAtom[Walker->nr][i];
+          if (Binder != BackEdge) { // only walk along DFS spanning tree (otherwise we always find SP of one being backedge Binder)
+            OtherAtom = Binder->GetOtherAtom(Walker);
+            //*out << Verbose(2) << "Current OtherAtom is: " << OtherAtom->Name << " for bond " << *Binder << "." << endl;
+            if (ColorList[OtherAtom->nr] == white) {
+              TouchedStack->Push(OtherAtom);
+              ColorList[OtherAtom->nr] = lightgray;
+              PredecessorList[OtherAtom->nr] = Walker;  // Walker is the predecessor
+              ShortestPathList[OtherAtom->nr] = ShortestPathList[Walker->nr]+1;
+              //*out << Verbose(2) << "Coloring OtherAtom " << OtherAtom->Name << " lightgray, its predecessor is " << Walker->Name << " and its Shortest Path is " << ShortestPathList[OtherAtom->nr] << " egde(s) long." << endl;
+              if (MinimumRingSize[OtherAtom->GetTrueFather()->nr] != AtomCount) { // if the other atom is connected to a ring
+                MinimumRingSize[Root->GetTrueFather()->nr] = ShortestPathList[OtherAtom->nr]+MinimumRingSize[OtherAtom->GetTrueFather()->nr];
+                OtherAtom = NULL; //break;
+                break;
+              } else
+                BFSStack->Push(OtherAtom);
+            } else {
+              //*out << Verbose(3) << "Not Adding, has already been visited." << endl;
+            }
+          } else {
+            //*out << Verbose(3) << "Not Visiting, is a back edge." << endl;
+          }
+        }
+        ColorList[Walker->nr] = black;
+        //*out << Verbose(1) << "Coloring Walker " << Walker->Name << " black." << endl; 
+      }
+
+      // now clean the lists
+      while (!TouchedStack->IsEmpty()){
+        Walker = TouchedStack->PopFirst();
+        PredecessorList[Walker->nr] = NULL;
+        ShortestPathList[Walker->nr] = -1;
+        ColorList[Walker->nr] = white;
+      }
+    }
+    *out << Verbose(1) << "Minimum ring size of " << *Root << " is " << MinimumRingSize[Root->GetTrueFather()->nr] << "." << endl;
+  }
+  
         
-
-  if (MinimumRingSize != -1)
-    *out << Verbose(1) << "Minimum ring size is " << MinimumRingSize << ", over " << NumCycles << " cycles total." << endl;
+  if (MinRingSize != -1)
+    *out << Verbose(1) << "Minimum ring size is " << MinRingSize << ", over " << NumCycles << " cycles total." << endl;
   else
     *out << Verbose(1) << "No rings were detected in the molecular structure." << endl;
@@ -2148,47 +2211,113 @@
   bool status = false;
   ifstream InputFile;
-  stringstream line;
+
+  // initialize mask list
+  for(int i=0;i<AtomCount;i++)
+    AtomMask[i] = false;
   
   if (Order < 0) { // adaptive increase of BondOrder per site
-    for(int i=0;i<AtomCount;i++)
-      AtomMask[i] = false;
+    if (AtomMask[AtomCount] == true)  // break after one step
+      return false;
     // parse the EnergyPerFragment file
-    line.str(path);
-    line << "/" << FRAGMENTPREFIX << ENERGYPERFRAGMENT;
-    InputFile.open(line.str().c_str(), ios::in);
-    if (InputFile != NULL) {
-      char *buffer = (char *) Malloc(sizeof(char)*MAXSTRINGSIZE, "molecule::CheckOrderAtSite: *buffer");
+    char *buffer = (char *) Malloc(sizeof(char)*MAXSTRINGSIZE, "molecule::CheckOrderAtSite: *buffer");
+    sprintf(buffer, "%s/%s%s.dat", path, FRAGMENTPREFIX, ENERGYPERFRAGMENT);
+    InputFile.open(buffer, ios::in);
+    if ((InputFile != NULL) && (GlobalKeySetList != NULL)) {
+      // transmorph graph keyset list into indexed KeySetList
+      map<int,KeySet> IndexKeySetList;
+      for(Graph::iterator runner = GlobalKeySetList->begin(); runner != GlobalKeySetList->end(); runner++) {
+        IndexKeySetList.insert( pair<int,KeySet>(runner->second.first,runner->first) );
+      }
       int lines = 0;
       // count the number of lines, i.e. the number of fragments
+      InputFile.getline(buffer, MAXSTRINGSIZE); // skip comment lines
+      InputFile.getline(buffer, MAXSTRINGSIZE);
       while(!InputFile.eof()) {
         InputFile.getline(buffer, MAXSTRINGSIZE);
         lines++;
       }
+      *out << Verbose(2) << "Scanned " << lines-1 << " lines." << endl;   // one endline too much
       InputFile.clear();
       InputFile.seekg(ios::beg);
-      map<double, int> FragmentEnergies;
-      int No;
-      int Value;
+      map<int, pair<double,int> > AdaptiveCriteriaList;  // (Root No., (Value, Order)) !
+      int No, FragOrder;
+      double Value;
       // each line represents a fragment root (Atom::nr) id and its energy contribution
+      InputFile.getline(buffer, MAXSTRINGSIZE); // skip comment lines
+      InputFile.getline(buffer, MAXSTRINGSIZE);
       while(!InputFile.eof()) {
         InputFile.getline(buffer, MAXSTRINGSIZE);
-        InputFile >> No;
-        InputFile >> Value;
-        FragmentEnergies.insert( make_pair(Value, No) );
-      }
-      for(map<double, int>::iterator runner = FragmentEnergies.lower_bound(pow(1.,Order)); runner != FragmentEnergies.begin(); runner++) {
-        // as the smallest number in each set has always been the root (we use global id to keep the doubles away), seek smallest and insert into AtomMask
-        AtomMask[(*runner).second] = true;
+        if (strlen(buffer) > 2) {
+          //*out << Verbose(2) << "Scanning: " << buffer;
+          stringstream line(buffer);
+          line >> FragOrder;
+          line >> ws >> No;
+          line >> ws >> Value; // skip time entry
+          line >> ws >> Value;
+          No -= 1;  // indices start at 1 in file, not 0 
+          //*out << Verbose(2) << " - yields (" << No << "," << Value << ")" << endl;
+
+          // clean the list of those entries that have been superceded by higher order terms already
+          map<int,KeySet>::iterator marker = IndexKeySetList.find(No);    // find keyset to Frag No.
+          if (marker != IndexKeySetList.end()) {  // if found
+            // as the smallest number in each set has always been the root (we use global id to keep the doubles away), seek smallest and insert into AtomMask
+            pair <map<int, pair<double,int> >::iterator, bool> InsertedElement = AdaptiveCriteriaList.insert( make_pair(*((*marker).second.begin()), pair<double,int>( Value, Order) ));
+            map<int, pair<double,int> >::iterator PresentItem = InsertedElement.first; 
+            if (!InsertedElement.second) { // this root is already present
+              if ((*PresentItem).second.second < FragOrder)  // if order there is lower, update entry with higher-order term 
+                //if ((*PresentItem).second.first < (*runner).first)    // as higher-order terms are not always better, we skip this part (which would always include this site into adaptive increase) 
+                {  // if value is smaller, update value and order
+                (*PresentItem).second.first = Value;
+                (*PresentItem).second.second = FragOrder;
+                *out << Verbose(2) << "Updated element (" <<  (*PresentItem).first << ",[" << (*PresentItem).second.first << "," << (*PresentItem).second.second << "])." << endl;
+              }
+            } else {
+              *out << Verbose(2) << "Inserted element (" <<  (*PresentItem).first << ",[" << (*PresentItem).second.first << "," << (*PresentItem).second.second << "])." << endl;
+            }
+          } else {
+            *out << Verbose(1) << "No Fragment under No. " << No << "found." << endl;
+          }
+        }
+      }
+      // then map back onto (Value, (Root Nr., Order)) (i.e. sorted by value to pick the highest ones)
+      map<double, pair<int,int> > FinalRootCandidates;
+      *out << Verbose(1) << "Root candidate list is: " << endl;
+      for(map<int, pair<double,int> >::iterator runner = AdaptiveCriteriaList.begin(); runner != AdaptiveCriteriaList.end(); runner++) {
+        Walker = FindAtom((*runner).first);
+        if (Walker != NULL) {
+          if ((*runner).second.second >= Walker->AdaptiveOrder) { // only insert if this is an "active" root site for the current order 
+            *out << Verbose(2) << "(" << (*runner).first << ",[" << (*runner).second.first << "," << (*runner).second.second << "])" << endl;
+            FinalRootCandidates.insert( make_pair( (*runner).second.first, pair<int,int>((*runner).first, (*runner).second.second) ) );
+          }
+        } else {
+          cerr << "Atom No. " << (*runner).second.first << " was not found in this molecule." << endl;
+        }
+      }
+      // pick the ones still below threshold and mark as to be adaptively updated
+      for(map<double, pair<int,int> >::iterator runner = FinalRootCandidates.upper_bound(pow(10.,Order)); runner != FinalRootCandidates.end(); runner++) {
+        No = (*runner).second.first;
+        *out << Verbose(2) << "Root " << No << " is still above threshold (10^{" << Order <<"}: " << runner->first << ", setting entry " << No << " of Atom mask to true." << endl; 
+        AtomMask[No] = true;
+        status = true;
       }
       // close and done
       InputFile.close();
       InputFile.clear();
-      Free((void **)&buffer, "molecule::CheckOrderAtSite: *buffer");
     } else {
-      cerr << "Unable to parse " << FRAGMENTPREFIX << ENERGYPERFRAGMENT << " file." << endl;
-      return false;
-    }
+      cerr << "Unable to parse " << buffer << " file, incrementing all." << endl;
+      while (Walker->next != end) {
+        Walker = Walker->next;
+    #ifdef ADDHYDROGEN
+        if (Walker->type->Z != 1) // skip hydrogen
+    #endif
+        {
+          AtomMask[Walker->nr] = true;  // include all (non-hydrogen) atoms
+          status = true;
+        }
+      }
+    }
+    Free((void **)&buffer, "molecule::CheckOrderAtSite: *buffer");
     // pick a given number of highest values and set AtomMask
-  } else if (Order > 0) { // global increase of Bond Order
+  } else { // global increase of Bond Order
     while (Walker->next != end) {
       Walker = Walker->next;
@@ -2198,17 +2327,23 @@
       {
         AtomMask[Walker->nr] = true;  // include all (non-hydrogen) atoms
-        if (Walker->AdaptiveOrder < Order)
+        if ((Order != 0) && (Walker->AdaptiveOrder < Order))
           status = true;
       }
     }
+    if ((Order == 0) && (AtomMask[AtomCount] == true))  // single stepping, just check
+      status = false;
+      
     if (!status)
       *out << Verbose(1) << "Order at every site is already equal or above desired order " << Order << "." << endl;
-  } else {  // single stepping, just check
-    if (AtomMask[AtomCount] != true)  // if true, we would've done a step already
-      for(int i=0;i<=AtomCount;i++)
-        AtomMask[i] = true;
-    else
-      status = false;
-  }
+  }
+  
+  // print atom mask for debugging
+  *out << "              ";
+  for(int i=0;i<AtomCount;i++)
+    *out << (i % 10);
+  *out << endl << "Atom mask is: ";
+  for(int i=0;i<AtomCount;i++)
+    *out << (AtomMask[i] ? "t" : "f");
+  *out << endl;
  
   return status;
@@ -2268,5 +2403,5 @@
 	MoleculeListClass *BondFragments = NULL;
   int *SortIndex = NULL;
-  int MinimumRingSize;
+  int *MinimumRingSize = NULL;
   int FragmentCounter;
   MoleculeLeafClass *MolecularWalker = NULL;
@@ -2315,43 +2450,37 @@
   
   // =================================== Begin of FRAGMENTATION =============================== 
-  // check cyclic lengths
-  if ((MinimumRingSize != -1) && (Order >= MinimumRingSize)) {
-    *out << Verbose(0) << "Bond order " << Order << " greater than or equal to Minimum Ring size of " << MinimumRingSize << " found is not allowed." << endl;
-  } else {
-    // ===== 6a. assign each keyset to its respective subgraph ===== 
-    Subgraphs->next->AssignKeySetsToFragment(out, this, ParsedFragmentList, ListOfLocalAtoms, FragmentList, (FragmentCounter = 0), false);
-
-    KeyStack *RootStack = new KeyStack[Subgraphs->next->Count()];
-    AtomMask = new bool[AtomCount+1];
-    for (int i=0;i<AtomCount;i++) // need to set to false to recognize in single-stepping (one global step set them all to true)
-      AtomMask[i] = false;
-    while (CheckOrderAtSite(out, AtomMask, ParsedFragmentList, Order)) {
-      AtomMask[AtomCount] = true;   // last plus one entry is used as marker that we have been through this loop once already in CheckOrderAtSite()
-      // ===== 6b. fill RootStack for each subgraph (second adaptivity check) ===== 
-      Subgraphs->next->FillRootStackForSubgraphs(out, RootStack, AtomMask, Order, (FragmentCounter = 0));
-
-      // ===== 7. fill the bond fragment list =====
-      FragmentCounter = 0;
-      MolecularWalker = Subgraphs;
-      while (MolecularWalker->next != NULL) {
-        MolecularWalker = MolecularWalker->next;
-        *out << Verbose(1) << "Fragmenting subgraph " << MolecularWalker << "." << endl;
-        // output ListOfBondsPerAtom for debugging
-        MolecularWalker->Leaf->OutputListOfBonds(out);
-        if (MolecularWalker->Leaf->first->next != MolecularWalker->Leaf->last) {
-        
-          // call BOSSANOVA method
-          *out << Verbose(0) << endl << " ========== BOND ENERGY of subgraph " << FragmentCounter << " ========================= " << endl;
-          MolecularWalker->Leaf->FragmentBOSSANOVA(out, FragmentList[FragmentCounter], RootStack[FragmentCounter]);
-        } else {
-          cerr << "Subgraph " << MolecularWalker << " has no atoms!" << endl;
-        }
-        FragmentCounter++;  // next fragment list
-      }
-    }
-    delete[](RootStack);
-    delete[](AtomMask);
-  }
+  // ===== 6a. assign each keyset to its respective subgraph ===== 
+  Subgraphs->next->AssignKeySetsToFragment(out, this, ParsedFragmentList, ListOfLocalAtoms, FragmentList, (FragmentCounter = 0), false);
+
+  KeyStack *RootStack = new KeyStack[Subgraphs->next->Count()];
+  AtomMask = new bool[AtomCount+1];
+  while (CheckOrderAtSite(out, AtomMask, ParsedFragmentList, Order, configuration->configpath)) {
+    AtomMask[AtomCount] = true;   // last plus one entry is used as marker that we have been through this loop once already in CheckOrderAtSite()
+    // ===== 6b. fill RootStack for each subgraph (second adaptivity check) ===== 
+    Subgraphs->next->FillRootStackForSubgraphs(out, RootStack, AtomMask, (FragmentCounter = 0));
+
+    // ===== 7. fill the bond fragment list =====
+    FragmentCounter = 0;
+    MolecularWalker = Subgraphs;
+    while (MolecularWalker->next != NULL) {
+      MolecularWalker = MolecularWalker->next;
+      *out << Verbose(1) << "Fragmenting subgraph " << MolecularWalker << "." << endl;
+      // output ListOfBondsPerAtom for debugging
+      MolecularWalker->Leaf->OutputListOfBonds(out);
+      if (MolecularWalker->Leaf->first->next != MolecularWalker->Leaf->last) {
+      
+        // call BOSSANOVA method
+        *out << Verbose(0) << endl << " ========== BOND ENERGY of subgraph " << FragmentCounter << " ========================= " << endl;
+        MolecularWalker->Leaf->FragmentBOSSANOVA(out, FragmentList[FragmentCounter], RootStack[FragmentCounter], MinimumRingSize);
+      } else {
+        cerr << "Subgraph " << MolecularWalker << " has no atoms!" << endl;
+      }
+      FragmentCounter++;  // next fragment list
+    }
+  }
+  delete[](RootStack);
+  delete[](AtomMask);
   delete(ParsedFragmentList);
+  delete[](MinimumRingSize);
   
   // free the index lookup list
@@ -3627,7 +3756,8 @@
  * \param Fragment&*List list of already present keystacks (adaptive scheme) or empty list
  * \param &RootStack stack with all root candidates (unequal to each atom in complete molecule if adaptive scheme is applied)
+ * \param *MinimumRingSize minimum ring size for each atom (molecule::Atomcount)
  * \return pointer to Graph list
  */
-void molecule::FragmentBOSSANOVA(ofstream *out, Graph *&FragmentList, KeyStack &RootStack)
+void molecule::FragmentBOSSANOVA(ofstream *out, Graph *&FragmentList, KeyStack &RootStack, int *MinimumRingSize)
 {
   Graph ***FragmentLowerOrdersList = NULL;
@@ -3675,85 +3805,90 @@
     RootKeyNr = RootStack.front();
     RootStack.pop_front();
-    // increase adaptive order by one
     Walker = FindAtom(RootKeyNr);
-    Walker->GetTrueFather()->AdaptiveOrder++;
-    Order = Walker->AdaptiveOrder = Walker->GetTrueFather()->AdaptiveOrder;
-
-    // initialise Order-dependent entries of UniqueFragments structure
-    FragmentSearch.BondsPerSPList = (bond **) Malloc(sizeof(bond *)*Order*2, "molecule::PowerSetGenerator: ***BondsPerSPList");
-    FragmentSearch.BondsPerSPCount = (int *) Malloc(sizeof(int)*Order, "molecule::PowerSetGenerator: *BondsPerSPCount");
-    for (int i=0;i<Order;i++) {
-      FragmentSearch.BondsPerSPList[2*i] = new bond();    // start node
-      FragmentSearch.BondsPerSPList[2*i+1] = new bond();  // end node
-      FragmentSearch.BondsPerSPList[2*i]->next = FragmentSearch.BondsPerSPList[2*i+1];     // intertwine these two 
-      FragmentSearch.BondsPerSPList[2*i+1]->previous = FragmentSearch.BondsPerSPList[2*i];
-      FragmentSearch.BondsPerSPCount[i] = 0;
-    }  
-
-    // allocate memory for all lower level orders in this 1D-array of ptrs
-    NumLevels = 1 << (Order-1); // (int)pow(2,Order);
-    FragmentLowerOrdersList[RootNr] = (Graph **) Malloc(sizeof(Graph *)*NumLevels, "molecule::FragmentBOSSANOVA: **FragmentLowerOrdersList[]");
-  
-    // create top order where nothing is reduced
-    *out << Verbose(0) << "==============================================================================================================" << endl;
-    *out << Verbose(0) << "Creating KeySets of Bond Order " << Order << " for " << *Walker << ", NumLevels is " << NumLevels << ", " << (RootStack.size()-RootNr-1) << " Roots remaining." << endl;
-
-    // Create list of Graphs of current Bond Order (i.e. F_{ij})
-    FragmentLowerOrdersList[RootNr][0] =  new Graph;
-    FragmentSearch.TEFactor = 1.;
-    FragmentSearch.Leaflet = FragmentLowerOrdersList[RootNr][0];      // set to insertion graph
-    FragmentSearch.Root = Walker;
-    NumMoleculesOfOrder[RootNr] = PowerSetGenerator(out, Walker->AdaptiveOrder, FragmentSearch, CompleteMolecule);
-    *out << Verbose(1) << "Number of resulting KeySets is: " << NumMoleculesOfOrder[RootNr] << "." << endl;
-    NumMolecules = 0;
+    // check cyclic lengths
+    if ((MinimumRingSize[Walker->GetTrueFather()->nr] != -1) && (Walker->GetTrueFather()->AdaptiveOrder+1 >= MinimumRingSize[Walker->GetTrueFather()->nr])) {
+      *out << Verbose(0) << "Bond order " << Walker->GetTrueFather()->AdaptiveOrder << " of Root " << *Walker << " greater than or equal to Minimum Ring size of " << MinimumRingSize << " found is not allowed." << endl;
+    } else {
+      // increase adaptive order by one
+      Walker->GetTrueFather()->AdaptiveOrder++;
+      Order = Walker->AdaptiveOrder = Walker->GetTrueFather()->AdaptiveOrder;
+  
+      // initialise Order-dependent entries of UniqueFragments structure
+      FragmentSearch.BondsPerSPList = (bond **) Malloc(sizeof(bond *)*Order*2, "molecule::PowerSetGenerator: ***BondsPerSPList");
+      FragmentSearch.BondsPerSPCount = (int *) Malloc(sizeof(int)*Order, "molecule::PowerSetGenerator: *BondsPerSPCount");
+      for (int i=0;i<Order;i++) {
+        FragmentSearch.BondsPerSPList[2*i] = new bond();    // start node
+        FragmentSearch.BondsPerSPList[2*i+1] = new bond();  // end node
+        FragmentSearch.BondsPerSPList[2*i]->next = FragmentSearch.BondsPerSPList[2*i+1];     // intertwine these two 
+        FragmentSearch.BondsPerSPList[2*i+1]->previous = FragmentSearch.BondsPerSPList[2*i];
+        FragmentSearch.BondsPerSPCount[i] = 0;
+      }  
+  
+      // allocate memory for all lower level orders in this 1D-array of ptrs
+      NumLevels = 1 << (Order-1); // (int)pow(2,Order);
+      FragmentLowerOrdersList[RootNr] = (Graph **) Malloc(sizeof(Graph *)*NumLevels, "molecule::FragmentBOSSANOVA: **FragmentLowerOrdersList[]");
     
-    if ((NumLevels >> 1) > 0) {
-      // create lower order fragments
-      *out << Verbose(0) << "Creating list of unique fragments of lower Bond Order terms to be subtracted." << endl;
-      Order = Walker->AdaptiveOrder;
-      for (int source=0;source<(NumLevels >> 1);source++) { // 1-terms don't need any more splitting, that's why only half is gone through (shift again)
-        // step down to next order at (virtual) boundary of powers of 2 in array
-        while (source >= (1 << (Walker->AdaptiveOrder-Order))) // (int)pow(2,Walker->AdaptiveOrder-Order))    
-          Order--;
-        *out << Verbose(0) << "Current Order is: " << Order << "." << endl;
-        for (int SubOrder=Order-1;SubOrder>0;SubOrder--) {
-          int dest = source + (1 << (Walker->AdaptiveOrder-(SubOrder+1)));
-          *out << Verbose(0) << "--------------------------------------------------------------------------------------------------------------" << endl;
-          *out << Verbose(0) << "Current SubOrder is: " << SubOrder << " with source " << source << " to destination " << dest << "." << endl;
-    
-          // every molecule is split into a list of again (Order - 1) molecules, while counting all molecules
-          //*out << Verbose(1) << "Splitting the " << (*FragmentLowerOrdersList[RootNr][source]).size() << " molecules of the " << source << "th cell in the array." << endl;
-          //NumMolecules = 0;
-          FragmentLowerOrdersList[RootNr][dest] = new Graph;
-          for(Graph::iterator runner = (*FragmentLowerOrdersList[RootNr][source]).begin();runner != (*FragmentLowerOrdersList[RootNr][source]).end(); runner++) {
-            for (KeySet::iterator sprinter = (*runner).first.begin();sprinter != (*runner).first.end(); sprinter++) {
-              Graph TempFragmentList;
-              FragmentSearch.TEFactor = -(*runner).second.second;
-              FragmentSearch.Leaflet = &TempFragmentList;      // set to insertion graph
-              FragmentSearch.Root = FindAtom(*sprinter);
-              NumMoleculesOfOrder[RootNr] += PowerSetGenerator(out, SubOrder, FragmentSearch, (*runner).first);
-              // insert new keysets FragmentList into FragmentLowerOrdersList[Walker->AdaptiveOrder-1][dest]
-              *out << Verbose(1) << "Merging resulting key sets with those present in destination " << dest << "." << endl;
-              InsertGraphIntoGraph(out, *FragmentLowerOrdersList[RootNr][dest], TempFragmentList, &NumMolecules);
+      // create top order where nothing is reduced
+      *out << Verbose(0) << "==============================================================================================================" << endl;
+      *out << Verbose(0) << "Creating KeySets of Bond Order " << Order << " for " << *Walker << ", NumLevels is " << NumLevels << ", " << (RootStack.size()-RootNr-1) << " Roots remaining." << endl;
+  
+      // Create list of Graphs of current Bond Order (i.e. F_{ij})
+      FragmentLowerOrdersList[RootNr][0] =  new Graph;
+      FragmentSearch.TEFactor = 1.;
+      FragmentSearch.Leaflet = FragmentLowerOrdersList[RootNr][0];      // set to insertion graph
+      FragmentSearch.Root = Walker;
+      NumMoleculesOfOrder[RootNr] = PowerSetGenerator(out, Walker->AdaptiveOrder, FragmentSearch, CompleteMolecule);
+      *out << Verbose(1) << "Number of resulting KeySets is: " << NumMoleculesOfOrder[RootNr] << "." << endl;
+      NumMolecules = 0;
+      
+      if ((NumLevels >> 1) > 0) {
+        // create lower order fragments
+        *out << Verbose(0) << "Creating list of unique fragments of lower Bond Order terms to be subtracted." << endl;
+        Order = Walker->AdaptiveOrder;
+        for (int source=0;source<(NumLevels >> 1);source++) { // 1-terms don't need any more splitting, that's why only half is gone through (shift again)
+          // step down to next order at (virtual) boundary of powers of 2 in array
+          while (source >= (1 << (Walker->AdaptiveOrder-Order))) // (int)pow(2,Walker->AdaptiveOrder-Order))    
+            Order--;
+          *out << Verbose(0) << "Current Order is: " << Order << "." << endl;
+          for (int SubOrder=Order-1;SubOrder>0;SubOrder--) {
+            int dest = source + (1 << (Walker->AdaptiveOrder-(SubOrder+1)));
+            *out << Verbose(0) << "--------------------------------------------------------------------------------------------------------------" << endl;
+            *out << Verbose(0) << "Current SubOrder is: " << SubOrder << " with source " << source << " to destination " << dest << "." << endl;
+      
+            // every molecule is split into a list of again (Order - 1) molecules, while counting all molecules
+            //*out << Verbose(1) << "Splitting the " << (*FragmentLowerOrdersList[RootNr][source]).size() << " molecules of the " << source << "th cell in the array." << endl;
+            //NumMolecules = 0;
+            FragmentLowerOrdersList[RootNr][dest] = new Graph;
+            for(Graph::iterator runner = (*FragmentLowerOrdersList[RootNr][source]).begin();runner != (*FragmentLowerOrdersList[RootNr][source]).end(); runner++) {
+              for (KeySet::iterator sprinter = (*runner).first.begin();sprinter != (*runner).first.end(); sprinter++) {
+                Graph TempFragmentList;
+                FragmentSearch.TEFactor = -(*runner).second.second;
+                FragmentSearch.Leaflet = &TempFragmentList;      // set to insertion graph
+                FragmentSearch.Root = FindAtom(*sprinter);
+                NumMoleculesOfOrder[RootNr] += PowerSetGenerator(out, SubOrder, FragmentSearch, (*runner).first);
+                // insert new keysets FragmentList into FragmentLowerOrdersList[Walker->AdaptiveOrder-1][dest]
+                *out << Verbose(1) << "Merging resulting key sets with those present in destination " << dest << "." << endl;
+                InsertGraphIntoGraph(out, *FragmentLowerOrdersList[RootNr][dest], TempFragmentList, &NumMolecules);
+              }
             }
+            *out << Verbose(1) << "Number of resulting molecules for SubOrder " << SubOrder << " is: " << NumMolecules << "." << endl;
           }
-          *out << Verbose(1) << "Number of resulting molecules for SubOrder " << SubOrder << " is: " << NumMolecules << "." << endl;
         }
       }
-    }
-    // now, we have completely filled each cell of FragmentLowerOrdersList[] for the current Walker->AdaptiveOrder
-    //NumMoleculesOfOrder[Walker->AdaptiveOrder-1] = NumMolecules;
-    TotalNumMolecules += NumMoleculesOfOrder[RootNr];
-    *out << Verbose(1) << "Number of resulting molecules for Order " << (int)Walker->GetTrueFather()->AdaptiveOrder << " is: " << NumMoleculesOfOrder[RootNr] << "." << endl;
-    RootStack.push_back(RootKeyNr); // put back on stack
-    RootNr++;
-
-    // free Order-dependent entries of UniqueFragments structure for next loop cycle
-    Free((void **)&FragmentSearch.BondsPerSPCount, "molecule::PowerSetGenerator: *BondsPerSPCount");
-    for (int i=0;i<Order;i++) {
-      delete(FragmentSearch.BondsPerSPList[2*i]);
-      delete(FragmentSearch.BondsPerSPList[2*i+1]);
-    }  
-    Free((void **)&FragmentSearch.BondsPerSPList, "molecule::PowerSetGenerator: ***BondsPerSPList");
+      // now, we have completely filled each cell of FragmentLowerOrdersList[] for the current Walker->AdaptiveOrder
+      //NumMoleculesOfOrder[Walker->AdaptiveOrder-1] = NumMolecules;
+      TotalNumMolecules += NumMoleculesOfOrder[RootNr];
+      *out << Verbose(1) << "Number of resulting molecules for Order " << (int)Walker->GetTrueFather()->AdaptiveOrder << " is: " << NumMoleculesOfOrder[RootNr] << "." << endl;
+      RootStack.push_back(RootKeyNr); // put back on stack
+      RootNr++;
+  
+      // free Order-dependent entries of UniqueFragments structure for next loop cycle
+      Free((void **)&FragmentSearch.BondsPerSPCount, "molecule::PowerSetGenerator: *BondsPerSPCount");
+      for (int i=0;i<Order;i++) {
+        delete(FragmentSearch.BondsPerSPList[2*i]);
+        delete(FragmentSearch.BondsPerSPList[2*i+1]);
+      }  
+      Free((void **)&FragmentSearch.BondsPerSPList, "molecule::PowerSetGenerator: ***BondsPerSPList");
+    }
   }
   *out << Verbose(0) << "==============================================================================================================" << endl;
Index: src/molecules.hpp
===================================================================
--- src/molecules.hpp	(revision de293ac530466215e29d973ad379a2e65165a40c)
+++ src/molecules.hpp	(revision fc850d10e0fcb47d27b1b6baf17186a20debf1ea)
@@ -326,6 +326,6 @@
   
   // Graph analysis
-  MoleculeLeafClass * DepthFirstSearchAnalysis(ofstream *out, bool ReturnStack, int &MinimumRingSize);
-  void CyclicStructureAnalysis(ofstream *out, class StackClass<bond *> *BackEdgeStack, int &MinimumRingSize);
+  MoleculeLeafClass * DepthFirstSearchAnalysis(ofstream *out, bool ReturnStack, int *&MinimumRingSize);
+  void CyclicStructureAnalysis(ofstream *out, class StackClass<bond *> *BackEdgeStack, int *&MinimumRingSize);
   bond * FindNextUnused(atom *vertex);
   void SetNextComponentNumber(atom *vertex, int nr);
@@ -353,5 +353,5 @@
   void BreadthFirstSearchAdd(ofstream *out, molecule *Mol, atom **&AddedAtomList, bond **&AddedBondList, atom *Root, bond *Bond, int BondOrder, bool IsAngstroem);
   /// -# BOSSANOVA
-  void FragmentBOSSANOVA(ofstream *out, Graph *&FragmentList, KeyStack &RootStack);
+  void FragmentBOSSANOVA(ofstream *out, Graph *&FragmentList, KeyStack &RootStack, int *MinimumRingSize);
 	int PowerSetGenerator(ofstream *out, int Order, struct UniqueFragments &FragmentSearch, KeySet RestrictedKeySet);
   bool BuildInducedSubgraph(ofstream *out, const molecule *Father);
@@ -413,5 +413,5 @@
   bool AddLeaf(molecule *ptr, MoleculeLeafClass *Previous);
   bool FillBondStructureFromReference(ofstream *out, molecule *reference, int &FragmentCounter, atom ***&ListOfLocalAtoms, bool FreeList = false);
-  bool FillRootStackForSubgraphs(ofstream *out, KeyStack *&RootStack, bool *AtomMask, int Order, int &FragmentCounter);
+  bool FillRootStackForSubgraphs(ofstream *out, KeyStack *&RootStack, bool *AtomMask, int &FragmentCounter);
   bool AssignKeySetsToFragment(ofstream *out, molecule *reference, Graph *KeySetList, atom ***&ListOfLocalAtoms, Graph **&FragmentList, int &FragmentCounter, bool FreeList = false);
   bool FillListOfLocalAtoms(ofstream *out, atom ***&ListOfLocalAtoms, int &FragmentCounter, int GlobalAtomCount, bool &FreeList);
