以下是用邻接表存储表示,实现图的广度优先遍历的示例。
用于遍历的无向图如下:
//v0-v7依次为a-h
#include<iostream>
#define MaxVertexNum 6
#define MaxSize 7
using namespace std;
//抽象数据类型
typedef char vertextype;
typedef int datatype;
//队列
typedef struct
{
datatype Q[MaxSize];
int front;
int rear;
}SeqQueue;
//边结点类型
typedef struct edge
{
int mark;
int no;
struct edge *next;
}edgetype;
//点结点类型
typedef struct
{
int mark;
vertextype vertex;
edgetype *firstArc;
}vertexNode;
//图
typedef struct
{
vertexNode vex[MaxVertexNum];
int n,e;
}ALGraph;
ALGraph G;
edgetype *p;//用于遍历边结点
//置空队
void ClearQueue(SeqQueue &QU)
{
QU.front=0;
QU.rear=0;
}
//入队,使用循环队列
void EnQueue(SeqQueue &QU,datatype x)
{
//参数检查
if(QU.front==(QU.rear+1)%MaxSize)
{
cout<<"overflow!"<<endl;
return;
}
QU.Q[QU.rear]=x;
QU.rear=(QU.rear+1)%MaxSize;
}
//出队
void DeQueue(SeqQueue &QU,datatype &x)
{
//参数检查
if(QU.rear==QU.front)
{
cout<<"underflow!"<<endl;
return;
}
x=QU.Q[QU.front];
QU.front=(QU.front+1)%MaxSize;
}
//判断队是否为空,为空则返回1,不为空则返回0
int QueueEmpty(SeqQueue &QU)
{
if(QU.rear==QU.front)
return 1;
return 0;
}
//构造无向图邻接表
void create(ALGraph &G)
{
G.n=8;
G.vex[0].vertex='a';
G.vex[1].vertex='b';
G.vex[2].vertex='c';
G.vex[3].vertex='d';
G.vex[4].vertex='e';
G.vex[5].vertex='f';
G.vex[6].vertex='g';
G.vex[7].vertex='h';
G.vex[0].mark=G.vex[1].mark=G.vex[2].mark=G.vex[3].mark=G.vex[4].mark=G.vex[5].mark=G.vex[6].mark=G.vex[7].mark=0;
edgetype *e1=new edgetype;
edgetype *e2=new edgetype;
edgetype *e3=new edgetype;
edgetype *e4=new edgetype;
edgetype *e5=new edgetype;
edgetype *e6=new edgetype;
edgetype *e7=new edgetype;
edgetype *e8=new edgetype;
edgetype *e9=new edgetype;
edgetype *e10=new edgetype;
edgetype *e11=new edgetype;
edgetype *e12=new edgetype;
edgetype *e13=new edgetype;
edgetype *e14=new edgetype;
edgetype *e15=new edgetype;
edgetype *e16=new edgetype;
edgetype *e17=new edgetype;
edgetype *e18=new edgetype;
edgetype *e19=new edgetype;
edgetype *e20=new edgetype;
e1->mark=e2->mark=e3->mark=e4->mark=e5->mark=0;
e6->mark=e7->mark=e8->mark=e9->mark=e10->mark=0;
e11->mark=e12->mark=e13->mark=e14->mark=e15->mark=0;
e16->mark=e17->mark=e18->mark=e19->mark=e20->mark=0;
e1->no=1;
e2->no=2;
e3->no=0;
e4->no=3;
e5->no=4;
e6->no=0;
e7->no=5;
e8->no=6;
e9->no=1;
e10->no=7;
e11->no=1;
e12->no=7;
e13->no=2;
e14->no=7;
e15->no=2;
e16->no=7;
e17->no=3;
e18->no=4;
e19->no=5;
e20->no=6;
G.vex[0].firstArc=e1;
e1->next=e2;
e2->next=NULL;
G.vex[1].firstArc=e3;
e3->next=e4;
e4->next=e5;
e5->next=NULL;
G.vex[2].firstArc=e6;
e6->next=e7;
e7->next=e8;
e8->next=NULL;
G.vex[3].firstArc=e9;
e9->next=e10;
e10->next=NULL;
G.vex[4].firstArc=e11;
e11->next=e12;
e12->next=NULL;
G.vex[5].firstArc=e13;
e13->next=e14;
e14->next=NULL;
G.vex[6].firstArc=e15;
e15->next=e16;
e16->next=NULL;
G.vex[7].firstArc=e17;
e17->next=e18;
e18->next=e19;
e19->next=e20;
e20->next=NULL;
}
//图的广度优先遍历
void BFS(ALGraph &G,int i)
{
edgetype *p;
int k;
SeqQueue QU;
ClearQueue(QU);
G.vex[i].mark=1;
EnQueue(QU,i);
while(!QueueEmpty(QU))
{
DeQueue(QU,k);
cout<<(char)G.vex[k].vertex<<endl;
p=G.vex[k].firstArc;
while(p!=NULL)
{
if(G.vex[p->no].mark==0)
{
G.vex[p->no].mark=1;
p->mark=1;
EnQueue(QU,p->no);
}
p=p->next;
}
}
}
//广度优先遍历图的算法
void BFS_Component(ALGraph &G)
{
int i;
for(i=0;i<8;i++)
if(G.vex[i].mark==0)
BFS(G,i);
cout<<endl;
}
//测试函数
int main()
{
create(G);
BFS_Component(G);
return 0;
}