votca 2024-dev
Loading...
Searching...
No Matches
graph.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 <algorithm>
22#include <cassert>
23#include <string>
24
25// Local VOTCA includes
26#include "votca/tools/graph.h"
27
28using namespace std;
29
30namespace votca {
31namespace tools {
32
33class GraphNode;
34
35bool nodeForEveryVertex_(vector<Index> vertices,
36 unordered_map<Index, GraphNode> nodes) {
37 for (const Index vertex : vertices) {
38 if (nodes.count(vertex) == 0) {
39 return false;
40 }
41 }
42 return true;
43}
44
45Graph::Graph(vector<Edge> edges, unordered_map<Index, GraphNode> nodes) {
47 vector<Index> vertices = edge_container_.getVertices();
48 assert(nodeForEveryVertex_(vertices, nodes) &&
49 "A node must exist for every vertex.");
50 nodes_ = nodes;
51 for (const pair<Index, GraphNode> id_and_node : nodes_) {
52 if (edge_container_.vertexExist(id_and_node.first) == false) {
53 edge_container_.addVertex(id_and_node.first);
54 }
55 }
56 calcId_();
57}
58
59bool Graph::operator!=(const Graph& graph) const {
60 return id_.compare(graph.id_);
61}
62
63bool Graph::operator==(const Graph& graph) const { return !(*(this) != graph); }
64
65vector<pair<Index, GraphNode>> Graph::getIsolatedNodes(void) const {
66 vector<pair<Index, GraphNode>> isolated_nodes;
67 vector<Index> vertices_degree_0 = edge_container_.getVerticesDegree(0);
68
69 for (const Index vertex : vertices_degree_0) {
70 pair<Index, GraphNode> id_and_node{vertex, nodes_.at(vertex)};
71 isolated_nodes.push_back(id_and_node);
72 }
73 return isolated_nodes;
74}
75
76vector<pair<Index, GraphNode>> Graph::getNeighNodes(Index vertex) const {
77 vector<Index> neigh_vertices = edge_container_.getNeighVertices(vertex);
78 vector<pair<Index, GraphNode>> neigh_ids_and_nodes;
79 for (const Index& neigh_vert : neigh_vertices) {
80 auto id_and_node =
81 pair<Index, GraphNode>(neigh_vert, nodes_.at(neigh_vert));
82 neigh_ids_and_nodes.push_back(id_and_node);
83 }
84 return neigh_ids_and_nodes;
85}
86
87void Graph::setNode(Index vertex, GraphNode& graph_node) {
88 assert(nodes_.count(vertex) && "Can only set a node that already exists");
89 nodes_[vertex] = graph_node;
90 calcId_();
91}
92
93void Graph::setNode(std::pair<Index, GraphNode>& id_and_node) {
94 setNode(id_and_node.first, id_and_node.second);
95}
96
97GraphNode Graph::getNode(const Index vertex) const {
98 assert(nodes_.count(vertex));
99 return nodes_.at(vertex);
100}
101
102vector<pair<Index, GraphNode>> Graph::getNodes(void) const {
103 vector<pair<Index, GraphNode>> vec_nodes;
104 for (const pair<const Index, GraphNode>& id_and_node : nodes_) {
105 vec_nodes.push_back(id_and_node);
106 }
107 return vec_nodes;
108}
109
110vector<Index> Graph::getJunctions() const {
111 vector<Index> junctions;
112 Index max_degree = edge_container_.getMaxDegree();
113 for (Index degree = 3; degree <= max_degree; ++degree) {
114 vector<Index> vertices = edge_container_.getVerticesDegree(degree);
115 junctions.insert(junctions.end(), vertices.begin(), vertices.end());
116 }
117 return junctions;
118}
119
120void Graph::clearNodes() { nodes_.clear(); }
121
123 assert(nodes_.size() == 0);
124 for (const pair<const Index, GraphNode>& id_and_node : graph.nodes_) {
125 this->nodes_[id_and_node.first] = id_and_node.second;
126 }
127}
128
130 auto nodes = getNodes();
131 sort(nodes.begin(), nodes.end(), cmpVertNodePair);
132 string struct_Id_temp = "";
133 for (const pair<Index, GraphNode>& id_and_node : nodes) {
134 struct_Id_temp.append(id_and_node.second.getStringId());
135 }
136 id_ = struct_Id_temp;
137 return;
138}
139
141 if (edge_container_.vertexExist(vertex)) {
142 return edge_container_.getDegree(vertex);
143 }
144 if (nodes_.count(vertex)) {
145 return 0;
146 }
147 throw invalid_argument(
148 "vertex does not exist within the graph the degree is "
149 "not defined.");
150}
151
152bool Graph::vertexExist(Index vertex) const {
153 if (edge_container_.vertexExist(vertex)) {
154 return true;
155 }
156 if (nodes_.count(vertex)) {
157 return true;
158 }
159 return false;
160}
161
162vector<Index> Graph::getVerticesDegree(Index degree) const {
163 return edge_container_.getVerticesDegree(degree);
164}
165
166vector<Index> Graph::getVertices() const {
168}
169
170ostream& operator<<(ostream& os, const Graph graph) {
171 os << "Graph" << endl;
172 for (const pair<const Index, GraphNode>& id_and_node : graph.nodes_) {
173 os << "Node " << id_and_node.first << endl;
174 os << id_and_node.second << endl;
175 }
176 return os;
177}
178
179bool cmpVertNodePair(const pair<Index, GraphNode>& id_and_node1,
180 const pair<Index, GraphNode>& id_and_node2) {
181 string str1_Id = id_and_node1.second.getStringId();
182 return str1_Id.compare(id_and_node2.second.getStringId()) < 0;
183}
184} // namespace tools
185} // namespace votca
EdgeContainer is responsible for operations on groups of edges.
std::vector< Index > getVerticesDegree(Index degree) const
Contains vector of all vertices with degree.
Index getDegree(const Index vertex) const
Determine the degree of the vertex/number of edges attached.
void addVertex(Index vertex)
Add a lone vertex.
std::vector< Index > getVertices() const
Get all the vertices in vector form.
std::vector< Index > getNeighVertices(Index vertex) const
Get the vertices neighboring vert.
bool vertexExist(Index vertex) const
Check if the vertex exists returns true or false.
Index getMaxDegree() const
Get the value of the max degree.
A graph node that will take a variety of different values.
Definition graphnode.h:46
std::vector< std::pair< Index, GraphNode > > getIsolatedNodes(void) const
Definition graph.cc:65
void copyNodes(Graph &graph)
Definition graph.cc:122
GraphNode getNode(const Index vertex) const
Return a copy of the graph node at vertex 'vert'.
Definition graph.cc:97
std::vector< Index > getJunctions() const
Gets all vertices with degree of 3 or greater.
Definition graph.cc:110
void setNode(Index vertex, GraphNode &graph_node)
set the Node associated with vertex 'vert'
Definition graph.cc:87
std::string id_
Definition graph.h:51
std::unordered_map< Index, GraphNode > nodes_
Definition graph.h:47
std::vector< Index > getVertices() const
Returns all the vertices in the graph.
Definition graph.cc:166
virtual std::vector< Index > getVerticesDegree(Index degree) const
Returns all the vertices with degree specified by degree
Definition graph.cc:162
EdgeContainer edge_container_
Definition graph.h:45
bool operator!=(const Graph &graph) const
Definition graph.cc:59
void calcId_()
Calculate the id of the graph.
Definition graph.cc:129
std::vector< std::pair< Index, GraphNode > > getNeighNodes(Index vertex) const
Definition graph.cc:76
bool operator==(const Graph &graph) const
Definition graph.cc:63
void clearNodes()
Remove contents of all nodes.
Definition graph.cc:120
virtual std::vector< std::pair< Index, GraphNode > > getNodes() const
Return all the vertices and their graph nodes that are within the graph.
Definition graph.cc:102
Index getDegree(Index vertex) const
Calcualtes the degree, or number of edges connected to vertex vertex
Definition graph.cc:140
bool vertexExist(Index vertex) const
Determines if a vertex exists within the graph.
Definition graph.cc:152
STL namespace.
bool cmpVertNodePair(const std::pair< Index, GraphNode > &id_and_node1, const std::pair< Index, GraphNode > &id_and_node2)
Compare function pair<Index ,GraphNode> object.
std::ostream & operator<<(std::ostream &out, const Correlate &c)
Definition correlate.h:53
bool nodeForEveryVertex_(vector< Index > vertices, unordered_map< Index, GraphNode > nodes)
Definition graph.cc:35
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26