My Project
|
00001 #ifndef CIS_H 00002 #define CIS_H 00003 00004 #include "MyTools.h" 00005 00006 #include "seed.h" 00007 #include "iterativescan.h" 00008 #include "cluster.h" 00009 00010 #include <queue> 00011 00021 template <typename R> 00022 class IterativeDensity:public ClusterAlg<R> 00023 { 00024 public: 00032 IterativeDensity(string ar, double a, double b, double weightThreshold, bool t):alpha(a), beta(b), wt(weightThreshold){ 00033 this->args = "CISParams " + ar; 00034 /*Get the clique size to percolate */ 00035 vector <string> aa; 00036 boost::split(aa, this->args, boost::is_any_of(" ")); 00037 00038 char** av = new char*[aa.size()]; 00039 for (sui r = 0; r < aa.size();r++){ 00040 av[r] = (char*)(malloc((aa[r].size() + 1) * sizeof(char))); 00041 strcpy(av[r], aa[r].c_str()); 00042 } 00043 std::map <string, vector < string > > subParam = getParameters(aa.size(), av); 00044 std::map <string, vector < string > >::iterator it_sub; 00045 00046 if ((it_sub = subParam.find("l")) != subParam.end()){ 00047 lambda = boost::lexical_cast<double>(it_sub->second[0]); 00048 } else { 00049 cerr << "Invalid Clique Percolation Params (need size (-k))" << endl; 00050 exit(2); 00051 } 00052 00053 if ((it_sub = subParam.find("d")) != subParam.end()){ 00054 if ((it_sub->second[0])[0] == 'n') 00055 this->dens = new negative_density<bidirected_t>(lambda); 00056 else 00057 this->dens = new complex_density<bidirected_t>(lambda); 00058 } else { 00059 cerr << "Using complex density" << endl; 00060 this->dens = new complex_density<bidirected_t>(lambda); 00061 exit(3); 00062 } 00063 00064 this->translate = t; 00065 this->fileout = &cout; 00066 }; 00067 00068 00074 ~IterativeDensity(){}; 00075 00079 void calculate(); 00080 00088 bool ReadGraph(ifstream* fin, string delim){ 00089 delimiters = delim; 00090 read_from_stream<bidirected_t>(graph, map, (this->idmap), *fin, alpha, beta, wt, delimiters); 00091 return true; 00092 }; 00093 00100 bool PrintCommunities(string filename){ 00101 ofstream fout(filename.c_str()); 00102 for (int i = 0; i < comms.size(); i++){ 00103 print_set<bidirected_t>(graph, comms[i].vertices(), fout, comms[i].dens()); 00104 } 00105 return true; 00106 }; 00107 00114 bool PrintTranslatedCommunities(string filename){ 00115 ofstream fout(filename.c_str()); 00116 for (int i = 0; i < comms.size(); i++){ 00117 print_set_trans<bidirected_t>(graph, comms[i].vertices(), fout, comms[i].dens(), this->idmap, delimiters[0]); 00118 } 00119 return true; 00120 }; 00121 00127 template <typename graph_t> 00128 bool TranslateCommunities(){ 00129 std::map <long unsigned int, string> rID; 00130 std::map <long unsigned int, string>::iterator it_r; 00131 std::map <string, long unsigned int>::iterator it_id; 00132 set <lui>::iterator it_sl; 00133 00134 for (it_id = (this->idmap).begin(); it_id != (this->idmap).end(); it_id++){ 00135 rID.insert(make_pair<lui, string>(it_id->second, it_id->first)); 00136 } 00137 00138 for (int i = 0; i < comms.size(); i++){ 00139 set<typename graph_t::vertex> v = comms[i].vertices(); 00140 if (v.size() > 2) 00141 { 00142 set <lui> clust; 00143 typename std::set<typename graph_t::vertex>::const_iterator i; 00144 for (i = v.begin(); i != v.end(); ++i) 00145 { 00146 typename graph_t::vertex_name_t u; 00147 u = boost::get(boost::vertex_name, graph, *i); 00148 clust.insert(boost::lexical_cast<lui>((rID.find(boost::lexical_cast<lui>(u)))->second)); 00149 } 00150 00151 (this->communities).push_back(clust); 00152 } 00153 } 00154 return true; 00155 }; 00156 00162 void setOutput(string filename){ 00163 this->fileout = new ofstream(filename.c_str()); 00164 }; 00165 00166 private: 00167 double lambda, alpha, beta, wt; 00168 vector <seed> comms; 00169 bidirected_t::graph graph; 00170 bidirected_t::map map; 00171 string delimiters; 00172 00177 class future_actions 00178 { 00179 public: 00180 typedef std::pair<const std::vector<seed>*, double> results; 00181 00185 virtual ~future_actions(){}; 00186 00190 virtual void operator ()(const results, std::queue<seed>* q, 00191 std::vector<seed>* ret) =0; 00192 }; 00193 00199 class take_best: public IterativeDensity<R>::future_actions 00200 { 00201 public: 00208 take_best(double threshold=0):thresh_(threshold){}; 00209 00216 void operator ()(const typename IterativeDensity<R>::future_actions::results r, std::queue<seed>* q, 00217 std::vector<seed>* ret){ 00218 /*Makes sure there is a graph to work with*/ 00219 if ((*r.first).size() < 1) 00220 { 00221 throw "ret too short"; 00222 } 00223 /*Initialize*/ 00224 seed best = (*r.first)[0]; 00225 00226 /*Find the disjoint subgraph from results.first with the highest connectivity.*/ 00227 for (unsigned int i = 0; i < r.first->size(); ++i) 00228 { 00229 if (best.dens() < (*r.first)[i].dens()) 00230 { 00231 best = (*r.first)[i]; 00232 }else if (best.dens() == (*r.first)[i].dens() && best.vertices().size() < (*r.first)[i].vertices().size()) 00233 { 00234 00235 } 00236 } 00237 00238 /*Depending on whether or not the set's connectiveness is good enough, sort it*/ 00239 /*NOTE: should be best.dens() ?*/ 00240 if (r.second <= thresh_) 00241 { 00242 ret->push_back(best); 00243 } 00244 else 00245 { 00246 q->push(best); 00247 } 00248 }; 00249 00250 private: 00251 double thresh_; 00252 }; 00253 00263 std::vector<seed>* cluster(const seed& S, future_actions* act, bidirected_t::graph* graph) 00264 { 00265 //Start with seed 00266 std::vector<seed>* ret = new std::vector<seed>(); 00267 std::queue<seed> to_consider; 00268 to_consider.push(S); 00269 00270 //Keep expanding seed until convergence 00271 while (!to_consider.empty()) 00272 { 00273 seed s = to_consider.front(); 00274 to_consider.pop(); 00275 00276 std::pair<std::vector<seed>*, double> results; 00277 results = expand_seed(s); 00278 00279 (*act)(results, &to_consider, ret); 00280 00281 delete results.first; 00282 } 00283 00284 return ret; 00285 } 00286 00287 }; 00288 00289 #endif