/** * Will return a formatted string containing: * - the output of human_readable_terse(), * - the list of vertices and edges in a DOT-like syntax. * * @return string containing complete human readable representation of the topology. */ std::string base::human_readable() const { std::ostringstream s; s << human_readable_terse(); s << "Connections:\n\n"; std::pair<v_iterator,v_iterator> vertices = get_vertices(); std::pair<a_iterator,a_iterator> adj_vertices; for (; vertices.first != vertices.second; ++vertices.first) { adj_vertices = get_adjacent_vertices(*vertices.first); s << (*vertices.first); if (adj_vertices.first != adj_vertices.second) { s << " -> {"; while (adj_vertices.first != adj_vertices.second) { s << (*adj_vertices.first); ++adj_vertices.first; if (adj_vertices.first != adj_vertices.second) { s << ' '; } } s << '}'; } s << '\n'; } return s.str(); }
void EdgeIterator::step( MsqError& err ) { if (adjIter != adjList.end()) { ++adjIter; } while (adjIter == adjList.end() && ++vertIdx < patchPtr->num_nodes()) { get_adjacent_vertices( err ); MSQ_ERRRTN(err); } }
EdgeIterator::EdgeIterator( PatchData* p, MsqError& err ) : patchPtr(p), vertIdx(0) { p->generate_vertex_to_element_data(); if (patchPtr->num_nodes()) { get_adjacent_vertices( err ); if (adjIter == adjList.end()) { step(err); MSQ_ERRRTN(err); } } else adjIter = adjList.end(); }
int main(){ int i, n ,output[100]; graph g = create_graph(); add_edge(g, 0, 1); add_edge(g, 0, 2); add_edge(g, 1, 2); add_edge(g, 1, 3); #define PRINT_ADJACENT_VERTIES(v) { \ n = get_adjacent_vertices(g, v, output); \ if(n == 0) \ printf("No adjacent vertices of node" #v " \n"); \ else{ \ printf("Adjacent vertices of node "#v": "); \ for(i = 0; i < n; i++) \ printf("%5d", output[i]); \ printf("\n"); \ } \ } PRINT_ADJACENT_VERTIES(0); PRINT_ADJACENT_VERTIES(1); PRINT_ADJACENT_VERTIES(2); PRINT_ADJACENT_VERTIES(3); #undef PRINT_ADJACENT_VERTIES printf("\n"); printf("\n\nTest drop !\n"); drop_graph(&g); n = get_adjacent_vertices(g, 1, output); if(n == 0) printf("No adjacent vertices of node 1 \n"); else{ printf("Adjacent vertices of node 1: "); for(i = 0; i < n; i++) printf("%5d", output[i]); } printf("\n"); return 0; }
void clustered_ba::connect(const vertices_size_type &idx) { pagmo_assert(get_number_of_vertices() > 0); const vertices_size_type prev_size = get_number_of_vertices() - 1; if (prev_size < m_m0) { // If we had not built the initial m0 nodes, do it. // We want to connect the newcomer island with high probability, and make sure that // at least one connection exists (otherwise the island stays isolated). // NOTE: is it worth to make it a user-tunable parameter? const double prob = 0.0; // Flag indicating if at least 1 connection was added. bool connection_added = false; // Main loop. for (std::pair<v_iterator,v_iterator> vertices = get_vertices(); vertices.first != vertices.second; ++vertices.first) { // Do not consider the new vertex itself. if (*vertices.first != idx) { if (m_drng() < prob) { connection_added = true; // Add the connections add_edge(*vertices.first,idx); add_edge(idx,*vertices.first); } } } // If no connections were established and this is not the first island being inserted, // establish at least one connection with a random island other than n. if ((!connection_added) && (prev_size != 0)) { // Get a random vertex index between 0 and n_vertices - 1. Keep on repeating the procedure if by // chance we end up on idx again. boost::uniform_int<vertices_size_type> uni_int(0,get_number_of_vertices() - 1); vertices_size_type rnd; do { rnd = uni_int(m_urng); } while (rnd == idx); // Add connections to the random vertex. add_edge(rnd,idx); add_edge(idx,rnd); } } else { // Now we need to add j edges, choosing the nodes with a probability // proportional to their number of connections. We keep track of the // connection established in order to avoid connecting twice to the same // node. // j is a random integer in the range 1 to m. boost::uniform_int<edges_size_type> uni_int2(1,m_m); std::size_t i = 0; std::size_t j = uni_int2(m_urng); std::pair<v_iterator,v_iterator> vertices; std::pair<a_iterator,a_iterator> adj_vertices; while (i < j) { // Let's find the current total number of edges. const edges_size_type n_edges = get_number_of_edges(); pagmo_assert(n_edges > 0); boost::uniform_int<edges_size_type> uni_int(0,n_edges - 1 - i); // Here we choose a random number between 0 and n_edges - 1 - i. const edges_size_type rn = uni_int(m_urng); edges_size_type n = 0; // Iterate over all vertices and accumulate the number of edges for each of them. Stop when the accumulated number of edges is greater // than rn. This is equivalent to giving a chance of connection to vertex v directly proportional to the number of edges departing from v. // You can think of this process as selecting a random edge among all the existing edges and connecting to the vertex from which the // selected edge departs. vertices = get_vertices(); for (; vertices.first != vertices.second; ++vertices.first) { // Do not consider it_n. if (*vertices.first != idx) { adj_vertices = get_adjacent_vertices(*vertices.first); n += boost::numeric_cast<edges_size_type>(std::distance(adj_vertices.first,adj_vertices.second)); if (n > rn) { break; } } } pagmo_assert(vertices.first != vertices.second); // If the candidate was not already connected, then add it. if (!are_adjacent(idx,*vertices.first)) { // Connect to nodes that are already adjacent to idx with probability p. // This step increases clustering in the network. adj_vertices = get_adjacent_vertices(idx); for(;adj_vertices.first != adj_vertices.second; ++adj_vertices.first) { if(m_drng() < m_p && *adj_vertices.first != *vertices.first && !are_adjacent(*adj_vertices.first,*vertices.first)) { add_edge(*adj_vertices.first, *vertices.first); add_edge(*vertices.first, *adj_vertices.first); } } // Connect to idx add_edge(*vertices.first,idx); add_edge(idx,*vertices.first); ++i; } } } }
/** * Adjacent vertices are those connected from the interested vertex. * * @param[in] idx index of the interested vertex. * * @return number of adjacent vertices. */ base::edges_size_type base::get_num_adjacent_vertices(const vertices_size_type &idx) const { const std::pair<base::a_iterator,base::a_iterator> v = get_adjacent_vertices(idx); return boost::numeric_cast<edges_size_type>(std::distance(v.first,v.second)); }
/** * Adjacent vertices are those connected from the interested index. * * @param[in] idx interested index. * * @return vector of adjacent indices. */ std::vector<base::vertices_size_type> base::get_v_adjacent_vertices(const vertices_size_type &idx) const { const std::pair<base::a_iterator,base::a_iterator> tmp = get_adjacent_vertices(idx); return std::vector<base::vertices_size_type>(tmp.first,tmp.second); }