-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestDijkstra.c
More file actions
90 lines (85 loc) · 1.79 KB
/
testDijkstra.c
File metadata and controls
90 lines (85 loc) · 1.79 KB
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
#include "Dijkstra.h"
#include "GraphVis.h"
#include <stdlib.h>
#include <stdio.h>
Graph readGraph(char* file) {
// ugly count
FILE * f = fopen(file , "r");
if(!f){
fprintf(stderr, "ERROR READING FILE %s\n", file);
return NULL;
}
int lines = 0;
char ch = 0;
while(!feof(f)){ch = fgetc(f); if(ch == '\n')lines++;}
fclose(f);
//ugly parse
f = fopen(file , "r");
if(!f){
fprintf(stderr, "ERROR READING FILE %s\n", file);
return NULL;
}
int a=0;
int b=0;
int c=0;
int i = 0;
int maxVert = -1;
int **nums = malloc(sizeof(int*)*lines);
for(i=0;i<lines;i++) nums[i] = malloc(sizeof(int)*3);
i=0;
while(i < lines) {
fscanf(f, "%d,", &a);
fscanf(f, "%d,", &b);
fscanf(f, "%d", &c);
if (a > maxVert) maxVert = a;
if (b > maxVert) maxVert = b;
nums[i][0] = a;
nums[i][1] = b;
nums[i][2] = c;
i++;
}
fclose(f);
Graph g = newGraph(maxVert);
i = 0;
while(i < lines) {
insertEdge(g,nums[i][0],nums[i][1],nums[i][2]);
i++;
}
for(i=0;i<lines;i++) free(nums[i]);
free(nums);
return g;
}
void displayShortestPathsStruct(ShortestPaths sps){
int i = 0;
printf("Node %d\n",sps.src);
printf(" Distance\n");
for (i = 0; i < sps.noNodes; i++) {
if(i == sps.src)
printf(" %d : X\n",i);
else
printf(" %d : %d\n",i,sps.dist[i]);
}
printf(" Preds\n");
for (i = 0; i < sps.noNodes; i++) {
printf(" %d : ",i);
PredNode* curr = sps.pred[i];
while(curr!=NULL) {
printf("[%d]->",curr->v);
curr = curr->next;
}
printf("NULL\n");
}
}
int main(int argc, char* argv[]){
if(argc < 2) {
printf("Usage: ./testDijkstra [file]\n");
return EXIT_FAILURE;
}
Graph g = readGraph(argv[1]);
int i;
for(i=0; i<numVerticies(g); i++) {
ShortestPaths paths = dijkstra(g,i);
displayShortestPathsStruct(paths);
freeShortestPaths(paths);
}
}