commit 96b893eeee4a3afd16527336972b376fc99da1cc
parent 30c427ab5478515fca4fcc599f840d3f69f463e7
Author: mjkloeckner <martin.cachari@gmail.com>
Date: Fri, 27 Jan 2023 18:02:31 -0300
print graph with graphviz format
modified graph_print to print in graphviz format so it can be parsed by
the `dot` program and generate a nice graphic, the graph_print_gv works
the same but adds a nicer output
Diffstat:
M | main.c | | | 46 | ++++++++++++++++++++++++++++++++++++++-------- |
1 file changed, 38 insertions(+), 8 deletions(-)
diff --git a/main.c b/main.c
@@ -31,6 +31,7 @@ typedef struct {
/* Functions declaration */
void graph_new(Graph **G);
void graph_print(Graph *G);
+void graph_print_gv(Graph *G);
void graph_add_vertex(Graph *G, Vertex *v);
void graph_rm_vertex(Graph *G, Vertex *v);
void graph_destroy(Graph *G);
@@ -76,23 +77,53 @@ void graph_destroy(Graph *G) {
free(G);
}
-/* TODO: make this function generic (data type independent) */
+/* TODO: make print functions generic (data type independent) */
void graph_print(Graph *G) {
- int *x;
+ int x;
size_t i, j;
Vertex *v;
+ puts("strict graph G {");
+
+ for(i = 0; i < G->vertices_no; i++) {
+ v = G->vertices[i];
+ vertex_get_value(v, &x);
+ printf(" %d%s", x, v->edges_no ? " -- " : "\n");
+
+ for(j = 0, printf("{"); j < v->edges_no; j++) {
+ vertex_get_value(v->edges[j], &x);
+ printf("%d%s", x, (j < (v->edges_no - 1)) ? ", " : "}");
+ }
+ printf("\n");
+ }
+ puts("}");
+}
+
+void graph_print_gv(Graph *G) {
+ int x;
+ size_t i, j;
+ Vertex *v;
+
+ puts("strict graph G {\n"
+ " rankdir=RL;\n"
+ " node [ shape=circle,\n"
+ " penwidth=2.0, \n"
+ " fontname=\"DejaVu Sans Mono\", \n"
+ " fontsize=20.0 ]\n"
+ " edge [ penwidth=2.0 ]\n");
+
for(i = 0; i < G->vertices_no; i++) {
v = G->vertices[i];
vertex_get_value(v, &x);
- printf("%d%s", x, v->edges_no ? " -> " : "\n");
+ printf(" %d%s", x, v->edges_no ? " -- " : "\n");
- for(j = 0; j < v->edges_no; j++) {
+ for(j = 0, printf("{"); j < v->edges_no; j++) {
vertex_get_value(v->edges[j], &x);
- printf("%d%s", x, (j < (v->edges_no - 1)) ? " -> " : "");
+ printf("%d%s", x, (j < (v->edges_no - 1)) ? ", " : "}");
}
printf("\n");
}
+ puts("}");
}
void vertex_new(Vertex **x, Vertex_getter getter, Vertex_setter setter) {
@@ -154,7 +185,7 @@ int main (void) {
graph_new(&graph);
- int x, res;
+ int x;
size_t i;
for(i = 0, x = 1; i < 6; i++, x++) {
@@ -183,8 +214,7 @@ int main (void) {
vertex_add_edge(graph->vertices[5], graph->vertices[3]);
- graph_print(graph);
-
+ graph_print_gv(graph);
graph_destroy(graph);
return 0;
}