Question: #include #include #include #include #include #include #include #include #include #include #include typedef std::size _ t vertex _ t; typedef std::tuple weighted _ edge _ t;
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef std::sizet vertext;
typedef std::tuple weightededget;
Get the source of a weighted edge.
This function returns the source of a weighted edge.
@param edge the edge
@return std::getedg
vertext getsourceconst weightededget &edg;
Get the destination of a weighted edge.
This function returns the destination of a weighted edge.
@param edge the edge
@return std::getedg
vertext getdestinationconst weightededget &edg;
Get the weight of a weighted edge.
This function returns the weight of a weighted edge.
@param edge the edge
@return std::getedg
vertext getweightconst weightededget &edg;
Determine the number of vertices in the graph.
This function determines the number of vertices in the graph represented
by a vector of weighted edges. It is assumed that the number of vertices
is given by the largest source or destination vertex in the list of edges
plus one.
@param edges a vector of weighted edges defining a graph
@return the number of vertices in the graph represented by edges
unsigned int getvertexcountconst std::vector &edges;
Reads weighted edges from a file.
This function reads weighted edges from filename and returns them as a
std::vector. each line of the file is assumed to be of the
form src dst wt where src is the source vertex, and dst is the destination
vertex, and wt is the weight of the edge. All vertices are assumed to be
unsigned integers that can be stored as a std::sizet ie in a vertext
Duplicate edges found in the file are ignored.
@param filename name of the file to read
@return a vector of edges read from filename
@throws std::runtimeerror thrown if there is an error reading the file
std::vector readgraphconst std::string &filename;
Solves the traveling salesman problem by brute force.
This function solves the TSP via brute force. It accepts a vector of
weighted edges and count of the number of vertices. These two values
together define a weighted graph.
@param edges a vector of weighted edges in the graph
@param nvertices the number of vertices in the graph;
the vertices are nvertices
@return the cost of the minimum Hamiltonian cycle or infinity if none exists
double TSPconst std::vector &edges, unsigned int nvertices;
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 f
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
