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 TSPconst std::vector &edges, unsigned int nvertices
IMPLEMENT THIS FUNCTION
double mincost std::numericlimits::infinity;
return mincost;
vertext getsourceconst weightededget &edg
return std::getedg;
vertext getdestinationconst weightededget &edg
return std::getedg;
vertext getweightconst weightededget &edg
return std::getedg;
unsigned int getvertexcountconst std::vector &edges
unsigned int nvertices ;
ifedges.empty
std::set vertexset;
add the source vertices to vertexset
std::transform
edges.begin
edges.end
std::insertervertexset, vertexset.end
getsource
;
add the destination vertices to vertexset
std::transform
edges.begin
edges.end
std::insertervertexset, vertexset.end
getdestination
;
get the largest vertex from vertexset and add
nvertices std::maxelementvertexset.begin vertexset.end;
return nvertices;
std::vector readgraphconst std::string &filename
std::vector edges;
std::ifstream filefilename;
iffile
read edges into a set to remove duplicates
std::set edgeset;
vertext src dst;
double wt;
whilefile src && file dst && file wt
edgeset.insert weightededgetsrcdstwt;
check if there was an error reading in the file
iffilebad io error
std::ostringstream oss;
oss filename : strerrorerrno;
throw std::runtimeerrorossstr;
else iffilefail && file.eof conversion error
std::ostringstream oss;
oss filename : error reading file";
throw std::runtimeerrorossstr;
copy the edges read to the edges vector
std::copyedgeset.begin edgeset.end std::backinserteredges;
else error opening file
std::ostringstream oss;
oss filename : strerrorerrno;
throw std::runtimeerrorossstr;
ifedgesempty
std::ostringstream oss;
oss filename : file does not contain any edges";
throw std::runtimeerrorossstr;
return edges;
void usageint argc, char argv
ifargc
std::cerr "Invalid number of command line arguments." std::endl std::endl;
std::cout "usage: ;
std::cout argv 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 mainint argc, char argv
ifargc
usageargc argv;
else
try
std::vector edges readgraphargv;
unsigned int nvertices getvertexcountedges;
double mincost TSPedges nvertices;
ifmincost std::numericlimits::infinity
std::cout No Hamiltonian cycle exists." std::endl;
else
std::cout mincost std::endl;
catch std::exception &ex
std::cerr exwhat std::endl;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
