Compare commits

...

4 Commits

3 changed files with 93 additions and 88 deletions

View File

@ -42,8 +42,9 @@ struct _graph_node
{ {
void* obj; void* obj;
struct _graph_node* next; struct _graph_node* next;
struct _graph_edge* edgehead;
bool visited; bool visited;
struct _graph_edge* edgehead;
}; };
struct _graph struct _graph
@ -77,22 +78,16 @@ struct _graph
bool (*del_edge)(struct _graph* self, void* from, void* to); bool (*del_edge)(struct _graph* self, void* from, void* to);
bool (*find_edge)(struct _graph* self, void* from, void* to); bool (*find_edge)(struct _graph* self, void* from, void* to);
// traverse bool (*empty)(struct _graph* self);
bool (*dfs)(struct _graph* self, uint32_t idx); bool (*full)(struct _graph* self);
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 // base
uint32_t(*size)(struct _graph* self); uint32_t(*size)(struct _graph* self);
uint32_t(*capacity)(struct _graph* self); uint32_t(*capacity)(struct _graph* self);
bool (*clear)(struct _graph* self); bool (*clear)(struct _graph* self);
bool (*empty)(struct _graph* self);
bool (*full)(struct _graph* self);
// iter // iter
iterator_t (*iter)(struct _graph* self, enum _graph_search search_type, void *obj); iterator_t (*iter)(struct _graph* self, enum _graph_search search, 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

@ -455,7 +455,7 @@ static void graph_print(struct _graph *self)
return; return;
} }
printf("vertex : \n"); printf("\nvertex : \n");
struct _graph_node *cur = self->_head->next; struct _graph_node *cur = self->_head->next;
while (cur != NULL) while (cur != NULL)
{ {
@ -470,23 +470,21 @@ static void graph_print(struct _graph *self)
{ {
if (cur->edgehead != NULL) if (cur->edgehead != NULL)
{ {
// struct _graph_edge* edge = cur->edgehead->next;
struct _graph_edge *edge = cur->edgehead; struct _graph_edge *edge = cur->edgehead;
while (edge != NULL) while (edge != NULL)
{ {
struct _graph_node *target = (struct _graph_node *)edge->target; struct _graph_node *target = (struct _graph_node *)edge->target;
printf("from "); // printf("from ");
self->print_obj(cur->obj); self->print_obj(cur->obj);
printf(" to "); // printf(" to ");
printf("--> ");
self->print_obj(target->obj); self->print_obj(target->obj);
printf(": %d \n", edge->weight); printf(": %d \n", edge->weight);
edge = edge->next; edge = edge->next;
} }
} }
// self->print_obj(cur->obj);
cur = cur->next; cur = cur->next;
} }
printf("\n"); printf("\n");
@ -776,6 +774,54 @@ 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_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) iterator_t graph_iter(struct _graph *self, enum _graph_search search_type, void *start)
{ {
assert(self != NULL); assert(self != NULL);
@ -855,25 +901,24 @@ 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 all unvisited target node and push them to queue
cur_edge = node->edgehead; cur_edge = node->edgehead;
while (cur_edge != NULL) while (cur_edge != NULL)
{ {
@ -881,10 +926,6 @@ const void *graph_iter_next(struct _iterator *iter)
if (target != NULL && target->visited != true) if (target != NULL && target->visited != true)
{ {
queue->push(queue, &target); queue->push(queue, &target);
// self->print_obj(node->obj);
// printf(" -> ");
// self->print_obj(target->obj);
} }
cur_edge = cur_edge->next; cur_edge = cur_edge->next;
} }
@ -901,73 +942,42 @@ const void *graph_iter_next(struct _iterator *iter)
// self->stack->push(self->stack, iter->_cur_node); // self->stack->push(self->stack, iter->_cur_node);
struct _graph_node *cur_node = self->_iter._cur_node; struct _graph_node *cur_node = self->_iter._cur_node;
struct _graph_node *node = NULL; struct _graph_node *node = NULL;
struct _graph_edge *cur_edge = cur_node->edgehead;
struct _graph_node *target = NULL;
stack_t stack = self->stack; stack_t stack = self->stack;
// while (!self->stack->empty(self->stack) || cur_node != NULL) // graph->DFS : similar to the preorder traversal of a tree
do while (!self->stack->empty(self->stack) || 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
{
// 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) if(cur_node == NULL)
{ {
break; cur_node = graph_find_unvisited_vertex(self);
} }
if(cur_node->visited == true)
{
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; iter->_cur_node = cur_node;
printf("--- end1 ---\n");
obj = node->obj; obj = node->obj;
printf("--- end2 ---\n");
} }
break; break;
default: default:
@ -1047,11 +1057,9 @@ static bool graph_init(struct _graph *self, uint32_t obj_size)
// others // others
self->from_matrix = NULL; self->from_matrix = NULL;
self->bfs = NULL;
self->dfs = NULL;
self->compare = NULL; self->compare = NULL;
// -------------------- debug -------------------- // -------------------- debug --------------------
self->print_obj = NULL; self->print_obj = NULL;
self->print = graph_print; self->print = graph_print;

View File

@ -103,7 +103,7 @@ void test_graph_add_edge(void)
// test find_edge // test find_edge
TEST_ASSERT_TRUE(graph->find_edge(graph, &data[0], &data[1])); 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[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[4], &data[3]));
TEST_ASSERT_FALSE(graph->find_edge(graph, &data[0], &temp)); 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[7], &data[6], 87));
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[8], &data[2], 92)); TEST_ASSERT_TRUE(graph->add_edge(graph, &data[8], &data[2], 92));
TEST_ASSERT_FALSE(graph->add_edge(graph, &temp, &data[1], 0)); TEST_ASSERT_FALSE(graph->add_edge(graph, &temp, &data[1], 0));
// graph->print(graph); graph->print(graph);
iterator_t iter_vertex = NULL; iterator_t iter_vertex = NULL;
@ -158,16 +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);
} }
printf("\n");
graph_free(&graph); graph_free(&graph);
TEST_ASSERT_NULL(graph); TEST_ASSERT_NULL(graph);