Boost图中的最长路径 对不起,如果这对你们中的一些人来说是一个非常基本的问题,但我是C++新手(更不用说提升图形库)了,无法解决这个问题。到目前为止,我已经能够使用下面的代码制定/收集代码来创建图形

Boost图中的最长路径 对不起,如果这对你们中的一些人来说是一个非常基本的问题,但我是C++新手(更不用说提升图形库)了,无法解决这个问题。到目前为止,我已经能够使用下面的代码制定/收集代码来创建图形,boost,graph,Boost,Graph,现在我试图找出在这个图中找到最长路径的代码 有人能帮我解释一下代码是什么吗?在试图找到路径时,我很难确定是否/如何遍历每个节点和/或边 我必须尝试返回最长路径中的所有节点和边 任何帮助都将不胜感激 有人知道C++是否组织了像JavaDoc??p> #include <boost/graph/dag_shortest_paths.hpp> #include <boost/graph/adjacency_list.hpp> #include <windows.h

现在我试图找出在这个图中找到最长路径的代码

有人能帮我解释一下代码是什么吗?在试图找到路径时,我很难确定是否/如何遍历每个节点和/或边

我必须尝试返回最长路径中的所有节点和边

任何帮助都将不胜感激

有人知道C++是否组织了像JavaDoc??p>
    #include <boost/graph/dag_shortest_paths.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <windows.h>
#include <iostream>



int main()
{
  using namespace boost;
  typedef adjacency_list<vecS, vecS, directedS, property<vertex_distance_t, double>, property<edge_weight_t, double> > graph_t;
  graph_t g(6);
  enum verts { stationA, stationB, stationC, stationD, stationE, stationF };
  char name[] = "rstuvx";


  add_edge(stationA, stationB, 5000.23, g);
  add_edge(stationA, stationC, 3001, g);
  add_edge(stationA, stationD, 2098.67, g);
  add_edge(stationA, stationE, 3298.84, g);
  add_edge(stationB, stationF, 2145, g);
  add_edge(stationC, stationF, 4290, g);
  add_edge(stationD, stationF, 2672.78, g);
  add_edge(stationE, stationF, 11143.876, g);
  add_edge(stationA, stationF, 1, g);




//Display all the vertices
  typedef property_map<graph_t, vertex_index_t>::type IndexMap;
  IndexMap index = get(vertex_index, g);
  std::cout << "vertices(g) = ";

  typedef graph_traits<graph_t>::vertex_iterator vertex_iter;
  std::pair<vertex_iter, vertex_iter> vp;
  for (vp = vertices(g); vp.first != vp.second; ++vp.first)
      std::cout << index[*vp.first] <<  " ";
  std::cout << std::endl;
  // ...

   // Display all the edges
    // ...
  std::cout << "edges(g) = " << std::endl;
    graph_traits<graph_t>::edge_iterator ei, ei_end;
    for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
  std::cout << "(" << index[source(*ei, g)] << "," << index[target(*ei, g)] << ") \n";
    std::cout << std::endl;
    // ...
#包括
#包括
#包括
#包括
int main()
{
使用名称空间boost;
typedef邻接列表图;
图g(6);
枚举顶点{stationA,stationB,stationC,stationD,stationE,stationF};
字符名[]=“rstuvx”;
添加边缘(stationA,stationB,5000.23,g);
添加边缘(stationA、stationC、3001、g);
添加边缘(stationA,stationD,2098.67,g);
添加_边(stationA,stationE,3298.84,g);
添加_边(站B、站F、2145、g);
添加边缘(stationC、stationF、4290、g);
添加_边(stationD,stationF,2672.78,g);
添加_边(StationOne,stationF,11143.876,g);
添加_边(stationA、stationF、1、g);
//显示所有顶点
typedef属性_映射::type IndexMap;
IndexMap index=get(顶点索引,g);

std::cout我认为您应该检查一下boost分布中的示例。 在线:


要使它找到最长路径,只需简单地将权重(W)倒数,可以使用常数-W或1/W。如果常数为0,则表示它是一个否定(-W)。

我同意必须小心符号反转。首先,大多数最短路径算法仅适用于正边图。您确实有一些选择(例如Bellman-Ford算法)推广到负权图,如果图中有负循环,它们不能保证返回最佳答案。首先,谢谢你的回答。我看了这个例子。我说我是C++新手,我希望看到我可以遍历整个图的代码。Dijkstra也可能会这样做。简单的基本图。但一般来说,对于较长/较大的图,否定权重可能不是解决方案,特别是如果它不是非循环的。检查图遍历:我认为Dijkstra的方法也适用于较大的图(你能说明原因吗?).如果图是循环的,最长路径当然是无穷大。好吧,我知道我们不能用Dijkstra的负权重。但是,想想看,1/| W |可能有效(考虑到我们所有的边权重都是正的)。非常感谢。谢谢你的反馈。这就是为什么我写它是常数-W。当常数大于最大值W时,我们将得到所有的正权重。错误:看看这里:最长路径问题是NP难问题。这意味着简单地反转图的权重将不起作用。它不会产生正确的结果。作为对e最长路径,只需从每个顶点开始,从任意点开始,走尽可能长的路。