mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-03 15:56:52 +08:00
graph封装底层,让dfs和dfs代码看起来更加直观
This commit is contained in:
parent
5a2c2ceccc
commit
9d5f71250e
49
src/graph.c
49
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)
|
||||
{
|
||||
if (cur_node->visited != true)
|
||||
cur_node = graph_find_unvisited_vertex(self);
|
||||
if(cur_node != NULL)
|
||||
{
|
||||
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)
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user