Question

In: Computer Science

IN JAVA A salesman wants to go to five different cities and sell some products. The...

IN JAVA

A salesman wants to go to five different cities and sell some products. The locations of the cities are listed in the following table.

City #

X_location

Y_location

City 1

1

1

City 2

1

3

City 3

4

1

City 4

5

3

City 5

3

5

The distance between two cities is defined as the Euclidean distance. That is:

Distance = sqrt( (x1 – x2)^2 + (y1 – y2)^2 )

For example, the distance between cities 1 and 2 will be:

Distance = sqrt( (1 – 1)^2 + (1 – 3)^2 ) = sqrt( 4 ) = 2

The purpose: The salesman starts his journey from city 1. He then has 4 remaining options for the next city (city 2, city 3, city 4, city 5). If he chooses city 3 as the next destination, then he will have 3 remaining options (city 2, city 4, city 5). He wants to travel all the cities and then come back to the start location (City 1). Not all the paths will be a good choice. We want to help the salesman by finding the shortest path (best order of cities to visit (visit all of them once) starting from city 1).

Steps:

Step 1 [7 points]: Create a class City with x and y as the class variables. The constructor with argument will get x and y and will initialize the city. Add a member function getDistanceFrom() to the class that gets a city as the input and finds the distance between the two cities.

city1.getDistanceFrom(city2) will be distance between city 1 and 2.

Step 2 [6 points]: Create 5 city objects and initialize their location (x and y) using the above table. Then put all of the five cities in a vector.

Step 3 [7 points]: Create a two dimensional array or vector DistanceVec of size 5 * 5 and initialize it such that DistanceVec[i,j] is the distance between city_i and city_j. Print the distanceVec and show the distance among all cities.

Step 4 [15 points]: If the salesman starts from the city 1, search all the possible paths and find the optimal path (order of cities to visit) that leads to the minimum total travel distance. Display the found optimal path (order of cities to travel) in your sample run.

Solutions

Expert Solution

import java.lang.Math;
import java.util.Stack;
import java.util.Vector;

public class City {
    public int x;
    public int y;

    public City(int x,int y){
        this.x = x;
        this.y = y;
    }

    double getDistanceFrom(City c){
        int x2 = c.x;
        int y2 = c.y;
        double d = Math.sqrt((x-x2)*(x-x2) + (y-y2)*(y-y2));
        return d;
    }

    public void findOptimalSolution(double dvec[][])
    {
        Stack<Integer> stack = new Stack<Integer>();
        int cities = dvec.length;
        System.out.println("Number of cities = " + cities);
        int[] visited = new int[cities + 1];
        visited[1] = 1;
        stack.push(1);
        int element;
        int distance = 0;
        int i;
        boolean foundPath = false;
        System.out.print(1 + "\t");

        while (!stack.isEmpty()){
            element = stack.peek();
            i = 1;
            double min = -1;
            while (i <= cities){
                if (dvec[element-1][i-1] > 1 && visited[i] == 0){
                    if (min == -1 || min > dvec[element-1][i-1]){
                        min = dvec[element-1][i-1];
                        distance = i;
                        foundPath = true;
                    }
                }
                i++;
            }
            if (foundPath){
                visited[distance] = 1;
                stack.push(distance);
                System.out.print(distance + "\t");
                foundPath = false;
                continue;
            }
            stack.pop();
        }
    }

    public static void main(String[] args){

        City city1 = new City(1,1);
        City city2 = new City(1,3);
        City city3 = new City(4,1);
        City city4 = new City(5,3);
        City city5 = new City(3,5);

        Vector<City> cityVector = new Vector<City>();

        cityVector.add(city1);
        cityVector.add(city2);
        cityVector.add(city3);
        cityVector.add(city4);
        cityVector.add(city5);

        double[][] DistanceVec = new double[5][5];

        for(int i=0;i<5;i++){
            for(int j=i;j<5;j++){
                City c1 = cityVector.get(i);
                City c2 = cityVector.get(j);
                DistanceVec[i][j] = c1.getDistanceFrom(c2);
                DistanceVec[j][i] = DistanceVec[i][j];
            }
        }

        System.out.println("Printing the DistanceVec \n");
        for(int i=0;i<5;i++){
            for(int j=0;j<5;j++){
                System.out.print("" + DistanceVec[i][j] + "\t");
            }
            System.out.println();
        }

        city1.findOptimalSolution(DistanceVec);

    }

}

