Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C++ (or Java) program called hw4_3 .cpp or (hw4_3.java) that reads an input graph data from a user first. Then, it should present

Write a C++ (or Java) program called hw4_3.cpp or (hw4_3.java) that reads an input graph data from a user first. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number of vertices in the input graph is less than or equal to 20.

Input format: This is a sample input from a user.

4

Monterey

LA

SF

SD

12

Monterey LA 2

Monterey SD 7

Monterey SF 5

LA Monterey 2

LA SF 8

LA SD 3

SF Monterey 5

SF LA 8

SF SD 1

SD Monterey 7

SD LA 9

SD SF 1

The first line (= 4 in the example) indicates that there are four vertices in the graph. The following four lines describe the names of four cities such as Monterey, LA, SF, and SD. For the problem, you should assume that the first city (= Monterey in the example) is the starting city of the TSP. In addition, you can assume that the city name is always one word. The next line (= 12 in the example) indicates the number of edges in the graph. The remaining 12 lines are the edge information with the source city, destination city, and cost. Thus, the input data describes a directed graph like below:

Sample Run 1: Assume that the user typed the following lines

4

Monterey

LA

SF

SD

12

Monterey LA 2

Monterey SD 7

Monterey SF 5

LA Monterey 2

LA SF 8

LA SD 3

SF Monterey 5

SF LA 8

SF SD 1

SD Monterey 7

SD LA 9

SD SF 1

This is the correct output. Your program should present the path and total cost in separate lines.

Path:Monterey->LA->SD->SF->Monterey

Cost:11

Sample Run 2: Assume that the user typed the following lines

5

Fresno

LA

SD

SF

NYC

6

Fresno SD 8

SD LA 7

SD NYC 3

LA NYC 100

SF Fresno 20

SF SD 19

This is the correct output.

Path:

Cost:-1

Note that if there is no path for the TSP, your program should present empty path and -1 cost.

Sample Run 3: Assume that the user typed the following lines

5

Fresno

LA

SD

SF

NYC

7

Fresno SD 8

SD LA 7

SD NYC 3

LA NYC 100

SF Fresno 20

SF SD 19

NYC SF 50

This is the correct output of your program.

Path:Fresno->SD->LA->NYC->SF->Fresno

Cost:185

This is the directed graph of the input data:

[Hint]: To solve this problem, you can use all permutations with the cities, except the origin city. For example, there are three cities, LA, SF, and SD in the first sample run, except the origin city Monterey. To make the explanation easy, you can think that LA is number 1, SF is number 2, and SD is number 3. This is all permutations with the three cities

1, 2, 3

1, 3, 2

2, 1, 3,

2, 3, 1

3, 1, 2

3, 2, 1

For each permutation, you can calculate the cost. For example, in the case of the permutation 1, 2, 3, add the origin city at the very beginning and end to the permutation which generates 0, 1, 2, 3, 0. This list corresponds to the path Monterey LA SF SD Monterey. Therefore, the cost of this path is 18. Similarly, the next permutation (= 1, 3, 2) corresponds to Monterey LA SD SF Monterey. The cost is 11. This way, you can check all possible paths for the TSP.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Database Programming

Authors: Chris Fehily

1st Edition

1937842312, 978-1937842314

More Books

Students also viewed these Databases questions

Question

10-9 How have social technologies changed e-commerce?

Answered: 1 week ago