广度优先搜索算法—BFS
来源:互联网 发布:js清除history 编辑:程序博客网 时间:2024/05/22 06:51
广度优先搜索算法—BFS
图的广度优先搜索遍历类似于树的按层次遍历,其基本思想是从图中的某个顶点
邻接表的建立过程可参考(http://blog.csdn.net/dear_mr/article/details/70551094),这里就不重复描述了。
BFS算法:
void BFS(Graph graph, int begin) { int value, i; printf("%c ", graph[begin].data); visited[begin] = 1; CSeQueue *cs = NULL; initSeQueue(&cs); inSeQueue(cs, begin); while(isEmpty(cs)) { value = outSeQueue(cs); for(i = firstVertex(graph, value); i >= 0; i = nextVertex(graph, value, i)) { if(!visited[i]) { printf("%c ", graph[i].data); visited[i] = 1; inSeQueue(cs, i); } } } destorySeQueue(&cs);}
void BFSTravel(Graph graph, int begin) { int i; BFS(graph, begin); for(i = 0; i < Num; i++) { if(!visited[i]) { BFS(graph, i); } }}
关于firstVertex、nextVertex这两个函数,可参考(http://blog.csdn.net/dear_mr/article/details/70551094)
循环队列的代码:
#ifndef _CSEQUEUE_H_#define _CSEQUEUE_H_#include<stdio.h>#include<malloc.h> #define MAXSIZE 100#define TRUE 1#define FALSE 0typedef int boolean;typedef int dataType;typedef struct CSeQueue { dataType data[MAXSIZE]; int front, rear;}CSeQueue; void initSeQueue(CSeQueue **cs);boolean inSeQueue(CSeQueue *cs, dataType data);dataType outSeQueue(CSeQueue *cs);void destorySeQueue(CSeQueue **cs);boolean isEmpty(CSeQueue *cs);boolean isFull(CSeQueue *cs);boolean isFull(CSeQueue *cs) { return (cs->rear + 1) % MAXSIZE == cs->front ? TRUE : FALSE; } boolean isEmpty(CSeQueue *cs) { return cs->front == cs->rear ? TRUE : FALSE;}void destorySeQueue(CSeQueue **cs) { if(*cs == NULL) { return; } free(*cs);}dataType outSeQueue(CSeQueue *cs) { if(cs->front == cs->rear) { printf("队列为空.\n"); return FALSE; } cs->front = (cs->front + 1) % MAXSIZE; return cs->data[cs->front];}boolean inSeQueue(CSeQueue *cs, dataType data) { if((cs->rear + 1) % MAXSIZE == cs->front) { printf("队列已满.\n"); return FALSE; } cs->rear = (cs->rear + 1) % MAXSIZE; cs->data[cs->rear] = data;}void initSeQueue(CSeQueue **cs) { if(*cs != NULL) { printf("队列已经初始化过了.\n"); return; } (*cs) = (CSeQueue*)malloc(sizeof(CSeQueue)); (*cs)->front = (*cs)->rear = MAXSIZE-1; //将 front, rear的值改为第一个单元的直接前驱的下标 }#endif
下载地址: http://download.csdn.net/detail/dear_mr/9824116
0 0
- 广度优先搜索算法—BFS
- 算法 BFS广度优先搜索
- 【算法】--广度优先搜索BFS
- 广度优先搜索BFS算法
- 算法系列—广度优先搜索(BFS)
- 广度优先搜索(BFS)算法
- BFS-广度优先搜索算法(图)
- 广度优先搜索算法(BFS)
- 【算法入门】广度/宽度优先搜索(BFS)
- 【算法入门】广度/宽度优先搜索(BFS)
- 【算法入门】广度/宽度优先搜索(BFS)
- 【算法】广度优先搜索(BFS)I
- 【算法】广度优先搜索(BFS)II
- 【算法入门】广度/宽度优先搜索(BFS)
- 【算法入门】广度/宽度优先搜索(BFS)
- 【算法入门】广度/宽度优先搜索(BFS)
- 广度优先搜索(BFS)算法
- 【算法入门】广度优先搜索(BFS)
- Navicat日期时间格式
- 微信自媒体
- Codeforces 472D Design Tutorial: Inverse the Problem【MST+SPFA+思维】
- Android ble setCharactersticNotification() 依然无法收到通知
- java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/项目包名
- 广度优先搜索算法—BFS
- 中序表达式转后序(计算版)
- SRM554 Div1Medium TheBrickTowerMediumDivOne
- 最短路练习9/poj/2240 Arbitrage;正环:floyd或spfa解法;
- DFS水题
- 整理的前端面试题1
- java模拟银行存取钱业务
- bean的实例化方法
- 无缝轮播图片