【问题标题】:Boost DFS how to save visited vertices?Boost DFS如何保存访问过的顶点?
【发布时间】:2014-08-06 17:27:27
【问题描述】:

我正在查看解决方案 here,这对我不起作用(但请阅读 === 行以实际查看当前问题)。

我试过了:

boost::undirected_dfs(G, vertex(0,G), boost::visitor(vis)); 

但我明白了

error C2780: 'void boost::undirected_dfs(const Graph &,const boost::bgl_named_params<P,T,R> &)' : expects 2 arguments - 3 provided
error C2780: 'void boost::undirected_dfs(const Graph &,DFSVisitor,VertexColorMap,EdgeColorMap)' : expects 4 arguments - 3 provided

等等。我有点明白问题是什么(我需要向它传递一些命名参数,但我认为我的图表中没有任何参数。此外,我根本不明白与彩色地图的关系是什么.

================================================ ===============================

我的图表已定义:

typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, boost::no_property, EdgeInfoProperty > Graph;
typedef Graph::edge_descriptor Edge;
typedef Graph::vertex_descriptor Vertex;

我只想做 DFS,至少现在是这样。

所以我把它改成了boost::depth_first_search,它似乎工作了。

我有(注意void discover_vertex 与上面链接的解决方案相比缺少const):

class MyVisitor : public boost::default_dfs_visitor {
public:
    void discover_vertex(Vertex v, const Graph& g)  { //note the lack of const
        if(boost::in_degree(v,g)!=0){ //only print the vertices in the connected component (I already did MCC and removed edges so all the extra vertices are isolated)
            std::cerr << v << std::endl;
            vv.push_back(v);
        }
        return;
    }
    std::vector<Vertex> GetVector() const  { return vv; }
private: 
    std::vector<Vertex> vv;
};

如果我离开const,我会得到error C2663: 'std::vector&lt;_Ty&gt;::push_back' : 2 overloads have no legal conversion for 'this' pointer with [ _Ty=size_t ]

现在,这可以正常工作,或者至少它以正确的顺序打印出正确的访问顶点:

MyVisitor vis;
boost::depth_first_search(G, boost::visitor(vis)); 

但是当我这样做时:

std::vector<Vertex> vctr = vis.GetVector();
std::cout<<vctr.size();

大小为零,因为我的vv 没有改变...

那么,当类用作boost::visitor 的参数时,如何获得适当的类行为? (我什至不确定这是适当的问题)。我需要能够根据之前访问过的节点来更改EdgeInfoProperty(或者更确切地说,基于在 DFS 遍历中哪个顶点是当前顶点的父节点,所以这可能只是朝着那个方向迈出的第一步)。

【问题讨论】:

    标签: c++ boost graph depth-first-search


    【解决方案1】:

    访问者是按值传递的,因此您需要与复制到函数调用中的 MyVisitor 实例共享它持有的向量。

    试试这个:

    class MyVisitor : public boost::default_dfs_visitor {
    public:
        MyVisitor(): vv(new std::vector<Vertex>()){}
    
        void discover_vertex(Vertex v, const Graph& g)  { //note the lack of const
            if(boost::in_degree(v,g)!=0){ //only print the vertices in the connected component (I already did MCC and removed edges so all the extra vertices are isolated)
                std::cerr << v << std::endl;
                vv->push_back(v);
            }
            return;
        }
        std::vector<Vertex>& GetVector() const  { return *vv; }
    private: 
        boost::shared_ptr< std::vector<Vertex> > vv;
    };
    

    【讨论】:

      猜你喜欢
      • 2015-10-27
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-15
      相关资源
      最近更新 更多