My Project
cluster.h
Go to the documentation of this file.
00001 
00009 #ifndef CLUSTER_H_
00010 #define CLUSTER_H_
00011 
00012 #include <queue>
00013 #include <vector>
00014 
00015 #include <map>
00016 #include <set>
00017 #include <vector>
00018 #include <fstream>
00019 #include <iostream>
00020 
00021 #include <stdarg.h>
00022 #include "graph.h"
00023 #include "density.h"
00024 #include "seed.h"
00025 
00026 #include "boost/tuple/tuple.hpp"
00027 #include "boost/tuple/tuple_comparison.hpp"
00028 #include "boost/tuple/tuple_io.hpp"
00029 
00030 #include "ComponentsLoop.h"
00031 
00032 namespace std {
00033 template<class T >
00034 class complex;}
00035 
00036 using namespace std;
00037 
00045 template <typename R>
00046 class ClusterAlg
00047 {
00048   public:
00050     virtual void calculate() = 0;
00051  
00052     static density<bidirected_t>* dens; 
00059   float get_Density(bidirected_t::graph* graph, const set < bidirected_t::vertex >* cluster)
00060   {      
00061       set < bidirected_t::vertex >::iterator it;
00062       it = cluster->begin();
00063       
00064       //Create a seed with the first vertex
00065       seed calc(dens, *graph, *it);
00066       
00067       //Add the remaining vertices to the seed. 
00068       for (++it; it != cluster->end(); ++it)
00069       {
00070         calc.add_vertex(*it);
00071       }
00072         
00073       //Calculate density
00074       return calc.dens();
00075   }
00076   
00082   float get_Density(set <lui>* cluster){
00083     double win = 0, wout = 0;
00084     
00085     set <lui>::iterator it_sl;
00086     typename map <lui, map <lui, R> >::iterator it_gr;
00087     typename map <lui, R>::iterator it_nei;
00088     
00089     //Calculate internal and external link weights
00090     sui k, t;
00091     for (it_sl = cluster->begin(), k = 0; it_sl != cluster->end() && k < cluster->size(); it_sl++, k++){
00092         if ((it_gr = network.find(*it_sl)) != network.end()){
00093           for (it_nei = it_gr->second.begin(), t = 0; it_nei != it_gr->second.end() && t < it_gr->second.size(); it_nei++, t++){
00094             if (cluster->find(it_nei->first) != cluster->end()){
00095               win += it_nei->second;
00096             } else {
00097               wout += it_nei->second;
00098             }
00099           }
00100         }
00101     }
00102    
00103     //Density calculation
00104     return (*(this->dens))(win, wout, cluster->size());
00105   }
00106   
00118   static ClusterAlg* Create (string type, string args, double alpha, double beta, double weightThresh, bool translate);
00119   
00129   bool ReadGraph(ifstream* fin, string delimiters = " ,\t", double alpha = 1, double beta = 1) {
00130       vector <string> fields;
00131       map < string, lui >::iterator it_id;
00132       lui nxtID = 1;
00133       typename map < lui, map < lui, R > >::iterator it_gr, it_gr2;
00134       lui source, target;
00135      
00136       while (fline_tr(fin, &fields, delimiters.c_str())){
00137       if (fields.size() != 3) continue;
00138      
00139       //Check to see if vertex already has ID. If not, give the next ID to vertex.
00140       if ((it_id = idmap.find(fields[0]))!=idmap.end()){
00141         source = it_id->second;
00142       } else {
00143         idmap.insert(make_pair<string, lui>(fields[0], nxtID));
00144         source = nxtID++;
00145       } 
00146       
00147       //Same for second edge endpoint
00148       if ((it_id = idmap.find(fields[1]))!=idmap.end()){
00149         target = it_id->second;
00150       } else {
00151         idmap.insert(make_pair<string, lui>(fields[1], nxtID));
00152         target = nxtID++;
00153       }
00154       
00155       R weight = boost::lexical_cast<R>(fields[2]);
00156       if (weight > 0)
00157         weight = alpha * weight;
00158       else
00159         weight = beta * weight;
00160       
00161       //Add the edge to the network      
00162       if ((it_gr = network.find(source)) != network.end()){
00163         if ((it_gr2 = network.find(target)) != network.end()){
00164           if (it_gr2->second.find(source) != it_gr2->second.end()){
00165             //The edge is already in the network
00166             cerr << "Duplicate edge : " << source << " - " << target << " :: " << fields[2] << endl;
00167           } else {
00168             it_gr->second.insert(make_pair<lui,R>(target, weight));
00169           }
00170         } else {
00171           it_gr->second.insert(make_pair<lui,R>(target, weight));
00172         }
00173       } else if ((it_gr2 = network.find(target)) != network.end()){
00174         it_gr2->second.insert(make_pair<lui,R>(source, weight));
00175       } else {
00176         map < lui, R > tmp;
00177         tmp.insert(make_pair<lui,R>(target, weight));
00178         network.insert(make_pair<lui, map <lui, R> >(source, tmp));
00179       }
00180     }
00181       
00182     for (it_id = this->idmap.begin(); it_id != this->idmap.end(); it_id++){
00183       idLookup.insert(make_pair<lui, string>(it_id->second, it_id->first));
00184     }
00185     return true;
00186   };
00187   
00195   bool PrintGraph(ofstream* fout){
00196     typename map < lui, map < lui, R > >::iterator source;
00197     typename map < lui, R >::iterator target;
00198     
00199     for (source = network.begin(); source != network.end(); source++){
00200       for (target = source->second.begin(); target != source->second.end(); target++){
00201         (*fout) << source->first << " " << target->first << " " << target->second << endl;
00202       }
00203     }
00204     return true;
00205   }
00206   
00215   bool PrintCommunities(string filename){
00216     ofstream fout(filename.c_str());
00217     set < lui >::iterator it_cl;
00218     
00219     for (int i = 0; i < communities.size(); i++){
00220       fout << this->get_Density(&communities[i]) << " " << communities[i].size();
00221       for (it_cl = communities[i].begin(); it_cl != communities[i].end(); it_cl++){
00222         fout << " " << *it_cl;
00223       }
00224       fout << endl;
00225     }
00226     
00227     return true;
00228   }
00229   
00237   void PrintCommunity(set <lui> * comm){
00238     set < lui >::iterator it_cl;
00239     
00240     *fileout << this->get_Density(&(*comm)) << " " << (*comm).size();
00241     for (it_cl = (*comm).begin(); it_cl != (*comm).end(); it_cl++){
00242       *fileout << " " << *it_cl;
00243     }
00244     *fileout << endl;
00245   }
00246   
00255   bool PrintTranslatedCommunities(string filename){
00256     set <lui>::iterator it_sl; 
00257     
00258     ofstream fout(filename.c_str());
00259     set < lui >::iterator it_cl;
00260     
00261     for (int i = 0; i < communities.size(); i++){
00262       fout << this->get_Density(&communities[i]) << " " << communities[i].size();
00263       for (it_cl = communities[i].begin(); it_cl != communities[i].end(); it_cl++){
00264         fout << " " << (idLookup.find(*it_cl))->second;
00265       }
00266     }
00267     
00268     fout << endl;
00269     
00270     return true;
00271   }
00272   
00280   void PrintTranslatedCommunity(set <lui> * comm){
00281     set < lui >::iterator it_cl;
00282     
00283     *fileout << this->get_Density(&(*comm)) << " " << (*comm).size();
00284     for (it_cl = (*comm).begin(); it_cl != (*comm).end(); it_cl++){
00285       *fileout << " " << (idLookup.find(*it_cl))->second;
00286     }
00287     *fileout << endl;
00288   }
00289   
00294   void TranslateCommunities(){
00295     
00296     set < lui >::iterator it_cl;
00297     
00298     vector < set <lui> > copy = communities;
00299     communities.clear();
00300     
00301     for (int i = 0; i < copy.size(); i++){
00302       set <lui> clust;
00303       for (it_cl = copy[i].begin(); it_cl != copy[i].end(); it_cl++){
00304         clust.insert(boost::lexical_cast<lui>((idLookup.find(*it_cl))->second));
00305       }
00306       communities.push_back(clust);
00307     }    
00308   }
00309   
00310   void setOutput(string filename){
00311       fileout = new ofstream(filename.c_str());
00312   }
00313   
00314   map <lui, map <lui, R> > network; 
00315   vector < set < lui > > communities; 
00316   map <string, lui> idmap; 
00317   map <lui, string> idLookup; 
00319   string args; 
00321   bool translate;
00322   ostream* fileout; 
00323 };
00324 
00325 #endif
 All Classes Files Functions Variables Typedefs