2

いくつかのトラバーサルアルゴリズムを使用してトラバースする必要があるオブジェクトのグラフを作成しようとしています。この瞬間、カスタムオブジェクトを使用してグラフを作成しようとして立ち往生しています。私がそれを達成しようとしている方法は次のとおりです。

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <iostream>

using namespace std;
typedef boost::adjacency_list<boost::vecS,boost::vecS,boost::directedS> CustomGraph;
typedef boost::graph_traits<CustomGraph>::vertex_descriptor CustomVertex;

class CustomVisitor:public boost::default_dfs_visitor
{
        public:
                void discover_vertex(CustomVertex v,const CustomGraph& taskGraph) const
                {
                        cerr<<v<<endl;
                        return;
                }


};

class CustomObject{
        private:
                int currentId;
        public:
                CustomObject(int id){
                        currentId = id;
                }

};


int main()
{
        CustomGraph customGraph;
        CustomObject* obj0 = new CustomObject(0);
        CustomObject* obj1 = new CustomObject(1);
        CustomObject* obj2 = new CustomObject(2);
        CustomObject* obj3 = new CustomObject(3);

        typedef std::pair<CustomObject*,CustomObject*> Edge;
        std::vector<Edge> edgeVec;
        edgeVec.push_back(Edge(obj0,obj1));
        edgeVec.push_back(Edge(obj0,obj2));
        edgeVec.push_back(Edge(obj1,obj2));
        edgeVec.push_back(Edge(obj1,obj3));
        customGraph(edgeVec.begin(),edgeVec.end());
        CustomVisitor vis;
        boost::depth_first_search(customGraph,boost::visitor(vis));

        return 0;
}

しかし、これは頂点内にオブジェクトを作成する正しい方法ではないようです。グラフをトラバースしながらオブジェクトを取得できるようにノードを作成する正しい方法について誰かに教えてもらえますか?

ありがとう

4

1 に答える 1

8

こんにちは私はこれがかなり古い質問であることを知っていますが、答えから利益を得ることができる他の人がいるかもしれません。

グラフに頂点としてカスタムクラスがあることを定義するのを忘れたようです。typedefに4番目のパラメーターを追加し、エッジにtypedefを追加する必要があります。

typedef boost::adjacency_list<boost::vecS,boost::vecS,boost::directedS, CustomObject> CustomGraph;
typedef boost::graph_traits<CustomGraph>::vertex_descriptor CustomVertex;
typedef boost::graph_traits<CustomGraph>::edge_descriptor CustomEdge;

次に、通常、ノードをエッジで接続する前にノードを追加します。

// Create graph and custom obj's
CustomGraph customGraph
CustomObject obj0(0);
CustomObject obj1(1);
CustomObject obj2(2);
CustomObject obj3(3);

// Add custom obj's to the graph 
// (Creating boost vertices)
CustomVertex v0 = boost::add_vertex(obj0, customGraph);
CustomVertex v1 = boost::add_vertex(obj1, customGraph);
CustomVertex v2 = boost::add_vertex(obj2, customGraph);
CustomVertex v3 = boost::add_vertex(obj3, customGraph);

// Add edge
CustomEdge edge; 
bool edgeExists;
// check if edge allready exist (only if you don't want parallel edges)
boost::tie(edge, edgeExists) = boost::edge(v0 , v1, customGraph);
if(!edgeExists)
    boost::add_edge(v0 , v1, customGraph);

// write graph to console
cout << "\n-- graphviz output START --" << endl;
boost::write_graphviz(cout, customGraph);
cout << "\n-- graphviz output END --" << endl;
于 2012-10-02T08:31:21.503 に答える