Java Program to Solve Travelling Salesman Problem Using Incremental Insertion Method

Incremental is actually used in software development where the model is designed, implemented, and tested incrementally (a little more is added each time) until the product is finished. It involves both development and maintenance. The product is defined as finished when it satisfies all of its requirements.
Let us briefly discuss the traveling salesman problem. Here we are supposed to find the simplest or shortest distance between all the cities the salesman has to travel i.e., finding the shortest and optimal route between the nodes of the graph. It is also known as TSP and is the most known computer science optimized problem in this modern world.
Algorithm:
- Start with a sub-graph consisting of node ‘i’ only.
- Find node r such that cir is minimal and form sub-tour i-r-i.
- (Selection step) Given a sub-tour, find node r not in the sub-tour closest to any node j in the sub-tour; i.e. with minimal crj
- (Insertion step) Find the arc (i, j) in the sub-tour which minimizes cir + crj – cij Insert ‘r’ between ‘i’ and ‘j’.
- If all the nodes are added to the tour, stop. Else go to step 3
Implementation:
Java
// Java Program to Solve Travelling Salesman Problem// Using Incremental Insertion MethodÂ
// Importing input output classesimport java.io.*;// Importing Scanner class to take input from the userimport java.util.Scanner;Â
// Main classpublic class GFG {    // Method 1    // Travelling Salesman Incremental Insertion Method    static int tspdp(int c[][], int tour[], int start,                     int n)    {Â
        int mintour[] = new int[10], temp[] = new int[10],            mincost = 999, ccost, i, j, k;Â
        if (start == n - 1)Â
        {Â
            return (c[tour[n - 1]][tour[n]]                    + c[tour[n]][1]);        }Â
        // Logic for implementing the minimal costÂ
        for (i = start + 1; i <= n; i++)Â
        {Â
            for (j = 1; j <= n; j++)Â
                temp[j] = tour[j];Â
            temp[start + 1] = tour[i];Â
            temp[i] = tour[start + 1];Â
            if ((c[tour[start]][tour[i]]                 + (ccost = tspdp(c, temp, start + 1, n)))                < mincost)Â
            {Â
                mincost = c[tour[start]][tour[i]] + ccost;Â
                for (k = 1; k <= n; k++)Â
                    mintour[k] = temp[k];            }        }Â
        // Now, iterating over the path (mintour) to        // compute its cost        for (i = 1; i <= n; i++)Â
            tour[i] = mintour[i];Â
        // Returning the cost of min path        return mincost;    }Â
    // Method 2    // Main driver method    public static void main(String[] args)Â
    {        // Creating an object of Scanner class to take user        // input        // 1. Number of cities        // 2. Cost matrix        Scanner in = new Scanner(System.in);Â
        // Creating matrices in the main body        int c[][] = new int[10][10], tour[] = new int[10];Â
        // Declaring variables        int i, j, cost;Â
        // Step 1: To read number of citiesÂ
        // Display message for asking user to        // enter number of cities        System.out.print("Enter No. of Cities: ");Â
        // Reading and storing using nextInt() of Scanner        int n = in.nextInt();Â
        // Base case        // If the city is 1 then        // path is not possible        // Cost doesnot play any role        if (n == 1) {            // Display on the console            System.out.println("Path is not possible!");Â
            // terminate            System.exit(0);        }Â
        // Case 2        // Many citiesÂ
        // Again, reading the cost of the matrixÂ
        // Display message        System.out.println("Enter the Cost Matrix:");Â
        // Travelling across cities using nested loops        for (i = 1; i <= n; i++)Â
            for (j = 1; j <= n; j++)Â
                c[i][j] = in.nextInt();Â
        for (i = 1; i <= n; i++)Â
            tour[i] = i;Â
        // Calling the above Method 1 to        cost = tspdp(c, tour, 1, n);Â
        // Now, coming to logic to print the optimal tourÂ
        // Display message for better readability        System.out.print("The Optimal Tour is: ");Â
                 for (i = 1; i <= n; i++)Â
            // Printing across which cities should Salesman            // travel            System.out.print(tour[i] + "->");Â
        // Starting off with the city 1->        System.out.println("1");Â
        // Print and display the (minimum)cost of the path        // traversed        System.out.println("Minimum Cost: " + cost);    }} |
Output:




