10 #ifndef REAL_DIRECTED_GRAPH_HPP_INCLUDED
11 #define REAL_DIRECTED_GRAPH_HPP_INCLUDED
70 std::ifstream edgelist_file;
71 std::stringstream one_line;
73 std::string full_line, name1_str, name2_str;
75 unsigned int node_cnt, edge_cnt, node1_int, node2_int, edge_int;
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;
81 std::map< std::string,unsigned int >::iterator name_it;
82 std::set< std::pair<unsigned int,unsigned int> >::iterator edge_it, edge_end;
84 edgelist_file.open(_name.c_str(), std::ios_base::in);
85 if( !edgelist_file.is_open() )
87 std::cout <<
"Could not open file: " << _name <<
"." << std::endl;
93 while( !edgelist_file.eof() )
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;
104 name_it = Name2ID.find(name1_str);
105 if( name_it == Name2ID.end() )
107 Name2ID[name1_str] = node_cnt;
108 ID2Name[node_cnt] = name1_str;
109 node1_int = node_cnt;
114 node1_int = name_it->second;
117 name_it = Name2ID.find(name2_str);
118 if( name_it == Name2ID.end() )
120 Name2ID[name2_str] = node_cnt;
121 ID2Name[node_cnt] = name2_str;
122 node2_int = node_cnt;
127 node2_int = name_it->second;
130 if(node1_int != node2_int)
132 edge_it = Edgelist.find( std::pair<unsigned int,unsigned int>(node1_int,node2_int) );
133 if( edge_it == Edgelist.end() )
136 Edgelist.insert( std::make_pair(node1_int,node2_int) );
142 this->set_nb_vertices(node_cnt);
143 this->set_nb_edges(edge_cnt);
145 edge_it = Edgelist.begin();
146 edge_end = Edgelist.end();
147 for(
unsigned int n(0), nn(this->get_nb_vertices()); n<nn; ++n)
150 (*this)(n)->set_name(ID2Name[n]);
152 for(; edge_it!=edge_end; ++edge_it)
155 node1_int = edge_it->first;
156 node2_int = edge_it->second;
162 edgelist_file.close();
170 std::ofstream graph_properties_file;
172 std::string graph_properties_filename = _name +
"_graph_properties.dat";
174 graph_properties_file.open(graph_properties_filename.c_str(), std::ios_base::out);
175 if( !graph_properties_file.is_open() )
177 std::cout <<
"Could not open file: " << _name <<
"." << std::endl;
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;
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 <<
"/";
195 graph_properties_file <<
"0";
196 graph_properties_file << month <<
"/";
198 graph_properties_file <<
"0";
199 graph_properties_file << day <<
" " << hours <<
":";
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;
223 graph_properties_file.close();
231 std::ofstream vertices_properties_file;
233 std::string vertices_properties_filename = _name +
"_vertices_properties.dat";
235 vertices_properties_file.open(vertices_properties_filename.c_str(), std::ios_base::out);
236 if( !vertices_properties_file.is_open() )
238 std::cout <<
"Could not open file: " << _name <<
"." << std::endl;
242 vertices_properties_file.precision(8);
243 for(
unsigned int n(0), nn(this->get_nb_vertices()); n<nn; ++n)
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;
252 vertices_properties_file.close();
260 base_directed_graph_clear();
262 this->set_type_of_graph(
"Real directed graph");
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