二、广度优先算法(BFS)
#include
#include
#define MAX_VEXTEX_NUM 9
#define ARC_NUM 12
#define MAX_QUEUEMEM (MAX_VEXTEX_NUM+1)
typedef struct{
int
queuemem[MAX_QUEUEMEM];
int header;
int rear;
}QUEUE;
int GraphEdge[ARC_NUM * 2][2] =
{{0,1},{1,0},{1,2},{2,1},{2,3},{3,2},{3,4},
{4,3},{4,5},{5,4},{5,0},{0,5},{0,6},{6,0},{6,8},
{8,6},{6,7},{7,6},{7,8},{8,7},{7,3},{3,7},{8,4},{4,8}};
int
visited[MAX_VEXTEX_NUM]={0,0,0,0,0,0,0,0,0};
typedef struct ArcNode{
int
adjvex;
struct ArcNode *
nextarc;
}ArcNode;
typedef struct VNode{
int data;
struct ArcNode *
firstarc;
}VNode,AdjList[MAX_VEXTEX_NUM];
typedef struct {
VNode vertices[MAX_VEXTEX_NUM];
int vexnum;
int arcnum;
}ALGraph;
void InitQueue(QUEUE *queue)
{
queue->header = 0;
queue->rear = 0;
}
void EnQueue(QUEUE *queue,int v)
{
queue->queuemem[queue->rear] = v;
queue->rear++;
}
int DelQueue(QUEUE *queue)
{
return queue->queuemem[queue->header++];
}
int EmptyQueue(QUEUE *queue)
{
if(queue->header
== queue->rear)
return 1;
return 0;
}
void CreateGraph(ALGraph * alGraph)
{
int i,j;
ArcNode * newnode;
ArcNode * vexNode;
alGraph->vexnum = MAX_VEXTEX_NUM;
alGraph->arcnum = ARC_NUM;
for(i=0;i
{
alGraph->vertices[i].data = i;
alGraph->vertices[i].firstarc =
NULL;
}
for(j=0;j<2*ARC_NUM;j++)
{
i = GraphEdge[j][0];
if(alGraph->vertices[i].firstarc==NULL)
{
newnode = ( ArcNode *
) malloc (sizeof(ArcNode));
newnode->adjvex =
GraphEdge[j][1];
newnode->nextarc =
NULL;
alGraph->vertices[i].firstarc
= newnode;
}
else
{
vexNode =
alGraph->vertices[i].firstarc;
while(vexNode->nextarc
!= NULL)
{
vexNode = vexNode->nextarc;
}
newnode = ( ArcNode *
) malloc (sizeof(ArcNode));
newnode->adjvex =
GraphEdge[j][1];
newnode->nextarc =
NULL;
vexNode->nextarc =
newnode;
}
}
}
void OutputGraph(ALGraph * alGraph)
{
int i;
ArcNode * vexNode;
printf("The graph dedicated by adjacency list
is:\n");
for(i=0;i
{
printf("the header is: [%d] ->
",alGraph->vertices[i].data);
vexNode =
alGraph->vertices[i].firstarc;
while(vexNode != NULL)
{
printf("[%d] ->
",vexNode->adjvex);
vexNode=vexNode->nextarc;
}
printf("[END]\n");
}
}
void BFSTraverse(ALGraph * alGraph)
{
int i;
int w;
ArcNode * vexNode;
QUEUE queue;
InitQueue(&queue);
for(i=0;i
{
visited[i] = 0;
}
printf("\n");
puts("********************************************");
puts("* the function
BFSTraverse will traverse *");
puts("*
the graph by Breadth First Search
*");
puts("********************************************");
puts("the result of BFS is:");
for(i=0;i
{
if(visited[i] == 0)
{
visited[i] = 1;
printf("[%d] -> ",i);
EnQueue(&queue,i);
while(!EmptyQueue(&queue))
{
w = DelQueue(&queue);
vexNode = alGraph->vertices[w].firstarc;
while(vexNode != NULL)
{
w = vexNode->adjvex;
if(visited[w]==0)
{
visited[w] = 1;
printf("[%d] -> ",w);
EnQueue(&queue,w);
}
vexNode = vexNode->nextarc;
}
}
}
}
printf("[end]\n");
}
int main()
{
ALGraph alGraph;
CreateGraph(&alGraph);
OutputGraph(&alGraph);
BFSTraverse(&alGraph);
return 0;
}