c - Why does my code crash when I try to use input file with more than 100 edges? -
#include <stdio.h> #define graphsize 2048 #define infinity graphsize*graphsize #define max(a, b) ((a > b) ? (a) : (b)) int e; /* number of nonzero edges in graph */ int n; /* number of nodes in graph */ long dist[graphsize][graphsize]; long d[graphsize]; int prev[graphsize]; void printpath(int dest) { if (prev[dest] != -1) printpath(prev[dest]); printf("%d ", dest); } void dijkstra(int s) { int i, k, mini; int visited[graphsize]; (i = 1; <= n; ++i) { d[i] = infinity; prev[i] = -1; /* no path has yet been found */ visited[i] = 0; /* i-th element has not yet been visited */ } d[s] = 0; (k = 1; k <= n; ++k) { mini = -1; (i = 1; <= n; ++i) if (!visited[i] && ((mini == -1) || (d[i] < d[mini]))) mini = i; visited[mini] = 1; (i = 1; <= n; ++i) if (dist[mini][i]) if (d[mini] + dist[mini][i] < d[i]) { d[i] = d[mini] + dist[mini][i]; prev[i] = mini; } } } int main(int argc, char *argv[]) { int i, j; int u, v, w; file *fin = fopen("/users/kostistougianni/downloads/networks/net100.txt", "r"); fscanf(fin, "%d", &e); (i = 0; < e; ++i) (j = 0; j < e; ++j) dist[i][j] = 0; n = -1; (i = 0; < e; ++i) { fscanf(fin, "%d%d%d", &u, &v, &w); dist[u][v] = w; n = max(u, max(v, n)); } fclose(fin); dijkstra(1); printf("\n"); (i = 1; <= n; ++i) { printf("path %d: ", i); printpath(i); printf("\n"); } dijkstra(1); printf("\n"); printf("path 1 100:"); printpath(100); return 0; }
my text looks like: 1 5 10
1 = u, 5 = v, 10 = w
the code works fine while use inout file 10-50 edges. after that, when give input file more 100 edges gives out crash without errors.
Comments
Post a Comment