Esempio n. 1
0
int create_layers(int & layer_index, int num_of_layers, int ori, ostream & comout, int max_assignment_prev, ostream & multiplexout) {
    
    
    // all edges in ori graph
    set<pair<int, int> > all_edges;
    // getting graph
    char buffer[1000];
    sprintf(buffer, "network_%d", ori);
    int n1,n2;
    ifstream gin(buffer);
    while(gin>>n1) {
        gin>>n2;
        all_edges.insert(make_pair(min(n1, n2), max(n1, n2)));
    }
    gin.close();
    cout<<"Read graph #"<<ori<<". Edges: "<<all_edges.size()<<endl;
    deque<pair<int, int> > all_edges_deque;
    for(set<pair<int, int> >::iterator its= all_edges.begin(); its!=all_edges.end(); its++) {
        all_edges_deque.push_back(*its);
    }

    // looping over layers
    
    map<int, int> node_assignment;
    sprintf(buffer, "community_%d", ori);
    ifstream gin2(buffer);
    while(gin2>>n1) {
        gin2>>n2;
        node_assignment[n1]=n2;
    }

    

    int max_assignment=0;
    
    for(int layer=0; layer<num_of_layers; layer++) {

        cout<<"printing layer "<<layer_index<<endl;
        
        sprintf(buffer, "cp community_%d community_layer_%d", ori, layer_index);
        int sy; sy=system(buffer);
        
        for(map<int, int>::iterator itm= node_assignment.begin(); itm!=node_assignment.end(); itm++) {
            comout<<layer_index+1<<" "<<itm->first<<" "<<max_assignment_prev+itm->second<<endl;
            max_assignment=max(max_assignment, max_assignment_prev+itm->second);
        }
        sprintf(buffer, "network_layer_%d", layer_index);
        ofstream multiplex_outfile(buffer);
        sample_graph(multiplex_outfile, all_edges_deque, 1./num_of_layers, multiplexout, layer_index+1);
        ++layer_index;
    }
    
    return max_assignment;
}
Esempio n. 2
0
int main(){
    graph_type graph = sample_graph();
    // graph.load_format(graph_dir, format);

    std::cout << "#vertices: " << graph.num_vertices() << " #edges:" << graph.num_edges() << std::endl;

    saedb::IEngine<Simulate> *engine = new saedb::EngineDelegate<Simulate>(graph);

    size_t n = graph.num_vertices();
    int round = 1;
    int ss_cnt = 2;
    std::string outpath = "result";

    // Initilization
    greedy_s = new double [ss_cnt+1];
    mark = new bool[n];

    std::priority_queue<std::pair<double, std::pair<int,int>>> q;
    std::vector<uint32_t> seed_list;
    // calculate the influenced number of nodes by each node, and store into q
    for (int i = 0; i < n; i++) {
        std::pair<int, int> tp = std::make_pair(i, 1);
        seed_list.push_back(i);
        std::cout << "simulating ..." << i << std::endl;
        double val = simulate(engine, graph, seed_list, round);
        q.push(make_pair(val, tp));
        seed_list.clear();
    }

    std::cout << "Simulating all nodes done..." << std::endl;
    double ret = 0;
    // try ss_cnt seeds
    seed_list.clear();
    for (int ss = 1; ss <= ss_cnt; ss++) {
        while (1) {
            // fetch the node that can influence the most nodes
            std::pair<double, std::pair<int, int> > tp = q.top();
            q.pop();
            if (tp.second.second == ss) {
                ret += tp.first;
                greedy_s[ss] = ret;
                seed_list.push_back(tp.second.first);
                break;
            } else {
                seed_list.push_back(tp.second.first);
                double val = simulate(engine, graph, seed_list, round);
                seed_list.pop_back();
                tp.second.second = ss;
                tp.first = val - ret;
                q.push(tp);
            }
        }
    }

    std::ofstream result_file;
    result_file.open (outpath);
    for (int i = 1; i <= ss_cnt; i++)
    {
        result_file << greedy_s[i] << std::endl;
    }
    result_file.close();
    return 0;

    delete greedy_s;
    delete mark;
    delete engine;
    return 0;
}