graph封装底层,让dfs和dfs代码看起来更加直观

This commit is contained in:
建峰 2025-04-26 18:43:59 +08:00
parent 5a2c2ceccc
commit 9d5f71250e
2 changed files with 39 additions and 19 deletions

View File

@ -776,6 +776,25 @@ static bool graph_find_edge(struct _graph *self, void *from, void *to)
return true; return true;
} }
static struct _graph_node * graph_find_unvisited_vertex(struct _graph *self)
{
assert(self != NULL);
if (self->empty(self))
{
return false;
}
struct _graph_node *node = self->_head->next;
while(node != NULL)
{
if(node->visited == false)
{
return node;
}
node = node->next;
}
return NULL;
}
static struct _graph_node * graph_find_unvisited_target(struct _graph *self, struct _graph_node *node) static struct _graph_node * graph_find_unvisited_target(struct _graph *self, struct _graph_node *node)
{ {
@ -884,25 +903,30 @@ const void *graph_iter_next(struct _iterator *iter)
queue_t queue = self->queue; queue_t queue = self->queue;
// 3. [graph special] if queue is empty, find a new root node
if (queue->empty(queue)) if (queue->empty(queue))
{ {
cur_node = self->_head->next; cur_node = graph_find_unvisited_vertex(self);
while (cur_node != NULL) if(cur_node != NULL)
{
if (cur_node->visited != true)
{ {
queue->push(queue, &cur_node); queue->push(queue, &cur_node);
break;
}
cur_node = cur_node->next;
} }
} }
// graph->BFS : similar to breadth-first traversal of a tree
if (!queue->empty(queue) && node != NULL) if (!queue->empty(queue) && node != NULL)
{ {
// 1. pop the root vertex from queue
queue->pop(queue, &node); queue->pop(queue, &node);
node->visited = true; node->visited = true;
// 2. find unvisited target node
// target = graph_find_unvisited_target(self, node);
// if(target != NULL)
// {
// queue->push(queue, &target);
// }
cur_edge = node->edgehead; cur_edge = node->edgehead;
while (cur_edge != NULL) while (cur_edge != NULL)
{ {
@ -957,15 +981,10 @@ const void *graph_iter_next(struct _iterator *iter)
// 5. find unvisited target node // 5. find unvisited target node
cur_node = graph_find_unvisited_target(self, cur_node); cur_node = graph_find_unvisited_target(self, cur_node);
// 6. If the graph contains isolated vertices // 6. [graph special] If the graph contains isolated vertices
cur_node = self->_head->next; if(cur_node == NULL)
while (cur_node != NULL)
{ {
if (cur_node->visited != true) cur_node = graph_find_unvisited_vertex(self);
{
break;
}
cur_node = cur_node->next;
} }
} }
} }

View File

@ -158,17 +158,18 @@ void test_graph_iter(void)
while(iter_vertex->hasnext(iter_vertex)) while(iter_vertex->hasnext(iter_vertex))
{ {
temp = *(int *)iter_vertex->next(iter_vertex); temp = *(int *)iter_vertex->next(iter_vertex);
// graph->print_obj(&temp); graph->print_obj(&temp);
} }
printf("\n");
iter_vertex = graph->iter(graph, GRAPH_DFS, &data[0]); iter_vertex = graph->iter(graph, GRAPH_DFS, &data[0]);
TEST_ASSERT_NOT_NULL(iter_vertex); TEST_ASSERT_NOT_NULL(iter_vertex);
while(iter_vertex->hasnext(iter_vertex)) while(iter_vertex->hasnext(iter_vertex))
{ {
temp = *(int *)iter_vertex->next(iter_vertex); temp = *(int *)iter_vertex->next(iter_vertex);
// printf("temp = %d\n", temp); graph->print_obj(&temp);
// graph->print_obj(&temp);
} }
printf("\n");
graph_free(&graph); graph_free(&graph);
TEST_ASSERT_NULL(graph); TEST_ASSERT_NULL(graph);