From: phfr24 Date: Sat, 23 May 2026 00:22:12 +0000 (-0300) Subject: initial commit X-Git-Url: https://git.wirehaze.ovh/graph-theory.git/commitdiff_plain/9bd63c3cb2b06bddf6159fd4d233196057a3633d initial commit --- 9bd63c3cb2b06bddf6159fd4d233196057a3633d diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cb4ead0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.*.swp +*.o diff --git a/graph/adj.c b/graph/adj.c new file mode 100644 index 0000000..ec9cd0c --- /dev/null +++ b/graph/adj.c @@ -0,0 +1,57 @@ +#include "adj.h" +#include +#include + +struct graph * +new_graph (unsigned int nv) +{ + struct graph *g; + + if (!nv) + return NULL; + + if (!(g = malloc (sizeof *g))) + return NULL; + + g->nv = nv; + + if (!(g->v = calloc (nv, sizeof *g->v))) + { + free (g); + return NULL; + } + + return g; +} + +int +new_arc (struct graph *g, unsigned int vi, unsigned int vj) +{ + struct edge *e; + + if (vi >= g->nv || vj >= g->nv) + return -EINVAL; + + if (!(e = malloc (sizeof *e))) + return -ENOMEM; + + e->to = &g->v[vj]; + e->next = g->v[vi].adj; + g->v[vi].adj = e; + + return 0; +} + +int +new_edge (struct graph *g, unsigned int vi, unsigned int vj) +{ + int err; + + if ((err = new_arc (g, vi, vj))) + return err; + + if (vi != vj && (err = new_arc (g, vj, vi))) + return err; /* should delete the other arc but fuck it we ball */ + + return 0; +} diff --git a/graph/adj.h b/graph/adj.h new file mode 100644 index 0000000..680d6b0 --- /dev/null +++ b/graph/adj.h @@ -0,0 +1,33 @@ +#ifndef GRAPH_H +#define GRAPH_H + +/* adjacency list implementation */ + +struct vertex; +struct edge; +struct graph; + +struct edge +{ + struct vertex *to; + struct edge *next; +}; + +struct vertex +{ + struct edge *adj; +}; + +struct graph +{ + struct vertex *v; + unsigned int nv; +}; + +struct graph * +new_graph (unsigned int nv); + +int +new_edge (struct graph *g, unsigned int vi, unsigned int vj); + +#endif /* GRAPH_H */