示例#1
0
static
bool pruneForwardUseless(NGHolder &h, const nfag_t &g, NFAVertex s,
                         vector<default_color_type> &vertexColor) {
    // Begin with all vertices set to white, as DFV only marks visited
    // vertices.
    fill(vertexColor.begin(), vertexColor.end(), boost::white_color);

    auto index_map = get(&NFAGraphVertexProps::index, g);

    depth_first_visit(g, s, make_dfs_visitor(boost::null_visitor()),
                      make_iterator_property_map(vertexColor.begin(),
                                                 index_map));

    vector<NFAVertex> dead;

    // All non-special vertices that are still white can be removed.
    for (auto v : vertices_range(g)) {
        u32 idx = g[v].index;
        if (!is_special(v, g) && vertexColor[idx] == boost::white_color) {
            DEBUG_PRINTF("vertex %u is unreachable from %u\n",
                         g[v].index, g[s].index);
            dead.push_back(v);
        }
    }

    if (dead.empty()) {
        return false;
    }

    DEBUG_PRINTF("removing %zu vertices\n", dead.size());
    remove_vertices(dead, h, false);
    return true;
}
示例#2
0
void PdfContentsGraph::Write(PdfOutputStream& outStream)
{
    typedef pair<PrintVertexVisitor<on_discover_vertex,true>,PrintVertexVisitor<on_finish_vertex,false> > EVList;
    dfs_visitor<EVList> vis = make_dfs_visitor(
            EVList( PrintVertexVisitor<on_discover_vertex,true>(&outStream),
                    PrintVertexVisitor<on_finish_vertex,false>(&outStream) ) );
    depth_first_search(m_graph, visitor(vis));
}
示例#3
0
  typename property_traits<ComponentsMap>::value_type
  kosaraju_strong_components(Graph& G, ComponentsMap c,
                             FinishTime finish_time, ColorMap color)
  {
    function_requires< MutableGraphConcept<Graph> >();
    // ...
    
    typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
    typedef typename property_traits<ColorMap>::value_type ColorValue;
    typedef color_traits<ColorValue> Color;
    typename property_traits<FinishTime>::value_type time = 0;
    depth_first_search
     (G, make_dfs_visitor(stamp_times(finish_time, time, on_finish_vertex())),
      color);

    Graph G_T(num_vertices(G));
    transpose_graph(G, G_T);

    typedef typename property_traits<ComponentsMap>::value_type count_type;

    count_type c_count(0);
    detail::components_recorder<ComponentsMap>
      vis(c, c_count);

    // initialize G_T
    typename graph_traits<Graph>::vertex_iterator ui, ui_end;
    for (tie(ui, ui_end) = vertices(G_T); ui != ui_end; ++ui)
      put(color, *ui, Color::white());

    typedef typename property_traits<FinishTime>::value_type D;
    typedef indirect_cmp< FinishTime, std::less<D> > Compare;

    Compare fl(finish_time);
    std::priority_queue<Vertex, std::vector<Vertex>, Compare > Q(fl);

    typename graph_traits<Graph>::vertex_iterator i, j, iend, jend;
    tie(i, iend) = vertices(G_T);
    tie(j, jend) = vertices(G);
    for ( ; i != iend; ++i, ++j) {
      put(finish_time, *i, get(finish_time, *j));
       Q.push(*i);
    }

    while ( !Q.empty() ) {
      Vertex u = Q.top();
      Q.pop();
      if  (get(color, u) == Color::white()) {
        depth_first_visit(G_T, u, vis, color);
        ++c_count; 
      }
    }
    return c_count;
  }
示例#4
0
 void
 undirected_dfs(const Graph& g, 
                const bgl_named_params<P, T, R>& params)
 {
   typedef typename get_param_type< vertex_color_t, bgl_named_params<P, T, R> >::type C;
   detail::udfs_dispatch<C>::apply
     (g,
      choose_param(get_param(params, graph_visitor),
                   make_dfs_visitor(null_visitor())),
      choose_param(get_param(params, root_vertex_t()),
                   *vertices(g).first),
      params,
      get_param(params, edge_color),
      get_param(params, vertex_color)
      );
 }
示例#5
0
/** Remove any vertices that can't be reached by traversing the graph in
 * reverse from acceptEod. */
void pruneUnreachable(NGHolder &g) {
    deque<NFAVertex> dead;

    if (!hasGreaterInDegree(1, g.acceptEod, g) &&
            !hasGreaterInDegree(0, g.accept, g) &&
            edge(g.accept, g.acceptEod, g).second) {
        // Trivial case: there are no in-edges to our accepts (other than
        // accept->acceptEod), so all non-specials are unreachable.
        for (auto v : vertices_range(g)) {
            if (!is_special(v, g)) {
                dead.push_back(v);
            }
        }
    } else {
        // Walk a reverse graph from acceptEod with Boost's depth_first_visit
        // call.
        typedef reverse_graph<NFAGraph, NFAGraph&> RevNFAGraph;
        RevNFAGraph revg(g.g);

        map<NFAVertex, default_color_type> colours;

        depth_first_visit(revg, g.acceptEod,
                          make_dfs_visitor(boost::null_visitor()),
                          make_assoc_property_map(colours));

        DEBUG_PRINTF("color map has %zu entries after DFV\n", colours.size());

        // All non-special vertices that aren't in the colour map (because they
        // weren't reached) can be removed.
        for (auto v : vertices_range(revg)) {
            if (is_special(v, revg)) {
                continue;
            }
            if (!contains(colours, v)) {
                dead.push_back(v);
            }
        }
    }

    if (dead.empty()) {
        DEBUG_PRINTF("no unreachable vertices\n");
        return;
    }

    remove_vertices(dead, g, false);
    DEBUG_PRINTF("removed %zu unreachable vertices\n", dead.size());
}
示例#6
0
static
vector<NFAVertex> findUnreachable(const NGHolder &g) {
    const boost::reverse_graph<NFAGraph, const NFAGraph &> revg(g.g);

    ue2::unordered_map<NFAVertex, boost::default_color_type> colours;
    colours.reserve(num_vertices(g));

    depth_first_visit(revg, g.acceptEod,
                      make_dfs_visitor(boost::null_visitor()),
                      make_assoc_property_map(colours));

    // Unreachable vertices are not in the colour map.
    vector<NFAVertex> unreach;
    for (auto v : vertices_range(revg)) {
        if (!contains(colours, v)) {
            unreach.push_back(v);
        }
    }
    return unreach;
}