MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / main

Method main

Dynamic Programming/TSP.java:3–39  ·  view source on GitHub ↗
(String[] args)

Source from the content-addressed store, hash-verified

1import java.util.Scanner;
2public class TSP{
3public static void main(String[] args)
4{
5int c[][]=new int[10][10], tour[]=new int[10];
6Scanner in = new Scanner(System.in);
7int i, j,cost;
8System.out.println("** TSP DYNAMIC PROGRAMMING ***");
9System.out.println("Enter the number of cities: ");
10int n = in.nextInt();
11if(n==1)
12{
13System.out.println("Path is not possible");
14System.exit(0);
15}
16System.out.println("Enter the cost matrix");
17for(i=1;i<=n;i++)
18for(j=1;j<=n;j++)
19c[i][j] = in.nextInt();
20System.out.println("The entered cost matrix is");
21for(i=1;i<=n;i++)
22{
23for(j=1;j<=n;j++)
24{
25System.out.print(c[i][j]+"\t");
26}
27System.out.println();
28}
29for(i=1;i<=n;i++)
30tour[i]=i;
31cost = tspdp(c, tour, 1, n);
32System.out.println("The accurate path is");
33for(i=1;i<=n;i++)
34System.out.print(tour[i]+"->");
35
36System.out.println("1");
37System.out.println("The accurate mincost is "+cost);
38System.out.println("*** ***** *****");
39}
40static int tspdp(int c[][], int tour[], int start, int n)
41{
42int mintour[]=new int[10], temp[]=new int[10], mincost=999, ccost, i, j, k;

Callers

nothing calls this directly

Calls 2

tspdpMethod · 0.95
printMethod · 0.45

Tested by

no test coverage detected