2013-01-23 61 views
6

我在編譯非常簡單的圖的BFS時遇到了問題。無論我做什麼我得到了無與倫比的方法調用各種編譯器消息(我試過boost::visitor和擴展boost::default_bfs_visitor等)如何在boost中使用遍歷圖BFS

#include <stdint.h> 
#include <iostream> 
#include <vector> 
#include <boost/graph/adjacency_list.hpp> 
#include <boost/graph/breadth_first_search.hpp> 

int main() { 
    typedef boost::adjacency_list<boost::vecS, boost::hash_setS, boost::undirectedS, uint32_t, uint32_t, boost::no_property> graph_t; 
    graph_t graph(4); 
    graph_t::vertex_descriptor a = boost::vertex(0, graph); 
    graph_t::vertex_descriptor b = boost::vertex(1, graph); 
    graph_t::vertex_descriptor c = boost::vertex(2, graph); 
    graph_t::vertex_descriptor d = boost::vertex(3, graph); 
    graph[a] = 0; 
    graph[b] = 1; 
    graph[c] = 2; 
    graph[d] = 3; 
    std::pair<graph_t::edge_descriptor, bool> result = boost::add_edge(a, b, 0, graph); 
    result = boost::add_edge(a, c, 1, graph); 
    result = boost::add_edge(c, b, 2, graph); 
    class { 
    public: 
    void initialize_vertex(const graph_t::vertex_descriptor &s, graph_t &g) { 
     std::cout << "Initialize: " << g[s] << std::endl; 
    } 
    void discover_vertex(const graph_t::vertex_descriptor &s, graph_t &g) { 
     std::cout << "Discover: " << g[s] << std::endl; 
    } 
    void examine_vertex(const graph_t::vertex_descriptor &s, graph_t &g) { 
     std::cout << "Examine vertex: " << g[s] << std::endl; 
    } 
    void examine_edge(const graph_t::edge_descriptor &e, graph_t &g) { 
     std::cout << "Examine edge: " << g[e] << std::endl; 
    } 
    void tree_edge(const graph_t::edge_descriptor &e, graph_t &g) { 
     std::cout << "Tree edge: " << g[e] << std::endl; 
    } 
    void non_tree_edge(const graph_t::edge_descriptor &e, graph_t &g) { 
     std::cout << "Non-Tree edge: " << g[e] << std::endl; 
    } 
    void gray_target(const graph_t::edge_descriptor &e, graph_t &g) { 
     std::cout << "Gray target: " << g[e] << std::endl; 
    } 
    void black_target(const graph_t::edge_descriptor &e, graph_t &g) { 
     std::cout << "Black target: " << g[e] << std::endl; 
    } 
    void finish_vertex(const graph_t::vertex_descriptor &s, graph_t &g) { 
     std::cout << "Finish vertex: " << g[s] << std::endl; 
    } 
    } bfs_visitor; 
    boost::breadth_first_search(graph, a, bfs_visitor); 
    return 0; 
} 

如何訪問使用bfs_visitor圖?

PS。我已經看到並編譯"How to create a C++ Boost undirected graph and traverse it in depth first search (DFS) order?",但它沒有幫助。

回答

4

您可以看到here重載列表breadth_first_search。如果您不想指定每個參數都需要使用命名參數版本。它應該是這樣的:

breadth_first_search(graph, a, boost::visitor(bfs_visitor)); 

這上班是,如果你使用過vecS爲您VertexList時,存儲在您的圖形清晰度,或者如果你已經建造並初始化一個內部的vertex_index屬性映射。由於您使用hash_setS你需要調用更改爲:

breath_first_search(graph, a, boost::visitor(bfs_visitor).vertex_index_map(my_index_map)); 

您已經使用您的uint32_t的捆綁性的指標圖。您可以使用get(boost::vertex_bundle, graph)來訪問它。

您的訪客也有問題。您應該從boost::default_bfs_visitor中獲得它,並且您的成員函數的參數graph_t需要是const限定的。

全碼:

#include <stdint.h> 
#include <iostream> 
#include <vector> 
#include <boost/graph/adjacency_list.hpp> 
#include <boost/graph/breadth_first_search.hpp> 

typedef boost::adjacency_list<boost::vecS, boost::hash_setS, boost::undirectedS, uint32_t, uint32_t, boost::no_property> graph_t; 


struct my_visitor : boost::default_bfs_visitor{ 

