static void dfs_recursive(tl_graph* graph, int v, int *visited, graph_printf* p_func) { int i = 0; p_func(graph->v[v]); visited[v] = 1; printf(", "); for(i=0; i<LinkList_Length(graph->la[v]); i++) { t_list_node* node = (t_list_node*)LinkList_Get(graph->la[v], i); if(!visited[node->v]) { dfs_recursive(graph, node->v, visited, p_func); } } } void DFS_traverse(LGraph* graph, int v, graph_printf* p_func) { tl_graph* t_graph = (tl_graph*)graph; int *visited = NULL; int ok = 1; ok = (ok) && (t_graph != NULL) && (p_func != NULL); ok = (ok) && (0 <= v) && (v < t_graph->count); /*借鉴其申请数组空间的方式*/ ok = (ok) && ((visited = (int*)calloc(t_graph->count, sizeof(int))) != NULL); if(ok) { int i = 0; dfs_recursive(t_graph, v, visited, p_func); for(i=0; i<t_graph->count; i++) { if(!visited[i]) { dfs_recursive(t_graph, i, visited, p_func); } } printf("\n"); } free(visited); } static void bfs_recursive(tl_graph* graph, int v, int visited[], graph_printf* p_func) { LinkQueue* queue = LinkQueue_Create(); if(queue != NULL) { int i = 0; t_list_node* node = NULL; visited[v] = 1; /*将在图中下标为v的顶点放入队列, 又vk可能为0,而在实现队列时,传入队列的参数不能为零, 则加上graph->v的基地址(graph->v只是一个无用的地址),从而避免v为0时的错误。 在取出队列元素是应减去该基地址*/ LinkQueue_Append(queue, graph->v + v); while( LinkQueue_Length(queue) > 0 ) { /*传入的是标号地址,传出自然也是标号地址*/ v = (l_vertex**)LinkQueue_Retrieve(queue) - graph->v; p_func(graph->v[v]); printf(", "); for(i=0; i<LinkList_Length(graph->la[v]); i++) { node = (t_list_node*)LinkList_Get(graph->la[v], i); if(!visited[node->v]) { LinkQueue_Append(queue, graph->v + node->v); visited[node->v] = 1; } } } } LinkQueue_Destroy(queue); } void BFS_traverse(LGraph* graph, int v, graph_printf* p_func) { tl_graph* t_graph = (tl_graph*)graph; int *visited = NULL; int ok = 1; ok = (ok) && (t_graph != NULL) && (p_func != NULL); ok = (ok) && (0 <= v) && (v < t_graph->count); /*借鉴其申请数组空间的方式*/ ok = (ok) && ((visited = (int*)calloc(t_graph->count, sizeof(int))) != NULL); if(ok) { int i = 0; bfs_recursive(t_graph, v, visited, p_func); for(i=0; i<t_graph->count; i++) { if(!visited[i]) { bfs_recursive(t_graph, i, visited, p_func); } } printf("\n"); } free(visited); }
/*参数说明: graph:指向图头结点的指针 v:顶点在整个顶点数组下的标号 visited:标记访问的顶点 p_func:打印结点信息函数 */ static void dfs_recursive(tm_graph* graph, int v, int *visited, graph_printf* p_func) { int i = 0; p_func(graph->v[v]); visited[v] = 1; printf(", "); for(i=0; i<graph->count; i++) { if((graph->matrix[v][i] != 0) && (!visited[i])) { dfs_recursive(graph, i, visited, p_func); } } } void DFS_traverse(MGraph* graph, int v, graph_printf* p_func) { tm_graph* t_graph = (tm_graph*)graph; int *visited = NULL; int ok = 1; ok = (ok) && (t_graph != NULL) && (p_func != NULL); ok = (ok) && (0 <= v) && (v < t_graph->count); /*借鉴其申请数组空间的方式*/ ok = (ok) && ((visited = (int*)calloc(t_graph->count, sizeof(int))) != NULL); if(ok) { int i = 0; dfs_recursive(t_graph, v, visited, p_func); for(i=0; i<t_graph->count; i++) { if(!visited[i]) { dfs_recursive(t_graph, i, visited, p_func); } } printf("\n"); } free(visited); } static void bfs_recursive(tm_graph* graph, int v, int visited[], graph_printf* p_func) { LinkQueue* queue = LinkQueue_Create(); if(queue != NULL) { int i = 0; visited[v] = 1; LinkQueue_Append(queue, graph->v + v); while( LinkQueue_Length(queue) > 0 ) { v = (m_vertex**)LinkQueue_Retrieve(queue) - graph->v; p_func(graph->v[v]); printf(", "); for(i=0; i<graph->count; i++) { if((graph->matrix[v][i]) && (!visited[i])) { LinkQueue_Append(queue, graph->v + i); visited[i] = 1; } } } } LinkQueue_Destroy(queue); } void BFS_traverse(MGraph* graph, int v, graph_printf* p_func) { tm_graph* t_graph = (tm_graph*)graph; int *visited = NULL; int ok = 1; ok = (ok) && (t_graph != NULL) && (p_func != NULL); ok = (ok) && (0 <= v) && (v < t_graph->count); /*借鉴其申请数组空间的方式*/ ok = (ok) && ((visited = (int*)calloc(t_graph->count, sizeof(int))) != NULL); if(ok) { int i = 0; bfs_recursive(t_graph, v, visited, p_func); for(i=0; i<t_graph->count; i++) { if(!visited[i]) { bfs_recursive(t_graph, i, visited, p_func); } } printf("\n"); } free(visited); }
原文地址:http://blog.csdn.net/u011467781/article/details/45271923