void parallel_dc(graph_t& g, unsigned threadnum, gBenchPerf_multi & perf, int perf_group) { uint64_t chunk = (unsigned)ceil(g.num_vertices()/(double)threadnum); #pragma omp parallel num_threads(threadnum) { unsigned tid = omp_get_thread_num(); perf.open(tid, perf_group); perf.start(tid, perf_group); unsigned start = tid*chunk; unsigned end = start + chunk; if (end > g.num_vertices()) end = g.num_vertices(); for (unsigned vid=start;vid<end;vid++) { vertex_iterator vit = g.find_vertex(vid); // out degree vit->property().outdegree = vit->edges_size(); // in degree edge_iterator eit; for (eit=vit->edges_begin(); eit!=vit->edges_end(); eit++) { vertex_iterator targ = g.find_vertex(eit->target()); __sync_fetch_and_add(&(targ->property().indegree), 1); } } perf.stop(tid, perf_group); } }
void randomgraph_construction(graph_t &g, size_t vertex_num, size_t edge_num, gBenchPerf_event & perf, int perf_group) { vector<pair<size_t,size_t> > edges; for (size_t i=0;i<edge_num;i++) { edges.push_back(make_pair(rand()%vertex_num, rand()%vertex_num)); } perf.open(perf_group); perf.start(perf_group); for (size_t i=0;i<vertex_num;i++) { vertex_iterator vit = g.add_vertex(); vit->set_property(vertex_property(i)); } #ifdef SIM SIM_BEGIN(true); #endif for (size_t i=0;i<edge_num;i++) { edge_iterator eit; g.add_edge(edges[i].first, edges[i].second, eit); #ifndef SIM eit->set_property(edge_property(i)); #endif } #ifdef SIM SIM_END(true); #endif perf.stop(perf_group); }
//==============================================================// void dc(graph_t& g, gBenchPerf_event & perf, int perf_group) { perf.open(perf_group); perf.start(perf_group); #ifdef SIM SIM_BEGIN(true); #endif vertex_iterator vit; for (vit=g.vertices_begin(); vit!=g.vertices_end(); vit++) { // out degree vit->property().outdegree = vit->edges_size(); // in degree edge_iterator eit; for (eit=vit->edges_begin(); eit!=vit->edges_end(); eit++) { vertex_iterator targ = g.find_vertex(eit->target()); (targ->property().indegree)++; } } #ifdef SIM SIM_END(true); #endif perf.stop(perf_group); }// end dc
void degree_analyze(graph_t& g, uint64_t& indegree_max, uint64_t& indegree_min, uint64_t& outdegree_max, uint64_t& outdegree_min) { vertex_iterator vit; indegree_max=outdegree_max=0; indegree_min=outdegree_min=numeric_limits<uint64_t>::max(); for (vit=g.vertices_begin(); vit!=g.vertices_end(); vit++) { if (indegree_max < vit->property().indegree) indegree_max = vit->property().indegree; if (outdegree_max < vit->property().outdegree) outdegree_max = vit->property().outdegree; if (indegree_min > vit->property().indegree) indegree_min = vit->property().indegree; if (outdegree_min > vit->property().outdegree) outdegree_min = vit->property().outdegree; } return; }
// ヒントを与えた上でgreedy探索を行う // graph: 元となったグラフ構造 // parent_nodes, child_nodes: parent_nodesに含まれるnodeからchild_nodeに含まれるnodeにしか辺を張らない double learn_with_hint(graph_t& graph, std::vector<vertex_type> parent_nodes, std::vector<vertex_type> child_nodes) { // 子ノードの出現順序をランダムに std::shuffle(std::begin(child_nodes), std::end(child_nodes), engine_); // 最高評価値を保存しておく sampling_.make_cpt(graph); double eval_now = eval_(graph); // 親候補と子候補を全部回して様子見る for(auto const& child : child_nodes) { // 親ノードの出現順序をランダムに std::shuffle(std::begin(parent_nodes), std::end(parent_nodes), engine_); for(auto const& parent : parent_nodes) { if(auto edge = graph.add_edge(parent, child)) { // 辺が張れたならば,評価をする sampling_.make_cpt(graph); auto const eval_next = eval_(graph); if(eval_next < eval_now) // 良くなってるscore eval_now = eval_next; else // 変わらない or 悪い -> 元に戻す graph.erase_edge(edge); } } } return eval_now; }
void output(graph_t& g) { cout<<"Results: \n"; vertex_iterator vit; for (vit=g.vertices_begin(); vit!=g.vertices_end(); vit++) { cout<<"== vertex "<<vit->id()<<": edge#-"<<vit->edges_size()<<"\n"; } }
void reset_graph(graph_t & g) { vertex_iterator vit; for (vit=g.vertices_begin(); vit!=g.vertices_end(); vit++) { vit->property().indegree = 0; vit->property().outdegree = 0; } }
//==============================================================// void output(graph_t& g) { cout<<"Betweenness Centrality Results: \n"; vertex_iterator vit; for (vit=g.vertices_begin(); vit!=g.vertices_end(); vit++) { cout<<"== vertex "<<vit->id()<<": "<<vit->property().BC<<"\n"; } }
void reset_graph(graph_t & g) { vertex_iterator vit; for (vit=g.vertices_begin(); vit!=g.vertices_end(); vit++) { vit->property().root = vit->id(); } }
void output(graph_t& g) { cout<<"WCC Results: \n"; vertex_iterator vit; for (vit=g.vertices_begin(); vit!=g.vertices_end(); vit++) { cout << vit->id() << " " << vit->property().root << "\n"; } }
void graph_update(graph_t &g, vector<uint64_t> IDs) { for (size_t i=0;i<IDs.size();i++) { if (g.num_vertices()==0) break; g.delete_vertex(IDs[i]); } }
void output(graph_t& g) { cout<<"Degree Centrality Results: \n"; vertex_iterator vit; for (vit=g.vertices_begin(); vit!=g.vertices_end(); vit++) { cout<<"== vertex "<<vit->id()<<": in-"<<vit->property().indegree <<" out-"<<vit->property().outdegree<<"\n"; } }
void reset_graph(graph_t & g) { vertex_iterator vit; for (vit=g.vertices_begin(); vit!=g.vertices_end(); vit++) { vit->property().degree = 0; vit->property().removed = false; } }
void prepareGraphIncrementaly(graph_t &graph) { obj_t obj; P::Pose p; P::InformMatrix inform; graph.addNode(node_t(p, obj)); for (size_t i = 0; i < 19; ++i) { graph.addNode(node_t(p, obj)); graph.addEdge(edge_t(&graph.getNode(i), &graph.getNode(i + 1), p, inform)); } }
void parallel_init(graph_t& g, unsigned threadnum, vector<vector<uint64_t> >& global_input_tasks) { global_input_tasks.resize(threadnum); for (vertex_iterator vit=g.vertices_begin(); vit!=g.vertices_end(); vit++) { vit->property().root = vit->id(); global_input_tasks[vertex_distributor(vit->id(), threadnum)].push_back(vit->id()); } }
void parallel_init(graph_t& g, unsigned threadnum, vector<vector<uint64_t> >& global_input_tasks) { global_input_tasks.resize(threadnum); for (uint64_t vid=0;vid<g.vertex_num();vid++) { g.csr_vertex_property(vid).root = vid; global_input_tasks[vertex_distributor(vid, threadnum)].push_back(vid); } }
void delete_edge(graph_t& g, uint32_t u, uint32_t v) { auto it = g.find(u); // u does not exist if (it == g.end()) { return; } vector<uint32_t>& neighbors = it->second; neighbors.erase(remove_if(neighbors.begin(), neighbors.end(), [v](uint32_t n) {return n == v;}), neighbors.end()); }
size_t kcore(graph_t& g, size_t k, gBenchPerf_event & perf, int perf_group) { size_t remove_cnt=0; queue<vertex_iterator> process_q; // initialize for (vertex_iterator vit=g.vertices_begin(); vit!=g.vertices_end(); vit++) { size_t degree = vit->edges_size(); vit->property().degree = degree; if (degree < k) { process_q.push(vit); vit->property().removed = true; remove_cnt++; } } perf.open(perf_group); perf.start(perf_group); // remove vertices iteratively while (!process_q.empty()) { vertex_iterator vit = process_q.front(); process_q.pop(); for (edge_iterator eit=vit->edges_begin(); eit!=vit->edges_end(); eit++) { size_t targ = eit->target(); vertex_iterator targ_vit = g.find_vertex(targ); if (targ_vit->property().removed==false) { targ_vit->property().degree--; if (targ_vit->property().degree < k) { targ_vit->property().removed = true; process_q.push(targ_vit); remove_cnt++; } } } } perf.stop(perf_group); return remove_cnt; } // end kcore
//==============================================================// void output(graph_t& g) { cout<<"kCore Results: \n"; vertex_iterator vit; for (vit=g.vertices_begin(); vit!=g.vertices_end(); vit++) { cout<<"== vertex "<<vit->id()<<": degree-"<<vit->property().degree <<" removed-"; if (vit->property().removed) cout<<"true\n"; else cout<<"false\n"; } }
size_t biconnected_components(graph_t& g, vector<graph_t*>& bicomponents) { map< edge_descriptor_t, unsigned > bicomponent_edges; vector<vertex_discripter_t> articulation_points; articulation_points.reserve(num_vertices(g)); int n = biconnected_components(g, make_assoc_property_map(bicomponent_edges), back_inserter(articulation_points) ).first; int o = bicomponents.size(); for (int i=0; i<n; i++) bicomponents.push_back( &(g.create_subgraph()) ); graph_traits<graph_t>::edge_iterator e, e_end; for (tie(e, e_end) = edges(g); e != e_end; ++e) { add_vertex_once( source(*e,g), *bicomponents[o+bicomponent_edges[*e]] ); add_vertex_once( target(*e,g), *bicomponents[o+bicomponent_edges[*e]] ); } // cout << "Graph #" << ... << " : " << get(graph_name, g); // cout << " |V|=" << num_vertices(g) << endl; // cout << " |Component|=" << bicomponents.size() << endl; vector<vertex_discripter_t>::iterator v,v_end; cout << "Art.pts.\tComponents\n"; // cout << articulation_points.size() << endl; for (v=articulation_points.begin(), v_end=articulation_points.end(); v != v_end; v++) { cout << get(&vertex_properties::id, g, *v) << "\t\t"; for (int i=0; i<n; i++) if ((bicomponents[o+i]->find_vertex(*v)).second) cout << ' ' << o+i << ' '; cout << endl; } return n; }
dists_t dijkstra_shortest_path(graph_t g, vert_t s) { set<pair<dist_t, vert_t> > q; // implicit ordering by distance // ps_t ps(g.size(), -1); dists_t dists(g.size(), dist_infty); dists[s] = 0; q.insert(make_pair(dists[s], s)); while( !q.empty() ) { dist_t dist = q.begin()->first; vert_t v = q.begin()->second; q.erase( q.begin() ); // pop first for(int i=0; i<g[v].size(); i++) { vert_t t = g[v][i].to; dist_t d_with = dist + g[v][i].len; if( d_with < dists[t] ) { // relax q.erase( make_pair(dists[t], t) ); q.insert( make_pair(d_with, t) ); dists[t] = d_with; // ps[t] = v; } } } return dists; }
vector<vector<edge_t> > enumerate_edges(graph_t const &graph, int count) { vector<edge_t> edges; // now, consider edges for(graph_t::vertex_iterator i(graph.begin_verts());i!=graph.end_verts();++i) { unsigned int v = *i; for(graph_t::edge_iterator j(graph.begin_edges(v));j!=graph.end_edges(v);++j) { unsigned int w = j->first; // now add this edge(s) to nauty graph if(v <= w) { edges.push_back(edge_t(v,w)); } } } return enumerate_edges_helper(edges,count); }
state_t find_stop_state(const graph_t<NodeCount> & graph, const state_t & start_state, size_t length, sigma_t sigma) { state_t state(start_state); for(size_t i = 0; i < length; ++i) state = graph.next(state, sigma); return state; }
void constructGraph(graph_t &graph, int n){ while(n--){ int a,b; cin >> a >> b; if(graph.find(a)==graph.end()){ vector<int> v; v.push_back(b); graph[a] = v; }else{ graph[a].push_back(b); } if(graph.find(b)==graph.end()){ vector<int> v; v.push_back(a); graph[b] = v; }else{ graph[b].push_back(b); } } }
void parallel_randomgraph_construction(graph_t &g, size_t vertex_num, size_t edge_num) { vector<pair<size_t,size_t> > edges; for (size_t i=0;i<edge_num;i++) { edges.push_back(make_pair(rand()%vertex_num, rand()%vertex_num)); } for (size_t i=0;i<vertex_num;i++) { vertex_iterator vit = g.add_vertex(); vit->set_property(vertex_property(i)); } uint64_t chunk = (unsigned)ceil(edge_num/(double)threadnum); #pragma omp parallel num_threads(threadnum) { unsigned tid = omp_get_thread_num(); unsigned start = tid*chunk; unsigned end = start + chunk; if (end > edge_num) end = edge_num; #ifdef SIM SIM_BEGIN(true); #endif for (size_t i=start;i<end;i++) { edge_iterator eit; g.add_edge(edges[i].first, edges[i].second, eit); #ifndef SIM eit->set_property(edge_property(i)); #endif } #ifdef SIM SIM_END(true); #endif } }
double operator()(graph_t& graph, std::vector<vertex_type> vertexes) { std::shuffle(vertexes.begin(), vertexes.end(), engine_); // bestな構造を保持しておく sampling_.make_cpt(graph); double eval_now = eval_(graph); for(auto it = vertexes.begin(); it != vertexes.end();) { auto const child_iter = it; std::shuffle(++it, vertexes.end(), engine_); for(auto parent_iter = it; parent_iter != vertexes.end(); ++parent_iter) { if(auto edge = graph.add_edge(*parent_iter, *child_iter)) { // 辺を貼れたなら調べてみる sampling_.make_cpt(graph); auto const eval_next = eval_(graph); if(eval_next < eval_now) { // 良くなってる eval_now = eval_next; } else { // 変わらない,もしくは悪い == 元に戻す graph.erase_edge(edge); } } } } return eval_now; }
path_ps_t bfs( graph_t g, capmat_t caps, vert_t s, vert_t t ) { path_ps_t ps( g.size(), -1 ); // parents ps[s] = s; queue<vert_t> q; q.push(s); while( !q.empty() ) { vert_t u = q.front(); q.pop(); if( u == t ) return ps; for( int i=0; i<g[u].size(); i++ ) { vert_t v = g[u][i]; if( ps[v] < 0 && caps[u][v] > 0 ) { ps[v] = u; q.push(v); } } } }
//#undef __FUNCT__ //#define __FUNCT__ "LowStretchSpanningTreeHelper" void LowStretchSpanningTreeHelper(graph_t& g,const int root,const float alpha, graph_t& h)//,int perm[]) { int n,i,j,k; std::vector<int> size,x,y; std::vector<std::vector<int> > idx; int ierr; // PetscFunctionBegin; weight_map_t edge_weight_g = get(edge_weight_t(),g); // VertexWeight vertex_weight_h = get(vertex_weight_t(),h); n = num_vertices(g); if (n > 2) { ierr = StarDecomp(g,root,1.0/3.0,alpha,k,size,idx,x,y); j = 0; for (i=1;i<=k;i++) { graph_t& g1 = g.create_subgraph(idx[i].begin(),idx[i].end()); graph_t& h1 = h.create_subgraph(idx[i].begin(),idx[i].end()); LowStretchSpanningTreeHelper(g1,g1.global_to_local(g.local_to_global(x[i-1])),alpha,h1);//,perm+j); j += size[i]; } graph_t& g1 = g.create_subgraph(idx[0].begin(),idx[0].end()); graph_t& h1 = h.create_subgraph(idx[0].begin(),idx[0].end()); LowStretchSpanningTreeHelper(g1,g1.global_to_local(g.local_to_global(root)),alpha,h1);//,perm+j); for (i=0;i<k;i++) { float w = get(edge_weight_g,edge(x[i],y[i],g).first); add_edge(x[i],y[i],w,h); // put(vertex_weight_h,x[i],get(vertex_weight_h,x[i])+w); // put(vertex_weight_h,y[i],get(vertex_weight_h,y[i])+w); } } else if (n == 2) { graph_traits<graph_t>::edge_descriptor e = *(out_edges(root,g).first); int t = target(e,g); float w = get(edge_weight_g,e); add_edge(root,t,w,h); // put(vertex_weight_h,root,get(vertex_weight_h,root)+w); // put(vertex_weight_h,t,get(vertex_weight_h,t)+w); // perm[0] = g.local_to_global(t); // perm[1] = g.local_to_global(root); } else /* n == 1 */ { // perm[0] = g.local_to_global(root); } //return 0; }
state_t generate_observation_sequence(const graph_t<NodeCount> & graph, size_t length, ObservationIt it) { /* Randomize start position */ sigma_t sigma(random_sigma<NodeCount>()); //std::cerr << "sigma: " << sigma << std::endl; state_t state(rand() % NodeCount, GRAPH_0); //std::cerr << "state chosen: " << state.v << std::endl; for(size_t i = 0; i < length; ++i) { state = graph.next(state, sigma); //std::cerr << "state: " << state << std::endl; it++ = state.e; } return state; }
int shortest_path(const graph_t& g, const graph_t& g_r, uint32_t u, uint32_t v) { // check if u exists and has outgoing edges auto it = g.find(u); if (it == g.end() || (it->second).size() == 0) return -1; // check if v exists and has incoming edges it = g_r.find(v); if (it == g_r.end() || (it->second).size() == 0) return -1; // find shortest path distance from u to v with a bidi-BFS vector<uint32_t> f {u}; // forward fringe vector<uint32_t> r {v}; // reverse fringe unordered_map<uint32_t,bool> visited_f; unordered_map<uint32_t,bool> visited_r; visited_f[u] = true; visited_r[v] = true; int path_length_f = 0; int path_length_r = 0; while (f.size() > 0 && r.size() > 0) { if (f.size() <= r.size()) { // expand the forward fringe /*cout << "f: "; for (uint32_t i : f) cout << i << " "; cout << endl;*/ path_length_f++; auto this_level = move(f); // f is now empty for (auto node : this_level) { if (g.find(node) == g.end()) continue; for (auto neighbor : g.at(node)) { if (!visited_f[neighbor]) { f.push_back(neighbor); visited_f[neighbor] = true; } if (visited_r[neighbor]) { return path_length_f + path_length_r; } } } } else { /*cout << "r: "; for (uint32_t i : r) cout << i << " "; cout << endl;*/ path_length_r++; auto this_level = move(r); for (auto node : this_level) { if (g_r.find(node) == g_r.end()) continue; for (auto neighbor : g_r.at(node)) { if (!visited_r[neighbor]) { r.push_back(neighbor); visited_r[neighbor] = true; } if (visited_f[neighbor]) { return path_length_f + path_length_r; } } } } } return -1; }