#include <iostream>
using namespace std;
#define Maxsize 100
typedef char VertexType;
typedef int EdgeType;
typedef struct ArcNode{ //存储边
int adjvex;
struct ArcNode *nextarc;
}ArcNode;
typedef struct VNode{
VertexType data;
ArcNode *firstarc;
}VNode;
typedef struct{
VNode adjlist[Maxsize];
int vexnum,arcnum;
}AGraph;
bool visited[Maxsize];
void BFS(AGraph *G,int v){
ArcNode *p;
Queue Q;
InitQueue(Q);
visit(v);
visited[v]=True;
Enqueue(&Q,v);
while(!IsEmpty(Q)){
DeQueue(&Q,v);
p=G->adjlist[v].firstarc;
while(p!=NULL){
if(visited[p->adjvex]==False){
visit(p->adjvex);
visited[p->adjvex]=True;
EnQueue(&Q,p->adjvex);
}
p=p->nextarc;
}
}
}
bool DFS(AGraph *G,int v){
ArcNode *p;
visit(v);
visited[v]=True;
p=G->adjlist[v].firstarc;
while(p!=NULL){
if(visited[p->adjvex]==False)
DFS(G,p->adjvex);
p=p->nextarc;
}
}
利用邻接表完成图的BFS和DFS
猜你喜欢
转载自blog.csdn.net/KK_2018/article/details/109756469
今日推荐
周排行