Question

In: Computer Science

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 pair of cities is given below: A B C D E F A - 21 13 17 25 9 B 21 - 14 19 21 27 C 13 14 - 8 18 20 D 17 19 8 - 29 17 E 25 21 18 29 - 15 F 9 27 20 17 16 - Implement the following Hill Climbing local search algorithm to solve the above travelling salesman problem.

Use Java programming language.

Algorithm:

1. Start with an initial current state (generated randomly), such as: C, B, E, F, A, D The cost of this state is: = C to B + B to E + E to F + F to A + A to D + D to C = 14 + 21 + 15 + 9 + 17 + 8 = 84 LOOP: 2. Generate three different possible successor moves from the current state as defined below:

i) Generate three random pairs of numbers: (c1, c2), (c3, c4), (c5, c6) Such that: Ci ( 1 <= i <= 6), has a random value between 1 and 6 (inclusive).

Make sure that the two random values in each pair are different. Also, make sure that no two pairs have the same random values. i.e. all pairs are unique.

ii) Then, the possible three successor moves from the current state are: Move 1: swap the city in position c1 with the city in position c2.

For example: given the current state: C, B, E, F, A, D if c1 = 2 and c2= 5, then swap city B (in position 2) with city A (in position 5) in the current sate to get the following move: C, A, E, F, B, D Move

2: swap the city in position c3 with the city in position c4 Move 3: swap the city in position c5 with the city in position c6

3. Calculate the cost of each of these three successor moves.

4. Choose the move with the best move (i.e. least cost) from these three successor moves.

5. If this best move state has a higher cost than the cost of current state, then return this current state as a solution with its cost and exit the algorithm. Otherwise, replace the current state by this best move state. Output: Initial state Best state found by the algorithm The cost of the best state Number of iterations the algorithm execute to find this best state

Solutions

Expert Solution


class GFG 
{ 

        static int tsp(int[][] graph, boolean[] v, 
                                int currPos, int n, 
                                int count, int cost, int ans) 
        { 

        
                if (count == n && graph[currPos][0] > 0) 
                { 
                        ans = Math.min(ans, cost + graph[currPos][0]); 
                        return ans; 
                } 


                for (int i = 0; i < n; i++) 
                { 
                        if (v[i] == false && graph[currPos][i] > 0) 
                        { 

                                v[i] = true; 
                                ans = tsp(graph, v, i, n, count + 1, 
                                                cost + graph[currPos][i], ans); 

                                v[i] = false; 
                        } 
                } 
                return ans; 
        } 

        // Driver code 
        public static void main(String[] args) 
        { 

                int n = 6; 

                int[][] graph = {{0, 21, 13, 17,25,9}, 
                                                {21,0,14,19,21,27}, 
                                                {13,14,0,8,18,20}, 
                                                {17,19,8,0,29,17},
                        {25,21,18,29,0,15},
                        {9,27,20,17,16,0}
}; 


                boolean[] v = new boolean[n]; 

                v[0] = true; 
                int ans = Integer.MAX_VALUE; 

         
                ans = tsp(graph, v, 0, n, 1, 0, ans); 

        
                System.out.println(ans); 
        } 
} 

Related Solutions

The Traveling Salesman Problem was named after the job of a traveling salesman but finds many...
The Traveling Salesman Problem was named after the job of a traveling salesman but finds many applications elsewhere. Research the traveling salesman and provide two examples of real-world applications. Make sure to explain how these applications are applied.
state the input and output of the traveling salesman problem. give the best lower bound on...
state the input and output of the traveling salesman problem. give the best lower bound on length of optimal tour and prove
Q4) The temperature degrees of certain set of cities are normally distributed. The cities are classified...
Q4) The temperature degrees of certain set of cities are normally distributed. The cities are classified into three categories. The lowest 12.92% are in category I, The highest 10.38% are in Category III, and the other cities are in category II, with degrees between α and β. i. Find the z- scores of α and β. ii. Given the mean temperature degrees is 6.84 and the standard deviation is 0.25. Find the values of α and β
solve travelling salesman problem using different simulated annealing cooling schedule
solve travelling salesman problem using different simulated annealing cooling schedule
Lab 1. Java and the Command Prompt a) Visit the Java SE JDK website and see...
Lab 1. Java and the Command Prompt a) Visit the Java SE JDK website and see which version of Java you need to download. b) If granted permissions, try to see how to modify environment variables (PATH and CLASSPATH). c) Open the command prompt/shell. Use cd and dir (ls on UNIX-like systems) commands to navigate your file system and see what files do you have in different locations e.g. on your flash drive. Pay attention to the file extensions. d)...
A) Please set up The Simultaneous Equations for both plans and solve for the breakeven point...
A) Please set up The Simultaneous Equations for both plans and solve for the breakeven point where Plan A and Plan B will have the same cost outlay. ( B)Which plan will be cheaper in the short run and which plan will be cheaper in the long run? Please show Graph.
solve following travelling salesman problem using branch and bound and show matrix and graph at each...
solve following travelling salesman problem using branch and bound and show matrix and graph at each step 5 locations : a, b, c, d, e from a to remaining places = [infinity, 4, 7, 3, 4] from b to remaining places = [4, infinity, 6, 3, 4] from c to remaining = [7, 6, infinity, 7, 5] from d to remaining = [3, 3, 7, infinty, 7] from e to remaining = [4, 4, 5, 7, infinity] PLEASE show ALL...
Please solve this problem with Python Language. P#2. A Pythagorean triplet is a set of three...
Please solve this problem with Python Language. P#2. A Pythagorean triplet is a set of three natural numbers, a < b < c, for which a2 + b2 = c2 . For example, 32 + 42 = 52. And a + b + c = 3 + 4 + 5 = 12. There exists exactly two Pythagorean triplet for which a + b + c = 300. Find a, b, c. Hints: Level-0 Algorithm: 1. Find the possible ranges of...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) Requirements Choose one problem with an algorithm and implement it. You should show and explain the result whatever you got. I recommend using N-Queen problem (at least N=8 or more) or any simple perfect games. For example, - N-Queen problem with hill climbing - N-Queen problem with simulated annealing - N-Queen problem with genetic algorithm - Tic-Tac-Toe with Minimax
please, solve this problem.: implementing with java language create a bank account management system according to...
please, solve this problem.: implementing with java language create a bank account management system according to the following specifications: BankAccount Abstract Class contains the following constructors and methods: BankAccount(name, balance): a constructor that creates a new account with a name and starting balance. getBalance(): a method that returns the balance of a specific account. abstract deposit(amount): abstract method to be implemented in both Checking and SavingAccount classes. abstract withdraw(amount): abstract method to be implemented in both Checking and SavingAccount classes....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT