bfs的遍历还存在问题

This commit is contained in:
建峰 2025-04-26 15:01:15 +08:00
parent ce973043d8
commit 98eb005a4a
3 changed files with 210 additions and 89 deletions

View File

@ -13,6 +13,9 @@
#include "common.h" #include "common.h"
#include "stack.h"
#include "queue.h"
enum _graph_type enum _graph_type
{ {
GRAPH_UNDIRECTED, GRAPH_UNDIRECTED,
@ -21,6 +24,13 @@ enum _graph_type
// GRAPH_DIRECTED_WEIGHT, // GRAPH_DIRECTED_WEIGHT,
}; };
enum _graph_search
{
GRAPH_DFS,
GRAPH_BFS,
};
struct _graph_edge struct _graph_edge
{ {
uint32_t weight; uint32_t weight;
@ -47,6 +57,11 @@ struct _graph
uint32_t _ratio; uint32_t _ratio;
enum _graph_type _type; enum _graph_type _type;
enum _graph_search _search;
stack_t stack;
queue_t queue;
struct _iterator _iter; struct _iterator _iter;
void (*_destory)(struct _graph* self); void (*_destory)(struct _graph* self);
@ -77,7 +92,7 @@ struct _graph
bool (*full)(struct _graph* self); bool (*full)(struct _graph* self);
// iter // iter
iterator_t (*iter)(struct _graph* self, void *obj); iterator_t (*iter)(struct _graph* self, enum _graph_search search_type, void *obj);
// config // config
compare_fun_t compare; // !!! you have to implement this function compare_fun_t compare; // !!! you have to implement this function

View File

@ -310,10 +310,6 @@ void graph_free(graph_t* graph)
#endif #endif
static struct _graph_node *graph_node_new(void *obj, uint32_t obj_size) static struct _graph_node *graph_node_new(void *obj, uint32_t obj_size)
{ {
void *new_obj = (void *)calloc(1, obj_size); void *new_obj = (void *)calloc(1, obj_size);
@ -441,6 +437,15 @@ static void graph_destory(struct _graph* self)
free(self->_head); free(self->_head);
self->_head = NULL; self->_head = NULL;
} }
if (self->stack != NULL)
{
stack_free(&self->stack);
}
if (self->queue != NULL)
{
queue_free(&self->queue);
}
} }
static void graph_print(struct _graph *self) static void graph_print(struct _graph *self)
@ -608,7 +613,6 @@ static struct _graph_node* find_node(struct _graph* self, void* obj)
return cur; return cur;
} }
static bool graph_add_edge(struct _graph *self, void *from, void *to, uint32_t weight) static bool graph_add_edge(struct _graph *self, void *from, void *to, uint32_t weight)
{ {
assert(self != NULL); assert(self != NULL);
@ -772,21 +776,49 @@ static bool graph_find_edge(struct _graph* self, void* from, void* to)
return true; return true;
} }
iterator_t graph_iter(struct _graph *self, enum _graph_search search_type, void *start)
iterator_t graph_iter(struct _graph* self, void* start_obj)
{ {
assert(self != NULL); assert(self != NULL);
iterator_t iter = &self->_iter; iterator_t iter = &self->_iter;
iter->_parent = self; iter->_parent = self;
iter->_cur = 0; iter->_cur = 0;
iter->_cur_node = self->_head; iter->_cur_node = self->_head->next;
struct _graph_node* start = find_node(self, start_obj); struct _graph_node *start_node = find_node(self, start);
if(start != NULL) if (start_node == NULL)
{ {
iter->_cur_node = start; goto done;
} }
iter->_cur_node = start_node;
struct _graph_node *node = self->_head->next;
while (node != NULL)
{
node->visited = false;
node = node->next;
}
self->_search = search_type;
switch (self->_search)
{
case GRAPH_BFS:
{
self->queue->push(self->queue, &iter->_cur_node);
}
break;
case GRAPH_DFS:
{
// pass
}
break;
default:
{
}
break;
}
done:
return iter; return iter;
} }
@ -811,14 +843,74 @@ const void* graph_iter_next(struct _iterator* iter)
graph_t self = (graph_t)iter->_parent; graph_t self = (graph_t)iter->_parent;
void *obj = NULL; void *obj = NULL;
// base on linklist iter->_cur += 1;
struct _graph_node * node = (struct _graph_node *)iter->_cur_node; switch (self->_search)
if(node != NULL)
{ {
obj = node->obj; case GRAPH_BFS:
iter->_cur_node = node->next; {
struct _graph_node *cur_node = iter->_cur_node;
struct _graph_edge *cur_edge = cur_node->edgehead;
struct _graph_node *target = NULL;
struct _graph_node* node = cur_node;
queue_t queue = self->queue;
if (!queue->empty(queue) && node != NULL)
{
queue->pop(queue, &node);
node->visited = true;
cur_edge = node->edgehead;
while (cur_edge != NULL)
{
target = cur_edge->target;
if(target != NULL && target->visited != true)
{
queue->push(queue, &target);
self->print_obj(node->obj);
printf(" -> ");
self->print_obj(target->obj);
} }
self->_iter._cur += 1; cur_edge = cur_edge->next;
}
cur_node = node;
}
else
{
// if queue is empty, find next unvisited node
cur_node = self->_head->next;
while (cur_node != NULL)
{
if(cur_node->visited != true)
{
break;
}
cur_node = cur_node->next;
}
if(cur_node != NULL)
{
queue->push(queue, &cur_node);
iter->_cur_node = cur_node;
}
}
iter->_cur_node = cur_node;
obj = cur_node->obj;
}
break;
case GRAPH_DFS:
{
// self->stack->push(self->stack, iter->_cur_node);
}
break;
default:
{
}
break;
}
return obj; return obj;
} }
@ -836,9 +928,23 @@ static bool graph_init(struct _graph* self, uint32_t obj_size)
self->_capacity = UINT32_MAX; self->_capacity = UINT32_MAX;
self->_ratio = 1; self->_ratio = 1;
self->stack = stack_new(sizeof(struct _graph_node *));
if (self->stack == NULL)
{
return false;
}
self->queue = queue_new(sizeof(struct _graph_node *));
if (self->queue == NULL)
{
stack_free(&self->stack);
return false;
}
self->_head = (struct _graph_node *)malloc(sizeof(struct _graph_node)); self->_head = (struct _graph_node *)malloc(sizeof(struct _graph_node));
if (self->_head == NULL) if (self->_head == NULL)
{ {
stack_free(&self->stack);
queue_free(&self->queue);
return false; return false;
} }
self->_head->visited = false; self->_head->visited = false;

