图(邻接表)的深度、广度优先遍历
来源:互联网 发布:小猫软件scratch下载 编辑:程序博客网 时间:2024/06/08 17:58
/******************************************************************************** **图(邻接表存储)的深度优先和广度优先 ** ********************************************************************************/#include<iostream>#define MaxVertexNum 30/*最大顶点数30*/bool visited[MaxVertexNum];/*区分顶点是否被访问的标志数组,初值为false*/using namespace std;typedef struct node{/*表节点*/int adjvertex;//顶点对应的序号struct node * next;}EdgeNode;typedef int VertexType;typedef struct vnode{/*顶点节点*/VertexType vertex;//顶点域EdgeNode * firstedge;}VertexNode;typedef struct{/*图*/VertexNode adjlist[MaxVertexNum];//邻接表int vertexNum,edgeNum;//顶点数和边数}ALGraph;typedef int DataType;typedef struct{/*队列*/DataType data[MaxVertexNum];//数据域int front,rear;//队头和队尾指针}SeqQueue,* PSeqQueue;void Create_ALGraph(ALGraph * G);/*创建图*/void DFS(ALGraph * G,int v);/*深度优先搜索算法*/void DFStraverse(ALGraph * G);/*深度优先*/void BFS(ALGraph * G,int v);/*广度优先搜索算法*/void BFStraverse(ALGraph * G);/*广度优先*/PSeqQueue Init_SeqQueue(void);/*循环队列初始化*/int Empty_SeqQueue(PSeqQueue Q);/*判断队空*/void In_SeqQueue(PSeqQueue Q,DataType x);/*入队*/void Out_SeqQueue(PSeqQueue Q,DataType * y);/*出队*/int main(){cout<<"创建图:";ALGraph * G=(ALGraph *)malloc(sizeof(ALGraph));Create_ALGraph(G);cout<<"\n深度优先遍历(图):";DFStraverse(G);cout<<"\b\b";cout<<"\n广度优先遍历(图):";BFStraverse(G);cout<<"\b\b";return 0;}/*创建图*/void Create_ALGraph(ALGraph * G){int i,j,k;cout<<"请输入图的顶点数和边数:";cin>>G->vertexNum>>G->edgeNum;cout<<"输入"<<G->vertexNum<<"个顶点的表示形式:";for(i=0;i<G->vertexNum;i++){cin>>G->adjlist[i].vertex;/*顶点是字符还是数字*/G->adjlist[i].firstedge=NULL;/*顶点的边表头指针设为空*/}VertexType v1,v2;EdgeNode * p;for(k=0;k<G->edgeNum;k++){/*根据顶点序号查找指定顶点*/cout<<"读入边(Vi,Vj)起点和终点对应的序号:";cin>>v1>>v2;for(i=0;v1!=G->adjlist[i].vertex;i++);for(j=0;v2!=G->adjlist[j].vertex;j++);p=(EdgeNode *)malloc(sizeof(EdgeNode));/*生成新边表节点*/p->adjvertex=j;/*领节点序号*/p->next=G->adjlist[i].firstedge;/*将新边表节点插入到顶点Vi的链表头部*/G->adjlist[i].firstedge=p;p=(EdgeNode *)malloc(sizeof(EdgeNode));p->adjvertex=i;p->next=G->adjlist[j].firstedge;G->adjlist[j].firstedge=p;}}/*深度优先搜索算法*/void DFS(ALGraph * G,int v){int w;EdgeNode * p;cout<<G->adjlist[v].vertex<<"->";visited[v]=true;/*访问第v个顶点,并把访问标志置true*/for(p=G->adjlist[v].firstedge;p;p=p->next){w=p->adjvertex;if(!visited[w])/*对v尚未访问的邻接顶点w递归调用DFS*/DFS(G,w);}}/*深度优先*/void DFStraverse(ALGraph * G){int v;for(v=0;v<G->vertexNum;v++)visited[v]=false;for(v=0;v<G->vertexNum;v++){if(!visited[v])DFS(G,v);}}/*广度优先搜索算法*/void BFS(ALGraph * G,int v){int u,w;EdgeNode * p;cout<<G->adjlist[v].vertex<<"->";visited[v]=true;/*访问第v个顶点,并把访问标志置true*/PSeqQueue Q=Init_SeqQueue();In_SeqQueue(Q,v);while(!Empty_SeqQueue(Q)){Out_SeqQueue(Q,&u);for(p=G->adjlist[u].firstedge;p;p=p->next){w=p->adjvertex;if(!visited[w]){/*对v尚未访问的邻接顶点w入队列Q*/cout<<G->adjlist[w].vertex<<"->";visited[w]=true;In_SeqQueue(Q,w);}}}}/*广度优先*/void BFStraverse(ALGraph * G){int v;for(v=0;v<G->vertexNum;v++)visited[v]=false;for(v=0;v<G->vertexNum;v++){if(!visited[v])BFS(G,v);}}/*循环队列初始化*/PSeqQueue Init_SeqQueue(void){PSeqQueue Q;Q=(PSeqQueue)malloc(sizeof(SeqQueue));if(Q){Q->front=0;Q->rear=0;}return Q;}/*判断队空*/int Empty_SeqQueue(PSeqQueue Q){if(Q && Q->front==Q->rear)return 1;else return 0;}/*入队*/void In_SeqQueue(PSeqQueue Q,DataType x){if(Q->front == (Q->rear+1)%MaxVertexNum){cout<<"队满不能入队!";return;}else{Q->rear=(Q->rear+1)%MaxVertexNum;Q->data[Q->rear]=x;}}/*出队*/void Out_SeqQueue(PSeqQueue Q,DataType * y){if(Empty_SeqQueue(Q)){cout<<"队空不能出队!";return;}else{Q->front=(Q->front+1)%MaxVertexNum;* y=Q->data[Q->front];}}
0 1
- 图(邻接表)的深度、广度优先遍历
- 邻接表-图的遍历-广度和深度优先遍历
- 图: 广度深度优先遍历(邻接表)
- 无向图的深度优先遍历和广度优先遍历(邻接链表)
- 邻接图的广度优先遍历和深度优先遍历
- 邻接表的深度优先遍历以及广度优先遍历
- 图的深度优先和广度优先遍历算法(用邻接表作为存储结构)
- 图邻接表存储 深度优先和广度优先遍历
- 图算法:1、邻接表实现图的深度优先遍历,广度优先遍历
- 图的邻接表存储 深度优先遍历 广度优先遍历 C语言实现
- 图的邻接表存储 深度优先遍历 广度优先遍历 C语言实现
- 图的邻接表(广度优先遍历,深度优先遍历,最小生成树(Kruskal算法))
- 邻接表实现--图的深度优先遍历DFS和广度优先遍历BFS
- 用邻接表实现图的深度优先遍历、广度优先遍历、最短路径(无权图)
- 基于邻接表储存的图的深度优先和广度优先遍历
- 图的深度、广度优先搜索(邻接表)
- 邻接表存储图,深度和广度优先遍历
- 建立图的邻接表储存并实现深度优先和广度优先遍历
- Makefile经典教程(掌握这些足够)
- 设置mysql的编码为utf8的方法
- MFC调用OpenCV的程序移植问题——静态编译
- android studio
- C++::增加目标码(object code)的大小的操作
- 图(邻接表)的深度、广度优先遍历
- VS2010下 DLL的创建和使用总结
- android 系统回调onSizeChange, onMeasure, onLayout调用时机
- 【黑马程序员】C语言学习笔记(3)-预处理指令
- PHP开发调试环境配置(基于wampserver+Eclipse for PHP Developers )
- 一次memcached的排查
- poj 1986 Distance Queries(LCA离线Tarjan算法)
- TextView和EditText小结
- 没有躲过的坑--This function or variable may be unsafe.