-
Notifications
You must be signed in to change notification settings - Fork 368
/
Travelling_Salesman.c
132 lines (65 loc) · 1.7 KB
/
Travelling_Salesman.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
#include<stdio.h>
//creating a 2D array cities to store the cost of travelling from one city to another
//cities_visited is a 1D array that store the cities already visited
int cities[10][10],cities_visited[10],n,cost=0;
int main()
{
int i,j;
printf("Enter the number of Cities: ");//asking user to input the number of cities
scanf("%d",&n);
printf("\nEnter the Cost Matrix\n");
for(i=0;i < n;i++)
{
printf("\nEnter Elements of Row: %d\n",i+1);
for( j=0;j < n;j++)
scanf(" %d",&cities[i][j]);
cities_visited[i]=0;
}
printf("\n\nThe cost list is:");
for( i=0;i < n;i++)
{
printf("\n");
for(j=0;j < n;j++)
printf("\t%d",cities[i][j]);
}
//printing the possibe path and the minimum cost required
printf("\n\nThe Possible Path is:\n");
min_cost(0); //passing 0 because starting vertex
printf("\n\nMinimum cost is %d\n ",cost);
return 0;
}
//this function returns the minimum cost required to visit all the cities
void min_cost(int city)
{
int i,ncity;
cities_visited[city]=1;
printf("%d--",city+1);
ncity=least(city);
if(ncity==9999)//checking if there is a way to move from one city to another (here infinity==9999)
{
ncity=0;
printf("%d",ncity+1);
cost+=cities[city][ncity];
return;
}
min_cost(ncity);
}
//calculates the mimimum cost required to travel from one city to another
int least(int c)
{
int i,nc=9999;
int min=9999,kmin;
for(i=0;i < n;i++)
{
if((cities[c][i]!=0)&&(cities_visited[i]==0))
if(cities[c][i]+cities[i][c] < min)
{
min=cities[i][0]+cities[c][i];
kmin=cities[c][i];
nc=i;
}
}
if(min!=9999)
cost+=kmin;
return nc;
}