From 0cafaf72753a31d47e96bc8318b70ad8db215e6f Mon Sep 17 00:00:00 2001 From: phfr24 Date: Sun, 24 May 2026 00:45:47 -0300 Subject: [PATCH] add gt.c --- .gitignore | 2 +- Makefile | 4 +- graph/adj.c | 3 ++ gt.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 gt.c diff --git a/.gitignore b/.gitignore index ff3c331..429fdbe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ .*.swp *.o -dfs +gt diff --git a/Makefile b/Makefile index 90d603b..1af8984 100644 --- a/Makefile +++ b/Makefile @@ -13,8 +13,8 @@ CFLAGS := -I. \ -Wdouble-promotion \ -Wno-variadic-macros -objs := dfs.o graph/adj.o graph/pr.o -bin := dfs +objs := gt.o graph/adj.o graph/dfs.o graph/pr.o +bin := gt all : $(bin) diff --git a/graph/adj.c b/graph/adj.c index dfcf036..fbad922 100644 --- a/graph/adj.c +++ b/graph/adj.c @@ -127,6 +127,9 @@ free_graph (struct graph *g) struct vertex *v; unsigned int i; + if (!g) + return; + for (i = 0; i < g->nv; ++i) { v = &g->v[i]; diff --git a/gt.c b/gt.c new file mode 100644 index 0000000..e9247e4 --- /dev/null +++ b/gt.c @@ -0,0 +1,126 @@ +#define _GNU_SOURCE +#include +#include +#include +#include "dbg.h" +#include "graph/adj.h" +#include "graph/dfs.h" + +#define bfs dfs /* stub until tomorrow */ + +enum algo +{ + ALGO_NONE, + ALGO_DFS, + ALGO_BFS +}; + +static void +print_dot_graph (struct graph *g, const char *name) +{ + unsigned int i; + struct edge *e; + unsigned int vi, vj; + + printf ("graph %s {\n", name); + for (i = 0; i < g->nv; ++i) + { + printf (" %d;\n", i); + for (e = g->v[i].adj; e; e = e->next) + { + vi = i; + vj = vertex_id (g, e->to); + if (vi <= vj) + printf (" %d -- %d;\n", vi, vj); + } + } + printf ("}\n"); +} + +static void +print_dot_digraph (struct graph *g, const char *name) +{ + unsigned int i; + struct edge *e; + unsigned int vi, vj; + + printf ("digraph %s {\n", name); + for (i = 0; i < g->nv; ++i) + { + printf (" %d;\n", i); + for (e = g->v[i].adj; e; e = e->next) + { + vi = i; + vj = vertex_id (g, e->to); + printf (" %d -> %d;\n", vi, vj); + } + } + printf ("}\n"); +} + +int +main (int argc, char **argv) +{ + int c; + enum algo a = ALGO_NONE; + struct graph *g, *dt; + unsigned int nv, u, v; + int i; + + static struct option long_opts[] = { + {"dfs", no_argument, 0, 'd'}, + {"bfs", no_argument, 0, 'b'}, + {0, 0, 0, 0} + }; + + while ((c = getopt_long (argc, argv, "db", long_opts, NULL)) != -1) + { + switch (c) + { + case 'd': + a = ALGO_DFS; + break; + case 'b': + a = ALGO_BFS; + break; + default: + fprintf (stderr, "Usage: %s [--dfs|--bfs] [u1 v1 ...]\n", argv[0]); + exit (EXIT_FAILURE); + } + } + + if (a == ALGO_NONE) + fatal ("no algorithm specified"); + + if (optind >= argc) + fatal ("missing number of vertices"); + + nv = (unsigned int) atoi (argv[optind]); + if (!(g = new_graph (nv))) + fatal ("new_graph ()"); + + for (i = optind + 1; i < argc - 1; i += 2) + { + u = (unsigned int) atoi (argv[i]); + v = (unsigned int) atoi (argv[i + 1]); + if (add_edge (g, u, v)) + fatal ("add_edge ()"); + } + + if (a == ALGO_DFS) + dt = dfs (g); + else + dt = bfs (g); + + if (!dt) + fatal ("algorithm failed"); + + print_dot_graph (g, "g"); + printf ("\n"); + print_dot_digraph (dt, "dt"); + + free_graph (g); + free_graph (dt); + + return 0; +} -- 2.53.0