views:

52

answers:

1

Hi,

Sorry if this is a very basic questions for some of you but I'm new to C++ (let alone Boost Graph Library) and couldn't figure out this problem. So far I've been able to formulate/gather code to create a graph using the code below.

Now I'm trying to figure out the code to find the longest path in this graph.

Can someone please help with what would the code be? I was having trouble trying to figure out if/how to traverse through each node and/or edge when trying to find the path?

I have to try to return all the nodes and edges in the longest path.

Any help will be greatly appreciated.

P.S. does anyone know if C++ has organized documentation like Javadoc??

    #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;
    // ...
+2  A: 

I think you should check the example in your boost distribution. Online : http://www.boost.org/doc/libs/1_38_0/libs/graph/example/dijkstra-example.cpp

To make it find the longest path you need to simply inverse the weight (W), either use a Constant - W, or 1/W. If the constant is 0, then it means it's a negation (-W).

monn
First of all, thanks for your reply. I looked at this example. As I said I'm new to C++ I was hoping to see the code of how I can traverse through the entire graph?Also, Dijkstra might work on this simple basic graph. But in general for longer/bigger graphs, negating the weights might not be the solution, especially if its not acyclic.
TheTSPSolver
Check this for graph traversal:http://www.boost.org/doc/libs/1_43_0/libs/graph/doc/depth_first_search.htmlhttp://www.boost.org/doc/libs/1_43_0/libs/graph/example/dfs-example.cppI think Dijkstra's would also works on bigger graphs (could you specify the reason?). And if the graphs is cyclic, of course the longest path would be infinity.
monn
Well, from what I know is that we can't use the negative weights with Dijkstra.However, come to think of it, 1/|W| might work (considering all the edge weights we have are positive anyways).Thanks a lot. for your feedback.
TheTSPSolver
That's why I wrote it's Constant - W. When Constant is bigger that max W, we will end up with all positive weights.
monn