Ejemplo n.º 1
0
int main(int argc, char* argv[]) {

  //Here is the input file name:
  ifstream input(argv[1]);
  Network my_net(input);
 
  ComponentPart cp;
  auto_ptr<NetworkPartition> components(cp.partition(my_net));
  int component = 0;
  auto_ptr<Iterator<Network*> > comp_it(components->getComponents());
  while(comp_it->moveNext()) {
    stringstream name;
    name << argv[1] << "." << component;
    component++;
    ofstream output(name.str().c_str());
    Network* this_comp = comp_it->current();
    this_comp->printTo(output);
  }
  return 1;
}
Ejemplo n.º 2
0
int main(int argc, char** argv) {

  Network my_net(cin);
  cout << "graph G {" << endl; 
  Network::EdgeSet printed_edges;
  //Look on components:
  ComponentPart cp;
  NewmanCom comfinder;
  auto_ptr<NetworkPartition> components_np(cp.partition(my_net));
  const vector<Network*>& components = components_np->asVector();
  vector<Network*>::const_iterator comp_it;
  int community = 0;
  for(comp_it = components.begin(); comp_it != components.end(); comp_it++) {
    stringstream com;
    com << community++;
    Network* this_net = *comp_it;
    printCommunities(comfinder, com.str(), *this_net, cout, printed_edges, 1);
  }
  printEdges(my_net, printed_edges, "[len=16, color=green]");
  cout << "}" << endl;
  return 1;
}
Ejemplo n.º 3
0
int main(int argc, char* argv[]) {

  if( argc < 4 ) {
    //
    cout << "Usage: " << argv[0] << " graph blacklist whitelist" << endl;
    return 0;
  }
  ifstream input(argv[1]);
  ofstream spam(argv[2]);
  ofstream ham(argv[3]);
  Network graph(input);
  
  double min_cc = 0.1;
  double kmfrac = 0.6;
  int min_size = 10;
  double whole_graph_c = graph.getTransitivity();
  //The subgraphs of spam and nonspam
  set<Network*> spamg, hamg;
  
     ComponentPart cp;
     auto_ptr<NetworkPartition> netpart(cp.partition(graph));
     vector<Network*> components(netpart->asVector());
     vector<Network*>::iterator comp_it;
     Edge* cut_edge = 0;
     while( components.size() > 0 ) {
       //Choose the biggest graph
       comp_it = components.begin();
       Network* this_net = *comp_it;
       Network::NodePSet::const_iterator n_it;
       int kmax = 0;
       auto_ptr<NodeIterator> ni( this_net->getNodeIterator() );
       while( ni->moveNext() ) {
	 Node* this_node = ni->current();
         if( kmax < this_net->getDegree( this_node ) ) {
           kmax = this_net->getDegree( this_node );
	 }
       }
       int size = this_net->getNodeSize();
       if( size > min_size ) {
         if( this_net->getClusterCoefficient() > min_cc ) {
	   //Ham:
	   hamg.insert( this_net );
	 }
	 //Maybe spam:
	 else if( (kmax + 1) < kmfrac * size ) { 
           if( this_net->getClusterCoefficient() == 0.0 ) {
             //Spam:
             spamg.insert( this_net );
	   }
           else {
             //Ambiguous
             map<Edge*, double> bet;
	     Network* this_comp = *comp_it;
             cut_edge = this_comp->getEdgeBetweenness(bet);
             this_comp->remove( cut_edge->first );
             this_comp->remove( cut_edge->second );
	     auto_ptr<NetworkPartition> split_part(cp.partition(*this_comp)); 
             const vector<Network*>& split = split_part->asVector();
             components.insert(components.end(), split.begin(), split.end());
           }
         }
       }
       //cout << "graphs: " << components.size() << endl;
       components.erase( comp_it );
     }

  //Print out the nodes in each spamg and hamg
  Network::NodePSet::const_iterator nit;
  set<Network*>::iterator netsetit;
  for(netsetit = spamg.begin(); netsetit != spamg.end(); netsetit++) {
    auto_ptr<NodeIterator> ni2( (*netsetit)->getNodeIterator() );
    while( ni2->moveNext() ) {
      Node* this_node = ni2->current();
      spam << this_node->toString() << endl;
    }
  }
  for(netsetit = hamg.begin(); netsetit != hamg.end(); netsetit++) {
    auto_ptr<NodeIterator> ni2( (*netsetit)->getNodeIterator() );
    while( ni2->moveNext() ) {
      Node* this_node = ni2->current();
      ham << this_node->toString() << endl;
    }
  }
  //Delete the memory:
  return 1;
}
Ejemplo n.º 4
0
int main(int argc, char* argv[]) {

     //Here we consider a network for a P2P topology
     Ran1Random r1(time(NULL)), r2(-1000), r3(-1000000), r4(-5678);
     ContentNetwork* my_cnet = 0;
     Node* a_node = 0;
     ContentNode* c_node = 0;
     vector<Node*> node_vector;
     
     //Here is all the content
     vector<ContentNode*> content_vector;
     int cont_count = 10; 
     //Look for each content 10 times:
     int runs = 10*cont_count;
     content_vector.reserve(cont_count);
     for(int i = 0; i < cont_count; i++) {
         content_vector.push_back(new ContentNode);
     }
     
     Network* my_net = 0;
     //my_net = new PrefDelCompNetwork(RandomNetwork(10,1.0,r1),r1,0.75,1,0.6666);
     //my_net = new DoublePrefAtNetwork( RandomNetwork(10,0.6,r1), r1, 1 );
     //my_net = new PrefAtNetwork( RandomNetwork(2,1.0,r1), r1, 1 );
     //ifstream input("internal.dat");
     //my_net = new Network(input);
     /*while(my_net->getNodes().size() < 40000) {
       dynamic_cast<Incrementable*>(my_net)->incrementTime();
       }*/
     //The exponent -2.236 is what we fit to DPA network of the same size
     PowerLawDRV pl(r1, -2.3,2,1000);
     DegreeLawNetFac nf(40000, pl, r1, true);
     my_net = nf.create();
		    
     //my_net = new DegreeLawRandomNetwork(nodes, *pl, r1);
     //my_net = new GnutellaNetwork(argv[1],"limecrawler");
     //my_net = new GnutellaNetwork(argv[1],"ripeanu");

     Network *net = 0;
     int choice = 2;
     if(choice == 1){
       ComponentPart cp;
       auto_ptr<NetworkPartition> netpart(cp.partition(*my_net));
       const vector<Network*>& net_set = netpart->asVector();
       cout << "The number of components of this network is: " << net_set.size() << endl;
       vector<Network*>::const_iterator comp_it;
       cout << "The components have the following sizes: ";
       for(comp_it = net_set.begin(); comp_it != net_set.end(); comp_it++){
	 net = *(comp_it);
	 cout << net->getNodeSize() << " ";
       }
       net = 0;
     }
     else{
       //The exponent -1.81 is what we want for the email network
       int nodes = 10000;
       PowerLawDRV pl(r1,-2.1,2,(int)sqrt(nodes));
       DegreeLawNetFac my_fact(nodes, pl, r1, true);
       net = my_fact.create();
     }
     cout << endl;
     //use the biggest connected component;
     IntStats ns;
     auto_ptr<NodeIterator> ni( net->getNodeIterator() );
     ns.collect(net, &Network::getDegree, ni.get());
     cout << "#nodes: " << net->getNodeSize() << endl 
          << "#edges: " << net->getEdgeSize() << endl
	  << "#<k>: " << ns.getAverage() << endl
	  << "#<k^2>: " << ns.getMoment2() << endl;
     cout << "#ttl p hit_rate edges_crossed nodes_reached" << endl;
     //Put the nodes into a vector so we can randomly select them easier:
     node_vector.clear();
     ni->reset();
     while( ni->moveNext() ) {
       node_vector.push_back( ni->current() );
     }
     
     Message* query = 0;
     Message* implant = 0;
     
     int hits;
     double last_hr = 0.0;
     double hit_rate = 0.0;
     double av_edges = 0.0;
     double av_nodes = 0.0;
     double p, delta_p = 0.01;
     int ttl=15;
     //for(ttl = 2; ttl < 12; ttl++)
     {
       p = 0.01;
       //p = 1.0;
       while(p < 0.51)
       {
	 delete query;
	 query = new WalkAndPercMessage(r2,p,ttl,ttl);
	 //query = new MagnetMessage(r2,p,ttl);
	 //query = new BroadcastMessage(ttl);
	 
	 delete implant;
	 implant = new AnycastMessage(r4,1,ttl);
	 //implant = new MagnetMessage(r2,0.75, 2 * ttl);
	 //implant = new WalkAndPercMessage(r4,p,ttl);
	 //implant = new AnycastMessage(r4,1,0);
	 
	 delete my_cnet;
	 my_cnet = new ContentNetwork(*net);
	 // Insert the content into the network
	 for(int i = 0; i < cont_count; i++) 
	 {
	     a_node = node_vector[ r3.getInt( node_vector.size() - 1 ) ];
	     c_node = content_vector[i];
	     auto_ptr<Network> implantnet( implant->visit( a_node, *net) );
	     auto_ptr<NodeIterator> nit( implantnet->getNodeIterator() );
	     my_cnet->insertContent(c_node, nit.get() );
         }
         //Do a random search for each node and see how many are successful.
         Network::NodePSet res_nodes;
         hits = 0;
      
	 int hit_nodes = 0, crossed_edges = 0;
         for(int i = 0; i < runs; i++) {
	     a_node = node_vector[ r3.getInt( node_vector.size() - 1 ) ];
	     c_node = content_vector[ i % cont_count ];
	     auto_ptr<Network> visited( query->visit(a_node, *net) );
	     auto_ptr<NodeIterator> nit( visited->getNodeIterator() );
	     auto_ptr<Network> res_nodes(
			     my_cnet->queryForContent(c_node, nit.get() ) );
	     if( res_nodes->getNodeSize() != 0) {
	       ++hits;
	     }
	     hit_nodes += visited->getNodeSize();
	     crossed_edges += visited->getEdgeSize();
         }
	 hit_rate = (double)hits/(double)runs;
	 av_edges = (double)crossed_edges/(double)runs;
	 av_nodes = (double)hit_nodes/(double)runs;
	 cout << ttl << " " << p
		     << " " << hit_rate
		     << " " << av_edges
		     << " " << av_nodes
		     << endl;
	 /* Do an adapative step:
	 delta_p = 0.00005/abs(hit_rate - last_hr);
	 if( delta_p < 0.001 ) {
           delta_p = 0.001;
	 }
	 if( delta_p > 0.1 ) {
           delta_p = 0.1;
	 }*/
	 p += delta_p;
       }
     }
     //Delete all the content
     my_cnet->deleteContent();
     delete my_cnet;
     delete my_net;
     delete net;
	
  return 1;
	
}