votca 2024.2-dev
Loading...
Searching...
No Matches
graphvisitor.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team
3 * (http://www.votca.org)
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License")
6 *
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20// Standard includes
21#include <exception>
22#include <iostream>
23#include <vector>
24
25// Local VOTCA includes
26#include "votca/tools/edge.h"
27#include "votca/tools/graph.h"
29
30using namespace std;
31
32namespace votca {
33namespace tools {
34
35class GraphNode;
36
37bool GraphVisitor::queEmpty() const { return true; }
38
39void GraphVisitor::exploreNode(pair<Index, GraphNode>& vertex_and_node, Graph&,
40 Edge) {
41 explored_.insert(vertex_and_node.first);
42}
43
44vector<Index> GraphVisitor::getUnexploredVertex(const Edge edge) const {
45 vector<Index> unexp_vert;
46 if (explored_.count(edge.getEndPoint1()) == 0) {
47 unexp_vert.push_back(edge.getEndPoint1());
48 }
49 if (explored_.count(edge.getEndPoint2()) == 0) {
50 unexp_vert.push_back(edge.getEndPoint2());
51 }
52 return unexp_vert;
53}
54
55bool GraphVisitor::vertexExplored(const Index vertex) const {
56 return explored_.count(vertex) == 1;
57}
58
60 vector<Edge> neigh_eds = graph.getNeighEdges(startingVertex_);
61 GraphNode graph_node = graph.getNode(startingVertex_);
62 pair<Index, GraphNode> vertex_and_graph_node(startingVertex_, graph_node);
63 exploreNode(vertex_and_graph_node, graph);
65}
66
67void GraphVisitor::exec(Graph& graph, Edge edge) {
68 vector<Index> unexp_vert = getUnexploredVertex(edge);
69 // If no vertices are return than just ignore it means the same
70 // vertex was explored from a different direction
71 if (!unexp_vert.size()) {
72 return;
73 }
74 // If two values are returned this is a problem
75 if (unexp_vert.size() > 1) {
76 throw runtime_error(
77 "More than one unexplored vertex in an edge,"
78 " did you set the starting node");
79 }
80
81 pair<Index, GraphNode> vertex_and_node(unexp_vert.at(0),
82 graph.getNode(unexp_vert.at(0)));
83
84 exploreNode(vertex_and_node, graph, edge);
85}
86
88
89 // Get the edge and at the same time remove it from whatever queue it is in
90
91 Edge edge = getEdge_();
92 vector<Index> unexplored_vertices = getUnexploredVertex(edge);
93 // Do not add neighboring edges if they belong to a vertex that has already
94 // been explored because they will have already been added
95 if (unexplored_vertices.size()) {
96 addEdges_(graph, unexplored_vertices.at(0));
97 }
98 return edge;
99}
100
101set<Index> GraphVisitor::getExploredVertices() const { return explored_; }
102
103} // namespace tools
104} // namespace votca
Connects to vertices.
Definition edge.h:42
Index getEndPoint1() const
grab the smaller integer
Definition edge.h:55
Index getEndPoint2() const
grab the larger integer
Definition edge.h:57
A graph node that will take a variety of different values.
Definition graphnode.h:46
virtual void exec(Graph &graph, Edge edge)
std::vector< Index > getUnexploredVertex(const Edge edge) const
Determine which vertices in the edge, if any, have not been explored.
std::set< Index > getExploredVertices() const
Get the set of all the vertices that have been explored.
virtual Edge getEdge_()=0
Index startingVertex_
The vertex the visitor started on.
virtual bool queEmpty() const
virtual void addEdges_(const Graph &graph, Index vertex)=0
What is done to an individual graph node as it is explored.
virtual void exploreNode(std::pair< Index, GraphNode > &vertex_and_node, Graph &graph, Edge edge=DUMMY_EDGE)
Edge(0,0) is a dummy value.
Edge nextEdge(Graph graph)
std::set< Index > explored_
set containing all the vertix ids that have been explored
void initialize(Graph &graph)
Initialize the graphvisitor the default starting point is 0.
bool vertexExplored(const Index vertex) const
Has the vertex been explored.
GraphNode getNode(const Index vertex) const
Return a copy of the graph node at vertex 'vert'.
Definition graph.cc:97
std::vector< Edge > getNeighEdges(Index vertex) const
Returns all the edges in the graph connected to vertex vertex
Definition graph.h:106
STL namespace.
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26