From 9d5f71250e5c7a76b69edc8886181b94e6d8a3b4 Mon Sep 17 00:00:00 2001 From: jf-home Date: Sat, 26 Apr 2025 18:43:59 +0800 Subject: [PATCH] =?UTF-8?q?graph=E5=B0=81=E8=A3=85=E5=BA=95=E5=B1=82?= =?UTF-8?q?=EF=BC=8C=E8=AE=A9dfs=E5=92=8Cdfs=E4=BB=A3=E7=A0=81=E7=9C=8B?= =?UTF-8?q?=E8=B5=B7=E6=9D=A5=E6=9B=B4=E5=8A=A0=E7=9B=B4=E8=A7=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/graph.c | 51 ++++++++++++++++++++++++++++++++--------------- test/test_graph.c | 7 ++++--- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/graph.c b/src/graph.c index 304e504..fa5723b 100644 --- a/src/graph.c +++ b/src/graph.c @@ -776,6 +776,25 @@ static bool graph_find_edge(struct _graph *self, void *from, void *to) 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) { @@ -884,25 +903,30 @@ const void *graph_iter_next(struct _iterator *iter) queue_t queue = self->queue; + // 3. [graph special] if queue is empty, find a new root node if (queue->empty(queue)) { - cur_node = self->_head->next; - while (cur_node != NULL) + cur_node = graph_find_unvisited_vertex(self); + if(cur_node != NULL) { - if (cur_node->visited != true) - { - queue->push(queue, &cur_node); - break; - } - cur_node = cur_node->next; + queue->push(queue, &cur_node); } } + // graph->BFS : similar to breadth-first traversal of a tree if (!queue->empty(queue) && node != NULL) { + // 1. pop the root vertex from queue queue->pop(queue, &node); 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; while (cur_edge != NULL) { @@ -957,15 +981,10 @@ const void *graph_iter_next(struct _iterator *iter) // 5. find unvisited target node cur_node = graph_find_unvisited_target(self, cur_node); - // 6. If the graph contains isolated vertices - cur_node = self->_head->next; - while (cur_node != NULL) + // 6. [graph special] If the graph contains isolated vertices + if(cur_node == NULL) { - if (cur_node->visited != true) - { - break; - } - cur_node = cur_node->next; + cur_node = graph_find_unvisited_vertex(self); } } } diff --git a/test/test_graph.c b/test/test_graph.c index 5aecca9..dd66780 100644 --- a/test/test_graph.c +++ b/test/test_graph.c @@ -158,17 +158,18 @@ void test_graph_iter(void) while(iter_vertex->hasnext(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]); TEST_ASSERT_NOT_NULL(iter_vertex); while(iter_vertex->hasnext(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); TEST_ASSERT_NULL(graph);