图的广度和深度优先遍历
2014-02-08 08:50
323 查看
#include <iostream> #include <deque> using namespace std; typedef char VextexType; typedef int EdgeType; #define VexNum 5 struct EdgeNode; struct EdgeNode { VextexType HeadName; VextexType TailName; EdgeType weight; EdgeNode *VexOut; EdgeNode *VexIn; }; typedef struct { VextexType name; EdgeNode *VexOutlink; EdgeNode *VexInlink; }VexNode; VexNode adjList[VexNum]; bool visit[VexNum]; void creatGraph() { VextexType vextemp; EdgeType edgetemp; char a[]={'A','B','C','D','E'}; int b[] = { 0,2, 0, 54, 0, 0, 0, 0, 0, 13, 9 ,0 ,0 ,0 ,0, 0 ,0, 5, 0, 8, 0 ,0 ,0 ,0 ,0}; //input n vextex for ( int i=0; i<VexNum ; ++i ){ cin>>vextemp; //vextemp = a[i]; adjList[i].name = vextemp; adjList[i].VexOutlink = NULL; adjList[i].VexInlink = NULL; } for ( int i=0; i<VexNum*VexNum; ++i ){ cin>>edgetemp; //edgetemp = b[i]; if ( edgetemp==0 ){ continue; } EdgeNode *pEdge = new EdgeNode; pEdge->HeadName = adjList[i/VexNum].name; pEdge->TailName = adjList[i%VexNum].name; pEdge->weight = edgetemp; pEdge->VexOut = adjList[i/VexNum].VexOutlink; if ( pEdge->VexOut ){ while ( pEdge->VexOut->VexOut ){ pEdge->VexOut =pEdge->VexOut->VexOut; } pEdge->VexOut->VexOut = pEdge; pEdge->VexOut=NULL; } else { adjList[i/VexNum].VexOutlink = pEdge; pEdge->VexOut = NULL; } } for ( int i=0 ;i<VexNum ;++i ){ EdgeNode **pInLink = &adjList[i].VexInlink; for ( int j=0; j<VexNum; ++j ){ if ( i==j ){ continue; } EdgeNode *p = adjList[j].VexOutlink; while ( p ){ if ( p->TailName != adjList[i].name ){ p = p->VexOut; continue; } *pInLink = p; pInLink = &p->VexIn; p = p->VexOut; } } *pInLink = NULL; } } void destroyGrape() { for ( int i=0; i<VexNum ;++i ){ EdgeNode *p = adjList[i].VexOutlink; EdgeNode *q; while ( p ){ q = p; p = p->VexOut; delete q; } } } void printGrape() { for ( int i=0; i<VexNum; ++i ){ cout<<adjList[i].name<<"-->"; EdgeNode *p = adjList[i].VexOutlink; while ( p ){ cout<<"("<<p->HeadName<<","<<p->TailName<<","<<p->weight<<")"; p = p->VexOut; } cout<<endl; p = adjList[i].VexInlink; cout<<adjList[i].name<<"-->"; while ( p ){ cout<<"("<<p->HeadName<<","<<p->TailName<<","<<p->weight<<")"; p = p->VexIn; } cout<<endl; } } void BFS(int i) { cout<<adjList[i].name; visit[i]=true; deque<VexNode*> s; VexNode *tempVex; EdgeNode *tempEdge; int vexPos; s.push_back(&adjList[i]); while ( !s.empty() ){ tempVex = s.front(); s.pop_front(); if ( !tempVex->VexOutlink ){ continue; } tempEdge = tempVex->VexOutlink; while ( tempEdge ){ vexPos = tempEdge->TailName-'A'; tempEdge = tempEdge->VexOut; if ( visit[vexPos] ){ continue; } cout<<"------>"; cout<<adjList[vexPos].name; visit[vexPos]= true; s.push_back(&adjList[vexPos]); } } } void BFSTraverse() { //memset(visit,0,sizeof(bool)*VexNum); for ( int i=0; i<VexNum; ++i ){ visit[i]=false; } for ( int i=0; i<VexNum; ++i ){ if ( visit[i] ){ continue; } BFS(i); cout<<endl; } } void DFS(int i,bool flage) { if ( visit[i] ){ return; } if ( flage ){ cout<<"-->"; } cout<<adjList[i].name; visit[i] = true; if ( !adjList[i].VexOutlink ){ return ; } int Pos =adjList[i].VexOutlink->TailName-'A'; DFS(Pos,true); } void DFSTraserve() { for ( int i=0; i<VexNum; ++i ){ visit[i]=false; } for ( int i=0; i<VexNum; ++i ){ if ( visit[i] ){ continue; } DFS(i,false); cout<<endl; } } int main() { creatGraph(); printGrape(); BFSTraverse(); DFSTraserve(); destroyGrape(); }
相关文章推荐
- 邻接矩阵深度优先和广度优先遍历(DFS和BFS)
- 【图】图的邻接矩阵存储和广度、深度优先遍历
- 图的理解:深度优先和广度优先遍历及其 Java 实现
- 图的遍历(深度优先遍历和广度优先遍历)
- 泛型应用--图的深度(广度)优先遍历.成语接龙例子,含代码下载.
- 数据结构:图的遍历--深度优先、广度优先
- 图的邻接矩阵生成算法,深度优先遍历算法,广度优先遍历算法,Prime算法。):
- 个人模板 图的深度,广度优先遍历
- 图——广度优先遍历和深度优先遍历——邻接表表示法
- 二叉树与图的深度优先和广度优先遍历
- 深度优先遍历和广度优先遍历思考
- 图的遍历:深度优先、广度优先
- java 图的邻接矩阵表示,深度优先遍历,广度优先遍历
- 【数据结构】邻接表表示法的图的深度广度优先遍历递归和非递归遍历
- python深度优先与广度优先的遍历算法区别
- 百度面试题——简要说明树的深度优先、广度优先遍历算法,及非递归实现的特点
- 二叉树的深度优先和广度优先遍历
- 无向图的邻接矩阵,深度优先遍历和广度优先遍历的递归与非递归算法
- (百度笔试)简要说明树的深度优先、广度优先遍历算法,及非递归实现的特点
- 图的邻接矩阵存储表示 深度优先和广度优先遍历