Graph C++ Library
 All Classes Namespaces Files Functions Variables Typedefs Groups Pages
base_graph.hpp
Go to the documentation of this file.
1 
10 #ifndef BASE_GRAPH_HPP_INCLUDED
11 #define BASE_GRAPH_HPP_INCLUDED
12 
13 // Standard Library
14 #include <algorithm> // set_intersection
15 #include <cstdlib> // abort
16 #include <iostream>
17 #include <set>
18 #include <string>
19 #include <vector>
20 
22 namespace gcl
23 {
24 
32  template <class VertexType>
33  class base_graph
34  {
35  // ============================================================================================
36  // *** Objects related to the graph.
37  private:
38  std::vector< VertexType > the_graph;
39  unsigned int nb_of_types_of_degrees;
40  // ============================================================================================
41  // *** Information about the graph.
42  private:
43  std::string type_of_graph;
44  unsigned int nb_vertices;
45  unsigned int nb_edges;
46  // std::vector< std::vector<unsigned int> > degrees_distribution; ///< Degrees distributions (in absolute number of vertices).
47  std::vector< unsigned int > min_degrees;
48  std::vector< unsigned int > max_degrees;
49  std::vector< double > avg_degrees;
50  unsigned int nb_zerodegree_vertices;
51  // ============================================================================================
52  // *** Status of the properties computed.
53  protected:
54  bool have_the_degree_distributions_been_surveyed;
55  // ============================================================================================
56  // *** Member functions.
57  public:
58  base_graph() {}
59  virtual ~base_graph() {}
60  std::string get_type_of_graph();
61  unsigned int get_nb_vertices();
62  unsigned int get_nb_edges();
63  unsigned int get_nb_zerodegree_vertices();
65  VertexType* operator()(unsigned int _id);
66  virtual void write_edgelist(std::string _name) = 0;
67  virtual void clear() = 0;
68  protected:
69  void set_type_of_graph(std::string _type_of_graph);
70  void set_nb_vertices(unsigned int _value);
71  void set_nb_edges(unsigned int _value);
72  void set_nb_of_types_of_degrees(unsigned int _value);
73  unsigned int get_base_graph_min_degrees(unsigned int _type);
74  unsigned int get_base_graph_max_degrees(unsigned int _type);
75  double get_base_graph_avg_degrees(unsigned int _type);
76  void base_graph_clear();
77  };
78 
79 } // End of namespace gcl.
80 
81 
82 
83 // ================================================================================================
84 // ================================================================================================
85 template <class VertexType>
87 {
88  return type_of_graph;
89 }
90 
91 // ================================================================================================
92 // ================================================================================================
93 template <class VertexType>
95 {
96  return nb_vertices;
97 }
98 
99 // ================================================================================================
100 // ================================================================================================
101 template <class VertexType>
103 {
104  return nb_edges;
105 }
106 
107 // ================================================================================================
108 // ================================================================================================
109 template <class VertexType>
111 {
112  return nb_zerodegree_vertices;
113 }
114 
115 // ================================================================================================
116 // ================================================================================================
117 template <class VertexType>
118 VertexType* gcl::base_graph<VertexType>::operator()(unsigned int _id)
119 {
120  return &the_graph[_id];
121 }
122 
123 // ================================================================================================
124 // ================================================================================================
125 template <class VertexType>
127 {
128  nb_vertices = _value;
129  the_graph.resize(nb_vertices);
130 }
131 
132 // ================================================================================================
133 // ================================================================================================
134 template <class VertexType>
136 {
137  nb_edges = _value;
138 }
139 
140 // ================================================================================================
141 // ================================================================================================
142 template <class VertexType>
143 void gcl::base_graph<VertexType>::set_type_of_graph(std::string _type_of_graph)
144 {
145  type_of_graph = _type_of_graph;
146 }
147 
148 // ================================================================================================
149 // ================================================================================================
150 template <class VertexType>
152 {
153  nb_of_types_of_degrees = _value;
154 }
155 
156 // ================================================================================================
157 // ================================================================================================
158 template <class VertexType>
160 {
161  return min_degrees[_type];
162 }
163 
164 // ================================================================================================
165 // ================================================================================================
166 template <class VertexType>
168 {
169  return max_degrees[_type];
170 }
171 
172 // ================================================================================================
173 // ================================================================================================
174 template <class VertexType>
176 {
177  return avg_degrees[_type];
178 }
179 
180 // ================================================================================================
181 // ================================================================================================
182 template <class VertexType>
184 {
185  // Integer objects.
186  unsigned int d, tot_degree;
187  // Resizes and sets the initial values of min_degrees, max_degrees and avg_degrees.
188  min_degrees.clear();
189  min_degrees.resize(nb_of_types_of_degrees,nb_vertices);
190  max_degrees.clear();
191  max_degrees.resize(nb_of_types_of_degrees,0);
192  avg_degrees.clear();
193  avg_degrees.resize(nb_of_types_of_degrees,0);
194  for(unsigned int n(0); n<nb_vertices; ++n)
195  {
196  // Resets the sum of the degree of the vertex.
197  tot_degree = 0;
198  // Sums the degree of the vertex.
199  for(unsigned int t(0); t<nb_of_types_of_degrees; ++t)
200  {
201  tot_degree += the_graph[n].get_base_vertex_degrees(t);
202  }
203  if(tot_degree == 0)
204  {
205  // Counts the number of nodes with no edges.
206  ++nb_zerodegree_vertices;
207  }
208  else
209  {
210  for(unsigned int t(0); t<nb_of_types_of_degrees; ++t)
211  {
212  // Gets the degree of the node (in-, out-).
213  d = the_graph[n].get_base_vertex_degrees(t);
214  // Average degree (1 of 2).
215  avg_degrees[t] += d;
216  // Maximal and minimal degrees.
217  if(d > 0)
218  if(d < min_degrees[t])
219  min_degrees[t] = d;
220  if(d > max_degrees[t])
221  max_degrees[t] = d;
222  }
223  }
224  }
225  // Average degree (2 of 2).
226  for(unsigned int t(0); t<nb_of_types_of_degrees; ++t)
227  {
228  avg_degrees[t] /= nb_vertices;
229  }
230 
231  // // Resize the degrees_distribution.
232  // degrees_distribution.resize(nb_of_types_of_degrees);
233  // // Populates the degree distributions.
234  // for(unsigned int t(0); t<nb_of_types_of_degrees; ++t)
235  // {
236  // // Resizes the degree distribution.
237  // degrees_distribution[t].resize(max_degrees[t]+1,0);
238  // // Fills the degree distribution.
239  // for(unsigned int n(0); n<nb_vertices; ++n)
240  // {
241  // // Gets the degree of the node (in-, out-).
242  // d = the_graph[n].get_base_vertex_degrees(t);
243  // // Adds the vertex to the degree distribution.
244  // degrees_distribution[t][d] += 1;
245  // }
246  // }
247 
248  // Indicates that the calculations has been done.
249  have_the_degree_distributions_been_surveyed = true;
250 }
251 
252 // ================================================================================================
253 // ================================================================================================
254 template <class VertexType>
256 {
257  // Clears the the_graph vector.
258  the_graph.clear();
259  // Resets the type of the graph.
260  type_of_graph = "Base graph";
261  // Resets the number of vertices.
262  nb_vertices = 0;
263  // Resets the number of edges.
264  nb_edges = 0;
265  // Resets the degres distribution and related quantities.
266  // degrees_distribution.clear();
267  // degrees_distribution.resize(nb_of_types_of_degrees);
268  min_degrees.clear();
269  min_degrees.resize(nb_of_types_of_degrees,0);
270  max_degrees.clear();
271  max_degrees.resize(nb_of_types_of_degrees,0);
272  avg_degrees.clear();
273  avg_degrees.resize(nb_of_types_of_degrees,0);
274  nb_zerodegree_vertices = 0;
275  have_the_degree_distributions_been_surveyed = false;
276 }
277 
278 
279 
280 #endif // BASE_GRAPH_HPP_INCLUDED
double get_base_graph_avg_degrees(unsigned int _type)
Returns the average degree found in the graph.
Definition: base_graph.hpp:175
unsigned int get_nb_zerodegree_vertices()
Returns the number of vertices with no edge.
Definition: base_graph.hpp:110
VertexType * operator()(unsigned int _id)
Returns a VertexType object corresponding to the _id-th vertex.
Definition: base_graph.hpp:118
virtual void write_edgelist(std::string _name)=0
Exports the undirected graph to an edgelist.
unsigned int get_base_graph_min_degrees(unsigned int _type)
Returns the minimal degree found in the graph.
Definition: base_graph.hpp:159
void base_graph_clear()
Reinitializes the graph (inherited variables).
Definition: base_graph.hpp:255
void survey_degrees_distribution()
Computes the quantities related to the degrees distribution.
Definition: base_graph.hpp:183
unsigned int get_nb_vertices()
Returns the number of vertices.
Definition: base_graph.hpp:94
Virtual template base class for graph objects.
Definition: base_graph.hpp:33
void set_nb_vertices(unsigned int _value)
Sets the number of vertices.
Definition: base_graph.hpp:126
virtual ~base_graph()
Destructor.
Definition: base_graph.hpp:59
virtual void clear()=0
Reinitializes the graph (specific variables).
void set_type_of_graph(std::string _type_of_graph)
Sets the type of graph.
Definition: base_graph.hpp:143
unsigned int get_nb_edges()
Returns the number of edges.
Definition: base_graph.hpp:102
std::string get_type_of_graph()
Returns the type of graph.
Definition: base_graph.hpp:86
void set_nb_edges(unsigned int _value)
Sets the number of edges.
Definition: base_graph.hpp:135
unsigned int get_base_graph_max_degrees(unsigned int _type)
Returns the maximal degree found in the graph.
Definition: base_graph.hpp:167
base_graph()
Constructor.
Definition: base_graph.hpp:58
void set_nb_of_types_of_degrees(unsigned int _value)
Sets the number of types of degrees.
Definition: base_graph.hpp:151