5.3.2连通图的广度优先搜索

#include "stdafx.h"
#include 
<iostream.h>
#include 
<malloc.h>

//------------------------------------------------使用顺序表建立的队列结构------------------------------
int const maxsize=20;
typedef 
struct cycqueue
{
    
int data[maxsize];
    
int front;
    
int rear;
}
CycqueueTP;
//初始化队列,队列结构为保留一个结点作为标志,即front所在的点为标志点,不存储内容
void InitCycQueue(CycqueueTP & sq)
{
    sq.front
=0;
    sq.rear
=0;
}

//入队,需要使用模除,用以实现循环
int EnCycQueue(CycqueueTP & sq,int value)
{
    
if ((sq.rear+1)%maxsize==sq.front)
    
{
        
//溢出
        return 0;
    }

    sq.rear
=(sq.rear+1)%maxsize;
    sq.data[sq.rear]
=value;
    
return 1;
}

//出队
int OutCycQueue(CycqueueTP & sq,int & value)
{
    
if (sq.front==sq.rear)
        
return 0;        //空队列
    sq.front=(sq.front+1)%maxsize;
    value
=sq.data[sq.front];
    
return 1;
}

//判断是否为空队列
int EmptyCycQueue(CycqueueTP & sq)
{
    
if (sq.front==sq.rear)
        
return 1;
    
else
        
return 0;
}

//-------------------------------------------------------------------------------------------
//邻接表,存储P114的5-10
int const vnum = 8;

//单链表定义
typedef struct arcnode
{
    
int adjvex;                                            //与其相连的下个结点的编号
    struct arcnode * nextarc;                            //指向下一个结点的指针
}
ArcNodeTp;

//头结点定义
typedef struct vexnode
{
    
int vertex;                                            //结点编号
    ArcNodeTp * firstarc;                                //指向单链表的指针
}
AdjList[vnum];

//对此图的定义
typedef struct graph
{
    AdjList adjlist;                                    
//头结点    
    int vexnum;                                            //结点数
    int arcnum;                                            //边数
}
GraphTp;

//建立一个新的结点
ArcNodeTp * CreateNewNode(int adjvex)
{
    ArcNodeTp 
* node=(ArcNodeTp *)malloc(sizeof(ArcNodeTp));
    node
->adjvex=adjvex;
    node
->nextarc=NULL;

    
return node;
}


//建立P114的5-10的无向图
void CreateGrapth(GraphTp & graph)
{
    graph.vexnum
=7;
    graph.arcnum
=8;

    ArcNodeTp 
* p=NULL;
    
int index=0;
    
//建立结点
    index=0;
    graph.adjlist[index].vertex
=index;
    p
=CreateNewNode(1);
        
//修改头结点
    graph.adjlist[index].firstarc=p;
        
//指向下一个结点
    p->nextarc=CreateNewNode(2);

    index
=1;
    graph.adjlist[index].vertex
=index;
    p
=CreateNewNode(3);
    graph.adjlist[index].firstarc
=p;
    p
->nextarc=CreateNewNode(4);

    index
=2;
    graph.adjlist[index].vertex
=index;
    p
=CreateNewNode(5);
    graph.adjlist[index].firstarc
=p;
    p
->nextarc=CreateNewNode(6);

    index
=3;
    graph.adjlist[index].vertex
=index;
    p
=CreateNewNode(1);
    graph.adjlist[index].firstarc
=p;
    p
->nextarc=CreateNewNode(7);

    index
=4;
    graph.adjlist[index].vertex
=index;
    p
=CreateNewNode(1);
    graph.adjlist[index].firstarc
=p;
    p
->nextarc=CreateNewNode(7);

    index
=5;
    graph.adjlist[index].vertex
=index;
    p
=CreateNewNode(2);
    graph.adjlist[index].firstarc
=p;
    p
->nextarc=CreateNewNode(6);

    index
=6;
    graph.adjlist[index].vertex
=index;
    p
=CreateNewNode(2);
    graph.adjlist[index].firstarc
=p;
    p
->nextarc=CreateNewNode(5);

    index
=7;
    graph.adjlist[index].vertex
=index;
    p
=CreateNewNode(3);
    graph.adjlist[index].firstarc
=p;
    p
->nextarc=CreateNewNode(4);
}


//广度优先算法
void Bfs(GraphTp graph,int value,int visited[])
{
    CycqueueTP queue;
    InitCycQueue(queue);
    cout
<<"访问节点"<<value<<endl;
    visited[value]
=1;
    EnCycQueue(queue,value);
    
while(!EmptyCycQueue(queue))
    
{
        
int v;
        OutCycQueue(queue,v);
        arcnode 
* node=graph.adjlist[v].firstarc;
        
while(node!=NULL)
        
{
            
if (!visited[node->adjvex])
            
{
                cout
<<"访问节点"<<node->adjvex<<endl;
                visited[node
->adjvex]=1;
                EnCycQueue(queue,node
->adjvex);
            }

            node
=node->nextarc;
        }

    }

}


int main(int argc, char* argv[])
{
    
int visited[vnum];
    
for(int i=0;i<vnum;i++)
    
{
        visited[i]
=0;
    }

    GraphTp graph;
    CreateGrapth(graph);
    Bfs(graph,
0,visited);
    
return 0;
}

    
posted @ 2007-07-28 11:29  吴东雷  阅读(481)  评论(0编辑  收藏  举报