Graph C++ Library
 All Classes Namespaces Files Functions Variables Typedefs Groups Pages
real_directed_graph.hpp
Go to the documentation of this file.
1 
10 #ifndef REAL_DIRECTED_GRAPH_HPP_INCLUDED
11 #define REAL_DIRECTED_GRAPH_HPP_INCLUDED
12 
13 // Standard Library
14 #include <ctime> // time, gmtime
15 #include <cstdlib> // abort
16 #include <fstream>
17 #include <iostream>
18 #include <map>
19 #include <set>
20 #include <sstream>
21 #include <string>
22 #include <vector>
23 #include <utility> // pair, make_pair
24 // Graph C++ Library
25 #include "base_directed_graph.hpp"
26 #include "real_directed_vertex.hpp"
27 
29 namespace gcl
30 {
31 
41  class real_directed_graph : public gcl::base_directed_graph<gcl::real_directed_vertex>
42  {
43  // ============================================================================================
44  // *** Member functions.
45  public:
48  void read_edgelist(std::string _name);
49  void write_graph_properties(std::string _name);
50  void write_vertices_properties(std::string _name);
51  void clear();
52  };
53 
54 } // End of namespace gcl.
55 
56 
57 
58 // ================================================================================================
59 // ================================================================================================
61 {
62  clear();
63 }
64 
65 // ================================================================================================
66 // ================================================================================================
68 {
69  // Stream objects.
70  std::ifstream edgelist_file;
71  std::stringstream one_line;
72  // String objects.
73  std::string full_line, name1_str, name2_str;
74  // Integer objects.
75  unsigned int node_cnt, edge_cnt, node1_int, node2_int, edge_int;
76  // Map objects to assure the uniqueness of edges.
77  std::map< std::string, unsigned int > Name2ID;
78  std::map< unsigned int, std::string > ID2Name;
79  std::set< std::pair<unsigned int, unsigned int> > Edgelist;
80  // Iterator objects.
81  std::map< std::string,unsigned int >::iterator name_it;
82  std::set< std::pair<unsigned int,unsigned int> >::iterator edge_it, edge_end;
83  // Opens the stream and aborts if the operation did not succeed.
84  edgelist_file.open(_name.c_str(), std::ios_base::in);
85  if( !edgelist_file.is_open() )
86  {
87  std::cout << "Could not open file: " << _name << "." << std::endl;
88  abort();
89  }
90  // Reads the edgelist and registers the nodes and the edges.
91  node_cnt = 0;
92  edge_cnt = 0;
93  while( !edgelist_file.eof() )
94  {
95  // Reads a line of the edgelist.
96  std::getline(edgelist_file,full_line); edgelist_file >> std::ws;
97  one_line.str(full_line); one_line >> std::ws;
98  one_line >> name1_str >> std::ws;
99  one_line >> name2_str >> std::ws;
100  one_line.clear();
101  // edgelist_file >> name1_str >> std::ws;
102  // edgelist_file >> name2_str >> std::ws;
103  // Is name1 new?
104  name_it = Name2ID.find(name1_str);
105  if( name_it == Name2ID.end() )
106  {
107  Name2ID[name1_str] = node_cnt;
108  ID2Name[node_cnt] = name1_str;
109  node1_int = node_cnt;
110  ++node_cnt;
111  }
112  else
113  {
114  node1_int = name_it->second;
115  }
116  // Is name2 new?
117  name_it = Name2ID.find(name2_str);
118  if( name_it == Name2ID.end() )
119  {
120  Name2ID[name2_str] = node_cnt;
121  ID2Name[node_cnt] = name2_str;
122  node2_int = node_cnt;
123  ++node_cnt;
124  }
125  else
126  {
127  node2_int = name_it->second;
128  }
129  // Is this a new edge? (excludes self- and multi- edges).
130  if(node1_int != node2_int)
131  {
132  edge_it = Edgelist.find( std::pair<unsigned int,unsigned int>(node1_int,node2_int) );
133  if( edge_it == Edgelist.end() )
134  {
135  // Registers this new edge.
136  Edgelist.insert( std::make_pair(node1_int,node2_int) );
137  ++edge_cnt;
138  }
139  }
140  }
141  // Sets the number of vertices and edges.
142  this->set_nb_vertices(node_cnt);
143  this->set_nb_edges(edge_cnt);
144  // Populates the graph.
145  edge_it = Edgelist.begin();
146  edge_end = Edgelist.end();
147  for(unsigned int n(0), nn(this->get_nb_vertices()); n<nn; ++n)
148  {
149  // Sets the id of the vertex.
150  (*this)(n)->set_name(ID2Name[n]);
151  }
152  for(; edge_it!=edge_end; ++edge_it)
153  {
154  // Finds the vertices' identity.
155  node1_int = edge_it->first;
156  node2_int = edge_it->second;
157  // Creates the edge.
158  (*this)(node1_int)->out_neighbour_insert(gcl::simple_edge(node2_int));
159  (*this)(node2_int)->in_neighbour_insert(gcl::simple_edge(node1_int));
160  }
161  // Closes the stream.
162  edgelist_file.close();
163 }
164 
165 // ================================================================================================
166 // ================================================================================================
168 {
169  // Streams objects.
170  std::ofstream graph_properties_file;
171  // String objects.
172  std::string graph_properties_filename = _name + "_graph_properties.dat";
173  // Opens the stream and aborts if the operation did not succeed.
174  graph_properties_file.open(graph_properties_filename.c_str(), std::ios_base::out);
175  if( !graph_properties_file.is_open() )
176  {
177  std::cout << "Could not open file: " << _name << "." << std::endl;
178  abort();
179  }
180  // Gets the current date/time.
181  time_t theTime = time(NULL);
182  struct tm *aTime = gmtime(&theTime);
183  int year = aTime->tm_year + 1900;
184  int month = aTime->tm_mon + 1;
185  int day = aTime->tm_mday;
186  int hours = aTime->tm_hour;
187  int minutes = aTime->tm_min;
188  // Writes the properties of the graph.
189  graph_properties_file << "===================================================================================" << std::endl;
190  graph_properties_file << "General information." << std::endl;
191  graph_properties_file << "type of graph: " << this->get_type_of_graph() << std::endl;
192  graph_properties_file << "graph: " << _name << std::endl;
193  graph_properties_file << "computed on: " << year << "/";
194  if(month < 10)
195  graph_properties_file << "0";
196  graph_properties_file << month << "/";
197  if(day < 10)
198  graph_properties_file << "0";
199  graph_properties_file << day << " " << hours << ":";
200  if(minutes < 10)
201  graph_properties_file << "0";
202  graph_properties_file << minutes << " UTC" << std::endl;
203  graph_properties_file << "===================================================================================" << std::endl;
204  graph_properties_file << "Observables (excluding zero-degree vertices)" << std::endl;
205  graph_properties_file << "Number of nodes: " << this->get_nb_vertices()-this->get_nb_zerodegree_vertices() << std::endl;
206  graph_properties_file << "Number of edges: " << this->get_nb_edges() << std::endl;
207  graph_properties_file << " - reciprocals: " << this->get_nb_reciprocal_edges() << std::endl;
208  graph_properties_file << " - reciprocity: " << this->get_reciprocity() << std::endl;
209  graph_properties_file << "Undirected projection" << std::endl;
210  graph_properties_file << " - number of wedges: " << this->get_nb_undirected_projection_wedges() << std::endl;
211  graph_properties_file << " - number of triangles: " << this->get_nb_undirected_projection_triangles() << std::endl;
212  graph_properties_file << " - global clustering coefficient: " << this->get_global_undirected_projection_clustering_coefficient() << std::endl;
213  graph_properties_file << " - average local clustering coefficient: " << this->get_avg_local_undirected_projection_clustering_coefficient() << std::endl;
214  graph_properties_file << "In-degree distribution" << std::endl;
215  graph_properties_file << " - average in-degree: " << this->get_avg_in_degree() << std::endl;
216  graph_properties_file << " - minimum in-degree (excluding zero): " << this->get_min_in_degree() << std::endl;
217  graph_properties_file << " - maximum in-degree: " << this->get_max_in_degree() << std::endl;
218  graph_properties_file << "Out-degree distribution" << std::endl;
219  graph_properties_file << " - average out-degree: " << this->get_avg_out_degree() << std::endl;
220  graph_properties_file << " - minimum out-degree (excluding zero): " << this->get_min_out_degree() << std::endl;
221  graph_properties_file << " - maximum out-degree: " << this->get_max_out_degree() << std::endl;
222  // Closes the stream.
223  graph_properties_file.close();
224 }
225 
226 // ================================================================================================
227 // ================================================================================================
229 {
230  // Streams objects.
231  std::ofstream vertices_properties_file;
232  // String objects.
233  std::string vertices_properties_filename = _name + "_vertices_properties.dat";
234  // Opens the stream and aborts if the operation did not succeed.
235  vertices_properties_file.open(vertices_properties_filename.c_str(), std::ios_base::out);
236  if( !vertices_properties_file.is_open() )
237  {
238  std::cout << "Could not open file: " << _name << "." << std::endl;
239  abort();
240  }
241  // Writes the properties of each vertex.
242  vertices_properties_file.precision(8);
243  for(unsigned int n(0), nn(this->get_nb_vertices()); n<nn; ++n)
244  {
245  vertices_properties_file << std::fixed << (*this)(n)->get_name() << " ";
246  vertices_properties_file << std::fixed << (*this)(n)->get_out_degree() << " ";
247  vertices_properties_file << std::fixed << (*this)(n)->get_in_degree() << " ";
248  vertices_properties_file << std::fixed << (*this)(n)->get_nb_reciprocal_edges() << " ";
249  vertices_properties_file << std::fixed << (*this)(n)->get_local_undirected_projection_clustering_coefficient() << std::endl;
250  }
251  // Closes the stream.
252  vertices_properties_file.close();
253 }
254 
255 // ================================================================================================
256 // ================================================================================================
258 {
259  // Reintializes the variables inherited from the class base_directed_graph.
260  base_directed_graph_clear();
261  // Sets the type of the graph.
262  this->set_type_of_graph("Real directed graph");
263 }
264 
265 
266 
267 #endif // REAL_DIRECTED_GRAPH_HPP_INCLUDED
~real_directed_graph()
Destructor.
Definition: real_directed_graph.hpp:47
Class of the attributes of a simple edge.
Definition: edge_attributes.hpp:24
real_directed_graph()
Constructor.
Definition: real_directed_graph.hpp:60
Virtual template base class for directed graph objects.
Definition: base_directed_graph.hpp:37
void write_vertices_properties(std::string _name)
Exports the vertices' properties.
Definition: real_directed_graph.hpp:228
void read_edgelist(std::string _name)
Imports a directed graph from an edgelist.
Definition: real_directed_graph.hpp:67
void write_graph_properties(std::string _name)
Exports the graph's properties.
Definition: real_directed_graph.hpp:167
void clear()
Reinitializes the graph.
Definition: real_directed_graph.hpp:257
Virtual base class for directed graphs.
Class for vertices in real directed graphs.
Class for real directed graphs.
Definition: real_directed_graph.hpp:41