[6e5907] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2017 Frederik Heber. All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | *
|
---|
| 7 | * This file is part of MoleCuilder.
|
---|
| 8 | *
|
---|
| 9 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
| 10 | * it under the terms of the GNU General Public License as published by
|
---|
| 11 | * the Free Software Foundation, either version 2 of the License, or
|
---|
| 12 | * (at your option) any later version.
|
---|
| 13 | *
|
---|
| 14 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 17 | * GNU General Public License for more details.
|
---|
| 18 | *
|
---|
| 19 | * You should have received a copy of the GNU General Public License
|
---|
| 20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | /*
|
---|
| 24 | * BreadthFirstSearchGatherer.cpp
|
---|
| 25 | *
|
---|
| 26 | * Created on: May 17, 2017
|
---|
| 27 | * Author: heber
|
---|
| 28 | */
|
---|
| 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 "BreadthFirstSearchGatherer.hpp"
|
---|
| 39 |
|
---|
| 40 | #include "CodePatterns/Log.hpp"
|
---|
| 41 |
|
---|
| 42 | #include "Graph/BoostGraphCreator.hpp"
|
---|
| 43 |
|
---|
| 44 | #include <boost/graph/breadth_first_search.hpp>
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * I have no idea why this is so complicated with BGL ...
|
---|
| 48 | *
|
---|
| 49 | * This is taken from the book "The Boost Graph Library: User Guide and Reference Manual, Portable Documents",
|
---|
| 50 | * chapter "Basic Graph Algorithms", example on calculating the bacon number.
|
---|
| 51 | */
|
---|
| 52 | template <typename DistanceMap>
|
---|
| 53 | class distance_recorder : public boost::default_bfs_visitor
|
---|
| 54 | {
|
---|
| 55 | public:
|
---|
| 56 | distance_recorder(DistanceMap dist) : d(dist) {}
|
---|
| 57 |
|
---|
| 58 | template <typename Edge, typename Graph>
|
---|
| 59 | void tree_edge(Edge e, const Graph &g) const {
|
---|
| 60 | typename boost::graph_traits<Graph>::vertex_descriptor u = source(e,g), v = target(e,g);
|
---|
| 61 | d[v] = d[u] + 1;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | private:
|
---|
| 65 | DistanceMap d;
|
---|
| 66 | };
|
---|
| 67 |
|
---|
| 68 | template <typename DistanceMap>
|
---|
| 69 | distance_recorder<DistanceMap> record_distance(DistanceMap d)
|
---|
| 70 | {
|
---|
| 71 | return distance_recorder<DistanceMap>(d);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | BreadthFirstSearchGatherer::BreadthFirstSearchGatherer(BoostGraphCreator &_BGcreator) :
|
---|
| 75 | BGcreator(_BGcreator)
|
---|
| 76 | {}
|
---|
| 77 |
|
---|
| 78 | std::vector<atomId_t> BreadthFirstSearchGatherer::operator()(const atomId_t &_discoverfrom)
|
---|
| 79 | {
|
---|
| 80 | std::vector<atomId_t> returnids;
|
---|
| 81 |
|
---|
| 82 | // Do BFS through graph from given vertex
|
---|
| 83 | const size_t num_vertices = BGcreator.getNumVertices();
|
---|
| 84 | distances_t distances(num_vertices, num_vertices+1); // set distance to num+1
|
---|
| 85 | const BoostGraphCreator::UndirectedGraph &BGgraph = BGcreator.get();
|
---|
| 86 | const BoostGraphCreator::nodeId_t discoverfrom_nodeid = BGcreator.getNodeId(_discoverfrom);
|
---|
| 87 | distances[ boost::vertex(discoverfrom_nodeid, BGgraph) ] = 0;
|
---|
| 88 | boost::breadth_first_search(
|
---|
| 89 | BGgraph,
|
---|
| 90 | boost::vertex(discoverfrom_nodeid, BGgraph),
|
---|
| 91 | boost::visitor(record_distance(&distances[0])));
|
---|
| 92 | LOG(3, "DEBUG: From atom #" << _discoverfrom
|
---|
| 93 | << " BFS discovered the following distances " << distances);
|
---|
| 94 |
|
---|
| 95 | // any node was discovered whose distances is less than num_vertices+1
|
---|
| 96 | const BoostGraphCreator::const_name_map_t name_map = boost::get(boost::vertex_name, BGgraph);
|
---|
| 97 | BoostGraphCreator::vertex_iter vp, vpend;
|
---|
| 98 | for (boost::tie(vp, vpend) = vertices(BGgraph); vp != vpend; ++vp) {
|
---|
| 99 | BoostGraphCreator::Vertex v = *vp;
|
---|
| 100 | if (distances[v] != (num_vertices+1)) {
|
---|
| 101 | returnids.push_back( boost::get(name_map, v) );
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | return returnids;
|
---|
| 106 | }
|
---|