    void initialize_vertex(const graph_t::vertex_descriptor &s, const graph_t &g) const { 
     std::cout << "Initialize: " << g[s] << std::endl; 
    } 
    void discover_vertex(const graph_t::vertex_descriptor &s, const graph_t &g) const { 
     std::cout << "Discover: " << g[s] << std::endl; 
    } 
    void examine_vertex(const graph_t::vertex_descriptor &s, const graph_t &g) const { 
     std::cout << "Examine vertex: " << g[s] << std::endl; 
    } 
    void examine_edge(const graph_t::edge_descriptor &e, const graph_t &g) const { 
     std::cout << "Examine edge: " << g[e] << std::endl; 
    } 
    void tree_edge(const graph_t::edge_descriptor &e, const graph_t &g) const { 
     std::cout << "Tree edge: " << g[e] << std::endl; 
    } 
    void non_tree_edge(const graph_t::edge_descriptor &e, const graph_t &g) const { 
     std::cout << "Non-Tree edge: " << g[e] << std::endl; 
    } 
    void gray_target(const graph_t::edge_descriptor &e, const graph_t &g) const { 
     std::cout << "Gray target: " << g[e] << std::endl; 
    } 
    void black_target(const graph_t::edge_descriptor &e, const graph_t &g) const { 
     std::cout << "Black target: " << g[e] << std::endl; 
    } 
    void finish_vertex(const graph_t::vertex_descriptor &s, const graph_t &g) const { 
     std::cout << "Finish vertex: " << g[s] << std::endl; 
    } 
    }; 

int main() { 
    graph_t graph(4); 
    graph_t::vertex_descriptor a = boost::vertex(0, graph); 
    graph_t::vertex_descriptor b = boost::vertex(1, graph); 
    graph_t::vertex_descriptor c = boost::vertex(2, graph); 
    graph_t::vertex_descriptor d = boost::vertex(3, graph); 
    graph[a] = 0; 
    graph[b] = 1; 
    graph[c] = 2; 
    graph[d] = 3; 
    std::pair<graph_t::edge_descriptor, bool> result = boost::add_edge(a, b, 0, graph); 
    result = boost::add_edge(a, c, 1, graph); 
    result = boost::add_edge(c, b, 2, graph); 

    my_visitor vis; 

    breadth_first_search(graph, a, boost::visitor(vis).vertex_index_map(get(boost::vertex_bundle,graph))); 
    return 0; 
} 
+0

我無法讓它與嵌套在方法中的類一起工作(我仍然得到關於模板實例化失敗的奇怪編譯器消息)。你知道可能是什麼問題嗎? –

+1

