在labeled_graph上进行breadth_first_search [英] breadth_first_search on labeled_graph
问题描述
假设以下设置,应如何使用labeled_graph调用breadth_first_search?-导致2个错误:
How should breadth_first_search be called using labeled_graph assuming the following setup? - results in 2 errors:
二进制'[':未找到采用'顶点'类型的右操作数(或没有可接受的转换)的运算符
binary '[' : no operator found which takes a right-hand operand of type 'Vertex' (or there is no acceptable conversion)
'.id'左侧的错误2必须具有class/struct/union
Error 2 left of '.id' must have class/struct/union
#include<iostream>
#include<boost/graph/adjacency_list.hpp>
#include<boost/graph/breadth_first_search.hpp>
#include <boost/graph/labeled_graph.hpp>
using namespace boost;
struct NodeInfo{int id;};
struct EdgeInfo{};
typedef boost::labeled_graph< boost::adjacency_list<
boost::vecS, boost::vecS, boost::undirectedS, NodeInfo, EdgeInfo>,
std::string> Graph;
typedef boost::graph_traits<Graph>::vertex_descriptor GridVertex;
class Topology
{
public:
Graph grid;
std::map<std::string, GridVertex> vertices; //_id to Edge
struct custom_visitor : public boost::default_bfs_visitor
{
Graph& grid;
custom_visitor(Graph& grid) :grid(grid) {}
template <typename Vertex, typename Graph>
void discover_vertex(Vertex v, const Graph& g)
{
//vertex(...) in breadth_first_search is causing:
//binary '[' : no operator found which takes a right-hand operand of
//type 'Vertex' (or there is no acceptable conversion)
//left of .id must have class...
int m = grid[v].id;
}
};
void GetShortestPath(std::string s_id, std::string t_id)
{
custom_visitor vis(grid);
//vertex(...) causes error
breadth_first_search(grid.graph(), vertex(vertices[s_id],grid.graph()), visitor(vis));
}
void BuildNet()
{
Graph g;
GridVertex v;
v = add_vertex("A", NodeInfo(), g);
vertices["A"] = v;
v = add_vertex("B", NodeInfo(), g);
vertices["B"] = v;
add_edge_by_label("A", "B", EdgeInfo(), g);
}
};
int main()
{
Topology net;
net.GetShortestPath("A", "B");
return 0;
}
推荐答案
为什么要使用labelled_graph?
Why are you using labeled_graph?
标签图与 just adjacency_list的界面不同.这不足为奇,因为否则会是什么意思:)
Labeled graph has a different interface from just adjacency_list. This is not surprising, because, what would be the point otherwise :)
因此,如果 vertex(...)导致错误
,请使用 grid.vertex(s_id)
:
breadth_first_search(grid.graph(), grid.vertex(s_id), visitor(vis));
在访问者中,使用实际图形,以便可以使用其 operator []
:
In the visitor, use the actual graph so you can use its operator[]
:
int m = grid.graph()[v].id;
或者,实际上,为什么不使用存在的第二个参数来实现这一目的:
Or, in fact why not use the second parameter which exists for the purpose:
void discover_vertex(Vertex v, const Graph& g) {
int m = g[v].id;
}
通过代码做出明智的示例: 在Coliru上直播
An effort to make a sensible example from the code: Live On Coliru
这篇关于在labeled_graph上进行breadth_first_search的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!