-
Notifications
You must be signed in to change notification settings - Fork 0
/
a5_matrix.c
277 lines (245 loc) · 5.91 KB
/
a5_matrix.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
273
274
275
276
// Author: Nuoyu Yang Date: Feb. 22, 2019.
#include "a5.h"
// Heap Functions:
int parent(int index) {
return (index+1)/2 - 1;
}
int left(int index) {
return 2 * index + 1;
}
int right(int index) {
return 2 * index + 2;
}
void heapify(MinHeap* H, int index) {
int l = left(index);
int r = right(index);
int smallest;
if (l < H->size && H->heap[l]->weight < H->heap[index]->weight) {
smallest = l;
} else {
smallest = index;
}
if (r < H->size && H->heap[r]->weight < H->heap[smallest]->weight) {
smallest = r;
}
if (smallest != index) {
Dis* temp = H->heap[index];
H->heap[index] = H->heap[smallest];
H->heap[smallest] = temp;
heapify(H, smallest);
}
}
void buildHeap(MinHeap* H) {
for (int i = H->size/2-1; i >= 0; i--) {
heapify(H, i);
}
}
// Pops mininum element in heap
Dis* popMin(MinHeap* H) {
Dis* max;
if (H->size < 1) {
printf("Heap is empty.");
return NULL;
} else {
max = H->heap[0];
H->heap[0] = H->heap[H->size - 1];
H->heap[H->size - 1] = NULL;
H->size--;
heapify(H, 0);
return max;
}
}
// Push element in heap
void push(MinHeap* H, Dis* newDis) {
if (H->size == MAX_HEAP_SIZE) {
printf("Heap is full!");
return;
} else {
H->size++;
int i = H->size;
while (i > 1 && H->heap[parent(i)]->weight < newDis->weight) {
H->heap[i] = H->heap[parent(i)];
i = parent(i);
}
H->heap[i] = newDis;
}
}
// Creates a empty heap
MinHeap* createHeap() {
MinHeap* H = (MinHeap*)malloc(sizeof(MinHeap));
H->size = 0;
return H;
}
// Destroy a heap
void destroyHeap(MinHeap* H) {
for (int i = 0; i < H->size; i++) {
free(H->heap[i]);
H->heap[i] = NULL;
}
free(H);
H = NULL;
}
// Djikstra Algorithm with Matrix:
// Initializes the heap
void initialize(MinHeap* H, int size, int start) {
H->size = size;
for (int i = 0; i < size; i++) {
H->heap[i] = (Dis*) malloc(sizeof(Dis));
H->heap[i]->node = i;
H->heap[i]->weight = INFINITY;
for (int j = 0; j < MATRIX_SIZE; j++) {
H->heap[i]->path[j] = -1;
}
}
H->heap[start]->weight = 0;
}
// Updates the distance between start and adjacent node of u
void relaxMatrix(Dis* u, Dis* v, double** graph) {
if (graph[u->node][v->node] == -1) {
return;
} else if (v->weight > u->weight + graph[u->node][v->node]) {
v->weight = u->weight + graph[u->node][v->node];
for (int j = 0; j< MATRIX_SIZE; j++) {
if (u->path[j] != -1) {
v->path[j] = u->path[j];
} else {
v->path[j] = u->node;
break;
}
}
}
return;
}
// Djikstra Algorithm
Dis* djikstraMatrix(int start, int end, double** graph, int size) {
MinHeap* H = createHeap();
initialize(H, size, start);
buildHeap(H);
while (H->size) {
Dis* u = popMin(H);
if (u->node == end) {
destroyHeap(H);
return u;
} else {
for (int i = 0; i < H->size; i++) {
if (graph[u->node][H->heap[i]->node] > 0) {
relaxMatrix(u, H->heap[i], graph);
}
}
free(u);
buildHeap(H);
}
}
}
// Helper Functions:
// Create an array to store the name of cities.
char** createCities() {
char** cities = (char**) malloc(MATRIX_SIZE * sizeof(char*));
}
// Destroy the cities array.
void destoryCities(char** cities) {
for (int i = 0; i< MATRIX_SIZE; i++) {
free(cities[i]);
cities[i] = NULL;
}
free(cities);
cities = NULL;
}
// Creates an adjacency matrix
double** createGraphMatrix() {
double** graph = (double**) malloc(MATRIX_SIZE * sizeof(double*));
for (int i = 0; i< MATRIX_SIZE; i++) {
graph[i] = (double*) malloc(MATRIX_SIZE * sizeof(double));
}
return graph;
}
// Destroy the adjancency matrix
void destroyGraphMatrix(double** graph) {
for (int i = 0; i< MATRIX_SIZE; i++) {
free(graph[i]);
}
free(graph);
graph = NULL;
}
// Reads file and store the data in graph and cities array
void readFileMatrix(double** graph, char** cities) {
FILE *cfPtr;
char* pin = NULL;
if ((cfPtr = fopen("miles_graph_FINAL.csv", "r")) == NULL) {
printf("File could not be opened\n");
} else {
char c[MAX_LINE_SIZE];
int row = 0;
while (!feof(cfPtr)) {
fgets(c, MAX_LINE_SIZE, cfPtr);
pin = strtok(c, ",");
int col = 0;
if (row == 0) {
while (pin) {
if (col != 0) {
cities[col-1] = strdup(pin);
}
pin = strtok(NULL, ",");
col++;
}
} else {
while (pin) {
if (col != 0) {
graph[row-1][col-1] = atof(pin);
}
pin = strtok(NULL, ",");
col++;
}
}
row++;
}
fclose(cfPtr);
}
}
// Return the index of a city with city's name.
// Return -1 if there is no such city.
int getNode(char** cities, char* city) {
int i = 0;
for (; i < MATRIX_SIZE; i++) {
if (strcmp(cities[i], city) == 0) {
return i;
}
}
return -1;
}
int main() {
double** graph = createGraphMatrix();
char** cities = createCities();
Dis* result;
int start;
int end;
int pathPin;
readFileMatrix(graph, cities);
start = getNode(cities, "Seattle_WA");
end = getNode(cities, "Boston_MA");
result = djikstraMatrix(start, end, graph, MATRIX_SIZE);
printf("Seattle to Boston: %lf miles.\n", result->weight);
pathPin = 0;
while (result->path[pathPin] != -1) {
printf("%s -> ", cities[result->path[pathPin]]);
pathPin++;
}
printf("%s\n", cities[result->node]);
free(result);
printf("\n");
start = getNode(cities, "Minneapolis_MN");
end = getNode(cities, "Ann Arbor_MI");
result = djikstraMatrix(start, end, graph, MATRIX_SIZE);
printf("Minneapolis to Ann Arbor: %lf miles.\n", result->weight);
pathPin = 0;
while (result->path[pathPin] != -1) {
printf("%s -> ", cities[result->path[pathPin]]);
pathPin++;
}
printf("%s\n", cities[result->node]);
free(result);
destroyGraphMatrix(graph);
destoryCities(cities);
printf("\n\n");
return 0;
}