From 19d1c2d9849248b3a4f0f6950b751aa81f33f252 Mon Sep 17 00:00:00 2001 From: laentropia Date: Wed, 6 May 2026 22:29:22 -0600 Subject: [PATCH] addition: dijkstar printing --- include/graph.h | 3 ++ src/graph.c | 100 ++++++++++++++++++++++++++++++++++++++++++++-- src/main.c | 29 ++++++++++++++ test/test_graph.c | 5 +++ 4 files changed, 134 insertions(+), 3 deletions(-) diff --git a/include/graph.h b/include/graph.h index 8e45384..62cf3e0 100644 --- a/include/graph.h +++ b/include/graph.h @@ -41,4 +41,7 @@ GraphErr graph_vertex_count(Graph *graph, size_t *out); GraphErr graph_get_vertex(Graph *graph, size_t index, Vertex *out); GraphErr graph_dijkstra(Graph *graph, size_t source, DijkstraResult *result); +void graph_print(Graph *graph); +void dijkstra_print_result(DijkstraResult *result); + #endif diff --git a/src/graph.c b/src/graph.c index 83d5ce0..b10904e 100644 --- a/src/graph.c +++ b/src/graph.c @@ -5,6 +5,7 @@ #include #include #include +#include struct Vertex { ArrayList *edges; @@ -38,7 +39,7 @@ Graph *graph_create(GraphType type) { } GraphErr graph_destroy(Graph **graph) { - if (graph == NULL || *graph) { + if (graph == NULL || *graph == NULL) { return GRAPH_NULL_ARG; } @@ -52,6 +53,7 @@ GraphErr graph_destroy(Graph **graph) { arraylist_destroy(&(*graph)->vertices); + free(*graph); *graph = NULL; return GRAPH_OK; @@ -143,9 +145,9 @@ GraphErr graph_dijkstra(Graph *graph, size_t source, DijkstraResult *result) { ArrayList *prev = arraylist_init(vertex_count, sizeof(size_t)); for (size_t i = 0; i < vertex_count; i++) { - arraylist_push_back(distances, SIZE_MAX); + arraylist_push_back(distances, &(size_t){SIZE_MAX}); arraylist_push_back(visited, false); - arraylist_push_back(prev, SIZE_MAX); + arraylist_push_back(prev, &(size_t){SIZE_MAX}); } arraylist_set(distances, source, &(size_t){0}); @@ -204,3 +206,95 @@ GraphErr graph_dijkstra(Graph *graph, size_t source, DijkstraResult *result) { return GRAPH_OK; } + +void graph_print(Graph *graph) { + if (graph == NULL) { + return; + } + + size_t vertex_count = arraylist_size(graph->vertices); + + printf("Graph {\n"); + + for (size_t i = 0; i < vertex_count; i++) { + + Vertex vertex; + arraylist_get(graph->vertices, i, &vertex); + + printf(" [%zu] -> ", i); + + size_t edge_count = arraylist_size(vertex.edges); + + for (size_t j = 0; j < edge_count; j++) { + + Edge edge; + arraylist_get(vertex.edges, j, &edge); + + printf("(%zu, w=%zu)", edge.to, edge.weight); + + if (j + 1 < edge_count) { + printf(" -> "); + } + } + + printf("\n"); + } + + printf("}\n"); +} + +void dijkstra_print_result(DijkstraResult *result) { + if (result == NULL) { + return; + } + + size_t count = arraylist_size(result->distances); + + printf("Dijkstra Result {\n"); + + for (size_t i = 0; i < count; i++) { + + size_t dist; + + arraylist_get(result->distances, i, &dist); + + printf(" [%zu] ", i); + + if (dist == SIZE_MAX) { + printf("dist=INF\n"); + continue; + } + + printf("dist=%zu ", dist); + + size_t path[count]; + size_t path_len = 0; + + size_t current = i; + + while (current != SIZE_MAX) { + + path[path_len++] = current; + + size_t next; + arraylist_get(result->prev, current, &next); + + current = next; + } + + printf("path= "); + + for (size_t j = path_len; j > 0; j--) { + + printf("%zu", path[j - 1]); + + if (j > 1) { + printf(" -> "); + } + } + + printf("\n"); + } + + printf("}\n"); +} diff --git a/src/main.c b/src/main.c index 24acc10..f783a5a 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,34 @@ #include +#include +#include "graph.h" int main(void) { + Graph *graph = graph_create(GRAPH_UNDIRECTED); + + graph_add_vertex(graph); + graph_add_vertex(graph); + graph_add_vertex(graph); + graph_add_vertex(graph); + graph_add_vertex(graph); + graph_add_vertex(graph); + + graph_connect_vertex(graph, 0, 1, 9); + graph_connect_vertex(graph, 0, 2, 4); + graph_connect_vertex(graph, 1, 2, 2); + graph_connect_vertex(graph, 1, 4, 3); + graph_connect_vertex(graph, 1, 3, 7); + graph_connect_vertex(graph, 2, 3, 1); + graph_connect_vertex(graph, 2, 4, 6); + graph_connect_vertex(graph, 3, 4, 4); + graph_connect_vertex(graph, 3, 5, 8); + graph_connect_vertex(graph, 4, 5, 2); + + DijkstraResult result; + graph_dijkstra(graph, 0, &result); + + puts("Algoritmo de dijsktra"); + graph_print(graph); + dijkstra_print_result(&result); + return EXIT_SUCCESS; } diff --git a/test/test_graph.c b/test/test_graph.c index e69de29..24acc10 100644 --- a/test/test_graph.c +++ b/test/test_graph.c @@ -0,0 +1,5 @@ +#include + +int main(void) { + return EXIT_SUCCESS; +}