Exemple #1
0
UGraph::pGraph scn::GenSmallWorldNetworkByNW(size_t numberOfNodes, size_t k,
						   double probability)
{
   assert(2* k + 1 <= numberOfNodes);
   assert(probability >= 0);
   assert(probability <= 1);
   
   //generate k-nearest network
   UGraph::pGraph graph = GenKNearestNetwork(numberOfNodes, k);
   //add edges randomly
   size_t numberOfEdges = numberOfNodes * (numberOfNodes - 2 * k - 1) / 2 * probability;
   size_t sum_edges = 0;
   size_t one, two;
   srand(size_t(time(00)));

   do
   {
      one = rand() % numberOfNodes;
      two = rand() % numberOfNodes;
      if(!(one == two || graph->HasEdge(one, two)))
      {
	 graph->AddEdge(one, two);
	 sum_edges++;
      }
   }while(sum_edges < numberOfEdges);

   return graph;
}
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;
   }
}
size_t scn::RandomWalkBySARW(UGraph::pGraph graph,size_t indexOfSource, size_t indexOfTarget)
{
   unordered_set<size_t> neighbors_of_target(graph->find(indexOfTarget)->begin(),
					     graph->find(indexOfTarget)->end());
   vector<size_t> neighbors;
   size_t steps = 0;
   size_t next = indexOfSource;
   unordered_set<size_t> history;
   stack<size_t> precessors;
   history.insert(indexOfSource);
   precessors.push(indexOfSource);

   srand(size_t(time(00)));
// judge first
   if(graph->HasEdge(indexOfSource, indexOfTarget))
      return 1;

   do
   {
      auto other = graph->find(next);
      neighbors.clear();
      for(auto iter = other->begin(); iter != other->end(); iter++)
      {
	 if(history.find(*iter) == history.end())
	    neighbors.push_back(*iter);
      }
      if(neighbors.empty())
      {
	 history.insert(*other);
	 precessors.pop();
	 next = precessors.top();
	 history.insert(next);
	 steps++;
	 continue;
      }
      do
      {
	 next = neighbors[rand() % neighbors.size()];
      }while(history.find(next) != history.end());
      steps++;
      history.insert(next);
      precessors.push(next);
   }while(neighbors_of_target.find(next) == neighbors_of_target.end());

   return steps;
}
double scn::GetPearsonCorrCoeff(UGraph::pGraph graph)
{
   size_t sum = 0;
   size_t product = 0;
   size_t square_sum = 0;
   for(auto i = graph->begin(); i != graph->end(); i++)
   {
      for(auto j = i + 1; j != graph->end(); j++)
      {
	 if(graph->HasEdge(*i, *j))
	 {
	    sum += i->GetDegree() + j->GetDegree();
	    product += i->GetDegree() * j->GetDegree();
	    square_sum += i->GetDegree() * i->GetDegree() + j->GetDegree() * j->GetDegree();
	 }
      }
   }
   double tmp = 1.0 / static_cast<double>(graph->GetNumberOfEdges());
   return (tmp * product - (tmp * sum) * (tmp * sum) / 4) /
      (tmp * square_sum / 2 - (tmp * sum) * (tmp * sum) / 4); 
}
double scn::GetTransitivity(UGraph::pGraph graph)
{
   size_t numberOfTriangles = 0;
   size_t numberOfTriples = 0;
   for(auto node = graph->begin(); node != graph->end(); node++)
   {
      for(auto other1 = node->begin(); other1 != node->end(); other1++)
      {
	 for(auto other2= other1 + 1; other2 != node->end(); other2++)
	 {
	    if(graph->HasEdge(*other1, *other2))
	    {
	       numberOfTriangles++;
	    }
	 }
      }
      numberOfTriples += node->GetDegree() * (node->GetDegree() - 1) / 2;
   }
   return static_cast<double>(numberOfTriangles) / 
      static_cast<double>(numberOfTriples);
}
double scn::GetRichClubCoeff(UGraph::pGraph graph,size_t degree)
{
   IndexList setOfHighNode;//whose degree is greater than
                           //argument degree
   for(auto node = graph->begin(); node != graph->end(); node++)
   {
      if(node->GetDegree() > degree)
      {
	 setOfHighNode.push_back(*node);
      }
   }
   double sum = 0;
   for(auto one = setOfHighNode.begin(); one != setOfHighNode.end(); one++)
   {
      for(auto two = one + 1; two != setOfHighNode.end(); two++)
      {
	 if(graph->HasEdge(*one, *two))
	    sum++;
      }
   }
   return 2 * sum / static_cast<double>(setOfHighNode.size() * (setOfHighNode.size() - 1));
}
Exemple #7
0
UGraph::pGraph scn::RenormalizeByBoxCounting(UGraph::pGraph graph, size_t length)
{
   UGraph::pGraph temp_graph(new UGraph(*graph));
   Ruler ruler(temp_graph);
   Map<size_t> group_index;//stores the index of group of each
			   //previous nodes. pairs(index in graph,
			   //index of group)
   size_t numberOfGroups = 0;
   
   while(!temp_graph->empty())
   {
      auto list = ruler.FindClosureGroup(*(temp_graph->begin()), length);
      
      for(auto i : list)
      {
	 //add to group_index
	 group_index[i] = numberOfGroups;
	 //remove from temp_graph
	 temp_graph->RemoveNode(i);
      }
      numberOfGroups++;
   }
   //renormalize
   temp_graph.reset(new UGraph(numberOfGroups));
   //add edge
   for(auto i = group_index.begin(); i != group_index.end();i++)
   {
      for(auto j = i; j != group_index.end(); j++)
      {
	 if(i->second != j->second && graph->HasEdge(i->first, j->first))
	 {
	    temp_graph->AddEdge(i->second, j->second);
	 }
      }
   } 
   return temp_graph;
}