aboutsummaryrefslogtreecommitdiff
path: root/src/graph.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/graph.c32
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;
}