@MaciejPiechotka在C++ 11之前解釋[here](http://stackoverflow.com/questions/5751977/local-type-as-template-arguments-in-c)你不能使用本地類型作爲模板參數。 'boost :: visitor'返回一個結構,它的參數類型是其中一個參數。使用'-std = C++ 11'可以使用g ++ 4.8.0(儘管它在3.2版本時仍然失敗)。 – 2013-01-23 09:40:03

2

我面臨同樣的問題,但相比於user1252091我的頂點類型提供的答案是,不包括可用於創建vertex_index_map整數的結構,因此行

breadth_first_search(graph, a, boost::visitor(vis).vertex_index_map(get(boost::vertex_bundle,graph))); 

不會在我的情況下工作。最終我想出瞭如何創建一個外部vertex_index_map(同樣要感謝this answer)並將其傳遞給breadth_first_search函數。這是一個可以幫助其他人的例子:

#include <boost/graph/adjacency_list.hpp> 
#include <boost/graph/visitors.hpp> 
#include <boost/graph/breadth_first_search.hpp> 
#include <iostream> 

struct Person 
{ 
    std::string Name; 
    unsigned int YearBorn; 
}; 

typedef boost::adjacency_list <boost::vecS, boost::hash_setS, boost::bidirectionalS, Person, boost::no_property > FamilyTree; 
typedef boost::graph_traits<FamilyTree>::vertex_descriptor Vertex; 
typedef boost::graph_traits<FamilyTree>::edge_descriptor Edge; 

template <class Graph> 
class BfsVisitor : public boost::default_bfs_visitor 
{ 
public: 
    typedef typename boost::graph_traits<Graph>::vertex_descriptor VertexDescriptor; 
    typedef typename boost::graph_traits<Graph>::edge_descriptor EdgeDescriptor; 

    BfsVisitor(std::vector<VertexDescriptor>& nodesVisited) 
    : m_nodesVisited(nodesVisited){} 

    void tree_edge(EdgeDescriptor e, const Graph& g) const 
    { 
     VertexDescriptor u = source(e, g); 
     VertexDescriptor v = target(e, g); 
     m_nodesVisited.push_back(v); 
    } 

private: 
    std::vector<VertexDescriptor>& m_nodesVisited; 
}; 


const Person Abe_Simpson  {"Abe_Simpson", 0}; 
const Person Mona_Simpson  { "Mona_Simpson", 0}; 
const Person Herb_Simpson  { "Herb_Simpson", 0}; 
const Person Homer_Simpson  { "Homer_Simpson", 0}; 

const Person Clancy_Bouvier  { "Clancy_Bouvier", 0}; 
const Person Jacqueline_Bouvier { "Jacqueline_Bouvier", 0}; 
const Person Marge_Bouvier  { "Marge_Bouvier", 0}; 
const Person Patty_Bouvier  { "Patty_Bouvier", 0}; 
const Person Selma_Bouvier  { "Selma_Bouvier", 0}; 

const Person Bart_Simpson  { "Bart_Simpson", 0}; 
const Person Lisa_Simpson  { "Lisa_Simpson", 0}; 
const Person Maggie_Simpson  { "Maggie_Simpson", 0}; 
const Person Ling_Bouvier  { "Ling_Bouvier", 0}; 





int main(void) 
{ 
    std::cout << __FUNCTION__ << "\n"; 

    FamilyTree g; 


    // nodes 
    auto v_Abe_Simpson = boost::add_vertex(Abe_Simpson,g); 
    auto v_Mona_Simpson = boost::add_vertex(Mona_Simpson,g); 
    auto v_Herb_Simpson = boost::add_vertex(Herb_Simpson,g); 
    auto v_Homer_Simpson = boost::add_vertex(Homer_Simpson,g); 

    auto v_Clancy_Bouvier = boost::add_vertex(Clancy_Bouvier,g); 
    auto v_Jacqueline_Bouvier = boost::add_vertex(Jacqueline_Bouvier,g); 
    auto v_Marge_Bouvier = boost::add_vertex(Marge_Bouvier,g); 
    auto v_Patty_Bouvier = boost::add_vertex(Patty_Bouvier,g); 
    auto v_Selma_Bouvier = boost::add_vertex(Selma_Bouvier,g); 

    auto v_Bart_Simpson = boost::add_vertex(Bart_Simpson,g); 
    auto v_Lisa_Simpson = boost::add_vertex(Lisa_Simpson,g); 
    auto v_Maggie_Simpson = boost::add_vertex(Maggie_Simpson,g); 
    auto v_Ling_Bouvier = boost::add_vertex(Ling_Bouvier,g); 

    // connections 
    boost::add_edge(v_Abe_Simpson, v_Herb_Simpson, g); 
    boost::add_edge(v_Abe_Simpson, v_Homer_Simpson, g); 
    boost::add_edge(v_Mona_Simpson, v_Herb_Simpson, g); 
    boost::add_edge(v_Mona_Simpson, v_Homer_Simpson, g); 

    boost::add_edge(v_Clancy_Bouvier, v_Marge_Bouvier, g); 
    boost::add_edge(v_Clancy_Bouvier, v_Patty_Bouvier, g); 
    boost::add_edge(v_Clancy_Bouvier, v_Selma_Bouvier, g); 
    boost::add_edge(v_Jacqueline_Bouvier, v_Marge_Bouvier, g); 
    boost::add_edge(v_Jacqueline_Bouvier, v_Patty_Bouvier, g); 
    boost::add_edge(v_Jacqueline_Bouvier, v_Selma_Bouvier, g); 

    boost::add_edge(v_Homer_Simpson, v_Bart_Simpson, g); 
    boost::add_edge(v_Homer_Simpson, v_Lisa_Simpson, g); 
    boost::add_edge(v_Homer_Simpson, v_Maggie_Simpson, g); 
    boost::add_edge(v_Marge_Bouvier, v_Bart_Simpson, g); 
    boost::add_edge(v_Marge_Bouvier, v_Lisa_Simpson, g); 
    boost::add_edge(v_Marge_Bouvier, v_Maggie_Simpson, g); 

    boost::add_edge(v_Selma_Bouvier, v_Ling_Bouvier, g); 


    typedef std::map<Vertex, size_t>IndexMap; 
    IndexMap mapIndex; 
    boost::associative_property_map<IndexMap> propmapIndex(mapIndex); 
    size_t i=0; 
    FamilyTree::vertex_iterator vi, vi_end; 
    for (boost::tie(vi, vi_end) = boost::vertices(g); vi != vi_end; ++vi) 
    { 
     boost::put(propmapIndex, *vi, i++); 
    } 


    for (boost::tie(vi, vi_end) = boost::vertices(g); vi != vi_end; ++vi) 
    { 
     Vertex vParent = *vi; 

     std::vector<Vertex> vertexDescriptors; 
     BfsVisitor<FamilyTree> bfsVisitor(vertexDescriptors); 
     breadth_first_search(g, vParent, visitor(bfsVisitor).vertex_index_map(propmapIndex)); 


     std::cout << "\nDecendants of " << g[vParent].Name << ":\n"; 
     for (auto v : vertexDescriptors) 
     { 
      Person p = g[v]; 
      std::cout << p.Name << "\n"; 
     } 
    } 

    getchar(); 
    return 0; 
}