Question: After working on my program some more, i've encountered a logic error where the ouput should be No Hamiltonian Cycle exists but im getting
After working on my program some more, i've encountered a logic error where the ouput should be No Hamiltonian Cycle exists" but im getting How do i change my code to account for if no hamiltonian cycle exists?
Here is the code:
double TSPconst std::vector& edges, unsigned int nvertices
IMPLEMENT THIS FUNCTION
double mincost std::numericlimits::infinity;
Iterate through all permutations of vertices
std::vector permutationnvertices;
for vertext i ; i nvertices; i
permutationi i;
do
double cost ;
Calculate the cost of the current permutation
for sizet i ; i nvertices; i
vertext src permutationi;
vertext dst permutationi nvertices;
Find the corresponding edge and add its weight to the total cost
for const auto& edge : edges
if std::getedge src && std::getedge dst
cost std::getedge;
break;
Update the minimum cost if the current permutation has lower cost
if cost mincost
mincost cost;
while std::nextpermutationpermutationbegin permutation.end;
return mincost;
For more context, this is the Traveling Salesman Problem. I think it would be a simple if statement, but I don't know what to check.
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
