Question: I need help working on this C + + program revolving around the traveling salesman problem. I can't send the whole program so I'll send

I need help working on this C++ program revolving around the traveling salesman problem. I can't send the whole program so I'll send the part im supposed to work on. We are only supposed to work on the TSP function.
double TSP(const std::vector &edges, unsigned int n_vertices){
/* IMPLEMENT THIS FUNCTION */
double min_cost = std::numeric_limits::infinity();
return min_cost;
}
vertex_t get_source(const weighted_edge_t &edg){
return std::get<0>(edg);
}
vertex_t get_destination(const weighted_edge_t &edg){
return std::get<1>(edg);
}
vertex_t get_weight(const weighted_edge_t &edg){
return std::get<2>(edg);
}
unsigned int get_vertex_count(const std::vector &edges){
unsigned int n_vertices =0;
if(!edges.empty()){
std::set vertex_set;
/* add the source vertices to vertex_set */
std::transform(
edges.begin(),
edges.end(),
std::inserter(vertex_set, vertex_set.end()),
get_source
);
/* add the destination vertices to vertex_set */
std::transform(
edges.begin(),
edges.end(),
std::inserter(vertex_set, vertex_set.end()),
get_destination
);
/* get the largest vertex from vertex_set and add 1*/
n_vertices =*std::max_element(vertex_set.begin(), vertex_set.end())+1;
}
return n_vertices;
}
std::vector read_graph(const std::string &filename){
std::vector edges;
std::ifstream file(filename);
if(file){
/* read edges into a set to remove duplicates */
std::set edge_set;
vertex_t src, dst;
double wt;
while(file >> src && file >> dst && file >> wt){
edge_set.insert( weighted_edge_t(src,dst,wt));
}
/* check if there was an error reading in the file */
if(file.bad()){// i/o error
std::ostringstream oss;
oss << filename <<": "<< strerror(errno);
throw std::runtime_error(oss.str());
}
else if((file.fail() && !file.eof())){// conversion error
std::ostringstream oss;
oss << filename <<": error reading file";
throw std::runtime_error(oss.str());
}
/* copy the edges read to the edges vector */
std::copy(edge_set.begin(), edge_set.end(), std::back_inserter(edges));
}
else {// error opening file
std::ostringstream oss;
oss << filename <<": "<< strerror(errno);
throw std::runtime_error(oss.str());
}
if(edges.empty()){
std::ostringstream oss;
oss << filename <<": file does not contain any edges";
throw std::runtime_error(oss.str());
}
return edges;
}
void usage(int argc, char *argv[]){
if(argc !=2){
std::cerr << "Invalid number of command line arguments." << std::endl << std::endl;
}
std::cout << "usage: ";
std::cout << argv[0]<<" infile" << std::endl;
std::cout <<" infile - file containing a list of edges" << std::endl << std::endl;
std::cout <<"It is assumed that each line of contains an edge of the" <."<< std::endl;
}
int main(int argc, char *argv[]){
if(argc !=2){
usage(argc, argv);
}
else {
try {
std::vector edges = read_graph(argv[1]);
unsigned int n_vertices = get_vertex_count(edges);
double min_cost = TSP(edges, n_vertices);
if(min_cost == std::numeric_limits::infinity()){
std::cout <<"No Hamiltonian cycle exists." << std::endl;
}
else {
std::cout << min_cost << std::endl;
}
}
catch (std::exception &ex){
std::cerr << ex.what()<< std::endl;
}
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!