-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjabbamaps.c
272 lines (235 loc) · 8.33 KB
/
jabbamaps.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <limits.h>
typedef struct {
char *city1;
char *city2;
unsigned int distance;
} Route;
FILE *open_file(const char *filename) {
FILE *file = fopen(filename, "r");
if (!file) {
fprintf(stderr, "Failed to open %s\n", filename);
}
return file;
}
unsigned int **initialize_distance_matrix(size_t city_count) {
unsigned int **distance_matrix = malloc(city_count * sizeof(unsigned int *));
if (!distance_matrix) {
fprintf(stderr, "Memory allocation failure\n");
return NULL;
}
for (size_t i = 0; i < city_count; i++) {
distance_matrix[i] = malloc(city_count * sizeof(unsigned int));
if (!distance_matrix[i]) {
fprintf(stderr, "Memory allocation failure\n");
for (size_t j = 0; j < i; j++) {
free(distance_matrix[j]);
}
free(distance_matrix);
return NULL;
}
for (size_t j = 0; j < city_count; j++) {
distance_matrix[i][j] = (i == j) ? 0 : 4294967295; // 4294967295 is the maximum value of unsigned int
}
}
return distance_matrix;
}
void fill_distance_matrix(unsigned int **distance_matrix, Route *routes, size_t route_count, char **unique_cities, size_t city_count) {
for (size_t i = 0; i < route_count; i++) {
size_t from_city_index = 0, to_city_index = 0;
for (size_t j = 0; j < city_count; j++) {
if (strcmp(routes[i].city1, unique_cities[j]) == 0) {
from_city_index = j;
}
if (strcmp(routes[i].city2, unique_cities[j]) == 0) {
to_city_index = j;
}
}
distance_matrix[from_city_index][to_city_index] = routes[i].distance;
distance_matrix[to_city_index][from_city_index] = routes[i].distance; // Assuming undirected graph
}
}
void cleanup_resources(Route *routes, size_t route_count, char **unique_cities, size_t city_count,
unsigned int **distance_matrix, unsigned int **dp, size_t dp_size) {
// Free routes
for (size_t i = 0; i < route_count; i++) {
free(routes[i].city1);
free(routes[i].city2);
}
free(routes);
for (size_t i = 0; i < city_count; i++) {
free(distance_matrix[i]);
}
free(distance_matrix);
free(unique_cities);
if (dp) {
for (size_t i = 0; i < dp_size; i++) {
free(dp[i]);
}
free(dp);
}
}
size_t find_city_index(const char *city, char **unique_cities, size_t city_count) {
for (size_t i = 0; i < city_count; i++) {
if (strcmp(city, unique_cities[i]) == 0) {
return i;
}
}
return city_count; // Not found
}
bool is_city_unique(char *city, char **unique_cities, size_t city_count) {
for (size_t i = 0; i < city_count; i++) {
if (strcmp(city, unique_cities[i]) == 0) {
return false; // City already in the list
}
}
return true; // City is unique
}
char **extract_unique_cities(Route *routes, size_t route_count, size_t *city_count) {
char **unique_cities = NULL;
size_t count = 0;
for (size_t i = 0; i < route_count; i++) {
if (is_city_unique(routes[i].city1, unique_cities, count)) {
char **temp = realloc(unique_cities, (count + 1) * sizeof(char *));
if (!temp) {
fprintf(stderr, "Memory allocation failure\n");
free(unique_cities);
return NULL;
}
unique_cities = temp;
unique_cities[count++] = routes[i].city1;
}
if (is_city_unique(routes[i].city2, unique_cities, count)) {
char **temp = realloc(unique_cities, (count + 1) * sizeof(char *));
if (!temp) {
fprintf(stderr, "Memory allocation failure\n");
free(unique_cities);
return NULL;
}
unique_cities = temp;
unique_cities[count++] = routes[i].city2;
}
}
*city_count = count;
return unique_cities;
}
Route *parse_routes(FILE *file, size_t *route_count) {
Route *routes = NULL;
size_t count = 0;
char *buffer = NULL;
size_t buffer_size = 0;
while (getline(&buffer, &buffer_size, file) != -1) {
char *newline_pos = strchr(buffer, '\n');
if (newline_pos) {
*newline_pos = '\0';
}
char *dash_pos = strchr(buffer, '-');
char *colon_pos = strchr(buffer, ':');
if (!dash_pos || !colon_pos || dash_pos > colon_pos) {
fprintf(stderr, "Invalid line format\n");
free(buffer);
free(routes);
return NULL;
}
size_t length1 = dash_pos - buffer;
size_t length2 = colon_pos - dash_pos - 1;
Route *temp = realloc(routes, (count + 1) * sizeof(Route));
if (!temp) {
fprintf(stderr, "Memory allocation failure\n");
free(buffer);
free(routes);
return NULL;
}
routes = temp;
routes[count].city1 = malloc(length1 + 1);
routes[count].city2 = malloc(length2 + 1);
if (!routes[count].city1 || !routes[count].city2) {
fprintf(stderr, "Memory allocation failure\n");
free(buffer);
free(routes);
return NULL;
}
strncpy(routes[count].city1, buffer, length1);
routes[count].city1[length1] = '\0';
strncpy(routes[count].city2, dash_pos + 1, length2);
routes[count].city2[length2] = '\0';
routes[count].distance = (unsigned int)strtoul(colon_pos + 1, NULL, 10);
count++;
}
free(buffer);
*route_count = count;
return routes;
}
void held_karp_step_2(unsigned int **distance_matrix, unsigned int **dp, size_t city_count) {
// Iterate over all cities except the starting city (0)
for (size_t i = 1; i < city_count; i++) {
// Subset containing only the start city and city i
unsigned int subset = (1 << 0) | (1 << i); // Bitmask for subset {0, i}
dp[subset][i] = distance_matrix[0][i]; // Cost to travel directly from 0 to i
}
}
unsigned int **initialize_dp_table(size_t city_count) {
size_t subset_count = 1 << city_count; // Total subsets = 2^city_count
unsigned int **dp = malloc(subset_count * sizeof(unsigned int *));
if (!dp) {
fprintf(stderr, "Memory allocation failure\n");
return NULL;
}
for (size_t i = 0; i < subset_count; i++) {
dp[i] = malloc(city_count * sizeof(unsigned int));
if (!dp[i]) {
fprintf(stderr, "Memory allocation failure\n");
for (size_t j = 0; j < i; j++) {
free(dp[j]);
}
free(dp);
return NULL;
}
// Initialize all entries to a large value (infinity)
for (size_t j = 0; j < city_count; j++) {
dp[i][j] = UINT_MAX;
}
}
return dp;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
return 1;
}
FILE *file = open_file(argv[1]);
if (!file) {
fprintf(stderr, "Failed to open file");
return 1;
}
size_t route_count = 0;
Route *routes = parse_routes(file, &route_count);
fclose(file);
if (!routes) {
fprintf(stderr, "Memory allocation failure\n");
return 1;
}
size_t city_count = 0;
char **unique_cities = extract_unique_cities(routes, route_count, &city_count);
if (!unique_cities) {
cleanup_resources(routes, route_count, NULL, 0, NULL, NULL, 0);
return 1;
}
unsigned int **distance_matrix = initialize_distance_matrix(city_count);
if (!distance_matrix) {
cleanup_resources(routes, route_count, unique_cities, city_count, NULL, NULL, 0);
return 1;
}
fill_distance_matrix(distance_matrix, routes, route_count, unique_cities, city_count);
unsigned int **dp_table = initialize_dp_table(city_count);
if (!dp_table) {
cleanup_resources(routes, route_count, unique_cities, city_count, distance_matrix, NULL, 0);
return 1;
}
held_karp_step_2(distance_matrix, dp_table, city_count);
cleanup_resources(routes, route_count, unique_cities, city_count, distance_matrix, dp_table, 1 << city_count);
return 0;
}