/** Construct a reversed copy of an arbitrary NGHolder, mapping starts to * accepts. */ void reverseHolder(const NGHolder &g_in, NGHolder &g) { // Make the BGL do the grunt work. ue2::unordered_map<NFAVertex, NFAVertex> vertexMap; boost::transpose_graph(g_in.g, g.g, orig_to_copy(boost::make_assoc_property_map(vertexMap)). vertex_index_map(get(&NFAGraphVertexProps::index, g_in.g))); // The transpose_graph operation will have created extra copies of our // specials. We have to rewire their neighbours to the 'real' specials and // delete them. NFAVertex start = vertexMap[g_in.acceptEod]; NFAVertex startDs = vertexMap[g_in.accept]; NFAVertex accept = vertexMap[g_in.startDs]; NFAVertex acceptEod = vertexMap[g_in.start]; // Successors of starts. for (const auto &e : out_edges_range(start, g)) { NFAVertex v = target(e, g); add_edge(g.start, v, g[e], g); } for (const auto &e : out_edges_range(startDs, g)) { NFAVertex v = target(e, g); add_edge(g.startDs, v, g[e], g); } // Predecessors of accepts. for (const auto &e : in_edges_range(accept, g)) { NFAVertex u = source(e, g); add_edge(u, g.accept, g[e], g); } for (const auto &e : in_edges_range(acceptEod, g)) { NFAVertex u = source(e, g); add_edge(u, g.acceptEod, g[e], g); } // Remove our impostors. clear_vertex(start, g); remove_vertex(start, g); clear_vertex(startDs, g); remove_vertex(startDs, g); clear_vertex(accept, g); remove_vertex(accept, g); clear_vertex(acceptEod, g); remove_vertex(acceptEod, g); // Renumber so that g's properties (number of vertices, edges) are // accurate. g.renumberVertices(); g.renumberEdges(); assert(num_vertices(g) == num_vertices(g_in)); assert(num_edges(g) == num_edges(g_in)); }
void constraints() { v = add_vertex(g); clear_vertex(v, g); remove_vertex(v, g); p = add_edge(u, v, g); remove_edge(u, v, g); remove_edge(e, g); }
void MergeAllParents::mergeAll(std::pair<ParentsIterator,ParentsIterator> pair, VertexID child, set<VertexID> &A, set<VertexID> &B) { set<VertexID> parents2; set<VertexID> parents1; set<VertexID>::iterator pit,Aelt,Belt; ParentsIterator p,p_end,p2,p2_end; InEdgeIterator e,e_end; for (tie(p,p_end) = pair; p != p_end; p++) { if (*p != m_invartask) { parents1.insert(*p); } } for (pit=parents1.begin(); pit != parents1.end(); pit++) { addContainsTask(child,*pit); for (tie(p2,p2_end) = parents(*pit,*m_taskgraph); p2 != p2_end; p2++) { if (parents1.find(*pit) == parents1.end() && *p2 != m_invartask) parents2.insert(*p2); } // Add edge from parents^2 to child for(tie(e,e_end) = in_edges(*pit,*m_taskgraph); e != e_end; e++) { // Adding edge here invalidates pair iterators if (parents1.find(source(*e,*m_taskgraph)) == parents1.end()) { EdgeID newEdge = add_edge(source(*e,*m_taskgraph),child,m_taskgraph); ResultSet &set = getResultSet(newEdge,m_taskgraph); ResultSet &oldSet = getResultSet(*e,m_taskgraph); set.make_union(&oldSet); } } } // Add edge from new node to all in A for (Aelt=A.begin(); Aelt != A.end(); Aelt++) { add_edge(child,*Aelt,m_taskgraph); } // Can not iterate through pair since add_edge above invalidates that // iterator. for (pit=parents1.begin(); pit != parents1.end(); pit++) { if (B.find(*pit) == B.end() && *pit != m_invartask) { (*m_taskRemoved)[*pit] = true; clear_vertex(*pit,*m_taskgraph); remove_vertex(*pit,*m_taskgraph); } } // Remove edges from elts in B to child, the elts are already dupl. into child. for (Belt = B.begin(); Belt != B.end(); Belt++) { remove_edge(*Belt,child,*m_taskgraph); } }
bool CSimpleUGraph< ObjT, Compare >::RemoveVertex( const ObjT & oV ) { typename DataVertexMapT::iterator pMapPos = oDataToVertexMap.find( oV ); if( pMapPos == oDataToVertexMap.end() ) return false; Vertex u = pMapPos->second; clear_vertex ( u, oBoostGraph ); remove_vertex( u, oBoostGraph ); return true; }
bool are_uwv_ok(const VertexListGraph& graph, Vertex uwv[]) { // Checking whether the FIRST and THIRD are not heighbors. typedef typename graph_traits<VertexListGraph>::adjacency_iterator AdjIterator; AdjIterator n_v, n_vend; for(tie(n_v, n_vend) = adjacent_vertices(uwv[FIRST], graph); n_v != n_vend; ++n_v) { if(uwv[THIRD] == *n_v) return false; } // Removing FIRST and THIRD, then checking whether graph remains connected. VertexListGraph temp_graph; copy_graph(graph, temp_graph); clear_vertex(uwv[FIRST], temp_graph); clear_vertex(uwv[THIRD], temp_graph); remove_vertex(uwv[FIRST], temp_graph); remove_vertex(uwv[THIRD], temp_graph); return is_graph_connected(temp_graph); }
void remove_vertex(typename EdgeList::value_type u, std::vector<EdgeList, Allocator>& g) { typedef typename EdgeList::iterator iterator; clear_vertex(u, g); g.erase(g.begin() + u); for (std::size_t i = 0; i < g.size(); ++i) for ( iterator it = g[i].begin(); it != g[i].end(); ++it ) // after clear_vertex *it is never equal to u if ( *it > u ) --*it; }
inline void remove_branch( const typename graph_traits<Graph>::vertex_descriptor& u, Graph& g) { typedef typename graph_traits<Graph>::out_edge_iterator EdgeIter; typedef typename graph_traits<Graph>::vertex_descriptor Vertex; std::vector<Vertex> v_list; v_list.reserve(out_degree(u, g)); EdgeIter ei, ei_end; for( boost::tie(ei, ei_end) = out_edges(u,g); ei != ei_end; ++ei) v_list.push_back(target(*ei,g)); for( typename std::vector<Vertex>::iterator it = v_list.begin(); it != v_list.end(); ++it) remove_branch(*it, g); clear_vertex(u, g); remove_vertex(u, g); };
static void removeIsolatedEdges(Graph& g) { bool bEdgeRemoved; do { bEdgeRemoved = false; for (GraphVertex i = 0, iEnd = num_vertices(g); i < iEnd; ++ i) { if (out_degree(i, g) == 1) { std::pair<GraphOutEdgeItr, GraphOutEdgeItr> edgeItr = out_edges(i, g); GraphVertex v = target(*(edgeItr.first), g); if (out_degree(v, g) != 1) { clear_vertex(i, g); bEdgeRemoved = true; } } } } while (bEdgeRemoved); }
void clear_graph(NGHolder &h) { NGHolder::vertex_iterator vi, ve; for (tie(vi, ve) = vertices(h); vi != ve;) { NFAVertex v = *vi; ++vi; clear_vertex(v, h); if (!is_special(v, h)) { remove_vertex(v, h); } } assert(num_vertices(h) == N_SPECIALS); // Recreate special stylised edges. add_edge(h.start, h.startDs, h); add_edge(h.startDs, h.startDs, h); add_edge(h.accept, h.acceptEod, h); }
void SingleChildMerge::mergeTasks(VertexID n1, VertexID n2) { VertexID p,c; OutEdgeIterator oute,oute_end; InEdgeIterator ine,ine_end; tie(p,c)=determineParent(n1,n2); set<VertexID> parents,children; set<VertexID>::iterator child; for (tie(ine,ine_end)=in_edges(p,*m_taskgraph); ine != ine_end; ++ine) { assert(p != source(*ine,*m_taskgraph)); parents.insert(source(*ine,*m_taskgraph)); } for (tie(oute,oute_end)= out_edges(c,*m_taskgraph); oute != oute_end; ++oute) { assert(c != target(*oute,*m_taskgraph)); children.insert(target(*oute,*m_taskgraph)); } // Add edges from newtask = p to children and update costs. for(child = children.begin(); child != children.end(); ++child) { EdgeID newEdge,oldEdge; bool tmp; tie(newEdge,tmp)= add_edge(p,*child,*m_taskgraph); tie(oldEdge,tmp) = edge(c,*child,*m_taskgraph); //cerr << "old cost: " << getCommCost(oldEdge) << endl; setCommCost(newEdge, getCommCost(oldEdge),m_taskgraph); } addContainsTask(p,c); // Need to store that c is in p to be able to // go back to orig task graph when generating code. // Remove task and edges to the task (*m_taskRemoved)[c]=true; clear_vertex(c,*m_taskgraph); remove_vertex(c,*m_taskgraph); }
static void contractVertex(NGHolder &g, NFAVertex v, ue2::unordered_set<pair<NFAVertex, NFAVertex>> &all_edges) { for (auto u : inv_adjacent_vertices_range(v, g)) { if (u == v) { continue; // self-edge } for (auto w : adjacent_vertices_range(v, g)) { if (w == v) { continue; // self-edge } // Construct edge (u, v) only if it doesn't already exist. We use // the all_edges container here, as checking existence inside the // graph is expensive when u or v have large degree. if (all_edges.emplace(u, w).second) { add_edge(u, w, g); } } } // Note that edges to/from v will remain in all_edges. clear_vertex(v, g); }
static void detectArticulationPoints(std::vector<RelationEdge>& vecRelationEdge, Graph& g) { for (size_t i = 0, iEnd = vecRelationEdge.size(); i < iEnd; ++ i) { RelationEdge& relationEdge = vecRelationEdge[i]; GraphVertex u = relationEdge.getSource().getIdx(); GraphVertex v = relationEdge.getTarget().getIdx(); add_edge(u, v, EdgeProp(relationEdge.getType(), relationEdge.getScore()), g); } removeIsolatedEdges(g); std::vector<GraphVertex> vecArticulationPoint; articulation_points(g, std::back_inserter(vecArticulationPoint)); while (!vecArticulationPoint.empty()) { GraphVertex nWeakestPoint = 0; double nMinWeight = std::numeric_limits<double>::max(); for (GraphVertex i = 0; i < vecArticulationPoint.size(); ++ i) { double nWeight = 0; std::pair<GraphOutEdgeItr, GraphOutEdgeItr> edgeItr = out_edges(vecArticulationPoint[i], g); for (GraphOutEdgeItr it = edgeItr.first; it != edgeItr.second; it ++) { nWeight += g[*it].score; } if (nWeight < nMinWeight) { nMinWeight = nWeight; nWeakestPoint = vecArticulationPoint[i]; } } std::map<GraphVertex, EdgeProp> mapVertex2Edge; std::pair<GraphOutEdgeItr, GraphOutEdgeItr> edgeItr = out_edges(nWeakestPoint, g); for (GraphOutEdgeItr it = edgeItr.first; it != edgeItr.second; it ++) { mapVertex2Edge[target(*it, g)] = g[*it]; } clear_vertex(nWeakestPoint, g); std::vector<GraphVertex> component(num_vertices(g)); size_t nComponentNum = connected_components(g, &component[0]); std::vector<double> vecWeight(nComponentNum, 0.0); std::vector<int> vecCount(nComponentNum, 0); for (std::map<GraphVertex, EdgeProp>::iterator it = mapVertex2Edge.begin(); it != mapVertex2Edge.end(); it ++) { vecWeight[component[it->first]] += it->second.score; vecCount[component[it->first]] ++; } for (size_t i = 0; i < nComponentNum; ++ i) { if (vecCount[i] != 0) { vecWeight[i] /= vecCount[i]; } } size_t nStrongestComponent = std::distance(vecWeight.begin(), std::max_element(vecWeight.begin(), vecWeight.end())); for (std::map<GraphVertex, EdgeProp>::iterator it = mapVertex2Edge.begin(); it != mapVertex2Edge.end(); it ++) { GraphVertex v = it->first; if (component[v] == nStrongestComponent) { add_edge(nWeakestPoint, v, mapVertex2Edge[v], g); } } removeIsolatedEdges(g); vecArticulationPoint.clear(); articulation_points(g, std::back_inserter(vecArticulationPoint)); } return; }
BOOST_AUTO_TEST_CASE_TEMPLATE( intint_bgl_mutable_graph_test, Graph, intint_graphtest_types ) { typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex; typedef typename boost::graph_traits<Graph>::vertex_iterator VertexIter; typedef typename boost::graph_traits<Graph>::edge_descriptor Edge; typedef typename boost::graph_traits<Graph>::edge_iterator EdgeIter; typedef typename boost::graph_traits<Graph>::out_edge_iterator OutEdgeIter; typedef typename boost::graph_traits<Graph>::in_edge_iterator InEdgeIter; Graph g; Vertex v_root = Vertex(); BOOST_CHECK_NO_THROW( v_root = add_vertex(g) ); BOOST_CHECK_EQUAL( num_vertices(g), 1); BOOST_CHECK_NO_THROW( remove_vertex(v_root,g) ); BOOST_CHECK_EQUAL( num_vertices(g), 0); BOOST_CHECK_NO_THROW( v_root = add_vertex(1, g) ); g[v_root] = 1; BOOST_CHECK_EQUAL( g[v_root], 1 ); int vp_rc[] = {2,3,4,5}; int ep_rc[] = {1002,1003,1004,1005}; Vertex v_rc[4]; Edge e_rc[4]; for(int i = 0; i < 4; ++i) { BOOST_CHECK_NO_THROW( v_rc[i] = add_vertex(g) ); g[ v_rc[i] ] = vp_rc[i]; BOOST_CHECK_EQUAL( g[ v_rc[i] ], vp_rc[i] ); bool edge_added_success = false; BOOST_CHECK_NO_THROW( boost::tie(e_rc[i],edge_added_success) = add_edge(v_root, v_rc[i], g) ); BOOST_CHECK( edge_added_success ); g[ e_rc[i] ] = ep_rc[i]; BOOST_CHECK_EQUAL( g[ e_rc[i] ], ep_rc[i] ); }; BOOST_CHECK_EQUAL( num_vertices(g), 5 ); int vp_rc1c[] = {6,7,8,9}; int ep_rc1c[] = {2006,2007,2008,2009}; Vertex v_rc1c[4]; Edge e_rc1c[4]; for(std::size_t i = 0; i < 4; ++i) { BOOST_CHECK_NO_THROW( v_rc1c[i] = add_vertex(vp_rc1c[i], g) ); BOOST_CHECK_EQUAL( g[ v_rc1c[i] ], vp_rc1c[i] ); bool edge_added_success = false; BOOST_CHECK_NO_THROW( boost::tie(e_rc1c[i],edge_added_success) = add_edge(v_rc[0], v_rc1c[i], ep_rc1c[i], g) ); BOOST_CHECK( edge_added_success ); BOOST_CHECK_EQUAL( g[ e_rc1c[i] ], ep_rc1c[i] ); }; BOOST_CHECK_EQUAL( num_vertices(g), 9 ); BOOST_CHECK_EQUAL( g[v_root], 1 ); { OutEdgeIter ei, ei_end; BOOST_CHECK_NO_THROW( boost::tie(ei,ei_end) = out_edges(v_root,g) ); std::vector<int> e_list; for(; ei != ei_end; ++ei) { if(is_edge_valid(*ei,g)) { BOOST_CHECK_EQUAL( g[*ei], (g[source(*ei,g)] * 1000 + g[target(*ei,g)]) ); e_list.push_back(g[*ei]); }; }; std::sort(e_list.begin(), e_list.end()); BOOST_CHECK_EQUAL( e_list[0], 1002); BOOST_CHECK_EQUAL( e_list[1], 1003); BOOST_CHECK_EQUAL( e_list[2], 1004); BOOST_CHECK_EQUAL( e_list[3], 1005); InEdgeIter iei, iei_end; BOOST_CHECK_NO_THROW( boost::tie(iei, iei_end) = in_edges(v_rc[0], g) ); BOOST_CHECK( iei != iei_end ); BOOST_CHECK_EQUAL( g[*iei], 1002); ++iei; BOOST_CHECK( iei == iei_end ); BOOST_CHECK_NO_THROW( boost::tie(ei,ei_end) = out_edges(v_rc[0],g) ); std::vector<int> e_list2; for(; ei != ei_end; ++ei) { if(is_edge_valid(*ei,g)) { BOOST_CHECK_EQUAL( g[*ei], (g[source(*ei,g)] * 1000 + g[target(*ei,g)]) ); e_list2.push_back(g[*ei]); }; }; std::sort(e_list2.begin(), e_list2.end()); BOOST_CHECK_EQUAL( e_list2[0], 2006); BOOST_CHECK_EQUAL( e_list2[1], 2007); BOOST_CHECK_EQUAL( e_list2[2], 2008); BOOST_CHECK_EQUAL( e_list2[3], 2009); }; int vp_rc2c[] = {10,11,12,13}; int ep_rc2c[] = {3010,3011,3012,3013}; Vertex v_rc2c[4]; Edge e_rc2c[4]; for(std::size_t i = 0; i < 4; ++i) { #ifdef RK_ENABLE_CXX0X_FEATURES BOOST_CHECK_NO_THROW( v_rc2c[i] = add_vertex(std::move(vp_rc2c[i]), g) ); #else BOOST_CHECK_NO_THROW( v_rc2c[i] = add_vertex(vp_rc2c[i], g) ); #endif bool edge_added_success = false; #ifdef RK_ENABLE_CXX0X_FEATURES BOOST_CHECK_NO_THROW( boost::tie(e_rc2c[i],edge_added_success) = add_edge(v_rc[1], v_rc2c[i], ep_rc2c[i], g) ); #else BOOST_CHECK_NO_THROW( boost::tie(e_rc2c[i],edge_added_success) = add_edge(v_rc[1], v_rc2c[i], ep_rc2c[i], g) ); #endif BOOST_CHECK( edge_added_success ); }; BOOST_CHECK_EQUAL( num_vertices(g), 13 ); { OutEdgeIter ei, ei_end; BOOST_CHECK_NO_THROW( boost::tie(ei,ei_end) = out_edges(v_rc[1],g) ); std::vector<int> e_list; std::vector<int> vp_list; for(; ei != ei_end; ++ei) { if(is_edge_valid(*ei,g)) { BOOST_CHECK_EQUAL( g[*ei], (g[source(*ei,g)] * 1000 + g[target(*ei,g)]) ); e_list.push_back(g[*ei]); vp_list.push_back(g[target(*ei,g)]); }; }; std::sort(e_list.begin(), e_list.end()); BOOST_CHECK_EQUAL( e_list[0], 3010); BOOST_CHECK_EQUAL( e_list[1], 3011); BOOST_CHECK_EQUAL( e_list[2], 3012); BOOST_CHECK_EQUAL( e_list[3], 3013); std::sort(vp_list.begin(), vp_list.end()); BOOST_CHECK_EQUAL( vp_list[0], 10); BOOST_CHECK_EQUAL( vp_list[1], 11); BOOST_CHECK_EQUAL( vp_list[2], 12); BOOST_CHECK_EQUAL( vp_list[3], 13); }; BOOST_CHECK_NO_THROW( clear_vertex(v_rc[0],g) ); BOOST_CHECK_EQUAL( out_degree(v_rc[0], g), 0 ); BOOST_CHECK_EQUAL( in_degree(v_rc[0], g), 0 ); BOOST_CHECK_EQUAL( out_degree(v_root, g), 3 ); BOOST_CHECK_EQUAL( in_degree(v_rc1c[0], g), 0 ); BOOST_CHECK_EQUAL( in_degree(v_rc1c[1], g), 0 ); BOOST_CHECK_EQUAL( in_degree(v_rc1c[2], g), 0 ); BOOST_CHECK_EQUAL( in_degree(v_rc1c[3], g), 0 ); BOOST_CHECK_EQUAL( num_vertices(g), 13 ); { VertexIter vi, vi_end; BOOST_CHECK_NO_THROW( boost::tie(vi, vi_end) = vertices(g) ); std::vector<int> vp_list; for(; vi != vi_end; ++vi) if( is_vertex_valid(*vi, g) ) vp_list.push_back( g[*vi] ); std::sort(vp_list.begin(), vp_list.end()); BOOST_CHECK_EQUAL( vp_list[0], 1 ); BOOST_CHECK_EQUAL( vp_list[1], 2 ); BOOST_CHECK_EQUAL( vp_list[2], 3 ); BOOST_CHECK_EQUAL( vp_list[3], 4 ); BOOST_CHECK_EQUAL( vp_list[4], 5 ); BOOST_CHECK_EQUAL( vp_list[5], 6 ); BOOST_CHECK_EQUAL( vp_list[6], 7 ); BOOST_CHECK_EQUAL( vp_list[7], 8 ); BOOST_CHECK_EQUAL( vp_list[8], 9 ); BOOST_CHECK_EQUAL( vp_list[9], 10 ); BOOST_CHECK_EQUAL( vp_list[10], 11 ); BOOST_CHECK_EQUAL( vp_list[11], 12 ); BOOST_CHECK_EQUAL( vp_list[12], 13 ); }; BOOST_CHECK_EQUAL( num_edges(g), 7 ); { EdgeIter ei, ei_end; BOOST_CHECK_NO_THROW( boost::tie(ei, ei_end) = edges(g) ); std::vector<int> ep_list; for(; ei != ei_end; ++ei) if( is_edge_valid(*ei, g) ) ep_list.push_back( g[*ei] ); std::sort(ep_list.begin(), ep_list.end()); BOOST_CHECK_EQUAL( ep_list[0], 1003 ); BOOST_CHECK_EQUAL( ep_list[1], 1004 ); BOOST_CHECK_EQUAL( ep_list[2], 1005 ); BOOST_CHECK_EQUAL( ep_list[3], 3010 ); BOOST_CHECK_EQUAL( ep_list[4], 3011 ); BOOST_CHECK_EQUAL( ep_list[5], 3012 ); BOOST_CHECK_EQUAL( ep_list[6], 3013 ); }; BOOST_CHECK_NO_THROW( remove_edge(v_rc[1], v_rc2c[2], g) ); BOOST_CHECK_EQUAL( num_vertices(g), 13 ); { VertexIter vi, vi_end; BOOST_CHECK_NO_THROW( boost::tie(vi, vi_end) = vertices(g) ); std::vector<int> vp_list; for(; vi != vi_end; ++vi) { if( is_vertex_valid(*vi, g) ) { vp_list.push_back( g[*vi] ); }; }; std::sort(vp_list.begin(), vp_list.end()); BOOST_CHECK_EQUAL( vp_list[0], 1 ); BOOST_CHECK_EQUAL( vp_list[1], 2 ); BOOST_CHECK_EQUAL( vp_list[2], 3 ); BOOST_CHECK_EQUAL( vp_list[3], 4 ); BOOST_CHECK_EQUAL( vp_list[4], 5 ); BOOST_CHECK_EQUAL( vp_list[5], 6 ); BOOST_CHECK_EQUAL( vp_list[6], 7 ); BOOST_CHECK_EQUAL( vp_list[7], 8 ); BOOST_CHECK_EQUAL( vp_list[8], 9 ); BOOST_CHECK_EQUAL( vp_list[9], 10 ); BOOST_CHECK_EQUAL( vp_list[10], 11 ); BOOST_CHECK_EQUAL( vp_list[11], 12 ); BOOST_CHECK_EQUAL( vp_list[12], 13 ); }; BOOST_CHECK_EQUAL( num_edges(g), 6 ); { EdgeIter ei, ei_end; BOOST_CHECK_NO_THROW( boost::tie(ei, ei_end) = edges(g) ); std::vector<int> ep_list; for(; ei != ei_end; ++ei) { if( is_edge_valid(*ei, g) ) { ep_list.push_back( g[*ei] ); }; }; std::sort(ep_list.begin(), ep_list.end()); BOOST_CHECK_EQUAL( ep_list[0], 1003 ); BOOST_CHECK_EQUAL( ep_list[1], 1004 ); BOOST_CHECK_EQUAL( ep_list[2], 1005 ); BOOST_CHECK_EQUAL( ep_list[3], 3010 ); BOOST_CHECK_EQUAL( ep_list[4], 3011 ); BOOST_CHECK_EQUAL( ep_list[5], 3013 ); }; BOOST_CHECK_NO_THROW( remove_edge(e_rc2c[3], g) ); BOOST_CHECK_EQUAL( num_vertices(g), 13 ); { VertexIter vi, vi_end; BOOST_CHECK_NO_THROW( boost::tie(vi, vi_end) = vertices(g) ); std::vector<int> vp_list; for(; vi != vi_end; ++vi) { if( is_vertex_valid(*vi, g) ) { vp_list.push_back( g[*vi] ); }; }; std::sort(vp_list.begin(), vp_list.end()); BOOST_CHECK_EQUAL( vp_list[0], 1 ); BOOST_CHECK_EQUAL( vp_list[1], 2 ); BOOST_CHECK_EQUAL( vp_list[2], 3 ); BOOST_CHECK_EQUAL( vp_list[3], 4 ); BOOST_CHECK_EQUAL( vp_list[4], 5 ); BOOST_CHECK_EQUAL( vp_list[5], 6 ); BOOST_CHECK_EQUAL( vp_list[6], 7 ); BOOST_CHECK_EQUAL( vp_list[7], 8 ); BOOST_CHECK_EQUAL( vp_list[8], 9 ); BOOST_CHECK_EQUAL( vp_list[9], 10 ); BOOST_CHECK_EQUAL( vp_list[10], 11 ); BOOST_CHECK_EQUAL( vp_list[11], 12 ); BOOST_CHECK_EQUAL( vp_list[12], 13 ); }; BOOST_CHECK_EQUAL( num_edges(g), 5 ); { EdgeIter ei, ei_end; BOOST_CHECK_NO_THROW( boost::tie(ei, ei_end) = edges(g) ); std::vector<int> ep_list; for(; ei != ei_end; ++ei) { if( is_edge_valid(*ei, g) ) { ep_list.push_back( g[*ei] ); }; }; std::sort(ep_list.begin(), ep_list.end()); BOOST_CHECK_EQUAL( ep_list[0], 1003 ); BOOST_CHECK_EQUAL( ep_list[1], 1004 ); BOOST_CHECK_EQUAL( ep_list[2], 1005 ); BOOST_CHECK_EQUAL( ep_list[3], 3010 ); BOOST_CHECK_EQUAL( ep_list[4], 3011 ); }; BOOST_CHECK_NO_THROW( clear_vertex(v_rc2c[0], g) ); BOOST_CHECK_NO_THROW( remove_vertex(v_rc2c[0], g) ); BOOST_CHECK_EQUAL( num_vertices(g), 12 ); { VertexIter vi, vi_end; BOOST_CHECK_NO_THROW( boost::tie(vi, vi_end) = vertices(g) ); std::vector<int> vp_list; for(; vi != vi_end; ++vi) { if( is_vertex_valid(*vi, g) ) { vp_list.push_back( g[*vi] ); }; }; std::sort(vp_list.begin(), vp_list.end()); BOOST_CHECK_EQUAL( vp_list[0], 1 ); BOOST_CHECK_EQUAL( vp_list[1], 2 ); BOOST_CHECK_EQUAL( vp_list[2], 3 ); BOOST_CHECK_EQUAL( vp_list[3], 4 ); BOOST_CHECK_EQUAL( vp_list[4], 5 ); BOOST_CHECK_EQUAL( vp_list[5], 6 ); BOOST_CHECK_EQUAL( vp_list[6], 7 ); BOOST_CHECK_EQUAL( vp_list[7], 8 ); BOOST_CHECK_EQUAL( vp_list[8], 9 ); BOOST_CHECK_EQUAL( vp_list[9], 11 ); BOOST_CHECK_EQUAL( vp_list[10], 12 ); BOOST_CHECK_EQUAL( vp_list[11], 13 ); }; BOOST_CHECK_EQUAL( num_edges(g), 4 ); { EdgeIter ei, ei_end; BOOST_CHECK_NO_THROW( boost::tie(ei, ei_end) = edges(g) ); std::vector<int> ep_list; for(; ei != ei_end; ++ei) { if( is_edge_valid(*ei, g) ) { ep_list.push_back( g[*ei] ); }; }; std::sort(ep_list.begin(), ep_list.end()); BOOST_CHECK_EQUAL( ep_list[0], 1003 ); BOOST_CHECK_EQUAL( ep_list[1], 1004 ); BOOST_CHECK_EQUAL( ep_list[2], 1005 ); BOOST_CHECK_EQUAL( ep_list[3], 3011 ); }; };
bool somMayGoBackwards(NFAVertex u, const NGHolder &g, const ue2::unordered_map<NFAVertex, u32> ®ion_map, smgb_cache &cache) { /* Need to ensure all matches of the graph g up to u contain no infixes * which are also matches of the graph to u. * * This is basically the same as firstMatchIsFirst except we g is not * always a dag. As we haven't gotten around to writing an execute_graph * that operates on general graphs, we take some (hopefully) conservative * short cuts. * * Note: if the u can be jumped we will take jump edges * into account as a possibility of som going backwards * * TODO: write a generalised ng_execute_graph/make this less hacky */ assert(&g == &cache.g); if (contains(cache.smgb, u)) { return cache.smgb[u]; } DEBUG_PRINTF("checking if som can go backwards on %u\n", g[u].index); set<NFAEdge> be; BackEdges<set<NFAEdge>> backEdgeVisitor(be); depth_first_search( g.g, visitor(backEdgeVisitor) .root_vertex(g.start) .vertex_index_map(get(&NFAGraphVertexProps::index, g.g))); bool rv; if (0) { exit: DEBUG_PRINTF("using cached result\n"); cache.smgb[u] = rv; return rv; } assert(contains(region_map, u)); const u32 u_region = region_map.at(u); for (const auto &e : be) { NFAVertex s = source(e, g); NFAVertex t = target(e, g); /* only need to worry about big cycles including/before u */ DEBUG_PRINTF("back edge %u %u\n", g[s].index, g[t].index); if (s != t && region_map.at(s) <= u_region) { DEBUG_PRINTF("eek big cycle\n"); rv = true; /* big cycle -> eek */ goto exit; } } ue2::unordered_map<NFAVertex, NFAVertex> orig_to_copy; NGHolder c_g; cloneHolder(c_g, g, &orig_to_copy); for (NFAVertex v : vertices_range(g)) { if (!is_virtual_start(v, g)) { continue; } NFAVertex c_v = orig_to_copy[v]; orig_to_copy[v] = c_g.startDs; for (NFAVertex c_w : adjacent_vertices_range(c_v, c_g)) { add_edge_if_not_present(c_g.startDs, c_w, c_g); } clear_vertex(c_v, c_g); } NFAVertex c_u = orig_to_copy[u]; clear_in_edges(c_g.acceptEod, c_g); add_edge(c_g.accept, c_g.acceptEod, c_g); clear_in_edges(c_g.accept, c_g); clear_out_edges(c_u, c_g); if (hasSelfLoop(u, g)) { add_edge(c_u, c_u, c_g); } add_edge(c_u, c_g.accept, c_g); set<NFAVertex> u_succ; insert(&u_succ, adjacent_vertices(u, g)); u_succ.erase(u); for (auto t : inv_adjacent_vertices_range(u, g)) { if (t == u) { continue; } for (auto v : adjacent_vertices_range(t, g)) { if (contains(u_succ, v)) { add_edge(orig_to_copy[t], c_g.accept, c_g); break; } } } pruneUseless(c_g); be.clear(); depth_first_search(c_g.g, visitor(backEdgeVisitor).root_vertex(c_g.start). vertex_index_map(get(&NFAGraphVertexProps::index, c_g.g))); for (const auto &e : be) { NFAVertex s = source(e, c_g); NFAVertex t = target(e, c_g); DEBUG_PRINTF("back edge %u %u\n", c_g[s].index, c_g[t].index); if (s != t) { assert(0); DEBUG_PRINTF("eek big cycle\n"); rv = true; /* big cycle -> eek */ goto exit; } } DEBUG_PRINTF("checking acyclic+selfloop graph\n"); rv = !firstMatchIsFirst(c_g); DEBUG_PRINTF("som may regress? %d\n", (int)rv); goto exit; }
// WARNING: not a standard function void del_neuron(vertex_desc_t& vertex) { clear_vertex(vertex, this->_g); remove_vertex(vertex, this->_g); }
static void replaceAssertVertex(NGWrapper &g, NFAVertex t, edge_cache_t &edge_cache, u32 &assert_edge_count) { DEBUG_PRINTF("replacing assert vertex %u\n", g[t].index); const u32 flags = g[t].assert_flags; DEBUG_PRINTF("consider assert vertex %u with flags %u\n", g[t].index, flags); // Wire up all the predecessors to all the successors. for (const auto &inEdge : in_edges_range(t, g)) { NFAVertex u = source(inEdge, g); if (u == t) { continue; // ignore self-loops } const u32 flags_inc_in = conjunct(g[inEdge].assert_flags, flags); if (flags_inc_in == DEAD_EDGE) { DEBUG_PRINTF("fail, in-edge has bad flags %d\n", g[inEdge].assert_flags); continue; } for (const auto &outEdge : out_edges_range(t, g)) { NFAVertex v = target(outEdge, g); DEBUG_PRINTF("consider path [%u,%u,%u]\n", g[u].index, g[t].index, g[v].index); if (v == t) { continue; // ignore self-loops } const u32 flags_final = conjunct(g[outEdge].assert_flags, flags_inc_in); if (flags_final == DEAD_EDGE) { DEBUG_PRINTF("fail, out-edge has bad flags %d\n", g[outEdge].assert_flags); continue; } if ((g[u].assert_flags & POS_FLAG_MULTILINE_START) && v == g.acceptEod) { DEBUG_PRINTF("fail, (?m)^ does not match \\n at eod\n"); continue; } /* Replace path (u,t,v) with direct edge (u,v), unless the edge * already exists, in which case we just need to edit its * properties. * * Use edge_cache to prevent us going O(N). */ auto cache_key = make_pair(u, v); auto ecit = edge_cache.find(cache_key); if (ecit == edge_cache.end()) { DEBUG_PRINTF("adding edge %u %u\n", g[u].index, g[v].index); NFAEdge e = add_edge(u, v, g).first; edge_cache.emplace(cache_key, e); g[e].assert_flags = flags; if (++assert_edge_count > MAX_ASSERT_EDGES) { throw CompileError(g.expressionIndex, "Pattern is too large."); } } else { NFAEdge e = ecit->second; DEBUG_PRINTF("updating edge %u %u [a %u]\n", g[u].index, g[v].index, g[t].index); // Edge already exists. u32 &e_flags = g[e].assert_flags; e_flags = disjunct(e_flags, flags_final); assert(e_flags != DEAD_EDGE); } } } // Clear vertex t to remove all the old edges. /* no need to clear the cache, as we will never look up its edge as it is * unreachable */ clear_vertex(t, g); }
void AlnGraphBoost::markForReaper(VtxDesc n) { clear_vertex(n, _g); _reaperBag.push_back(n); }
void App::mouseClick(int x, int y) { if (x < SCREEN_WIDTH) { x /= cellSize; y /= cellSize; if (tryb == dodaj) { if (tableMap[x][y].color != 0x78AB46) { tableMap[x][y].color = 0x78AB46; //tableMap[(x/cellSize)+1][(y/cellSize)].color = 0x78AB46; //tableMap[(x/cellSize)+1][(y/cellSize)+1].color = 0x78AB46; //tableMap[(x/cellSize)][(y/cellSize)+1].color = 0x78AB46; // dodanie wierzcho³ka MapNode *node = map.nodeTab[x][y]; MapVertex v = add_vertex(*node, map.mapNodeGraph); map.XYToGraphNodeMap[coords(x, y)] = v; //EDGES //up if (map.XYToGraphNodeMap.find(coords(x, y - 1)) != map.XYToGraphNodeMap.end()) { add_edge(v, map.XYToGraphNodeMap[coords(x, y - 1)], 1, map.mapNodeGraph); add_edge(map.XYToGraphNodeMap[coords(x, y - 1)], v, 1, map.mapNodeGraph); } //down if (map.XYToGraphNodeMap.find(coords(x, y + 1)) != map.XYToGraphNodeMap.end()) { add_edge(v, map.XYToGraphNodeMap[coords(x, y + 1)], 1, map.mapNodeGraph); add_edge(map.XYToGraphNodeMap[coords(x, y + 1)], v, 1, map.mapNodeGraph); } //left if (map.XYToGraphNodeMap.find(coords(x + 1, y)) != map.XYToGraphNodeMap.end()) { add_edge(v, map.XYToGraphNodeMap[coords(x + 1, y)], 1, map.mapNodeGraph); add_edge(map.XYToGraphNodeMap[coords(x + 1, y)], v, 1, map.mapNodeGraph); } //right if (map.XYToGraphNodeMap.find(coords(x -1, y)) != map.XYToGraphNodeMap.end()) { add_edge(v, map.XYToGraphNodeMap[coords(x - 1, y)], 1, map.mapNodeGraph); add_edge(map.XYToGraphNodeMap[coords(x - 1, y)], v, 1, map.mapNodeGraph); } } } else { if (tableMap[x][y].color != 0x000000) { tableMap[x][y].color = 0x000000; //tableMap[(x/cellSize)+1][(y/cellSize)+1].color = 0x000000; //tableMap[(x/cellSize)+1][(y/cellSize)].color = 0x000000; //tableMap[(x/cellSize)][(y/cellSize)+1].color = 0x000000; clear_vertex(map.XYToGraphNodeMap[coords(x, y)], map.mapNodeGraph); remove_vertex(map.XYToGraphNodeMap[coords(x, y)], map.mapNodeGraph); map.XYToGraphNodeMap.erase(coords(x, y)); map.refreshMapping(map.XYToGraphNodeMap, map.mapNodeGraph); } } //seekerBot.Path = seekerBot.pathfindingComponent.updatePath(map.mapNodeGraph, seekerBot.Path, Pathfinder::manhattanDistanceHeuristic); seekerBot.Path = map.findPath(map.mapNodeGraph, map.XYToGraphNodeMap[coords(0, 0)], map.XYToGraphNodeMap[coords(10, 10)], Map::manhattanDistanceHeuristic); //map.pathToCoords(seekerBot.Path, map.mapNodeGraph); } }
template <typename PointT> void pcl::registration::ELCH<PointT>::loopOptimizerAlgorithm (LOAGraph &g, double *weights) { std::list<int> crossings, branches; crossings.push_back (static_cast<int> (loop_start_)); crossings.push_back (static_cast<int> (loop_end_)); weights[loop_start_] = 0; weights[loop_end_] = 1; int *p = new int[num_vertices (g)]; int *p_min = new int[num_vertices (g)]; double *d = new double[num_vertices (g)]; double *d_min = new double[num_vertices (g)]; double dist; bool do_swap = false; std::list<int>::iterator crossings_it, end_it, start_min, end_min; // process all junctions while (!crossings.empty ()) { dist = -1; // find shortest crossing for all vertices on the loop for (crossings_it = crossings.begin (); crossings_it != crossings.end (); ) { dijkstra_shortest_paths (g, *crossings_it, boost::predecessor_map (p).distance_map (d)); end_it = crossings_it; end_it++; // find shortest crossing for one vertex for (; end_it != crossings.end (); end_it++) { if (*end_it != p[*end_it] && (dist < 0 || d[*end_it] < dist)) { dist = d[*end_it]; start_min = crossings_it; end_min = end_it; do_swap = true; } } if (do_swap) { std::swap (p, p_min); std::swap (d, d_min); do_swap = false; } // vertex starts a branch if (dist < 0) { branches.push_back (static_cast<int> (*crossings_it)); crossings_it = crossings.erase (crossings_it); } else crossings_it++; } if (dist > -1) { remove_edge (*end_min, p_min[*end_min], g); for (int i = p_min[*end_min]; i != *start_min; i = p_min[i]) { //even right with weights[*start_min] > weights[*end_min]! (math works) weights[i] = weights[*start_min] + (weights[*end_min] - weights[*start_min]) * d_min[i] / d_min[*end_min]; remove_edge (i, p_min[i], g); if (degree (i, g) > 0) { crossings.push_back (i); } } if (degree (*start_min, g) == 0) crossings.erase (start_min); if (degree (*end_min, g) == 0) crossings.erase (end_min); } } delete[] p; delete[] p_min; delete[] d; delete[] d_min; boost::graph_traits<LOAGraph>::adjacency_iterator adjacent_it, adjacent_it_end; int s; // error propagation while (!branches.empty ()) { s = branches.front (); branches.pop_front (); for (boost::tuples::tie (adjacent_it, adjacent_it_end) = adjacent_vertices (s, g); adjacent_it != adjacent_it_end; ++adjacent_it) { weights[*adjacent_it] = weights[s]; if (degree (*adjacent_it, g) > 1) branches.push_back (static_cast<int> (*adjacent_it)); } clear_vertex (s, g); } }
void Internal_Graph::deleteNode(Package * p) { clear_vertex((*Nodes)[p], this->g); }
void AlnGraphBoost::markForReaper(VtxDesc n) { _g[n].deleted = true; clear_vertex(n, _g); _reaperBag.push_back(n); }
static bool expandCyclic(NGHolder &h, NFAVertex v) { DEBUG_PRINTF("inspecting %zu\n", h[v].index); bool changes = false; auto v_preds = preds(v, h); auto v_succs = succs(v, h); set<NFAVertex> start_siblings; set<NFAVertex> end_siblings; CharReach &v_cr = h[v].char_reach; /* We need to find start vertices which have all of our preds. * As we have a self loop, it must be one of our succs. */ for (auto a : adjacent_vertices_range(v, h)) { auto a_preds = preds(a, h); if (a_preds == v_preds && isutf8start(h[a].char_reach)) { DEBUG_PRINTF("%zu is a start v\n", h[a].index); start_siblings.insert(a); } } /* We also need to find full cont vertices which have all our own succs; * As we have a self loop, it must be one of our preds. */ for (auto a : inv_adjacent_vertices_range(v, h)) { auto a_succs = succs(a, h); if (a_succs == v_succs && h[a].char_reach == UTF_CONT_CR) { DEBUG_PRINTF("%zu is a full tail cont\n", h[a].index); end_siblings.insert(a); } } for (auto s : start_siblings) { if (out_degree(s, h) != 1) { continue; } const CharReach &cr = h[s].char_reach; if (cr.isSubsetOf(UTF_TWO_START_CR)) { if (end_siblings.find(*adjacent_vertices(s, h).first) == end_siblings.end()) { DEBUG_PRINTF("%zu is odd\n", h[s].index); continue; } } else if (cr.isSubsetOf(UTF_THREE_START_CR)) { NFAVertex m = *adjacent_vertices(s, h).first; if (h[m].char_reach != UTF_CONT_CR || out_degree(m, h) != 1) { continue; } if (end_siblings.find(*adjacent_vertices(m, h).first) == end_siblings.end()) { DEBUG_PRINTF("%zu is odd\n", h[s].index); continue; } } else if (cr.isSubsetOf(UTF_FOUR_START_CR)) { NFAVertex m1 = *adjacent_vertices(s, h).first; if (h[m1].char_reach != UTF_CONT_CR || out_degree(m1, h) != 1) { continue; } NFAVertex m2 = *adjacent_vertices(m1, h).first; if (h[m2].char_reach != UTF_CONT_CR || out_degree(m2, h) != 1) { continue; } if (end_siblings.find(*adjacent_vertices(m2, h).first) == end_siblings.end()) { DEBUG_PRINTF("%zu is odd\n", h[s].index); continue; } } else { DEBUG_PRINTF("%zu is bad\n", h[s].index); continue; } v_cr |= cr; clear_vertex(s, h); changes = true; } if (changes) { v_cr |= UTF_CONT_CR; /* we need to add in cont reach */ v_cr.set(0xc0); /* we can also add in the forbidden bytes as we require * valid unicode data */ v_cr.set(0xc1); v_cr |= CharReach(0xf5, 0xff); } return changes; }