Question

C++ data structure problem. (Thanks) Traveling Salesman Problem Exercise Consider 5 cities of interest, namely a)...

C++ data structure problem. (Thanks)

Traveling Salesman Problem Exercise

Consider 5 cities of interest, namely a) Reno, b) San Francisco, c) Salt Lake City, d) Seattle, and e) Las Vegas. Use information on the road network and derive the miles from one city to the other. Then on that basis, conduct the following:

 Create a graph with each of its vertices correspond to one of these cities and its edges being weighted by the associated weights. Note that if (and only if) to go from city A to B you must go through C then you must add one edge from A to C and one edge from C to B and there is no need to add an edge directly from A to B.

 Solve the Traveling Salesman Problem such that traveling salesman starts from Reno, visits all cities in the above list and returns to list. Solve this problem in the brutal forceway,

i.e. by identifying all possible paths. Submit your solution in terms of a) code, and b) a *.txt all possible paths and the best one selected by the algorithm.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// CPP program to implement traveling salesman
// problem using naive approach.
#include <bits/stdc++.h>
using namespace std;
#define V 5

// implementation of traveling Salesman Problem
float travllingSalesmanProblem(float graph[][V], int s)
{
   // store all vertex apart from source vertex
   vector<int> vertex;
   int path[V],temp[V];
   for (int i = 0; i < V; i++)
       if (i != s)
           vertex.push_back(i);

   // store minimum weight Hamiltonian Cycle.
   float min_path = INT_MAX;
   do {

       // store current Path weight(cost)
       float current_pathweight = 0;
      
       // compute current path weight
       int k = s;
       cout<<k+1<<"->";
       for (int i = 0; i < vertex.size(); i++) {
          
           current_pathweight += graph[k][vertex[i]];
           k = vertex[i];
           temp[i]=k;
       }
       current_pathweight += graph[k][s];
       for(int i = 0; i < V-1; i++)
       {
           if(i==V-2) cout<<temp[i]+1<<endl;
           else cout<<temp[i]+1<<"->";
           //i=i+2;
       }
       // update minimum
       min_path = min(min_path, current_pathweight);
       if(min_path==current_pathweight)
       {
           for(int i = 0; i < V-1; i++)
           {
               path[i]=temp[i];
           }
       }
      
   } while (next_permutation(vertex.begin(), vertex.end()));
   cout<<s+1<<"->";
   for(int i = 0; i < V-1; i++)
   {
       if(i==V-2) cout<<path[i]+1<<"->"<<s+1<<endl;
       else cout<<path[i]+1<<"->";
       //i=i+2;
   }
   return min_path;
}

