- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想用子图实现加权无向图。这是我对该图的初始化代码:
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/subgraph.hpp>
typedef boost::property<boost::edge_weight_t, int> EdgeWeightProperty;
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS,
boost::property<boost::vertex_index_t, int>, boost::property<boost::edge_index_t, int>,
EdgeWeightProperty>
UndirectedGraph;
typedef boost::subgraph<UndirectedGraph> UndirectedGraphS;
int main() {
enum { A, B, C, D, E, F, num_vertices };
// initialize our graph
UndirectedGraphS graph(num_vertices); // add the edges
UndirectedGraphS &coloredGraph = graph.create_subgraph(), // subgraph containing start nodes
uncoloredGraph = graph.create_subgraph(); // main subgraph
// fill the subgraph containing the start nodes
std::vector<UndirectedGraphS::vertex_descriptor> coloredVertLabels = { A, E, F };
auto addVerticesLabels = [](UndirectedGraphS &g, const std::vector<UndirectedGraphS::vertex_descriptor> &list) {
for (const auto &l : list) {
boost::add_vertex(l, g);
}
};
addVerticesLabels(coloredGraph, coloredVertLabels);
// fill the main subgraph
typedef UndirectedGraphS::vertex_descriptor vertex_descriptor;
//typedef boost::graph_traits<UndirectedGraphS>::edge_descriptor EdgeDesc;
std::vector<vertex_descriptor> uncoloredVertLabels = { B, C, D };
addVerticesLabels(uncoloredGraph, uncoloredVertLabels);
// add the edges
auto add_edge_wrap = [](const vertex_descriptor &v1, const vertex_descriptor &v2, int w, UndirectedGraphS &g) {
boost::add_edge(v1, v2, static_cast<UndirectedGraphS::edge_property_type>(w), g);
};
// boost::get_property(graph, boost::graph_name) = "common";
// boost::get_property(coloredGraph, boost::graph_name) = "colored";
// boost::get_property(uncoloredGraph, boost::graph_name) = "uncolored";
add_edge_wrap(A, B, 1, graph);
add_edge_wrap(B, D, 3, graph);
add_edge_wrap(D, E, 1, graph);
add_edge_wrap(E, C, 7, graph);
add_edge_wrap(C, A, 1, graph);
add_edge_wrap(A, D, 2, graph);
add_edge_wrap(C, D, 2, graph);
}
现在我想获取 Dijkstra 算法的权重图,并尝试使用代码:
boost::property_map<UndirectedGraphS, int>::type weightmap = get(boost::edge_weight, graph);
但是出现编译错误:
In file included from /usr/include/boost/graph/adjacency_list.hpp:246:0,
from /home/galactic/CLionProjects/kandinsky/main.cpp:9:
/usr/include/boost/graph/detail/adjacency_list.hpp: In instantiation of ‘struct boost::vec_adj_list_any_vertex_pa::bind_<int, boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, boost::property<boost::vertex_index_t, int>, boost::property<boost::edge_index_t, int>, boost::property<boost::edge_weight_t, int> >, boost::property<boost::vertex_index_t, int> >’:
/usr/include/boost/graph/detail/adjacency_list.hpp:2635:12: required from ‘struct boost::detail::vec_adj_list_choose_vertex_pa<int, boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, boost::property<boost::vertex_index_t, int>, boost::property<boost::edge_index_t, int>, boost::property<boost::edge_weight_t, int> >, boost::property<boost::vertex_index_t, int> >’
/usr/include/boost/graph/detail/adjacency_list.hpp:2761:12: required from ‘struct boost::vec_adj_list_vertex_property_selector::bind_<boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, boost::property<boost::vertex_index_t, int>, boost::property<boost::edge_index_t, int>, boost::property<boost::edge_weight_t, int> >, boost::property<boost::vertex_index_t, int>, int>’
/usr/include/boost/graph/properties.hpp:201:12: required from ‘struct boost::detail::vertex_property_map<boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, boost::property<boost::vertex_index_t, int>, boost::property<boost::edge_index_t, int>, boost::property<boost::edge_weight_t, int> >, int>’
/usr/include/boost/graph/properties.hpp:212:10: required from ‘struct boost::property_map<boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, boost::property<boost::vertex_index_t, int>, boost::property<boost::edge_index_t, int>, boost::property<boost::edge_weight_t, int> >, int, void>’
/usr/include/boost/graph/subgraph.hpp:869:65: required from ‘struct boost::detail::subgraph_global_pmap::bind_<int, boost::subgraph<boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, boost::property<boost::vertex_index_t, int>, boost::property<boost::edge_index_t, int>, boost::property<boost::edge_weight_t, int> > >, boost::property<boost::vertex_index_t, int> >’
/usr/include/boost/graph/subgraph.hpp:934:37: required from ‘struct boost::detail::subgraph_choose_pmap<int, boost::subgraph<boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, boost::property<boost::vertex_index_t, int>, boost::property<boost::edge_index_t, int>, boost::property<boost::edge_weight_t, int> > >, boost::property<boost::vertex_index_t, int> >’
/usr/include/boost/graph/subgraph.hpp:944:43: required from ‘struct boost::detail::subgraph_property_generator::bind_<boost::subgraph<boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, boost::property<boost::vertex_index_t, int>, boost::property<boost::edge_index_t, int>, boost::property<boost::edge_weight_t, int> > >, boost::property<boost::vertex_index_t, int>, int>’
/usr/include/boost/graph/properties.hpp:201:12: required from ‘struct boost::detail::vertex_property_map<boost::subgraph<boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, boost::property<boost::vertex_index_t, int>, boost::property<boost::edge_index_t, int>, boost::property<boost::edge_weight_t, int> > >, int>’
/usr/include/boost/graph/properties.hpp:212:10: required from ‘struct boost::property_map<boost::subgraph<boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, boost::property<boost::vertex_index_t, int>, boost::property<boost::edge_index_t, int>, boost::property<boost::edge_weight_t, int> > >, int>’
/home/galactic/CLionProjects/kandinsky/main.cpp:315:47: required from here
/usr/include/boost/graph/detail/adjacency_list.hpp:2584:29: error: forming reference to void
typedef value_type& reference;
^
/usr/include/boost/graph/detail/adjacency_list.hpp:2585:35: error: forming reference to void
typedef const value_type& const_reference;
^
/usr/include/boost/graph/detail/adjacency_list.hpp:2588:55: error: forming reference to void
<Graph, Graph*, value_type, reference, Tag> type;
^
/usr/include/boost/graph/detail/adjacency_list.hpp:2590:67: error: forming reference to void
<Graph, const Graph*, value_type, const_reference, Tag> const_type;
^
我有一个建议,子图与捆绑属性一起工作时出错,但是使用 UndirectedGraph 类型的 UndirectedGraphS 类的内部成员会导致相同的错误。请告诉我模板实例化出了什么问题
最佳答案
你的类型定义
typedef boost::property<boost::edge_weight_t, int> EdgeWeightProperty;
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS,
boost::property<boost::vertex_index_t, int>,
boost::property<boost::edge_index_t, int>,
EdgeWeightProperty> UndirectedGraph;
将 EdgeWeightProperty
定义为 Graph Property。相反,您需要边缘索引和权重:
typedef boost::property<boost::edge_index_t, int, boost::property<boost::edge_weight_t, int> > EdgeProperty;
并将它们都用于边缘:
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS,
boost::property<boost::vertex_index_t, int>,
EdgeProperty> UndirectedGraph;
现在您可以简单地boost::get(boost::edge_weight, g)
,例如:
boost::property_map<UndirectedGraphS, boost::edge_weight_t>::type
weightmap = boost::get(boost::edge_weight, graph);
UndirectedGraphS &coloredGraph = graph.create_subgraph(), // subgraph containing start nodes
uncoloredGraph = graph.create_subgraph(); // main subgraph
这意外地为 uncoloredGraph
创建了一个无用的拷贝:
static_assert(std::is_reference<decltype(uncoloredGraph)>{}, "oops");
通过更明确的方式防止这种情况:
UndirectedGraphS graph(num_vertices); // add the edges
auto &coloredGraph = graph.create_subgraph(); // subgraph containing start nodes
auto &uncoloredGraph = graph.create_subgraph(); // main subgraph
很多事情可以(而且应该)更简单:
enum { A, B, C, D, E, F, num_vertices };
Sub graph(num_vertices); // add the edges
auto &coloredGraph = graph.create_subgraph(); // subgraph containing start nodes
auto &uncoloredGraph = graph.create_subgraph(); // main subgraph
for (auto l : {A, E, F}) boost::add_vertex(l, coloredGraph);
for (auto l : {B, C, D}) boost::add_vertex(l, uncoloredGraph);
add_edge(A, B, EdgeProps{ 1, 1 }, graph);
add_edge(B, D, EdgeProps{ 2, 3 }, graph);
add_edge(D, E, EdgeProps{ 3, 1 }, graph);
add_edge(E, C, EdgeProps{ 4, 7 }, graph);
add_edge(C, A, EdgeProps{ 5, 1 }, graph);
add_edge(A, D, EdgeProps{ 6, 2 }, graph);
add_edge(C, D, EdgeProps{ 7, 2 }, graph);
只是为了感受图表的最终结果:
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/subgraph.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/property_map/function_property_map.hpp>
typedef boost::property<boost::edge_weight_t, int,
boost::property<boost::edge_index_t, int> > EdgeProps;
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS,
boost::property<boost::vertex_index_t, int>,
EdgeProps> Graph;
typedef boost::subgraph<Graph> Sub;
typedef Sub::vertex_descriptor Vertex;
int main() {
enum { A, B, C, D, E, F, num_vertices };
char const* names[num_vertices] = { "A", "B", "C", "D", "E", "F" };
// initialize our graph
Sub graph(num_vertices); // add the edges
auto &coloredGraph = graph.create_subgraph(); // subgraph containing start nodes
auto &uncoloredGraph = graph.create_subgraph(); // main subgraph
for (auto l : {A, E, F}) boost::add_vertex(l, coloredGraph);
for (auto l : {B, C, D}) boost::add_vertex(l, uncoloredGraph);
add_edge(A, B, EdgeProps{ 1, 1 }, graph);
add_edge(B, D, EdgeProps{ 2, 3 }, graph);
add_edge(D, E, EdgeProps{ 3, 1 }, graph);
add_edge(E, C, EdgeProps{ 4, 7 }, graph);
add_edge(C, A, EdgeProps{ 5, 1 }, graph);
add_edge(A, D, EdgeProps{ 6, 2 }, graph);
add_edge(C, D, EdgeProps{ 7, 2 }, graph);
//boost::property_map<Sub, boost::edge_weight_t>::type weightmap = boost::get(boost::edge_weight, graph);
auto make_name_map = [&names](Sub const& s) {
return boost::make_function_property_map<Vertex>([&](Vertex vd) { return names[s.local_to_global(vd)]; });
};
print_graph(graph, make_name_map(graph), std::cout << "\ngraph:\n");
print_graph(coloredGraph, make_name_map(coloredGraph), std::cout << "\ncoloredGraph:\n");
print_graph(uncoloredGraph, make_name_map(uncoloredGraph), std::cout << "\nuncoloredGraph:\n");
}
打印:
graph:
A <--> B C D
B <--> A D
C <--> E A D
D <--> B E A C
E <--> D C
F <-->
coloredGraph:
A <-->
E <-->
F <-->
uncoloredGraph:
B <--> D
C <--> D
D <--> B C
vertex_index
?它没有被使用,可能会导致很多困惑。我建议放弃它: Live On Coliru
关于c++ - BGL 获取图的权重图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50640890/
我想使用并行 MST 算法 dense_boruvka_minimum_spanning_tree从 boost 。 该算法接口(interface)的一个必需参数是“必须是顶点列表图和分布式边列表图
这个问题是关于Boost Graph Library的。 假设我的图形类型定义如下: using Graph = boost::adjacency_list; 现在,如果我理解正确的话,可以将新的St
我似乎无法弄清楚如何让 BGL 的推送重新标记最大流量算法与捆绑属性一起使用。 像这样设置图表: struct VertexProperties{ }; struct EdgeProperties{
我想用子图实现加权无向图。这是我对该图的初始化代码: #include #include typedef boost::property EdgeWeightProperty; typedef b
下面是一些使用 bgl 创建图形并遍历顶点的示例代码。我想以随机顺序进行此迭代 - 换句话说:循环应该操纵每个顶点,但是对于主函数的每次调用,顶点的顺序应该是随机的。我怎样才能做到这一点? 我用 st
我正在尝试通过编写自定义 DFS 访问器(class TarjanVisitor : public default_dfs_visitor)在 BGL 中实现桥接检测算法 (tarjan),并且我正在
我开始使用 BGL 来完成一些与图形相关的任务。我有很多边,每条边都有几个属性,其中之一就是它的权重。 (所有属性都是 float 和整数)。由于我以前从未使用过 BGL(和/或类似的 CPP 库),
我想让所有边都具有属性、重量和容量。我发现 BGL 已经定义了这些。所以我为图定义了 Edge 和 Vertex 属性 typedef property VertexProperty; typed
我的要求是有一个图形结构,其中每个顶点都由 boost::uuids::uuid 唯一标识。 .所有顶点都有一个颜色属性,相似类别的顶点将根据该颜色属性进行分组。我不是在静态 map 上工作,顶点和边
我正在努力从我的图中删除所有没有连接边的节点(使用定义的模式 here )。到目前为止,我的 (MWE) 代码如下: //g++ -O3 question.cpp -o question.exe #i
我是 BGL( boost 图形库)的新手。我正在学习breadth_first_search 界面,它看起来很方便。但是,在我的应用程序中,当满足其他一些终止条件(例如搜索空间节点数满足最大值)时,
借鉴this回复,我试过如下实现介数中心性: typedef struct vpr_ { int id; } VProp; typedef boost::adjacency_l
我尝试使用boost的transitive_reduction,但是我不知道怎么用。 我有一个图表定义为: typedef boost::adjacency_list Graph; typedef G
我想知道是否有一种方法可以在不使用 lambda 函数的情况下获得 boost 图边缘的排序 vector 。 即我目前正在这样排序: std::vector edgs = ...; std::sor
在 bgl iteration_makros.hpp , 它说 Use the _T versions when the graph type is a template parameter or d
所以我目前正在研究一个单词阶梯问题的项目,我已经构建了用于在其中存储所有字典单词的图形并在其中添加了边,我使用 boost 图形库完成了此操作。 但令我困惑的是 breadth_first_searc
我想在检查边缘时更改边缘权重,但它告诉 error: assignment of member ‘EdgeProperty::weight’ in read-only object g[e].weig
Example code来自 BGL: breadth_first_search(g, vertex(s, g), color_map(get(&VertexProps::color, g)).vis
我正在阅读某人的代码。这是来自 boost 图形库的函数。这是原始函数定义。 void dijkstra_shortest_paths (const Graph& g, t
我希望多线程使用BGL的dijkstra_shortest_paths和astar_search函数,然后读取结果顶点和边的属性映射。 我想知道我是否应该使用互斥锁来确保线程安全。 所以这是我的问题:
我是一名优秀的程序员,十分优秀!