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::size_t vertex_t;
typedef std::tuple weighted_edge_t;
/* Get the source of a weighted edge.
*
* This function returns the source of a weighted edge.
*
* @param edge - the edge
*
* @return std::get<0>(edg)
**/
vertex_t get_source(const weighted_edge_t &edg);
/* Get the destination of a weighted edge.
*
* This function returns the destination of a weighted edge.
*
* @param edge - the edge
*
* @return std::get<1>(edg)
**/
vertex_t get_destination(const weighted_edge_t &edg);
/* Get the weight of a weighted edge.
*
* This function returns the weight of a weighted edge.
*
* @param edge - the edge
*
* @return std::get<2>(edg)
**/
vertex_t get_weight(const weighted_edge_t &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 get_vertex_count(const 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::size_t (i.e., in a vertex_t).
* 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::runtime_error - thrown if there is an error reading the file
**/
std::vector read_graph(const 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 n_vertices - the number of vertices in the graph;
* the vertices are 0... n_vertices-1
*
* @return the cost of the minimum Hamiltonian cycle or infinity if none exists
**/
double TSP(const std::vector &edges, unsigned int n_vertices);
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 f

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 Programming Questions!