数据结构实验之图论二:基于邻接表的广度优先搜索遍历

来源:互联网 发布:mac删除文件命令行 编辑:程序博客网 时间:2024/06/01 08:48

数据结构实验之图论二:基于邻接表的广度优先搜索遍历

Time Limit: 1000MS Memory limit: 65536K

题目描述

给定一个无向连通图,顶点编号从0到n-1,用广度优先搜索(BFS)遍历,输出从某个顶点出发的遍历序列。(同一个结点的同层邻接点,节点编号小的优先遍历)

输入

输入第一行为整数n(0< n <100),表示数据的组数。
对于每组数据,第一行是三个整数k,m,t(0<k<100,0<m<(k-1)*k/2,0< t<k),表示有m条边,k个顶点,t为遍历的起始顶点。 
下面的m行,每行是空格隔开的两个整数u,v,表示一条连接u,v顶点的无向边。

输出

输出有n行,对应n组输出,每行为用空格隔开的k个整数,对应一组数据,表示BFS的遍历结果。

示例输入

16 7 00 30 41 41 52 32 43 5

示例输出

0 3 4 2 5 1

提示

用邻接表存储。

来源

 

示例程序

 

  • 提交 
  • 状态

 

#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAX 100typedef int Status;typedef struct VNode{    int data;    struct VNode *next;}VNode;typedef struct{    int vexnum,arcnum;    VNode *vertices[MAX];}ALGraph;Status visited[MAX],count,begin,top;typedef struct QNode{    int data;    struct QNode *next;}QNode,*QueuePtr;typedef struct{    QueuePtr front;    QueuePtr rear;}LinkQueue;Status InitQueue(LinkQueue *Q){    Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));    if(!Q->front)exit(-1);    Q->front->next=NULL;    return 1;}Status EnQueue(LinkQueue *Q,int e){    QueuePtr p;    p=(QueuePtr)malloc(sizeof(QNode));    if(!p)exit(-1);    p->data=e;p->next=NULL;    Q->rear->next=p;    Q->rear=p;    return 1;}Status QueueEmpty(LinkQueue *Q){    if(Q->front==Q->rear)        return 1;    else return 0;}Status DeQueue(LinkQueue *Q){    QueuePtr p;    if(Q->front==Q->rear)return 0;    p=Q->front->next;    top=p->data;    Q->front->next=p->next;    if(Q->rear==p)Q->rear=Q->front;    free(p);    return 1;}int visiT(int v){    if(count==0)        {        printf("%d",v);    count++;        }        else        printf(" %d",v);        return 1;}Status CreatUDG(ALGraph *G){    int i,v1,v2,t;    VNode *p,*q;    scanf("%d%d%d",&G->vexnum,&G->arcnum,&begin);    for(i=0;i<G->vexnum;i++)    {        G->vertices[i]=(VNode *)malloc(sizeof(VNode));        G->vertices[i]->next=NULL;    }    for(i=0;i<G->arcnum;i++)    {        scanf("%d%d",&v1,&v2);        p=(VNode *)malloc(sizeof(VNode));        p->data=v2;        p->next=G->vertices[v1]->next;        G->vertices[v1]->next=p;        q=(VNode *)malloc(sizeof(VNode));        q->data=v1;        q->next=G->vertices[v2]->next;        G->vertices[v2]->next=q;    }    for(i=0;i<G->vexnum;i++)    {        for(p=G->vertices[i]->next;p->next;p=p->next)            for(q=G->vertices[i]->next->next;q;q=q->next)            if(p->data>q->data)            {                t=p->data;p->data=q->data;q->data=t;            }    }    return 1;}void BFSTraverse(ALGraph *G,Status(*Visit)(int v)){    int v;    VNode *p;    LinkQueue Q;    for(v=0;v<G->vexnum;v++)        visited[v]=0;    InitQueue(&Q);    v=begin;        if(!visited[v])        {            visited[v]=1;            Visit(v);            EnQueue(&Q,v);            while(!QueueEmpty(&Q))            {                DeQueue(&Q);                for(p=G->vertices[top]->next;p;p=p->next)                    if(!visited[p->data])                {                    visited[p->data]=1;Visit(p->data);                    EnQueue(&Q,p->data);                }            }        }}int main(){    int n;    ALGraph G;    scanf("%d",&n);    while(n--)    {        count=0;        memset(visited,0,sizeof(visited));        CreatUDG(&G);        BFSTraverse(&G,visiT);        printf("\n");    }    return 0;}


 

0 0
原创粉丝点击