// driver program to test above function
int main()
{
   // matrix representation of graph

//0-Reno, 1- San Francisco, 2- Salt Lake City, 3- Seattle, 4- Las Vegas
   float graph[][V] = { { 0, 185.33, 426.99, 571.99, 344.60 },
                   { 185.33, 0, 599.30, 678.73, 416.83 },
                   { 426.99, 599.30, 0, 699.85, 362.57 },
                   { 571.99, 678.73, 699.85, 0, 871.36 },
                   {344.60, 416.83, 362.57, 871.36, 0 }
                   };
   int s = 0;
   cout << travllingSalesmanProblem(graph, s) << endl;
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ data structure problem. (Thanks) Traveling Salesman Problem Exercise Consider 5 cities of interest, namely a)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The Traveling Salesman problem (TSP) is famous. Given a list of cities and the distances in betwe...

    The Traveling Salesman problem (TSP) is famous. Given a list of cities and the distances in between them, the task is to find the shortest possible tour that starts at a city, visits each city exactly once and returns to a starting city. A particular tour can be described as list of all cities [c1,c2, c3, ,cn] ordered by the position in which they are visited with the assumption that you return from the last city to the start. This...

  • 1. In the following graph, suppose that the vertices A, B, C, D, E, and F represent towns, and th...

    question 1 and 2 please, thank you. 1. In the following graph, suppose that the vertices A, B, C, D, E, and F represent towns, and the edges between those vertices represent roads. And suppose that you want to start traveling from town A, pass through each town exactly once, and then end at town F. List all the different paths that you could take Hin: For instance, one of the paths is A, B, C, E, D, F. (These...

  • Hey guys, I am struggling with this problem. I dont know where to begin with. I...

    Hey guys, I am struggling with this problem. I dont know where to begin with. I have to make a model in excel for this problem and solve from A to D. Can someone help me solving this problem by showing each step please ? I would really apprecite it. PS: please make to put a clear picture and a good explanation about the answer you come up with. Problem 4 (6 points Baseball umpiring crews are currently in four...

  • 10. Consider the Traveling Salesperson problem (a) Write the brute-force algorithm for this proble that considers...

    10. Consider the Traveling Salesperson problem (a) Write the brute-force algorithm for this proble that considers (b) Implement the algorithm and use it to solve instances of size 6, 7, (c) Compare the performance of this algorithm to that of Algorithm all possible tours 8, 9, 10, 15, and 20 6.3 using the instances developed in (b) Algorithm 6.3 The Best-First Search with Branch-and-Bound Pruning Algorithm for the Traveling Salesperson problem Problem: Determine an optimal tour in a weighted, directed...

  • How can I get started in this program for this DelivC?

    SpecificationStart with your Java program "prog340" which implements Deliverables A and B.This assignment is based on the definition of the Traveling Salesperson Problem (the TSP): Given a set of cities, you want to find the shortest route that visits every city and ends up back at the original starting city. For the purposes of this problem, every city will be directly reachable from every other city (think flying from city to city).Your goal is to use a non-genetic local search...

  • C++ i want Lab#3 done can u make clear code so I could understand it. Lab#2The...

    C++ i want Lab#3 done can u make clear code so I could understand it. Lab#2The objective of this lab is compare the populations of various cities that lie in between Toledo and Dayton on I-75. Write a program that produces a bar illustrating the populations. The program should read the name of the city and its population from a file. Have the program continue this process until the end of file is reached. For each city, your program should...

  • Please help me with this answer. Performance Comparison for Dijkstra Algorithm and Bellman-Ford Algorithm Problem Description...

    Please help me with this answer. Performance Comparison for Dijkstra Algorithm and Bellman-Ford Algorithm Problem Description The shortest path problem is one of most important problems in graph theory and computer science in general. Shortest path problem is one of typical optimization problems. Given a graph G = (V,E), the goal is to nd a minimum cost path from s → t, s,t ∈ V . This variant is called one-to-one shortest path problem. Other variants are one-to-all (compute shortest...

  • ================Data Structures C++=============== – Implement the Eight Queens Problem This is a...

    ================Data Structures C++=============== – Implement the Eight Queens Problem This is an object oriented program. This is #1 on page 187 of your book with ONE difference, I’m requiring you add a client program to test your code (a main). If you have the old book the question says “Complete the Queen and Board class for the Eight Queens problem.” On page 179 of your book is a function placeQueens that solves the Eight Queens problem. On page 180 of...

  • Please solve these using matlab Problem 1 Given the transfer functions e S +5 (a) C(s)...

    Please solve these using matlab Problem 1 Given the transfer functions e S +5 (a) C(s) 20 S + 20 (b) Use the step function to determine the time constant and rise time for each system. Note: estimate these values from the plot and do not use the stepinfo function. Problem 2 Given the transfer function 100 G(S) = 22 +45 + 25 a. Use the plot resulting from the step function in MATLAB to determine the percent overshoot, settling...

  • C programming A linked list is a linear data structure that allows us to add and remove items fro...

    c programming A linked list is a linear data structure that allows us to add and remove items from the list very quickly, by simply changing a few pointers. There are many different variations of linked lists. We have studied the doubly-linked, circular, with a dummy-header-node version of a linked list. In the class notes we studied several functions to manipulate a Linked List. For this assignment you must write the code for the following additional linked list functions: addFirst,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT