Question: help fix lowest cost keeps retruning zero and the tour prints nothing Purpose: Traveling Salesman - find the lowest cost tour when traveling from US
help fix lowest cost keeps retruning zero and the tour prints nothing
Purpose: Traveling Salesman find the lowest cost
tour when traveling from US to other countries
and then back to US
#include
#include
#include
#include "GraphMatrix.h
#include
using namespace std;
const int SIZE ;
const string COUNTRYCODESSIZEAUBRCACNGLITNARUUSUS;
Use the following structure to store each tour & its cost
You will create an array of Tour variables in your program
struct Tour
string tourSIZE;
int cost;
;
Function Prototypes
int searchCountryCodestring;
GraphMatrix readFileMakeMatrix;
void printStringArraystring arr, int size;
void lexicographicstring countries, int size, Tour tourOptions, GraphMatrix matrix, int& permutationIndex;
void saveTourTour tourOptions, string permutation, GraphMatrix matrix, int& permutationIndex;
void findLowestTour tourOptions;
int main
Tour tourOptions new Tour;
read in the flight information from the file and then create the weight matrix
GraphMatrix matrix readFileMakeMatrix;
string countries new stringSIZE ;
cout
COUNTRIES
;
for int x ; x SIZE ; x
countriesx COUNTRYCODESx;
cout countriesx endl;
generate all possible tours starting & ending with US using lexicographic permute algorithm
you will need to call your lexicographic function, sending the modified countries array with the country codes
cout
SOLUTION
;
find the lowest cost tour and print it out including the cost
cout "Generating permutations and calculating costs...
;
int permutationIndex ;
lexicographiccountries SIZE tourOptions, matrix, permutationIndex; Updated function call
findLowesttourOptions;
cout
Happy Traveling!
;
delete countries;
delete tourOptions;
delete matrix;
return ;
Function: searchCountryCode
Parameters: a string with the country code like BR
Returns: an integer representing this country's index in the GraphMatrix.
For example, if the parameter is BR it should return index If the parameter is CA it should return index
It is returning the index value in the COUNTRYCODES array.
Implement this search function with the binary search algorithm!
int searchCountryCodestring country
for int i ; i SIZE; i
if COUNTRYCODESi country
return i;
return ; Not found
Title: readFileMakeMatrix
Purpose: this function will read the data in flights.txt and create an
adjacency matrix with the country codes. The matrix values will be the
cost of the flights.
GraphMatrix readFileMakeMatrix
ifstream inFile;
GraphMatrix matrix new GraphMatrixSIZE ;
cout
Created the matrix.";
string country country;
int price;
inFile.openC:UsersrockcDownloadsflightstxt;
cout
Reading from flights.txt
;
ifinFile
whileinFile country
inFile country;
inFile price;
add price to graph matrix
matrixaddEdgesearchCountryCodecountry searchCountryCodecountry price;
cout
Added edge from country to country with cost of $ price;
else
cout
Sorry, I am unable to open the file.
;
inFile.close;
cout
FLIGHT PRICE GRAPH MATRIX
;
matrixprintGraph;
cout endl;
return matrix;
Title: printStringArray
Purpose: this function will print the array of strings with a space
between each string
void printStringArraystring arr, int size
forint x ; x size; x
cout arrx;
cout endl;
Function: lexicographic
Parameters:
countries: array of country codes
size: size of the countries array
tourOptions: array to store all tours and their costs
matrix: graph matrix containing flight costs
permutationIndex: index to store the tour in tourOptions
Purpose: Generate all possible tours starting and ending with US using the lexicographic permutation algorithm.
calculate the cost of each tour and stores it in tourOptions.
void generatePermutationsstring countries, int start, int end, Tour tourOptions, GraphMatrix matrix, int& permutationIndex
if
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
