DFS、BFS搜索+题目
来源:互联网 发布:心蓝软件 编辑:程序博客网 时间:2024/05/02 04:29
这篇博主写的是图的深搜
图的DFS附代码
【转】、
/*图的深度优先遍历出处:一条鱼@博客园 http://www.cnblogs.com/yanlingyin/2011-12-26 */#include <stdlib.h>#include <stdio.h>struct node /* 图顶点结构定义 */{ int vertex; /* 顶点数据信息 */ struct node *nextnode; /* 指下一顶点的指标 */};typedef struct node *graph; /* 图形的结构新型态 */struct node head[9]; /* 图形顶点数组 */int visited[9]; /* 遍历标记数组 *//********************根据已有的信息建立邻接表********************/void creategraph(int node[20][2],int num)/*num指的是图的边数*/{ graph newnode; /*指向新节点的指针定义*/ graph ptr; int from; /* 边的起点 */ int to; /* 边的终点 */ int i; for ( i = 0; i < num; i++ ) /* 读取边线信息,插入邻接表*/ { from = node[i][0]; /* 边线的起点 */ to = node[i][1]; /* 边线的终点 */ /* 建立新顶点 */ newnode = ( graph ) malloc(sizeof(struct node)); newnode->vertex = to; /* 建立顶点内容 */ newnode->nextnode = NULL; /* 设定指标初值 */ ptr = &(head[from]); /* 顶点位置 */ while ( ptr->nextnode != NULL ) /* 遍历至链表尾 */ ptr = ptr->nextnode; /* 下一个顶点 */ ptr->nextnode = newnode; /* 插入节点 */ }}/********************** 图的深度优先搜寻法********************/void dfs(int current){ graph ptr; visited[current] = 1; /* 记录已遍历过 */ printf("vertex[%d]\n",current); /* 输出遍历顶点值 */ ptr = head[current].nextnode; /* 顶点位置 */ while ( ptr != NULL ) /* 遍历至链表尾 */ { if ( visited[ptr->vertex] == 0 ) /* 如过没遍历过 */ dfs(ptr->vertex); /* 递回遍历呼叫 */ ptr = ptr->nextnode; /* 下一个顶点 */ }}/****************************** 主程序******************************/int main(){ graph ptr; int node[20][2] = { {1, 2}, {2, 1}, /* 边线数组 */ {1, 3}, {3, 1}, {1, 4}, {4, 1}, {2, 5}, {5, 2}, {2, 6}, {6, 2}, {3, 7}, {7, 3}, {4, 7}, {4, 4}, {5, 8}, {8, 5}, {6, 7}, {7, 6}, {7, 8}, {8, 7} }; int i; //clrscr(); for ( i = 1; i <= 8; i++ ) /* 顶点数组初始化 */ { head[i].vertex = i; /* 设定顶点值 */ head[i].nextnode = NULL; /* 指针为空 */ visited[i] = 0; /* 设定遍历初始标志 */ } creategraph(node,20); /* 建立邻接表 */ printf("Content of the gragh's ADlist is:\n"); for ( i = 1; i <= 8; i++ ) { printf("vertex%d ->",head[i].vertex); /* 顶点值 */ ptr = head[i].nextnode; /* 顶点位置 */ while ( ptr != NULL ) /* 遍历至链表尾 */ { printf(" %d ",ptr->vertex); /* 印出顶点内容 */ ptr = ptr->nextnode; /* 下一个顶点 */ } printf("\n"); /* 换行 */ } printf("\nThe end of the dfs are:\n"); dfs(1); /* 打印输出遍历过程 */ printf("\n"); /* 换行 */ puts(" Press any key to quit..."); // getch();}
非递归:
- /********************** 图的深度优先搜寻法********************/
- void dfs(node head[9],int current){/*非递归深度优先遍历算法*/
- node* p;
- node* stack[9]; //栈
- int top=-1,vertex;
- printf("%d\n",current);
- visited[current] = 1;
- stack[++top] = head[current].nextnode;
- while(top > -1){
- p = stack[top];
- while(p != NULL){
- vertex = p->vertex;
- if(visited[vertex] == 0){
- printf("%d\n",vertex);
- visited[vertex] = 1;
- stack[++top] = head[vertex].nextnode;
- //printf("%dss\n",stack[top]->vertex);
- break;
- }
- p = p->nextnode;
- }
- if(p == NULL){
- top--;
- }
- }
- }
博主写的BFS 利用队列来写。
http://blog.csdn.net/zhongkeli/article/details/8799647
/********************** 图的广度优先搜寻法********************/void bfs(node head[9], int current) {/*队列广度优先遍历算法*/node* p; int vertex, nextvertex; //记录该顶点,以及下一个顶点printf("%d\n", current); //输出遍历顶点值visited[current] = 1; //记录该顶点已遍历过bqueue.push(current); //当前顶点进队列while (!bqueue.empty()) { //如果队列非空vertex = bqueue.front(); //队列头赋值给vertexbqueue.pop(); //队列头出队列(队列先进先出)p = head[vertex].nextnode; //将该顶点的下一个顶点给pwhile (p != NULL) { //遍历至链表尾nextvertex = p->vertex; //vertex的下一个顶点的值if (visited[nextvertex] == 0) { //如果没有遍历过printf("%d\n", nextvertex); //输出遍历顶点值(是current下的顶点遍历)visited[nextvertex] = 1;//记录已遍历bqueue.push(nextvertex); //该顶点入队列}p = p->nextnode; //下一个顶点}
0 0
- DFS、BFS搜索+题目
- BFS,DFS等搜索题目总结
- [搜索]DFS+BFS
- DFS/BFS 搜索训练
- DFS BFS 搜索基础
- DFS BFS 搜索总结
- 搜索bfs/dfs
- 搜索 (bfs,dfs) 总结
- poj 3083 搜索 DFS,BFS
- 搜索问题:DFS 与 BFS
- poj3083 dfs+bfs搜索迷宫
- DFS 与BFS优先搜索
- 博弈搜索练习-BFS&DFS
- bfs和dfs搜索总结
- 题目1091:棋盘游戏 -- DFS BFS
- BFS和DFS优先搜索算法
- BFS和DFS优先搜索算法
- BFS和DFS优先搜索算法
- 对称排序acm283
- 中缀表达式借用栈转换为后缀表达式
- C++ STL算法系列--copy函数
- Jboss运行WebService报错解决
- C++迭代器
- DFS、BFS搜索+题目
- Activity中 findViewById 获取控件对象正确的代码位置
- 总结三
- LeetCode题解——Verify Preorder Serialization of a Binary Tree
- Android Fragment 深入理解
- [Ubuntu]修改 Ubuntu 源为清华大学
- django 2
- 【cocos2d-x 手游研发小技巧 循环无限滚动的登陆背景】
- 2016"百度之星" - 初赛(Astar Round2A)Sitting in Line