コード例 #1
0
size_t scn::MRTofRandomWalk(UGraph::pGraph graph)
{
   
   size_t total_steps = 0;
   vector<size_t> neighbors;
   srand(size_t(time(00)));

//#pragma omp parallel for shared(total_steps) private(neighbors)
   for(size_t source = 0; source < graph->GetNumberOfNodes(); source++)
   {
  //    if(omp_get_thread_num() == 0)
	// cout<<"Random walk on "<<source<<"/"<<graph->GetNumberOfNodes() / omp_get_num_procs()<<endl;
      size_t next = source;
      size_t steps = 0;
      do
      {
	 neighbors.assign(graph->find(next)->begin(), graph->find(next)->end());
	 next = neighbors[rand() % neighbors.size()];
	 steps++;
      }while(next != source);
//#pragma omp critical
      {
	 total_steps += steps;
      }
   }
   return static_cast<double>(total_steps) / 
      static_cast<double>(graph->GetNumberOfNodes());
}
コード例 #2
0
void scn::GetClusteringCoeffDist(vector<pair<double, double>> &distribution, UGraph::pGraph graph, double slide)
{
   assert(slide >= 0 && slide <= 1);
   if(slide == 0)
   {
      unordered_map <double,double> cc_dist;
      for(auto node = graph->begin(); node != graph->end(); node++)
      {
	     cc_dist[GetClusteringCoeff(graph,*node)]++;
      }
      for(auto iter = cc_dist.begin(); iter != cc_dist.end(); iter++)
      {
	 iter->second /= graph->GetNumberOfNodes();
      }
      distribution.assign(cc_dist.begin(), cc_dist.end());
      sort(distribution.begin(), distribution.end());
   }
   else
   {
      unordered_map<size_t, double> cc_dist;
      for(auto node = graph->begin(); node != graph->end(); node++)
      {
	 cc_dist[static_cast<size_t>(GetClusteringCoeff(graph, *node) / slide)]++;
      }
      distribution.assign(cc_dist.begin(), cc_dist.end());
      sort(distribution.begin(), distribution.end());
      for(auto iter = distribution.begin(); iter != distribution.end(); iter++)
      {
	      iter->first = iter->first * slide + slide / 2;
	     if(iter->first > 1)  iter->first = 1;
	 	 iter->second /= graph->GetNumberOfNodes();
      }
   }
}
コード例 #3
0
Matrix scn::GetGeodesicMatrix(UGraph::pGraph graph)
{
   Matrix result(graph->GetNumberOfNodes(), graph->GetNumberOfNodes());
   std::unordered_map<size_t,size_t> distance;
   //auto& distance = distance_sssp;
   size_t diameter = 0;
   for(auto node = graph->begin(); node != graph->end(); node++)
   {
      RunSPFA(graph,*node,distance);
      for(auto iter = distance.begin(); iter != distance.end(); iter++)
      {
	 result(*node, iter->first) = iter->second;
	 if(iter->second > diameter)
	 {
	    diameter = iter->second;
	 }
      }
   }
   //result.Print("Matrix:");

   for(size_t i = 0; i < result.GetHeight(); i++)
   {
      result(i, i) = - valarray<double>(result.row(i)).sum();
   }

   result /= static_cast<double>(diameter);
   return result;
}
コード例 #4
0
double scn::GetAverageSearchInfo(UGraph::pGraph graph)
{
   double sum = 0;
   for(auto node1 = graph->begin(); node1 != graph->end(); node1++)
   {
      for(auto node2 = graph->begin(); node2 != graph->end(); node2++)
      {
	 if(node1 == node2)
	    continue;
	 sum += GetSearchInfo(graph,*node1, *node2);
      }
   } 
   return sum / static_cast<double>(graph->GetNumberOfNodes() * graph->GetNumberOfNodes());
}
コード例 #5
0
ファイル: NetworkGen.cpp プロジェクト: petalgem/ComplexNet
void scn::WriteToNetFile(char* path, UNetwork<>::pNetwork &network)
   {
      using std::endl;
	  std::ofstream outfile(path,ios_base::trunc);
      UGraph::pGraph graph = network->GetTopology();
      
      outfile<<"*Vertices "<<graph->GetNumberOfNodes()<<endl;
      //write node
      for(auto node = graph->begin(); node != graph->end(); node++)
      {
		  auto position = network->GetNodePosition(*node);
		  outfile<<*node + 1<<" "<<*node + 1<<" "<<position[0]<<" "
			  <<position[1]<<" "<<position[2]<<endl;
      }
      
      outfile<<"*Arcs"<<endl;
      outfile<<"*Edges"<<endl;

      //write edge
      for(auto node = graph->begin(); node != graph->end(); node++)
      {
	 for(auto other = node->begin(); other != node->end(); other++)
	 {
	    if(*other < *node)
	    {
	       outfile<<*node + 1<<" "<<*other + 1<<" 1"<<endl;
	    }
	 }
      }
      outfile.close();
   }
