addition: dijkstar printing
This commit is contained in:
@@ -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
|
||||
|
||||
100
src/graph.c
100
src/graph.c
@@ -5,6 +5,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
29
src/main.c
29
src/main.c
@@ -1,5 +1,34 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user