10 #ifndef REAL_UNDIRECTED_GRAPH_HPP_INCLUDED
11 #define REAL_UNDIRECTED_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;
102 name_it = Name2ID.find(name1_str);
103 if( name_it == Name2ID.end() )
105 Name2ID[name1_str] = node_cnt;
106 ID2Name[node_cnt] = name1_str;
107 node1_int = node_cnt;
112 node1_int = name_it->second;
115 name_it = Name2ID.find(name2_str);
116 if( name_it == Name2ID.end() )
118 Name2ID[name2_str] = node_cnt;
119 ID2Name[node_cnt] = name2_str;
120 node2_int = node_cnt;
125 node2_int = name_it->second;
128 if(node1_int < node2_int)
130 std::swap(node1_int,node2_int);
132 if(node1_int != node2_int)
134 edge_it = Edgelist.find( std::make_pair(node1_int,node2_int) );
135 if( edge_it == Edgelist.end() )
138 Edgelist.insert( std::make_pair(node1_int,node2_int) );
144 this->set_nb_vertices(node_cnt);
145 this->set_nb_edges(edge_cnt);
147 edge_it = Edgelist.begin();
148 edge_end = Edgelist.end();
149 for(
unsigned int n(0), nn(this->get_nb_vertices()); n<nn; ++n)
152 (*this)(n)->set_name(ID2Name[n]);
154 for(; edge_it!=edge_end; ++edge_it)
157 node1_int = edge_it->first;
158 node2_int = edge_it->second;
164 edgelist_file.close();
172 std::ofstream graph_properties_file;
174 std::string graph_properties_filename = _name +
"_graph_properties.dat";
176 graph_properties_file.open(graph_properties_filename.c_str(), std::ios_base::out);
177 if( !graph_properties_file.is_open() )
179 std::cout <<
"Could not open file: " << _name <<
"." << std::endl;
183 time_t theTime = time(NULL);
184 struct tm *aTime = gmtime(&theTime);
185 int year = aTime->tm_year + 1900;
186 int month = aTime->tm_mon + 1;
187 int day = aTime->tm_mday;
188 int hours = aTime->tm_hour;
189 int minutes = aTime->tm_min;
191 graph_properties_file <<
"===================================================================================" << std::endl;
192 graph_properties_file <<
"General information." << std::endl;
193 graph_properties_file <<
"type of graph: " << this->get_type_of_graph() << std::endl;
194 graph_properties_file <<
"graph: " << _name << std::endl;
195 graph_properties_file <<
"computed on: " << year <<
"/";
197 graph_properties_file <<
"0";
198 graph_properties_file << month <<
"/";
200 graph_properties_file <<
"0";
201 graph_properties_file << day <<
" " << hours <<
":";
203 graph_properties_file <<
"0";
204 graph_properties_file << minutes <<
" UTC" << std::endl;
205 graph_properties_file <<
"===================================================================================" << std::endl;
206 graph_properties_file <<
"Observables (excluding zero-degree vertices)" << std::endl;
207 graph_properties_file <<
"Number of nodes: " << this->get_nb_vertices()-this->get_nb_zerodegree_vertices() << std::endl;
208 graph_properties_file <<
"Number of edges: " << this->get_nb_edges() << std::endl;
209 graph_properties_file <<
"Number of wedges: " << this->get_nb_wedges() << std::endl;
210 graph_properties_file <<
"Number of triangles: " << this->get_nb_triangles() << std::endl;
211 graph_properties_file <<
"Global clustering coefficient: " << this->get_global_clustering_coefficient() << std::endl;
212 graph_properties_file <<
"Average local clustering coefficient: " << this->get_avg_local_clustering_coefficient() << std::endl;
213 graph_properties_file <<
"Degree distribution" << std::endl;
214 graph_properties_file <<
" - average degree: " << this->get_avg_degree() << std::endl;
215 graph_properties_file <<
" - minimum degree (excluding zero): " << this->get_min_degree() << std::endl;
216 graph_properties_file <<
" - maximum degree: " << this->get_max_degree() << std::endl;
218 graph_properties_file.close();
226 std::ofstream vertices_properties_file;
228 std::string vertices_properties_filename = _name +
"_vertices_properties.dat";
230 vertices_properties_file.open(vertices_properties_filename.c_str(), std::ios_base::out);
231 if( !vertices_properties_file.is_open() )
233 std::cout <<
"Could not open file: " << _name <<
"." << std::endl;
237 vertices_properties_file.precision(8);
238 for(
unsigned int n(0), nn(this->get_nb_vertices()); n<nn; ++n)
240 vertices_properties_file << std::fixed << (*this)(n)->get_name() <<
" ";
241 vertices_properties_file << std::fixed << (*this)(n)->get_degree() <<
" ";
242 vertices_properties_file << std::fixed << (*this)(n)->get_nb_triangles() <<
" ";
243 vertices_properties_file << std::fixed << (*this)(n)->get_local_clustering_coefficient() << std::endl;
246 vertices_properties_file.close();
254 base_undirected_graph_clear();
256 this->set_type_of_graph(
"Real undirected graph");
261 #endif // REAL_UNDIRECTED_GRAPH_HPP_INCLUDED
void write_graph_properties(std::string _name)
Exports the graph's properties.
Definition: real_undirected_graph.hpp:169
Class for vertices in real undirected graphs.
void clear()
Reinitializes the graph.
Definition: real_undirected_graph.hpp:251
Class of the attributes of a simple edge.
Definition: edge_attributes.hpp:24
real_undirected_graph()
Constructor.
Definition: real_undirected_graph.hpp:60
Virtual base class for undirected graphs.
Virtual template base class for undirected graph objects.
Definition: base_undirected_graph.hpp:37
void write_vertices_properties(std::string _name)
Exports the vertices' properties.
Definition: real_undirected_graph.hpp:223
Class for real undirected graphs.
Definition: real_undirected_graph.hpp:41
~real_undirected_graph()
Destructor.
Definition: real_undirected_graph.hpp:47
void read_edgelist(std::string _name)
Imports a undirected graph from an edgelist.
Definition: real_undirected_graph.hpp:67