提升图:Dijkstra算法

时间:2015-11-04 13:10:36

标签: c++ boost-graph

我正在尝试实现有向图,以便通过Dijkstra算法计算最短路径。为了做到这一点,我看一下boost网站提供的示例,并尝试应用示例中的内容。我尝试实现的图表有超过15k个节点和50k边缘。网站上提供的示例代码类似于:

typedef adjacency_list < listS, vecS, directedS, no_property, property <edge_weight_t, int > > graph_t;
typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor;
typedef std::pair<int, int> Edge;
const int num_nodes = 5;

enum nodes { A, B, C, D, E };
char name[] = "ABCDE";

Edge edge_array[] = { Edge(A, C), Edge(B, B), Edge(B, D), Edge(B, E),
Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B)
};
int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 };

int num_arcs = sizeof(edge_array) / sizeof(Edge);
graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes);
property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight,   g);
std::vector<vertex_descriptor> p(num_vertices(g));
std::vector<int> d(num_vertices(g));
vertex_descriptor s = vertex(A, g);

dijkstra_shortest_paths(g, s, predecessor_map(boost::make_iterator_property_map(p.begin(), get(boost::vertex_index, g))).distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, g))));
std::cout << "distances and parents:" << std::endl;
graph_traits < graph_t >::vertex_iterator vi, vend;
for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi) {
std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", ";
std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std::
  endl;
}

使用它来实现我自己的图形结果:

typedef adjacency_list < listS, vecS, directedS, no_property, property < edge_weight_t, int > > MyGraph;
typedef graph_traits < MyGraph >::vertex_descriptor vertex_descriptor;
typedef std::pair<int, int> Edge;

//填充了edge_array。假设它工作正常。

int num_arcs = sizeof(edge_array) / sizeof(Edge);//<-- Cause of the error
MyGraph Graph(edge_array, edge_array + num_arcs, weights, num_nodes);//<-- Exception is raised. 
property_map<MyGraph, edge_weight_t>::type weightmap = get(edge_weight,   Graph);

std::vector<vertex_descriptor> p(num_vertices(Graph));
std::vector<int> d(num_vertices(Graph));
vertex_descriptor s = vertex(A, Graph);

箭头被评论的地方是此后在该行发生的错误的原因。除了 celfpp.exe中0x012BA9A9的未处理异常之外没有输出错误:0xC0000005:访问冲突读取位置0x00434000。

如果使用两个edge_arrays,一个用于示例,另一个用于我自己的。如果要执行代码:

int num_arcs = sizeof(example_edge_array) / sizeof(Edge);
graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes);

代码不会遇到异常。对我来说,不清楚为什么,我认为这不是解决我的问题的方法。但是,它确实提供了导致问题的原因。

是否有人可能会建议我如何解决这个问题?

编辑(1):

我发现在下面的遗体中m_value仍未定义:

template <class Tag, class T, class  
Base = no_property>
struct property {
typedef Base next_type;
typedef Tag tag_type;
typedef T value_type;
property(const T& v = T()) : m_value(v) { }
property(const T& v, const Base& b) : m_value(v), m_base(b) { }
// copy constructor and assignment operator will be generated by compiler

T m_value;
Base m_base;
};  

0 个答案:

没有答案