Graph C++ Library
 All Classes Namespaces Files Functions Variables Typedefs Groups Pages
base_weighted_undirected_graph.hpp
Go to the documentation of this file.
1 
10 #ifndef BASE_WEIGHTED_UNDIRECTED_GRAPH_HPP_INCLUDED
11 #define BASE_WEIGHTED_UNDIRECTED_GRAPH_HPP_INCLUDED
12 
13 // Standard Library
14 #include <algorithm> // set_intersection
15 #include <cstdlib> // abort
16 #include <iostream>
17 #include <list>
18 #include <set>
19 #include <string>
20 #include <vector>
21 // Graph C++ Library
23 
25 namespace gcl
26 {
27 
36  template <class VertexType>
38  {
39  // ============================================================================================
40  // *** Information about the graph.
41  private:
42  // std::vector<double> strength_distribution; ///< Distribution of the strength of vertices.
43  double min_strength;
44  double max_strength;
45  double avg_strength;
46  // std::vector<double> weight_distribution; ///< Distribution of the weights of edges.
47  double min_weight;
48  double max_weight;
49  double avg_weight;
50  // ============================================================================================
51  // *** Member functions.
52  public:
55  double get_min_strength();
56  double get_max_strength();
57  double get_avg_strength();
58  double get_min_weight();
59  double get_max_weight();
60  double get_avg_weight();
61  void write_edgelist(std::string _name);
65  virtual void write_graph_properties(std::string _name) = 0;
66  virtual void write_vertices_properties(std::string _name) = 0;
67  protected:
69  };
70 
71 } // End of namespace gcl.
72 
73 
74 
75 // ================================================================================================
76 // ================================================================================================
77 template <class VertexType>
79 {
80  return min_strength;
81 }
82 
83 // ================================================================================================
84 // ================================================================================================
85 template <class VertexType>
87 {
88  return max_strength;
89 }
90 
91 // ================================================================================================
92 // ================================================================================================
93 template <class VertexType>
95 {
96  return avg_strength;
97 }
98 
99 // ================================================================================================
100 // ================================================================================================
101 template <class VertexType>
103 {
104  return min_weight;
105 }
106 
107 // ================================================================================================
108 // ================================================================================================
109 template <class VertexType>
111 {
112  return max_weight;
113 }
114 
115 // ================================================================================================
116 // ================================================================================================
117 template <class VertexType>
119 {
120  return avg_weight;
121 }
122 
123 // ================================================================================================
124 // ================================================================================================
125 template <class VertexType>
127 {
128  // Stream objects.
129  std::ofstream edgelist_file;
130  // String objects.
131  std::string edgelist_filename = _name + "_edgelist.dat";
132  std::string node1_str;
133  // Operator objects.
134  typename VertexType::iterator it, end;
135  // Opens the stream and aborts if the operation did not succeed.
136  edgelist_file.open(edgelist_filename.c_str(), std::ios_base::out);
137  if( !edgelist_file.is_open() )
138  {
139  std::cout << "Could not open file: " << edgelist_filename << "." << std::endl;
140  abort();
141  }
142  // Writes each edges in the format "node1 node2". Each edge appears once.
143  for(unsigned int node1(0), node2, nn(this->get_nb_vertices()); node1<nn; ++node1)
144  {
145  // Browses the neighbours of node1.
146  it = (*this)(node1)->neighbour_begin();
147  end = (*this)(node1)->neighbour_end();
148  node1_str = (*this)(node1)->get_name();
149  for(; it!=end; ++it)
150  {
151  node2 = it->id;
152  if(node1 < node2) // Ensures that undirected edges appear only once (excludes self-edges).
153  {
154  edgelist_file << node1_str << " " << (*this)(node2)->get_name() << " " << it->weight << std::endl;
155  }
156  }
157  }
158  // Closes the stream.
159  edgelist_file.close();
160 }
161 
162 // ================================================================================================
163 // ================================================================================================
164 template <class VertexType>
166 {
167  // Degree distributions (in- and out- degrees).
168  this->survey_degrees_distribution();
169  // Identifies the triangles.
170  this->survey_triangles();
171  // Computes the clustering coefficients.
172  this->compute_clustering_coefficients();
173  // Surveys the strength distribution.
174  this->survey_strength_distribution();
175  // Surveys the weight distribution.
176  this->survey_weight_distribution();
177 }
178 
179 // ================================================================================================
180 // ================================================================================================
181 template <class VertexType>
183 {
184  // Double objects.
185  double s;
186  // Resizes and sets the initial values of min_strength, max_strength and avg_strength.
187  min_strength = this->get_nb_vertices();
188  max_strength = 0;
189  avg_strength = 0;
190  // Sets the average, maximal and minimal strength.
191  for(unsigned int n(0), nn(this->get_nb_vertices()); n<nn; ++n)
192  {
193  // Gets the strength of the vertex n.
194  s = (*this)(n)->get_strength();
195  // Average strength (1 of 2).
196  avg_strength += s;
197  // Maximal and minimal strength.
198  if(s < min_strength)
199  min_strength = s;
200  if(s > max_strength)
201  max_strength = s;
202  }
203  // Average strength (2 of 2).
204  avg_strength /= this->get_nb_vertices();
205 }
206 
207 // ================================================================================================
208 // ================================================================================================
209 template <class VertexType>
211 {
212  // Double objects.
213  double w;
214  // Iterator objects.
215  typename VertexType::iterator it, end;
216  // Resizes and sets the initial values of min_weight, max_weight and avg_weight.
217  min_weight = this->get_nb_vertices();
218  max_weight = 0;
219  avg_weight = 0;
220  // Sets the average, maximal and minimal weight.
221  for(unsigned int v1(0), v2, nn(this->get_nb_vertices()); v1<nn; ++v1)
222  {
223  // Sets the iterator objects.
224  it = (*this)(v1)->neighbour_begin();
225  end = (*this)(v1)->neighbour_end();
226  // Loops over all neighbours of vertex n.
227  for(; it!=end; ++it)
228  {
229  // Gets the id of the neighbour.
230  v2 = it->id;
231  // Considers the edge only if v2 > v1 (ensures to count each edge only once).
232  if( v2 > v1 )
233  {
234  // Gets the weight of the edge.
235  w = it->weight;
236  // Average weight (1 of 2).
237  avg_weight += w;
238  // Maximal and minimal weight.
239  if(w < min_weight)
240  min_weight = w;
241  if(w > max_weight)
242  max_weight = w;
243  }
244  }
245  }
246  // Average weight (2 of 2).
247  avg_weight /= this->get_nb_edges();
248 }
249 
250 // ================================================================================================
251 // ================================================================================================
252 template <class VertexType>
254 {
255  // Resets the variables inherited from base_graph.
256  this->base_undirected_graph_clear();
257  // Resets the strength distribution and related quantities.
258  // strength_distribution.clear();
259  min_strength = 0;
260  max_strength = 0;
261  avg_strength = 0;
262  // Resets the weight distribution and related quantities.
263  // weight_distribution.clear();
264  min_weight = 0;
265  max_weight = 0;
266  avg_weight = 0;
267 }
268 
269 
270 
271 #endif // BASE_WEIGHTED_UNDIRECTED_GRAPH_HPP_INCLUDED
void survey_weight_distribution()
Fills the weight distribution and related quantities.
Definition: base_weighted_undirected_graph.hpp:210
double get_min_weight()
Returns the minimal weight found in the graph.
Definition: base_weighted_undirected_graph.hpp:102
double get_max_strength()
Returns the maximal strength found in the graph.
Definition: base_weighted_undirected_graph.hpp:86
virtual void write_graph_properties(std::string _name)=0
Exports the graph's properties.
Virtual base class for undirected graphs.
double get_max_weight()
Returns the maximal weight found in the graph.
Definition: base_weighted_undirected_graph.hpp:110
Virtual template base class for undirected graph objects.
Definition: base_undirected_graph.hpp:37
virtual ~base_weighted_undirected_graph()
Destructor.
Definition: base_weighted_undirected_graph.hpp:54
virtual void write_vertices_properties(std::string _name)=0
Exports the vertices' properties.
void write_edgelist(std::string _name)
Exports the directed graph to an edgelist.
Definition: base_weighted_undirected_graph.hpp:126
double get_avg_strength()
Returns the average strength found in the graph.
Definition: base_weighted_undirected_graph.hpp:94
void analyse_structural_properties()
Computes a set of structural properties.
Definition: base_weighted_undirected_graph.hpp:165
void survey_strength_distribution()
Fills the strength distribution and related quantities.
Definition: base_weighted_undirected_graph.hpp:182
double get_avg_weight()
Returns the average weight found in the graph.
Definition: base_weighted_undirected_graph.hpp:118
void base_weighted_undirected_graph_clear()
Reinitializes the graph (inherited variables).
Definition: base_weighted_undirected_graph.hpp:253
double get_min_strength()
Returns the minimal strength found in the graph.
Definition: base_weighted_undirected_graph.hpp:78
base_weighted_undirected_graph()
Constructor.
Definition: base_weighted_undirected_graph.hpp:53
Virtual template base class for weighted undirected graph objects.
Definition: base_weighted_undirected_graph.hpp:37