关于评论中的问题:
- 根据示例代码中的注释,VC++与所使用的命名参数机制存在一些问题。因此,我认为两个分支基本上都是做相同的事情,只不过 VC++ 版本更冗长(尽管我没有深入研究它,不能百分之百确定)。
property_map
将顶点/边映射到与特定顶点/边相关联的其他数据。例如,在示例中,weightmap
将每个边与其权重(行进成本)相关联。
predecessor_map
用于记录所有顶点的路径(记录从根到每个顶点的前继)。为什么需要这样做:这种信息通常是希望从算法中获取的。
- 参数在描述中清楚地列出。请注意两个版本,一个带有命名参数,另一个没有(后者在 VC++ 中使用)。
现在来看看
文档中给出的示例代码(请注意,我从未真正使用过 Boost.Graph,因此这并不保证正确性;此外,我只包含了相关部分,并省略了 VC++ 的
#if
):
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(&p[0]).distance_map(&d[0]));
如我在评论中提到的,个人认为Lemon比Boost.Graph更直观易用,也许你可以考虑使用它。
Boost.Graph
更直观(但这可能只是我的想法)。 - Grizzly