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());
   }
}
Exemple #2
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::GetVulnerability(UGraph::pGraph graph,size_t indexOfNode)
{
   double original_efficiency = GetGlobalEfficiency(graph);
   std::pair<IndexList, IndexList> result;
   
   if(indexOfNode != Graph::NaF)
   {//compute the vulnerability of one node
      //remove the current node, note: only in_degree is used in
      //UGraph
      result = graph->RemoveNode(indexOfNode);
      IndexList& edges = result.first;
	  //node edge
      double new_efficiency = GetGlobalEfficiency(graph);
      //restore the original graph
      graph->AddEdge(graph->AddNode(indexOfNode), edges);
      return (original_efficiency - new_efficiency) / original_efficiency;
   }
   else
   {//compute the vunlerability of the whole network
      IndexList setOfNode = graph->CopyIndexOfNodes();
      //compute
      double max_vul = -1e200, new_vul;
      for(auto node = setOfNode.begin(); node != setOfNode.end(); node++)
      {
	 //remove the current node, note: only in_degree is used in
	 //UGraph
	 result = graph->RemoveNode(*node);
	 IndexList& edges = result.first;
	 new_vul = (original_efficiency - GetGlobalEfficiency(graph)) / original_efficiency;
	 if(new_vul > max_vul)
	    max_vul = new_vul;
	 //restore the original graph
	 graph->AddEdge(graph->AddNode(*node), edges);
      }
      return max_vul;
   }
}