コード例 #6
0
ファイル: qnetwork.cpp プロジェクト: petalgem/scn
QUNetwork::QUNetwork(UGraph::pGraph &graph)
   :UNetwork(graph)
{
   CreateScene(graph->GetNumberOfNodes());
   //create draw node
   for(auto node = graph->begin(); node != graph->end(); node++)
   {
      pNode data = new QNodeItem(this);
      data->indexOfNode = *node;
      data->SetText(QString("%1").arg(*node));

      SetNodeData(node, data);
   }
   //create position
   CreateCirclePosition();
   //add draw edge
   pEdge data;
   for(auto node = graph->begin(); node != graph->end(); node++)
   {
      for(auto other = node->begin(); other != node->end(); other++)
      {
	 data = new QEdgeItem(GetNodeData(node)->pos(),
			      GetNodeData(*other)->pos());
	 
	 SetEdgeData(node, *other, data);
      }
   }
}
コード例 #7
0
double scn::ComputeAverageDegree(UGraph::pGraph graph)
{
   double sum = 0;
   for(auto node = graph->begin(); node != graph->end(); node++)
   {
      sum += node->GetDegree();
   }
   return sum / graph->GetNumberOfNodes();
}
コード例 #8
0
double scn::GetHideInfo(UGraph::pGraph graph,size_t indexOfNode)
{
   double sum = 0;
   for(auto node = graph->begin(); node != graph->end(); node++)
   {
      if(*node == indexOfNode)
	 continue;

      sum += GetSearchInfo(graph,*node, indexOfNode);
   }
   return sum / static_cast<double>(graph->GetNumberOfNodes());
}
コード例 #9
0
double scn::GetCyclicCoeff(UGraph::pGraph graph,size_t indexOfNode)
{
   //IndexList edges;
   std::pair<IndexList, IndexList> result;
   std::unordered_map<size_t,size_t> distance;
   //auto& distance = distance_sssp;
   distance.clear();
   if(indexOfNode != UGraph::NaF)
   {//one vertex
      result = graph->RemoveNode(indexOfNode);
	  IndexList& edges = result.first;
      double sum = 0;
      for(auto head = edges.begin(); head != edges.end(); head++)
      {
	 RunSPFA(graph,*head,distance);
	 for(auto tail = head + 1; tail != edges.end(); tail++)
	 {
	    sum += 1.0 / static_cast<double>(distance[*tail] + 2);
	 }
      }
      ///restore
      graph->AddEdge(graph->AddNode(indexOfNode), edges);
      return 2 * sum / static_cast<double>(edges.size() * (edges.size() - 1));
   }
   else
   {//the whole network
      //copy node list
      IndexList nodes = graph->CopyIndexOfNodes();
      double total_sum = 0;
      for(auto node = nodes.begin(); node != nodes.end(); node++)
      {
	 result = graph->RemoveNode(*node);
	 IndexList& edges = result.first;
	 double sum = 0;
	 for(auto head = edges.begin(); head != edges.end(); head++)
	 {
	    RunSPFA(graph,*head,distance);
	    for(auto tail = head + 1; tail != edges.end(); tail++)
	    {
	       sum += 1.0 / static_cast<double>(distance[*tail] + 2);
	    }
	 }
	 ///restore, add node first, then add list of edges of this node
	 graph->AddEdge(graph->AddNode(*node), edges);
	 //accumulate
	 total_sum += 2 * sum / static_cast<double>(edges.size() * (edges.size() - 1));
      }
      return total_sum / static_cast<double>(graph->GetNumberOfNodes());
   }
}
コード例 #10
0
void scn::RunDjikstra(UGraph::pGraph graph,size_t indexOfSource,std::unordered_map<size_t,size_t> &distance)
{
   //auto& distance = distance_sssp;//using distance_sssp eariler
   assert(graph->HasNode(indexOfSource));
   //init
   //distance.reserve(graph->GetNumberOfNodes());
   for(auto node = graph->begin(); node != graph->end(); node++)
   {
      distance[*node] = Graph::NaF;
   }
   distance[indexOfSource] = 0;

   list<size_t> queue;
   //fill index of nodes into queue
   for(size_t i = 0; i < graph->GetNumberOfNodes(); i++)
   {
      queue.push_back(i);
   }
   //begin
   size_t next_distance;
   while(!queue.empty())
   {
      //get min one
      auto min = min_element(queue.begin(), queue.end(), 
			     [&](const size_t &one, const size_t &two)->bool
			     {
				if(distance[one] < distance[two])
				   return true;
				else
				   return false;
			     });
      auto node = graph->find(*min);  

      if(distance[*node] < Graph::NaF)
	 next_distance = distance[*node] + 1;
      else
	 next_distance = Graph::NaF;
      //relax neighbors
      for(auto other = node->begin(); other != node->end(); other++)
      {
	 if(distance[*other] > next_distance)
	 {
	    distance[*other] = next_distance;
	 } 
      }
      queue.erase(min);
   }
}
コード例 #11
0
double scn::GetEntropyOfDegreeDist(UGraph::pGraph graph)
{
   //get degree distribution
	std::unordered_map<size_t,size_t> distribution;
   for(auto node = graph->begin(); node != graph->end(); node++)
   {
      distribution[node->GetDegree()]++;
   }
   //compute the entropy
   double sum = 0;
   double pk = 0;
   for(auto iter = distribution.begin(); iter != distribution.end(); iter++)
   {
      pk = static_cast<double>(iter->second) / graph->GetNumberOfNodes();
      sum -= pk * log(pk)/log(2.0);
   }
   return sum;
}
コード例 #12
0
double scn::GetClusteringCoeff(UGraph::pGraph graph,size_t indexOfNode)
{
   if(indexOfNode == UGraph::NaF)
   {//the whole network
      double coefficient = 0;
      for(auto node = graph->begin(); node != graph->end(); node++)
      {
	 size_t numberOfTriangles = 0;
	 for(auto other1 = node->begin(); other1 != node->end(); other1++)
	 {
	    for(auto other2 = other1 + 1; other2 != node->end(); other2++)
	    {
	       if(graph->HasEdge(*other1, *other2))
		  numberOfTriangles++;
	    }
	 }
	 if(node->GetDegree()>1)
	 {
	 coefficient += 2 * static_cast<double>(numberOfTriangles) /
	    (node->GetDegree() * (node->GetDegree() - 1));
	 }
      }
      return coefficient / graph->GetNumberOfNodes();
   }
   else
   {//one vertex
      auto node = graph->find(indexOfNode);
      double numberOfTriangles = 0;
      for(auto other1 = node->begin(); other1 != node->end(); other1++)
      {
	 for(auto other2 = other1 + 1; other2 != node->end(); other2++)
	 {
	    if(graph->HasEdge(*other1, *other2))
	       numberOfTriangles++;
	 }
      }
	  if(node->GetDegree()>1)
      return 2 * numberOfTriangles / (node->GetDegree() * (node->GetDegree() - 1));
	  else
	  return 0.0;
   }
}
コード例 #13
0
//注意:需要特征值计算,Fortran
void scn::ComputeSpectralDensity(std::unordered_map<double,double> &result,UGraph::pGraph graph,double slide)
{
   auto lambdas = ComputeSpectrum(graph);
   int start = floor(lambdas[0]);
   int end = ceil(lambdas[lambdas.size() - 1]);
   for(int i = 0; i < (end - start) / slide; i++)
   {
      result[double(start) + i * slide] = 0;
   }
   for(int ii=0;ii<lambdas.size();ii++)
   {
      result[floor(lambdas[ii] / slide) * slide]++;
   }
   //normalized
   for(auto iter = result.begin(); iter != result.end(); iter++)
   {
      iter->second /= graph->GetNumberOfNodes();
   }
   //return result;
}
コード例 #14
0
double scn::GetGlobalEfficiency(UGraph::pGraph graph)
{
   double sum = 0;
   std::unordered_map<size_t,size_t> distance;
   //auto& distance = distance_sssp;
   distance.clear();
   for(auto node = graph->begin(); node != graph->end(); node++)
   {
      RunSPFA(graph,*node,distance);
      //add distance
      for(auto iter = distance.begin(); iter != distance.end(); iter++)
      {
	 if(iter->first == *node)
	    continue;

	 sum += 1.0 / static_cast<double>(iter->second);
      }
   }
   double size = static_cast<double>(graph->GetNumberOfNodes());
   return size * (size - 1) / sum;
}
コード例 #15
0
void scn::GetClusteringDegreeCorre(pair<double, vector<pair<size_t, double>>> &correlation,UGraph::pGraph graph)
{
   unordered_map<size_t, double> degree_dist, degree_cc;
   //accumulate degree and clustering coefficient
   for(auto node = graph->begin(); node != graph->end(); node++)
   {
      degree_dist[node->GetDegree()]++;
      degree_cc[node->GetDegree()] += GetClusteringCoeff(graph,*node);
   }
   //get average
   double degree_average = 0;//1-order moment of degree
   double degree_sqr_average = 0;//2-order moment of degree
   for(auto iter = degree_dist.begin(); iter != degree_dist.end(); iter++)
   {
      //average clustering coefficient in each degree
      degree_cc[iter->first] /= iter->second;
      //probability of degree
      iter->second /= graph->GetNumberOfNodes();
      //1-order moment of degree
      degree_average += iter->first * iter->second;
      //2-order moment of degree
      degree_sqr_average += iter->first * iter->first * iter->second;
   }
   //compute global clustering degree correlation
   double clustering_degree_corre = 0;
   for(auto iter = degree_dist.begin(); iter != degree_dist.end(); iter++)
   {
      clustering_degree_corre += iter->first * (iter->first - 1) * iter->second
	 * degree_cc[iter->first];
   }
   clustering_degree_corre /= (degree_sqr_average - degree_average);
   //sort and get result
   correlation.second.assign(degree_cc.begin(), degree_cc.end());
   sort(correlation.second.begin(), correlation.second.end());
   correlation.first=clustering_degree_corre;
}