Java Program to Implement Traveling Salesman Problem using Nearest Neighbour Algorithm

This Java Program is to Implement Traveling Salesman Problem using Nearest neighbour Algorithm.The travelling salesman problem (TSP) or travelling salesperson problem asks the following question: Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city exactly once and returns to the origin city? It is an NP-hard problem in combinatorial optimization, important in operations research and theoretical computer science.

Here is the source code of the Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `import java.util.InputMismatchException;`
2. `import java.util.Scanner;`
3. `import java.util.Stack;`
4. ` `
5. `public class TSPNearestNeighbour`
6. `{`
7. `    private int numberOfNodes;`
8. `    private Stack<Integer> stack;`
9. ` `
10. `    public TSPNearestNeighbour()`
11. `    {`
12. `        stack = new Stack<Integer>();`
13. `    }`
14. ` `
15. `    public void tsp(int adjacencyMatrix[][])`
16. `    {`
17. `        numberOfNodes = adjacencyMatrix[1].length - 1;`
18. `        int[] visited = new int[numberOfNodes + 1];`
19. `        visited[1] = 1;`
20. `        stack.push(1);`
21. `        int element, dst = 0, i;`
22. `        int min = Integer.MAX_VALUE;`
23. `        boolean minFlag = false;`
24. `        System.out.print(1 + "\t");`
25. ` `
26. `        while (!stack.isEmpty())`
27. `        {`
28. `            element = stack.peek();`
29. `            i = 1;`
30. `            min = Integer.MAX_VALUE;`
31. `            while (i <= numberOfNodes)`
32. `            {`
33. `                if (adjacencyMatrix[element][i] > 1 && visited[i] == 0)`
34. `                {`
35. `                    if (min > adjacencyMatrix[element][i])`
36. `                    {`
37. `                        min = adjacencyMatrix[element][i];`
38. `                        dst = i;`
39. `                        minFlag = true;`
40. `                    }`
41. `                }`
42. `                i++;`
43. `            }`
44. `            if (minFlag)`
45. `            {`
46. `                visited[dst] = 1;`
47. `                stack.push(dst);`
48. `                System.out.print(dst + "\t");`
49. `                minFlag = false;`
50. `                continue;`
51. `            }`
52. `            stack.pop();`
53. `        }`
54. `    }`
55. ` `
56. `    public static void main(String... arg)`
57. `    {`
58. `        int number_of_nodes;`
59. `        Scanner scanner = null;`
60. `        try`
61. `        {`
62. `            System.out.println("Enter the number of nodes in the graph");`
63. `            scanner = new Scanner(System.in);`
64. `            number_of_nodes = scanner.nextInt();`
65. `            int adjacency_matrix[][] = new int[number_of_nodes + 1][number_of_nodes + 1];`
66. `            System.out.println("Enter the adjacency matrix");`
67. `            for (int i = 1; i <= number_of_nodes; i++)`
68. `            {`
69. `                for (int j = 1; j <= number_of_nodes; j++)`
70. `                {`
71. `                    adjacency_matrix[i][j] = scanner.nextInt();`
72. `                }`
73. `            }`
74. `            for (int i = 1; i <= number_of_nodes; i++)`
75. `            {`
76. `                for (int j = 1; j <= number_of_nodes; j++)`
77. `                {`
78. `                    if (adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0)`
79. `                    {`
80. `                        adjacency_matrix[j][i] = 1;`
81. `                    }`
82. `                }`
83. `            }`
84. `            System.out.println("the citys are visited as follows");`
85. `            TSPNearestNeighbour tspNearestNeighbour = new TSPNearestNeighbour();`
86. `            tspNearestNeighbour.tsp(adjacency_matrix);`
87. `        } catch (InputMismatchException inputMismatch)`
88. `         {`
89. `             System.out.println("Wrong Input format");`
90. `         }`
91. `        scanner.close();`
92. `    }`
93. `}`

```\$javac  TSPNearestNeighbour.java
\$java TSPNearestNeighbour

Enter the number of nodes in the graph
9
000 374 200 223 108 178 252 285 240 356
374 000 255 166 433 199 135 095 136 017
200 255 000 128 277 128 180 160 131 247
223 166 128 000 430 047 052 084 040 155
108 433 277 430 000 453 478 344 389 423
178 199 128 047 453 000 091 110 064 181
252 135 180 052 478 091 000 114 083 117
285 095 160 084 344 110 114 000 047 078
240 136 131 040 389 064 083 047 000 118
356 017 247 155 423 181 117 078 118 000
the citys are visited as follows
1	5	3	2	9	7	4	6	8```

Sanfoundry Global Education & Learning Series – 1000 Java Programs.