diff options
Diffstat (limited to 'graph.c')
-rw-r--r-- | graph.c | 45 |
1 files changed, 45 insertions, 0 deletions
@@ -29,6 +29,51 @@ graph load_graph(FILE *stream) { return k; } +graph load_graph_dimacs(FILE *stream) { + int i, n, m, a, b; + + + char buf = fgetc(stream); + while (buf == 'c') { + buf = fgetc(stream); + if (buf == '\n') buf = fgetc(stream); + else buf = 'c'; + } + if (buf != 'p') { + fprintf(stderr, "File format error (expected 'p' declaration)\n"); + return NULL; + } + fscanf(stream, " col %d %d\n", &n, &m); // read node count + fprintf(stderr, "Load DIMACS: n = %d, m = %d\n", n, m); + + graph k = malloc(sizeof(struct graph_descriptor)); + k->N = n; + k->neighbour = malloc(n * sizeof(set)); + for (i = 0; i < n; i++) k->neighbour[i] = empty_set(n); + + for (i = 0; i < m; i++) { + + buf = fgetc(stream); + while (buf == 'c') { + buf = fgetc(stream); + if (buf == '\n') buf = fgetc(stream); + else buf = 'c'; + } + if (buf != 'e') { + fprintf(stderr, "File format error (expected 'e' declaration)\n"); + return NULL; + } + + fscanf(stream, " %d %d\n", &a, &b); + fprintf(stderr, "Edge: (%d, %d)\n", a, b); + a--; b--; + set_add_ip(b, k->neighbour[a]); + set_add_ip(a, k->neighbour[b]); + } + + return k; +} + const set graph_neighbours(const graph g, int n) { return g->neighbour[n]; } |