图之图的广度优先遍历

广度优先遍历连通图的一种遍历策略。其基本思想如下:

1、从图中某个顶点V0出发,并访问此顶点;

2、从V0出发,访问V0的各个未曾访问的邻接点W1,W2,…,Wk;然后,依次从W1,W2,…,Wk出发访问各自未被访问的邻接点;

3、重复步骤2,直到全部顶点都被访问为止。


例如下图中:
图之图的广度优先遍历_第1张图片
1.从0开始,首先找到0的关联顶点3,4
2.由3出发,找到1,2;由4出发,找到1,但是1已经遍历过,所以忽略。
3.由1出发,没有关联顶点;由2出发,没有关联顶点。
所以最后顺序是0,3,4,1,2
c语言实现如下:(使用邻接矩阵存储)
#include 
#include 
#define VERTEXNUM 5

//队列的元素
typedef struct qElement{
        int value;
        struct qElement* pre;
        struct qElement* next;
}st_qElement;

//队列的前端和后端,最后一个入队列的元素为rear,第一个出队列的元素为front
st_qElement* front = NULL;
st_qElement* rear = NULL;

void createGraph(int (*edge)[VERTEXNUM], int start, int end);
void displayGraph(int (*edge)[VERTEXNUM]);
void BFT(int (*edge)[VERTEXNUM],int* vertexStatusArr);
void BFTcore(int (*edge)[VERTEXNUM],int i,int* vertexStatusArr);
void putQueue(int vertex);
int* getQueue();
void putRelatedInQueue(int (*edge)[VERTEXNUM], int vertex);

int main(void){
		//动态创建存放边的二维数组 
        int (*edge)[VERTEXNUM] = (int (*)[VERTEXNUM])malloc(sizeof(int)*VERTEXNUM*VERTEXNUM);
        int i,j;
        for(i=0;ivalue = vertex;
        qe->next = NULL;
        qe->pre = NULL;
        if(front == NULL || rear == NULL){
                front = rear = qe;
        }else{
                rear->next = qe;
                qe->pre = rear;
                rear = qe;
        }
}

//从队列中获取一个元素
int* getQueue(){
        if(front == NULL || rear == NULL){
                return NULL;
        }else{
                int* res = (int*)malloc(sizeof(int));
                *res = front->value;

                st_qElement* p = front;
                front = front->next;
                if(front == NULL){
                        rear = front;
                }else{
                        front->pre = NULL;
                }
                free(p);
                p = NULL;
                return res;
        }
}
//将顶点关联的元素放到队列中
void putRelatedInQueue(int (*edge)[VERTEXNUM], int vertex){
        int i;
        for(i=0;i


c语言实现如下:(使用邻接表存储)
#include 
#include 
#define VERTEXNUM 5

//存放顶点的邻接表元素
typedef struct edge{
        int vertex;
        struct edge* next;
}st_edge;

//队列的元素
typedef struct qElement{
        int value;
        struct qElement* pre;
        struct qElement* next;
}st_qElement;

//队列的前端和后端,最后一个入队列的元素为rear,第一个出队列的元素为front
st_qElement* front = NULL;
st_qElement* rear = NULL;

void createGraph(st_edge** edge, int start, int end);
void displayGraph(st_edge** edge);
void delGraph(st_edge** edge);
void BFT(st_edge** edge,int* vertexStatusArr);
void BFTcore(st_edge** edge,int i,int* vertexStatusArr);
void putQueue(int vertex);
int* getQueue();
void putRelatedInQueue(st_edge** edge, int vertex);

int main(void){
		//动态创建存放边的指针数组 
        st_edge** edge = (st_edge**)malloc(sizeof(st_edge*)*VERTEXNUM);
        int i;
        for(i=0;ivertex = end;
        newedge->next = NULL;
        edge = edge + start;
        while(*edge != NULL){
                edge = &((*edge)->next);
        }
        *edge = newedge;
}

//打印存储的图
void displayGraph(st_edge** edge){
        int i;
        st_edge* p;
        for(i=0;ivertex);
                        p = p->next;
                }
                printf("\n");
        }
}

//释放邻接表占用的内存
void delGraph(st_edge** edge){
        int i;
        st_edge* p;
        st_edge* del;
        for(i=0;inext;
                        free(del);
                }
                edge[i] = NULL;
        }
        free(edge);
}

//广度优先遍历
void BFT(st_edge** edge,int* vertexStatusArr){
        printf("start BFT graph:\n");
        int i;
        for(i=0;ivalue = vertex;
        qe->next = NULL;
        qe->pre = NULL;
        if(front == NULL || rear == NULL){
                front = rear = qe;
        }else{
                rear->next = qe;
                qe->pre = rear;
                rear = qe;
        }
}

//从队列中获取一个元素
int* getQueue(){
        if(front == NULL || rear == NULL){
                return NULL;
        }else{
                int* res = (int*)malloc(sizeof(int));
                *res = front->value;

                st_qElement* p = front;
                front = front->next;
                if(front == NULL){
                        rear = front;
                }else{
                        front->pre = NULL;
                }
                free(p);
                p = NULL;
                return res;
        }
}

//将顶点关联的元素放到队列中
void putRelatedInQueue(st_edge** edge, int vertex){
        st_edge* p = *(edge+vertex);
        while(p != NULL){
                putQueue(p->vertex);
                p = p->next;
        }
}

你可能感兴趣的:(算法,算法系列)