Output:

Please appreciate the solution if you find it helpful.

If you have any doubts in the solution, feel free to ask me in the comment section.


Related Solutions

Python 1. A salesman can sell five different items. Write a program that lets the salesman...
Python 1. A salesman can sell five different items. Write a program that lets the salesman enter the quantity of each item sold, calculates the total sales, and prints as below. Use a for loop to ask the salesman how many of each product he sold. tem 1 $2.50 Item 2 $1.98 Item 3 $5.75 Item 4 $3.45 Item 5 $4.00 2. Rewrite program #1 using a for loop to run for 3 salesmen and print the total sales for...
Please solve in Java, The traveling salesman, wishing to visit a set of cities in the...
Please solve in Java, The traveling salesman, wishing to visit a set of cities in the shortest time possible. A tour is a path that starts in one city, visits all the other cities, and then returns to the starting point. The relevant pieces of information, then, are the cities and the distances between them. The given set of cities in this problem are: A, B, C, D, E , and F The cost matrix (distances in miles) between any...
A company has three salespeople (1 to 3) who sell five different products (1 to 5)....
A company has three salespeople (1 to 3) who sell five different products (1 to 5). Once a day, each saleperson passes in a slip for each type of product sold. Each slip contains the following: a) The salesperson number b) The product number c) The total dollar value of that product sold that day Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that the information from all of the slips for last month...
A firm is a ______ when it can sell as much as it wants at some...
A firm is a ______ when it can sell as much as it wants at some given price P, but nothing at any higher price. monopoly oligopoly price taker price setter
The heating bills for a selected sample of houses in five different cities using various forms...
The heating bills for a selected sample of houses in five different cities using various forms of heating are given below (values are in dollars). City Gas Heated Homes Central Electric Heat Pump Ankara 83 90 81 Konya 80 88 83 Çorum 82 87 80 Yozgat 83 82 82 Kırşehir 82 83 79 Is there a significant difference among the average bills of the homes according to the different heating methods? Test at 10% significance level by using one-way ANOVA...
Suppose the unweighted graph graph G = (V, E), represents connections between cities in a country. Salesman wants to get from city A to city P using the unweighted graph G = (V, E) of cities.
Algorithm and Data StructuresSuppose the unweighted graph graph G = (V, E), represents connections between cities in a country. Salesman wants to get from city A to city P using the unweighted graph G = (V, E) of cities.a) Explain how the salesman use BFS algorithm to get from city A to city P passing smallest number of cities. (all steps required)b) Now the salesman likes to visit city R on his way to city P. Describe an efficient algorithm...
in java Write a Java Program that displays a menu with five different options: 1. Lab...
in java Write a Java Program that displays a menu with five different options: 1. Lab Test Average Calculator 2. Dice Roll 3. Circle Area Calculator 4. Compute Distance 5. Quit The program will display a menu with each of the options above, and then ask the user to enter their choice. There is also a fifth option to quit, in which case, the program will simply display a goodbye message. Based on the user’s choice, one of the options...
4. Provide some reasons why Boing wants to go global.
4. Provide some reasons why Boing wants to go global.
Why might a company sell its products for different prices in different markets even if the...
Why might a company sell its products for different prices in different markets even if the income levels of its target consumers were the same in all cases?
Select five operational manuals for five different products that used for the same purpose from five...
Select five operational manuals for five different products that used for the same purpose from five different suppliers. The suppliers should be from different continents. Review these manual and conduct a comparison between them. Based on your analysis of these manuals, prepare a report about your findings.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT