mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-06 00:46:52 +08:00
Compare commits
No commits in common. "9e270b7425300806192eeac58adb277af7792846" and "5e83dc291785394297cc165da9f301eed16a2f5d" have entirely different histories.
9e270b7425
...
5e83dc2917
@ -42,9 +42,8 @@ struct _graph_node
|
||||
{
|
||||
void* obj;
|
||||
struct _graph_node* next;
|
||||
|
||||
bool visited;
|
||||
struct _graph_edge* edgehead;
|
||||
bool visited;
|
||||
};
|
||||
|
||||
struct _graph
|
||||
@ -78,16 +77,22 @@ struct _graph
|
||||
bool (*del_edge)(struct _graph* self, void* from, void* to);
|
||||
bool (*find_edge)(struct _graph* self, void* from, void* to);
|
||||
|
||||
bool (*empty)(struct _graph* self);
|
||||
bool (*full)(struct _graph* self);
|
||||
// traverse
|
||||
bool (*dfs)(struct _graph* self, uint32_t idx);
|
||||
bool (*bfs)(struct _graph* self, uint32_t idx);
|
||||
|
||||
bool (*get)(struct _graph* self, uint32_t idx, void* obj);
|
||||
bool (*remove)(struct _graph* self, uint32_t idx);
|
||||
|
||||
// base
|
||||
uint32_t(*size)(struct _graph* self);
|
||||
uint32_t(*capacity)(struct _graph* self);
|
||||
bool (*clear)(struct _graph* self);
|
||||
bool (*empty)(struct _graph* self);
|
||||
bool (*full)(struct _graph* self);
|
||||
|
||||
// iter
|
||||
iterator_t (*iter)(struct _graph* self, enum _graph_search search, void *obj);
|
||||
iterator_t (*iter)(struct _graph* self, enum _graph_search search_type, void *obj);
|
||||
|
||||
// config
|
||||
compare_fun_t compare; // !!! you have to implement this function
|
||||
|
162
src/graph.c
162
src/graph.c
@ -455,7 +455,7 @@ static void graph_print(struct _graph *self)
|
||||
return;
|
||||
}
|
||||
|
||||
printf("\nvertex : \n");
|
||||
printf("vertex : \n");
|
||||
struct _graph_node *cur = self->_head->next;
|
||||
while (cur != NULL)
|
||||
{
|
||||
@ -470,21 +470,23 @@ static void graph_print(struct _graph *self)
|
||||
{
|
||||
if (cur->edgehead != NULL)
|
||||
{
|
||||
// struct _graph_edge* edge = cur->edgehead->next;
|
||||
struct _graph_edge *edge = cur->edgehead;
|
||||
while (edge != NULL)
|
||||
{
|
||||
struct _graph_node *target = (struct _graph_node *)edge->target;
|
||||
|
||||
// printf("from ");
|
||||
printf("from ");
|
||||
self->print_obj(cur->obj);
|
||||
// printf(" to ");
|
||||
printf("--> ");
|
||||
printf(" to ");
|
||||
self->print_obj(target->obj);
|
||||
printf(": %d \n", edge->weight);
|
||||
|
||||
edge = edge->next;
|
||||
}
|
||||
}
|
||||
|
||||
// self->print_obj(cur->obj);
|
||||
cur = cur->next;
|
||||
}
|
||||
printf("\n");
|
||||
@ -774,54 +776,6 @@ 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_next_unvisited_target(struct _graph *self, struct _graph_node *node)
|
||||
{
|
||||
assert(self != NULL);
|
||||
if (self->empty(self))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(node == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct _graph_edge *edge = node->edgehead;
|
||||
struct _graph_node *target = NULL;
|
||||
while(edge != NULL)
|
||||
{
|
||||
target = edge->target;
|
||||
if(target != NULL && target->visited != true)
|
||||
{
|
||||
return target;
|
||||
}
|
||||
edge = edge->next;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
iterator_t graph_iter(struct _graph *self, enum _graph_search search_type, void *start)
|
||||
{
|
||||
assert(self != NULL);
|
||||
@ -901,24 +855,25 @@ 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 = graph_find_unvisited_vertex(self);
|
||||
if(cur_node != NULL)
|
||||
cur_node = self->_head->next;
|
||||
while (cur_node != NULL)
|
||||
{
|
||||
queue->push(queue, &cur_node);
|
||||
if (cur_node->visited != true)
|
||||
{
|
||||
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 all unvisited target node and push them to queue
|
||||
cur_edge = node->edgehead;
|
||||
while (cur_edge != NULL)
|
||||
{
|
||||
@ -926,6 +881,10 @@ const void *graph_iter_next(struct _iterator *iter)
|
||||
if (target != NULL && target->visited != true)
|
||||
{
|
||||
queue->push(queue, &target);
|
||||
|
||||
// self->print_obj(node->obj);
|
||||
// printf(" -> ");
|
||||
// self->print_obj(target->obj);
|
||||
}
|
||||
cur_edge = cur_edge->next;
|
||||
}
|
||||
@ -942,42 +901,73 @@ const void *graph_iter_next(struct _iterator *iter)
|
||||
// self->stack->push(self->stack, iter->_cur_node);
|
||||
struct _graph_node *cur_node = self->_iter._cur_node;
|
||||
struct _graph_node *node = NULL;
|
||||
struct _graph_edge *cur_edge = cur_node->edgehead;
|
||||
struct _graph_node *target = NULL;
|
||||
|
||||
stack_t stack = self->stack;
|
||||
|
||||
// graph->DFS : similar to the preorder traversal of a tree
|
||||
while (!self->stack->empty(self->stack) || cur_node != NULL)
|
||||
// while (!self->stack->empty(self->stack) || cur_node != NULL)
|
||||
do
|
||||
{
|
||||
if (cur_node != NULL)
|
||||
if(cur_node == NULL)
|
||||
{
|
||||
// 1. get current node
|
||||
node = cur_node;
|
||||
|
||||
// 2. push current node to stack
|
||||
self->stack->push(self->stack, &cur_node);
|
||||
cur_node->visited = true;
|
||||
|
||||
// 3. find unvisited target node
|
||||
cur_node = graph_find_next_unvisited_target(self, cur_node);
|
||||
break;
|
||||
}
|
||||
else
|
||||
|
||||
if(cur_node->visited == true)
|
||||
{
|
||||
// 4. if cur_node is NULL, pop from stack and find unvisited target node
|
||||
self->stack->pop(self->stack, &cur_node);
|
||||
|
||||
// 5. find unvisited target node
|
||||
cur_node = graph_find_next_unvisited_target(self, cur_node);
|
||||
|
||||
// 6. [graph special] If the graph contains isolated vertices
|
||||
if(cur_node == NULL)
|
||||
{
|
||||
cur_node = graph_find_unvisited_vertex(self);
|
||||
}
|
||||
stack->pop(stack, &cur_node);
|
||||
cur_node->visited = true;
|
||||
node = cur_node;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool accessible_vertex_exists = false;
|
||||
|
||||
// 1. add cur_node to stack
|
||||
stack->push(stack, &cur_node);
|
||||
|
||||
// 2. find the first unvisited vertex
|
||||
cur_edge = cur_node->edgehead;
|
||||
while(cur_edge != NULL)
|
||||
{
|
||||
target = cur_edge->target;
|
||||
if(target != NULL && target->visited != true)
|
||||
{
|
||||
accessible_vertex_exists = true;
|
||||
|
||||
cur_node = target;
|
||||
break;
|
||||
}
|
||||
cur_edge = cur_edge->next;
|
||||
}
|
||||
|
||||
iterator_t iter = stack->iter(stack);
|
||||
struct _graph_node *temp_node = NULL;
|
||||
while (iter->hasnext(iter))
|
||||
{
|
||||
temp_node = *(struct _graph_node **)iter->next(iter);
|
||||
self->print_obj(temp_node->obj);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
if(accessible_vertex_exists != true ||
|
||||
cur_edge == NULL)
|
||||
{
|
||||
// if no accessible vertex, pop from stack
|
||||
stack->pop(stack, &node);
|
||||
node->visited = true;
|
||||
|
||||
stack->peek(stack, &cur_node);
|
||||
break;
|
||||
}
|
||||
}while(!stack->empty(stack));
|
||||
|
||||
printf("--- end ---\n");
|
||||
iter->_cur_node = cur_node;
|
||||
printf("--- end1 ---\n");
|
||||
obj = node->obj;
|
||||
printf("--- end2 ---\n");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@ -1057,9 +1047,11 @@ static bool graph_init(struct _graph *self, uint32_t obj_size)
|
||||
|
||||
// others
|
||||
self->from_matrix = NULL;
|
||||
|
||||
self->bfs = NULL;
|
||||
self->dfs = NULL;
|
||||
|
||||
self->compare = NULL;
|
||||
|
||||
// -------------------- debug --------------------
|
||||
self->print_obj = NULL;
|
||||
self->print = graph_print;
|
||||
|
@ -103,7 +103,7 @@ void test_graph_add_edge(void)
|
||||
// test find_edge
|
||||
TEST_ASSERT_TRUE(graph->find_edge(graph, &data[0], &data[1]));
|
||||
TEST_ASSERT_TRUE(graph->find_edge(graph, &data[1], &data[3]));
|
||||
// TEST_ASSERT_TRUE(graph->find_edge(graph, &data[6], &data[5])); // undirected graph
|
||||
TEST_ASSERT_TRUE(graph->find_edge(graph, &data[6], &data[5])); // undirected graph
|
||||
TEST_ASSERT_FALSE(graph->find_edge(graph, &data[4], &data[3]));
|
||||
TEST_ASSERT_FALSE(graph->find_edge(graph, &data[0], &temp));
|
||||
|
||||
@ -149,7 +149,7 @@ void test_graph_iter(void)
|
||||
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[7], &data[6], 87));
|
||||
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[8], &data[2], 92));
|
||||
TEST_ASSERT_FALSE(graph->add_edge(graph, &temp, &data[1], 0));
|
||||
graph->print(graph);
|
||||
// graph->print(graph);
|
||||
|
||||
iterator_t iter_vertex = NULL;
|
||||
|
||||
@ -158,18 +158,16 @@ 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);
|
||||
graph->print_obj(&temp);
|
||||
printf("temp = %d\n", temp);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
graph_free(&graph);
|
||||
TEST_ASSERT_NULL(graph);
|
||||
|
Loading…
Reference in New Issue
Block a user