有人可以向像我这样的Boost初学者解释什么是属性地图是在Boost?在尝试使用BGL计算强连接组件时,我遇到了这个问题.我去了属性地图和图形模块的文档,但仍然不知道该怎么做.以此代码为例: - make_iterator_property_map函数在做什么? - 这段代码的含义是什么:get(vertex_index,G)?
#include <boost/config.hpp>
#include <vector>
#include <iostream>
#include <boost/graph/strong_components.hpp>
#include <boost/graph/adjacency_list.hpp>
int
main()
{
using namespace boost;
typedef adjacency_list < vecS, vecS, directedS > Graph;
const int N = 6;
Graph G(N);
add_edge(0, 1, G);
add_edge(1, 1, G);
add_edge(1, 3, G);
add_edge(1, 4, G);
add_edge(3, 4, G);
add_edge(3, 0, G);
add_edge(4, 3, G);
add_edge(5, 2, G);
std::vector<int> c(N);
int num = strong_components
(G, make_iterator_property_map(c.begin(), get(vertex_index, G), c[0]));
std::cout << "Total number of components: " << num …Run Code Online (Sandbox Code Playgroud) 我有一个增强图,每个边都有多个权重(想象一天中每小时一组权重).这些权重值中的每一个都存储在propretyEdge类中:
class propretyEdge {
std::map<std::string,double> weights; // Date indexed
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个包含这些属性的图形,然后用正确的值填充它.现在的问题是我想在图上的特定权重集上启动Dijkstra算法:例如,一个函数可能是:
void Dijkstra (string date, parameters ... )
Run Code Online (Sandbox Code Playgroud)
那将使用
weights[date]
Run Code Online (Sandbox Code Playgroud)
图的每个边的值.
我一遍又一遍地阅读文档,我无法清楚地知道自己要做什么.我当然需要写这样的东西,但我不知道要开始:
boost::dijkstra_shortest_paths (
(*graph_m),
vertex_origin_num_l,
// weight_map (get (edge_weight, (*graph_m)))
// predecessor_map(boost::make_iterator_property_map(predecessors.begin(), get(boost::vertex_index, (*graph_m)))).
// distance_map(boost::make_iterator_property_map(distances.begin (), get(vertex_index,(*graph_m) )))
predecessor_map(predecessorMap).
distance_map(distanceMap)
);
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助.
编辑
感谢Sehe精彩的回答,我能够在MacOS和Ubuntu上做到我想要的.
但是当我们尝试在Visual Studio 2012上编译这段代码时,似乎VS并不是很擅长理解boost的指针功能.所以我们修改了Sehe的部分:
auto dated_weight_f = [&](Graph::edge_descriptor ed) {
return g[ed].weights.at(date);
};
auto dated_weight_map = make_function_property_map<Graph::edge_descriptor, double>(dated_weight_f);
Run Code Online (Sandbox Code Playgroud)
通过:
class dated_weight_f {
public:
dated_weight_f(Graph* graph_p,std::string date_p){
graph_m=graph_p;
date_m=date_p;
}
typedef double …Run Code Online (Sandbox Code Playgroud) 在BGL中,我无法弄清楚如何在bfs/dfs搜索期间访问图中的顶点的固有颜色(白色表示未触摸,灰色表示已访问,黑色表示完成).
有人可以说明如何从dfs/bfs访问者中访问顶点颜色吗?比如写自定义examine_edge?
我正在使用Boost Graph Libraries并且需要使用不是常数的weightmap,但它是参数K的函数(即边缘成本取决于K).在实践中,给出以下代码:
#include <boost/config.hpp>
#include <iostream>
#include <fstream>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/adjacency_list.hpp>
struct Edge {
Edge(float weight_) : weight(weight_) {}
float weight;
float getWeight(int K)
{
return K*weight;
}
};
int main(int, char**){
typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::directedS, boost::no_property, Edge > graph_t;
typedef boost::graph_traits < graph_t >::vertex_descriptor vertex_t;
graph_t g;
vertex_t a = boost::add_vertex(g);
vertex_t b = boost::add_vertex(g);
vertex_t c = boost::add_vertex(g);
vertex_t d = boost::add_vertex(g);
boost::add_edge(a, b, Edge(3), g);
boost::add_edge(b, c, Edge(3), g);
boost::add_edge(a, d, …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试定义增强图的外部属性.我使用一些捆绑属性作为内部属性:
struct VertexProperties
{
int demand;
};
struct EdgeProperties
{
uint capacity;
int cost;
};
typedef adjacency_list <vecS, vecS, bidirectionalS, VertexProperties, EdgeProperties> Graph;
Run Code Online (Sandbox Code Playgroud)
然而,在算法期间我需要一些外部属性,即我希望能够将我的图形的边/顶点映射到存储在std :: vector中的元素,以便我可以通过operator [](Edge)访问它们E).我站在提升文档前面,没有任何线索.好像我需要一个property_map,但我不知道如何将这些与vector一起使用.到目前为止,我发现的唯一例子涉及从顶点到矢量的映射,但由于顶点是无符号整数,因此这是微不足道的.
到目前为止,我对提升感到非常沮丧,我认为这样可以节省我很多时间来自己实现和测试图表类,我真的没有得到这个疯狂的模板元编程的东西......
如何创建图形,以使每个属性映射中的属性映射(边的权重)不同?是否可以创建这样的属性映射?像一系列属性地图?我没有在互联网上看到任何人使用它,我可以举个例子吗?
Graph g(10); // graph with 10 nodes
cin>>a>>b>>weight1>>weight2>>weight3>>weight4;
Run Code Online (Sandbox Code Playgroud)
并将每个权重放在属性映射中.
我已经阅读了这里的文档(https://www.boost.org/doc/libs/1_75_0/libs/graph/doc/using_adjacency_list.html)和几个堆栈溢出页面两个小时,但没有取得任何进展根本就在这里。我有一个图表,其中边缘同时具有距离和坑洞数量(车辆在遇到 3 个坑洞后就会破裂),所以我想我会使用自定义结构并设置边缘属性。
将其放置在类声明之上:
struct road_details_t {
typedef boost::edge_property_tag kind;
};
struct road_info {
unsigned miles;
bool pothole;
};
typedef boost::property<road_details_t, struct road_info> RoadDetailsProperty;
Run Code Online (Sandbox Code Playgroud)
编辑:我相信这是上述的替代方案,但也没有运气:
namespace boost {
enum road_details_t { road_details };
BOOST_INSTALL_PROPERTY(edge, road_details);
}
Run Code Online (Sandbox Code Playgroud)
类声明中的邻接图:
typedef boost::adjacency_list<boost::listS, // store edges in lists
boost::vecS, // store vertices in a vector
boost::undirectedS
RoadDetailsProperty
>
Graph;
typedef boost::graph_traits<Graph> GraphTraits;
typedef GraphTraits::vertex_descriptor Vertex;
typedef GraphTraits::edge_descriptor Edge;
Graph roadMap;
Run Code Online (Sandbox Code Playgroud)
稍后在添加道路的函数中:
void City::addRoad(Intersection intersection1, Intersection intersection2, unsigned time, bool pothole)
{
unsigned …Run Code Online (Sandbox Code Playgroud)