source: src/Graph/BreadthFirstSearchGatherer.cpp@ 3dedfbd

ForceAnnealing_oldresults IndependentFragmentGrids_IntegrationTest
Last change on this file since 3dedfbd was 6e5907, checked in by Frederik Heber <frederik.heber@…>, 8 years ago

Extracted extraction (subset of) nodes from BoostGraph into BreadthFirstSearchGatherer.

  • also added helper namespace BoostGraphHelpers.
  • we now treat the vertex indices and vertex names properly. Before that they had to coincide. Now, the name is the atomic id associated with the node and the index is the boost::graph internal index.
  • Property mode set to 100644
File size: 3.3 KB
Line 
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 */
52template <typename DistanceMap>
53class distance_recorder : public boost::default_bfs_visitor
54{
55public:
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
64private:
65 DistanceMap d;
66};
67
68template <typename DistanceMap>
69distance_recorder<DistanceMap> record_distance(DistanceMap d)
70{
71 return distance_recorder<DistanceMap>(d);
72}
73
74BreadthFirstSearchGatherer::BreadthFirstSearchGatherer(BoostGraphCreator &_BGcreator) :
75 BGcreator(_BGcreator)
76{}
77
78std::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}
Note: See TracBrowser for help on using the repository browser.