-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtsp.c
158 lines (149 loc) · 3.02 KB
/
tsp.c
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
struct SubSet
{
int data;
struct SubSet *next;
}**S;
void tableA(int **A,int n)
{
int i,j;
for(i=0;i<pow(2,n-1);i++)
{
for(j=0;j<n;j++)
{
printf("%d ",A[i][j]);
}
printf("\n");
}
}
void tableS(int n)
{
int i;
struct SubSet *temp;
for(i=0;i<pow(2,n-1);i++)
{
temp = S[i];
while(temp!= NULL)
{
printf("k-%d ",temp->data);
temp = temp->next;
}
printf("f\n");
}
}
struct SubSet *Create_element_SubSet(int i) //function to Create an element in a subset returns {i}
{
struct SubSet *temp;
temp = (struct SubSet*)malloc(sizeof(struct SubSet));
temp->data = i;
temp->next = NULL;
return temp;
}
int isHit(int i,int j) //function to Check if j'th bit is one in i returns 1/0
{
while(j > 0)
{
i = i/2;
--j;
}
if(i%2 == 1)
return 1;
return 0;
}
void Create_SubSet(int n) //function to create all 2^n subsets
{
S = (struct SubSet**)malloc(sizeof(struct SubSet*)*pow(2,n-1));
struct SubSet *temp,*Head = NULL,*Tail = NULL;
int i,j,k,flag = 0;
for(i=0;i<pow(2,n-1);i++)
{
for(j=0;j<n;j++)
{
if(isHit(i,j))
{
temp = Create_element_SubSet(j+1);
if(Head == NULL)
Head = Tail = temp;
else
{
Tail->next = temp;
Tail = temp;
}
}
}
S[i] = Head;
Head = Tail = NULL;
}
//uncomment the below line for printing contents of table S
//tableS(n);
}
void Solution(int **Sol,int i,int j) //function to print the Travelling Salesman Optimum path
{
if(i==0)
{
printf("0\n");
return;
}
printf("%d->",Sol[i][j]);
Solution(Sol,i-(int)pow(2,Sol[i][j]-1),Sol[i][j]);
}
void DP_tsp(int **D,int n) //function which uses dynamic programming technique to solve Travelling Salesman problem
{
int i,j,**A,**Sol,minval,x;
struct SubSet *temp;
A = (int**)malloc(sizeof(int*)*pow(2,n-1));
for(i=0;i<pow(2,n);i++)
A[i] = (int*)malloc(sizeof(int)*n);
Sol = (int**)malloc(sizeof(int*)*pow(2,n-1));
for(i=0;i<pow(2,n);i++)
Sol[i] = (int*)malloc(sizeof(int)*n);
Create_SubSet(n);
for(i=0;i<pow(2,n-1);i++)
{
for(j=0;j<n;j++)
{
if(i==0)
A[i][j] = D[i][j];
else
{
temp = S[i];
minval = INT_MAX;
Sol[i][j] = -1;
while(temp != NULL)
{
x = D[temp->data][j] + A[i-(int)pow(2,temp->data-1)][temp->data];
if(x < minval)
{
minval = x;
Sol[i][j] = temp->data;
}
temp = temp->next;
}
A[i][j] = minval;
}
}
}
//uncomment the below line for printing table entries in A
//tableA(A,n);
printf("the shortest possible route that visits every city exactly once and returns to the starting point is:-\n");
printf("0->");
Solution(Sol,(int)pow(2,n-1)-1,0);
}
int main()
{
int n,**D,i,j;
printf("Enter the number of cities\n");
scanf("%d",&n);
printf("Enter the distance between each cities\n");
D = (int**)malloc(sizeof(int*)*n);
for(i=0;i<n;i++)
D[i] = (int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&D[i][j]);
DP_tsp(D,n);
return 0;
}