我正在开发一个项目来查找图表中的最短路径,我必须能够指定起点和终点。我使用Boost完成了大部分工作,但我还有一个问题。如何指定起始顶点?这是我的代码。如果你能指出我正确的方向,我将非常感激。有问题的线是所有**的线,靠近底线。
#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");
}
答案 0 :(得分:0)
要定义刚开始和结束顶点,请在指定这些节点的字符串中读取,并使用name2v贴图来获取顶点对象。
虽然有几点,但Dijkstra的算法(http://en.wikipedia.org/wiki/Dijkstra's_algorithm)是所有最短路径算法的单一来源。这意味着它将计算从起始顶点到所有其他顶点的最短路径。
如果你只是两个顶点之间的最短路径,只需使用深度优先搜索。
查看示例DFS提升程序(http://www.boost.org/doc/libs/1_55_0/libs/graph/example/dfs-example.cpp)。您可以使用先前的映射来读出实际路径。