0

I have a Graph Represented With Following Code :

typedef adjacency_list < vecS, vecS, directedS, property < vertex_name_t, idType >, property < edge_weight_t, double > > graph_t;

I Have two Questions :

  1. how to feed My Graph into Dijkstra Algorithm in Boost, I mean how to extract the Properties from My Graph (which is thousands of Vertices and Edges) to Feed Dijkstra Parameters.

  2. The Output of The Algorithm in The Example is the Shortest Path between one node in The Graph, and The rest Nodes, so How I can Get Only The Shortest Path Between My Source and Target Node Only ? should I filter the Output result to build My Shortest Path Vector ?

//===========================================================================
MyAlgorithm::MyAlgorithm(graph_t AnyGraph, Vertex VSource){// Parameters Constructor

  MyGraph = AnyGraph;
  vector<Vertex> p(num_vertices(AnyGraph));
  //  for(auto j = p.begin(); j != p.end(); ++j)
  //      cout <<"P["<< *j<<"] = "<<p[*j]<<endl; 
  vector<double> d(num_vertices(AnyGraph));
  //  for(auto j2 = d.begin(); j2 != d.end(); ++(j2))
  //      cout <<"d ["<< *(j2)<<"] = "<<d[*(j2)]<<endl; 
  //===========================================================================
  //Dijkstra_Algorithm
  //===========================================================================
  cout<<"Before\t"<<endl;
  dijkstra_shortest_paths(AnyGraph, VSource,
                          predecessor_map(boost::make_iterator_property_map(p.begin(), get(boost::vertex_index, AnyGraph))).
                          distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, AnyGraph))));
  cout<<"After\t"<<endl;
  //===========================================================================
}// End of Parameters Constructor
Bilal
  • 3,191
  • 4
  • 21
  • 49

2 Answers2

1

I am answering your second question:

You can't ask dijkstra's algorithm for only one path, because the paths are made gradually, still, you can save a parent for each node which states which node is the previous node of this node in the path between the start node and the end node. after that you can start from the target node, and get its parent until you reach the start node.

Arman Babaei
  • 165
  • 1
  • 9
0

First, you can't use Dijkstra without a cost estimate for how close each node is.

That said, this problem is small enough that you can simply do a breadth-first search from the start for the best route to the target.

If you want to make it more efficient, you can do a search from both source and target at the same time, and find where they meet. Guaranteeing optimal is a little tricky, but that makes the search on average much quicker.

btilly
  • 43,296
  • 3
  • 59
  • 88