-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTravellingSalesPerson.java
53 lines (50 loc) · 1.36 KB
/
TravellingSalesPerson.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package Unit_2;
import java.util.*;
public class TravellingSalesPerson {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter no. of cities : ");
int n = sc.nextInt();
if(n == 1)
{
System.out.println("Path is not possible\n");
}
int c[][] = new int[n + 1][n + 1], tour[] = new int[n + 1], i, j, cost;
System.out.println("Enter Cost matrix : ");
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
c[i][j] = sc.nextInt();
for(i = 1; i <= n; i++)
tour[i] = i;
cost = tspdp(c, tour, 1, n);
for(i = 1; i <= n; i++)
System.out.print(tour[i] + " --> ");
System.out.println("1");
System.out.println("Min cost = " + cost);
sc.close();
}
static int tspdp(int c[][], int tour[], int start, int n)
{
int mintour[] = new int[n + 1], temp[] = new int[n + 1], mincost = Integer.MAX_VALUE, ccost, i, j, k;
if(start == n -1)
{
return (c[tour[n - 1]][tour[n]] + c[tour[n]][1]);
}
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];
}
}
for(i = 1; i <= n; i++)
tour[i] = mintour[i];
return mincost;
}
}