My Project
|
00001 00006 #ifndef GRAPH_H_ 00007 #define GRAPH_H_ 00008 00009 #include "MyTools.h" 00010 #include <iostream> 00011 #include <map> 00012 #include <vector> 00013 00014 #include <boost/graph/adjacency_list.hpp> 00015 00016 #include "types.h" 00017 00024 struct undirected_t 00025 { 00026 typedef boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS, 00027 boost::property<boost::vertex_name_t, double>, 00028 boost::property<boost::edge_name_t, double> > graph; 00029 typedef boost::graph_traits<graph>::vertex_descriptor vertex; 00030 typedef boost::graph_traits<graph>::edge_descriptor edge; 00031 typedef boost::graph_traits<graph>::edge_iterator edge_iterator; 00032 typedef std::map<lui, vertex> map; 00033 typedef double edge_name_t; 00034 typedef lui vertex_name_t; 00035 }; 00036 00037 00044 struct directed_t 00045 { 00046 typedef boost::adjacency_list<boost::setS, boost::listS, boost::directedS, 00047 boost::property<boost::vertex_name_t, lui>, 00048 boost::property<boost::edge_name_t, lui> > graph; 00049 typedef boost::graph_traits<graph>::vertex_descriptor vertex; 00050 typedef boost::graph_traits<graph>::edge_descriptor edge; 00051 typedef boost::graph_traits<graph>::edge_iterator edge_iterator; 00052 typedef std::map<lui, vertex> map; 00053 typedef double edge_name_t; 00054 typedef lui vertex_name_t; 00055 }; 00056 00057 00064 struct bidirected_t 00065 { 00066 typedef boost::adjacency_list<boost::setS, boost::listS, boost::bidirectionalS, 00067 boost::property<boost::vertex_name_t, lui>, 00068 boost::property<boost::edge_name_t, double> > graph; 00069 typedef boost::graph_traits<graph>::vertex_descriptor vertex; 00070 typedef boost::graph_traits<graph>::edge_descriptor edge; 00071 typedef boost::graph_traits<graph>::edge_iterator edge_iterator; 00072 typedef std::map<lui, vertex> map; 00073 typedef double edge_name_t; 00074 typedef lui vertex_name_t; 00075 }; 00076 00077 00090 template <typename graph_t> 00091 typename graph_t::vertex add_or_get_vertex(typename graph_t::graph &g, 00092 typename graph_t::map &vertices, lui id) 00093 { 00094 typename graph_t::map::iterator pos; 00095 bool inserted; 00096 typename graph_t::vertex v; 00097 00098 boost::tie(pos, inserted) = vertices.insert( 00099 std::make_pair(id, typename graph_t::vertex())); 00100 00101 if (inserted) 00102 { 00103 v = add_vertex(g); 00104 boost::put(boost::vertex_name, g, v, id); 00105 pos->second = v; 00106 } 00107 else 00108 { 00109 v = pos->second; 00110 } 00111 return v; 00112 } 00113 00114 00125 template <typename graph_t> 00126 void bump_edge(typename graph_t::graph &g, typename graph_t::map &vertices, 00127 typename graph_t::vertex_name_t fr, typename graph_t::vertex_name_t to) 00128 { 00129 typename graph_t::vertex u, v; 00130 typename graph_t::edge_name_t weight; 00131 u = add_or_get_vertex<graph_t>(g, vertices, fr); 00132 v = add_or_get_vertex<graph_t>(g, vertices, to); 00133 00134 typename graph_t::edge e; 00135 bool inserted; 00136 boost::tie(e, inserted) = boost::add_edge(u, v, g); 00137 if (inserted) 00138 { 00139 boost::put(boost::edge_name, g, e, 1); 00140 } 00141 else 00142 { 00143 weight = boost::get(boost::edge_name, g, e); 00144 boost::put(boost::edge_name, g, e, weight + 1); 00145 } 00146 } 00147 00162 template <typename graph_t> 00163 void read_from_stream(typename graph_t::graph& g, typename graph_t::map& vertices, map <string, lui>& id, 00164 std::ifstream& in, double alpha, double beta, double weightThresh, string delimiter) 00165 { 00166 typename graph_t::vertex_name_t fr; 00167 typename graph_t::vertex_name_t to; 00168 typename graph_t::edge_name_t wt; 00169 00170 vector <string> fields; 00171 map <string, lui>::iterator mit; 00172 lui NextId = 0; 00173 00174 for (mit = id.begin(); mit != id.end(); mit++) 00175 { 00176 if (mit->second > NextId) 00177 NextId = mit->second; 00178 } 00179 00180 NextId++; 00181 00186 double maxWeight = 0; 00187 00188 while (fline_tr(&in, &fields, delimiter.c_str())) 00189 { 00190 if (fields.size() != 3) 00191 continue; 00192 00193 double dw = boost::lexical_cast<double>(fields[2]); 00194 if (abs(dw) > maxWeight) 00195 maxWeight = abs(dw); 00196 } 00197 00198 beta = beta / maxWeight; 00199 alpha = alpha / maxWeight; 00200 00201 in.clear(); 00202 in.seekg(0, ios::beg); 00203 00204 ofstream gout("TrueNetwork.dat"); 00205 while (fline_tr(&in, &fields, delimiter.c_str()) ) 00206 { 00207 if (fields.size() != 3) 00208 continue; 00209 00210 double dw = boost::lexical_cast<double>(fields[2]); 00211 if (dw < 0) 00212 dw *= beta; 00213 else 00214 dw *= alpha; 00215 00216 if (abs(dw) < weightThresh) 00217 continue; 00218 00219 wt = boost::lexical_cast<typename graph_t::edge_name_t>(dw); 00220 00221 if ((mit = id.find(fields[0])) != id.end()) 00222 fr = mit->second; 00223 else 00224 { 00225 id.insert(make_pair<string, int>(fields[0], NextId)); 00226 fr = NextId++; 00227 } 00228 00229 if ((mit = id.find(fields[1])) != id.end()) 00230 to = mit->second; 00231 else 00232 { 00233 id.insert(make_pair<string, int>(fields[1], NextId)); 00234 to = NextId++; 00235 } 00236 00237 gout << fr << ", " << to << ", " << dw << endl; 00238 00239 typename graph_t::vertex u, v; 00240 u = add_or_get_vertex<graph_t>(g, vertices, fr); 00241 v = add_or_get_vertex<graph_t>(g, vertices, to); 00242 00243 if (u == v) 00244 { 00245 // Disallow self-edges. 00246 continue; 00247 } 00248 00249 typename graph_t::edge e; 00250 bool inserted; 00251 boost::tie(e, inserted) = boost::add_edge(u, v, g); 00252 if (inserted) 00253 { 00254 boost::put(boost::edge_name, g, e, wt); 00255 } 00256 else 00257 { 00258 typename graph_t::vertex_name_t a, b; 00259 a = boost::get(boost::vertex_name, g, u); 00260 b = boost::get(boost::vertex_name, g, v); 00261 cerr << a << " " << b << endl; 00262 throw "Duplicate edge not allowed. "; 00263 } 00264 } 00265 } 00266 00277 template <typename graph_t> 00278 void print_set(const typename graph_t::graph& g, 00279 const std::set<typename graph_t::vertex>& v, std::ostream& out, float density) 00280 { 00281 if (density <= 0) 00282 return; 00283 00284 string outString = ""; 00285 if (v.size() > 2) 00286 { 00287 outString += boost::lexical_cast<string>(density) + " " + boost::lexical_cast<string>(v.size()) + " "; 00288 typename std::set<typename graph_t::vertex>::const_iterator i; 00289 for (i = v.begin(); i != v.end(); ++i) 00290 { 00291 typename graph_t::vertex_name_t u; 00292 u = boost::get(boost::vertex_name, g, *i); 00293 outString += boost::lexical_cast<string>(u) + " "; 00294 } 00295 outString += "\n"; 00296 out << outString; 00297 out.flush(); 00298 } 00299 } 00313 template <typename graph_t> 00314 void print_set_trans(const typename graph_t::graph& g, 00315 const std::set<typename graph_t::vertex>& v, std::ostream& out, float density, map<string, lui> ID, char delim) 00316 { 00317 if (density <= 0){ 00318 return; 00319 } 00320 map <lui, string> rID; 00321 map <lui, string>::iterator it_r; 00322 map <string, lui>::iterator it_id; 00323 set <lui>::iterator it_sl; 00324 00325 for (it_id = ID.begin(); it_id != ID.end(); it_id++){ 00326 rID.insert(make_pair<lui, string>(it_id->second, it_id->first)); 00327 } 00328 00329 string outString = ""; 00330 if (v.size() > 2) 00331 { 00332 outString += boost::lexical_cast<string>(density) + boost::lexical_cast<string>(delim) + " " + boost::lexical_cast<string>(v.size()) + boost::lexical_cast<string>(delim) + " "; 00333 typename std::set<typename graph_t::vertex>::const_iterator i; 00334 for (i = v.begin(); i != v.end(); ++i) 00335 { 00336 typename graph_t::vertex_name_t u; 00337 u = boost::get(boost::vertex_name, g, *i); 00338 outString += (rID.find(boost::lexical_cast<lui>(u)))->second + boost::lexical_cast<string>(delim) + " "; 00339 } 00340 outString = outString.substr(0, outString.size()-2) + "\n"; 00341 out << outString; 00342 out.flush(); 00343 } 00344 } 00345 #endif