View File

@ -141,7 +141,7 @@ void test_graph_iter(void)
// graph->print(graph); // graph->print(graph);
// test add_edge // test add_edge
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[1], 12)); TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[5], 12));
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[2], 13)); TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[2], 13));
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[1], &data[3], 24)); TEST_ASSERT_TRUE(graph->add_edge(graph, &data[1], &data[3], 24));
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[5], &data[6], 67)); TEST_ASSERT_TRUE(graph->add_edge(graph, &data[5], &data[6], 67));
@ -149,7 +149,7 @@ void test_graph_iter(void)
graph->print(graph); graph->print(graph);
printf("test iter_vertex: \n"); printf("test iter_vertex: \n");
iterator_t iter_vertex = graph->iter(graph, &data[0]); iterator_t iter_vertex = graph->iter(graph, GRAPH_BFS, &data[0]);
TEST_ASSERT_NOT_NULL(iter_vertex); TEST_ASSERT_NOT_NULL(iter_vertex);
printf("start\n"); printf("start\n");
@ -157,7 +157,7 @@ void test_graph_iter(void)
{ {
printf("next : "); printf("next : ");
temp = *(int *)iter_vertex->next(iter_vertex); temp = *(int *)iter_vertex->next(iter_vertex);
printf("%d\n", temp); printf("temp = %d\n", temp);
} }
printf("end\n"); printf("end\n");