diff options
| author | Mikkel Thestrup <mithe24@student.sdu.dk> | 2025-12-05 11:33:16 +0100 |
|---|---|---|
| committer | Mikkel Thestrup <mithe24@student.sdu.dk> | 2025-12-05 11:41:09 +0100 |
| commit | 7a00f88749b00c7eb0404872b21e36adb17d8456 (patch) | |
| tree | 51c387c8a46b80984526aea74b4e4d703bf15bea /src/graph.c | |
| parent | f624eaf7cb786fea5e574637ccd31fbed52eec5a (diff) | |
| download | cycle-detector-7a00f88749b00c7eb0404872b21e36adb17d8456.tar.gz cycle-detector-7a00f88749b00c7eb0404872b21e36adb17d8456.zip | |
Small changes
Diffstat (limited to 'src/graph.c')
| -rw-r--r-- | src/graph.c | 32 |
1 files changed, 14 insertions, 18 deletions
diff --git a/src/graph.c b/src/graph.c index 44ce400..8c19a19 100644 --- a/src/graph.c +++ b/src/graph.c @@ -20,7 +20,7 @@ Graph *graph_new(int n) { graph->num_edges = 0; graph->num_vertices = n; - + for (int i = 0; i < n; i++) { graph->vertices[i].id = i; graph->vertices[i].out_neighbours = vector_new(); @@ -47,46 +47,42 @@ Graph *graph_read(const char *filename) { FILE *file = fopen(filename, "r"); if (!file) return NULL; - - char line[1024]; + + char line[4096]; + if (!fgets(line, sizeof(line), file)) { fclose(file); return NULL; } - + int n = atoi(line); if (n <= 0) { fclose(file); return NULL; } - + Graph *g = graph_new(n); if (!g) { fclose(file); return NULL; } - - for (int i = 0; i < n; i++) { + + for (int row = 0; row < n; row++) { if (!fgets(line, sizeof(line), file)) { - fprintf(stderr, "Error: Could not read row %d\n", i); fclose(file); return g; } - - int j = 0; + int col = 0; - while (col < n && line[j] != '\0' && line[j] != '\n') { - if (line[j] == '1') { - graph_add_edge(g, i, col); - col++; - } else if (line[j] == '0') { + for (int i = 0; line[i] != EOF && line[i] != '\n'; i++) { + if (line[i] == '0' || line[i] == '1') { + if (line[i] == '1') + graph_add_edge(g, row, col); col++; } - // skip any other characters - j++; } } - + fclose(file); return g; } |