10 #ifndef BASE_UNDIRECTED_GRAPH_HPP_INCLUDED
11 #define BASE_UNDIRECTED_GRAPH_HPP_INCLUDED
36 template <
class VertexType>
42 const unsigned int id_degree;
46 unsigned int nb_wedges;
47 unsigned int nb_triangles;
48 double global_clustering_coefficient;
49 double avg_local_clustering_coefficient;
50 std::list< std::vector<unsigned int> > triangles;
80 template <
class VertexType>
85 template <
class VertexType>
88 return this->get_base_graph_min_degrees(id_degree);
93 template <
class VertexType>
96 return this->get_base_graph_max_degrees(id_degree);
101 template <
class VertexType>
104 return this->get_base_graph_avg_degrees(id_degree);
109 template <
class VertexType>
117 template <
class VertexType>
125 template <
class VertexType>
128 return global_clustering_coefficient;
133 template <
class VertexType>
136 return avg_local_clustering_coefficient;
141 template <
class VertexType>
145 std::ofstream edgelist_file;
147 std::string edgelist_filename = _name +
"_edgelist.dat";
148 std::string node1_str;
150 typename VertexType::iterator it, end;
152 edgelist_file.open(edgelist_filename.c_str(), std::ios_base::out);
153 if( !edgelist_file.is_open() )
155 std::cout <<
"Could not open file: " << edgelist_filename <<
"." << std::endl;
159 for(
unsigned int node1(0), node2, nn(this->get_nb_vertices()); node1<nn; ++node1)
162 it = (*this)(node1)->neighbour_begin();
163 end = (*this)(node1)->neighbour_end();
164 node1_str = (*this)(node1)->get_name();
170 edgelist_file << node1_str <<
" " << (*this)(node2)->get_name() << std::endl;
175 edgelist_file.close();
193 template <
class VertexType>
197 std::vector<unsigned int> intersection;
199 std::set<unsigned int> neighbours_v1, neighbours_v2;
201 std::vector<unsigned int>::iterator it;
202 typename VertexType::iterator it1, end1, it2, end2;
204 for(
unsigned int v1(0), v2, v3, d1, d2, nn(this->get_nb_vertices()); v1<nn; ++v1)
207 d1 = (*this)(v1)->get_degree();
212 it1 = (*this)(v1)->neighbour_begin();
213 end1 = (*this)(v1)->neighbour_end();
214 neighbours_v1.clear();
215 for(; it1 != end1; ++it1)
217 neighbours_v1.insert(it1->id);
219 it1 = (*this)(v1)->neighbour_begin();
221 for(; it1!=end1; ++it1)
225 d2 = (*this)(v2)->get_degree();
227 if( v2 > v1 && d2 > 1 )
230 it2 = (*this)(v2)->neighbour_begin();
231 end2 = (*this)(v2)->neighbour_end();
232 neighbours_v2.clear();
233 for(; it2 != end2; ++it2)
238 neighbours_v2.insert(v3);
241 d2 = neighbours_v2.size();
243 intersection.clear();
244 intersection.resize(std::min(d1,d2));
245 it = std::set_intersection(neighbours_v1.begin(), neighbours_v1.end(), neighbours_v2.begin(), neighbours_v2.end(), intersection.begin());
246 intersection.resize(it-intersection.begin());
248 for(
unsigned int n(0), nn(intersection.size()); n<nn; ++n)
251 v3 = intersection[n];
256 triangles.push_back({v1,v2,v3});
267 template <
class VertexType>
271 unsigned int v1, v2, v3;
272 std::list< std::vector<unsigned int> >::iterator it = triangles.begin();
273 std::list< std::vector<unsigned int> >::iterator end = triangles.end();
274 for(
unsigned int v; it != end; ++it)
277 for(
unsigned int i(0); i<3; ++i)
280 (*this)(v)->set_nb_triangles((*
this)(v)->get_nb_triangles() + 1);
284 for(
unsigned int n(0), d, nn(this->get_nb_vertices()); n<nn; ++n)
287 d = (*this)(n)->get_degree();
288 nb_wedges += d * ( d - 1. ) / 2.;
290 avg_local_clustering_coefficient += (*this)(n)->get_local_clustering_coefficient();
292 avg_local_clustering_coefficient /= this->get_nb_vertices();
294 nb_triangles = triangles.size();
296 global_clustering_coefficient = 3. * nb_triangles / nb_wedges;
301 template <
class VertexType>
305 this->set_nb_of_types_of_degrees(1);
307 this->base_graph_clear();
311 global_clustering_coefficient = 0;
312 avg_local_clustering_coefficient = 0;
318 #endif // BASE_UNDIRECTED_GRAPH_HPP_INCLUDED
double get_avg_degree()
Returns the average degree found in the graph.
Definition: base_undirected_graph.hpp:102
virtual ~base_undirected_graph()
Destructor.
Definition: base_undirected_graph.hpp:55
unsigned int get_nb_wedges()
Returns the number of wegdes in the graph.
Definition: base_undirected_graph.hpp:118
double get_avg_local_clustering_coefficient()
Returns the average value of the local clustering coefficients.
Definition: base_undirected_graph.hpp:134
base_undirected_graph()
Constructor.
Definition: base_undirected_graph.hpp:81
void write_edgelist(std::string _name)
Exports the directed graph to an edgelist.
Definition: base_undirected_graph.hpp:142
void base_undirected_graph_clear()
Reinitializes the graph (inherited variables).
Definition: base_undirected_graph.hpp:302
unsigned int get_nb_triangles()
Returns the number of triangles in the graph.
Definition: base_undirected_graph.hpp:110
Virtual template base class for undirected graph objects.
Definition: base_undirected_graph.hpp:37
void survey_triangles()
Creates a list of the triangles in the graph.
Definition: base_undirected_graph.hpp:194
unsigned int get_min_degree()
Returns the minimal degree found in the graph.
Definition: base_undirected_graph.hpp:86
Virtual template base class for graph objects.
Definition: base_graph.hpp:33
virtual void write_graph_properties(std::string _name)=0
Exports the graph's properties.
double get_global_clustering_coefficient()
Returns the global clustering coefficient (3 x [number of triangles] / [number of wedges])...
Definition: base_undirected_graph.hpp:126
virtual void write_vertices_properties(std::string _name)=0
Exports the vertices' properties.
void compute_clustering_coefficients()
Computes the clustering coefficients (individual and global).
Definition: base_undirected_graph.hpp:268
unsigned int get_max_degree()
Returns the maximal degree found in the graph.
Definition: base_undirected_graph.hpp:94
Virtual base class for graphs.