0

I'm working on a project to find the shortest path in a graph, and I must be able to specify the start and end vertices. I've got most of it done using Boost, but I have one question left. How do I specify the start vertex? Here's my code. If you could point me in the right direction, I would be greatly appreciative. The line in question is the one with all of the **'s, near the bottom.

#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <string>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/visitors.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/metis.hpp>

using namespace std;

typedef boost::adjacency_list_traits<
boost::vecS, boost::vecS, boost::undirectedS, boost::listS> GraphTraits;
typedef GraphTraits::vertex_descriptor Vertex;
struct VertexProperty {
string name;
Vertex predecessor;
double distance;
boost::default_color_type color;
VertexProperty(const string& aName = "") : name(aName) { };
};
struct EdgeProperty {
double weight;
EdgeProperty(double aWeight = 0.0) : weight(aWeight) { };
};
struct do_nothing_dijkstra_visitor {
template <typename Vertex, typename Graph>
void initialize_vertex(Vertex u, const Graph& g) const { };
template <typename Vertex, typename Graph>
void examine_vertex(Vertex u, const Graph& g) const { };
template <typename Edge, typename Graph>
void examine_edge(Edge e, const Graph& g) const { };
template <typename Vertex, typename Graph>
void discover_vertex(Vertex u, const Graph& g) const { };
template <typename Edge, typename Graph>
void edge_relaxed(Edge e, const Graph& g) const { };
template <typename Edge, typename Graph>
void edge_not_relaxed(Edge e, const Graph& g) const { };
template <typename Vertex, typename Graph>
void finish_vertex(Vertex u, const Graph& g) const { };
};
int main() {
string tempName1;
string tempName2;
string tempString;
string data2;
double weight;
cout << "please enter the data file name: ";
char strFileName[256];
cin >> strFileName;
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,               VertexProperty, EdgeProperty> Graph;
Graph g;
// preparing the data
ifstream fin;
fin.open(strFileName);
if (!fin) {
    cerr << "Can't open data file, leaving...\n";
    return EXIT_FAILURE;
}
else{
    cout << "file loaded." << endl << endl;
}
std::map<std::string, Vertex> name2v;
std::getline(fin, tempString); //Vertices:
std::getline(fin, tempString); //Location1, Location2, ...
std::stringstream tempSS(tempString);
while (std::getline(tempSS, tempName1, ',')){
    name2v[tempName1] = add_vertex(VertexProperty(tempName1), g);
}
std::getline(fin, tempString); //Edges:
while (std::getline(fin, tempString)){ // (Location1, Location2, 6)
    //remove parentheses
    tempString.erase(tempString.begin(), tempString.begin() +
tempString.find('(') + 1);
    tempString.erase(tempString.begin() + tempString.find(')'),  
tempString.end());
    std::stringstream temp_ss(tempString);
    std::getline(temp_ss, tempName1, ',');
    std::getline(temp_ss, tempName2, ',');
    temp_ss >> weight;
    add_edge(name2v[tempName1], name2v[tempName2], EdgeProperty(weight), g);
}
char x;
cout << endl << "How would you like to process your data file?" << endl;
cout << "1.) shortest path" << endl;
cout << "2.) minimum spanning tree" << endl;
cout << "3.) Travelling Salesman" << endl << endl;
returnQuestion:
cout << "please enter 1,2,3 or Q to quit: ";
cin >> x;
switch (x){
case 1: //do the work for shortest path
    cout << "please enter the node ";
    dijkstra_shortest_paths(
        **g, start_vertex;*******************
        get(&VertexProperty::predecessor, g),
        get(&VertexProperty::distance, g),
        get(&EdgeProperty::weight, g),
        boost::identity_property_map(), // index-map
        less<double>(), // compare
        plus<double>(), // combine 
        numeric_limits<double>::infinity(), // infinity
        0.0, // zero
        do_nothing_dijkstra_visitor(),
        get(&VertexProperty::color, g));
    break;
case 2: //do the work for minimum spanning
    break;
case 3: //do the work for travelling salesman
    break;
case 'q':
case 'Q':
    return EXIT_SUCCESS;
    break;
default:
    goto returnQuestion;
}
system("pause");
}
user2896852
  • 57
  • 1
  • 6

1 Answers1

0

To define the Start and End vertices just read in the strings that designate those nodes and use your name2v map to get the vertex objects.

A few points though, Dijkstra's algorithm (http://en.wikipedia.org/wiki/Dijkstra's_algorithm) is a single source all shortest paths algorithm. Meaning that it will calculate the shortest paths from a start vertex to ALL other vertices.

If you only what the shortest path between two vertices, just use depth first search.

Check out the example DFS boost program (http://www.boost.org/doc/libs/1_55_0/libs/graph/example/dfs-example.cpp). You can use the predecessor map to read out the actual path.

pbible
  • 1,259
  • 1
  • 18
  • 34