BGL 获取图形的权重图

BGL get weight map for graph

本文关键字:权重 图形 获取 BGL      更新时间:2023-10-16

我想用子图实现加权无向图。这是我对该图的初始化代码:

#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定义为图形属性。相反,您需要同时使用边索引和权重:

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

它没有被使用,可能会导致很多混乱。我建议放弃它:住在科里鲁