アニールネットワーク
#include<stdio.h> #include<math.h> #include<set> #include<map> #include<vector> #include<sstream> #include<fstream> #include<iostream> #include<string.h> #include<stdlib.h> int main(){ using namespace std; FILE *fp; std::string index; std::string str; std::string str2; std::map<std::string,int> outdegree; std::map<std::string,int> indegree; std::map<std::pair<int,int>,int> aij; std::pair<int,int> k12; std::map<int,int> out_num; std::map<int,int> in_num; std::set<std::string> nodelist; std::vector<string> in1; std::vector<string> out1; int retuno=0; int nodeno; //std::ifstream ifs("Gou125b.net"); std::ifstream ifs("random.net"); while(ifs && getline(ifs,str)){ str=strtok(const_cast<char*>(str.c_str())," "); str2=(strtok(NULL," ")); outdegree[str]=outdegree[str]+1; indegree[str2]=indegree[str2]+1; nodelist.insert(str); nodelist.insert(str2); out1.push_back(str) ; in1.push_back(str2); retuno++; //cout << retuno<<endl; } nodeno=nodelist.size(); std::ostringstream os; //printf("%d %d",nodeno,retuno); int i; for(i=0;i<retuno;i++){ (k12).first=outdegree[out1[i]]; (k12).second=indegree[in1[i]] ; aij[k12]=aij[k12] +1; //out_num[outdegree[out1[i]]]=out_num[outdegree[out1[i]]]+1; //in_num[indegree[in1[i]]]=in_num[indegree[in1[i]]]+1; } set<string>::iterator it2=nodelist.begin(); while(it2!=nodelist.end()){ out_num[outdegree[*it2]]=out_num[outdegree[*it2]]+1; in_num[indegree[*it2]]=in_num[indegree[*it2]]+1; ++it2; } map<pair<int,int>,int>::iterator it=aij.begin(); while(it!=aij.end()){ //str=it->first; cout << (it->first).first <<" "<<(it->first).second<<" "<<it->se cond<<" "<<(double)(it->second)/(double)out_num[(it->first).first]/(double)in_nu m[(it->first).second]<<" "<<out_num[(it->first).first]<<" "<<in_num[(it->first). second]<<endl; ++it; } //map<string,int>::iterator it2=indegree.begin(); /* while(it2!=indegree.end()){ cout << it2->first <<" "<<it2->second << endl; ++it2; } */ }