数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历

本文介绍了一种使用邻接矩阵实现的无向连通图的广度优先搜索(BFS)算法,并提供了完整的C语言代码示例。该算法能够遍历给定的顶点并输出遍历序列。

数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历

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的遍历结果。

示例输入

1
6 7 0
0 3
0 4
1 4
1 5
2 3
2 4
3 5

示例输出

0 3 4 2 5 1

提示

以邻接矩阵作为存储结构。

来源

 

示例程序

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
typedef int Status;
typedef struct
{
    int arcs[MAX][MAX];
    int vexnum,arcnum;
}MGraph;
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;
}
Status CreatUDG(MGraph *G)
{
    int i,j,k,v1,v2;
    scanf("%d%d%d",&G->vexnum,&G->arcnum,&begin);
    for(i=0;i<G->vexnum;i++)
        for(j=0;j<G->vexnum;j++)
        G->arcs[i][j]=0;
    for(k=0;k<G->arcnum;k++)
    {
        scanf("%d%d",&v1,&v2);
    G->arcs[v1][v2]=1;
    G->arcs[v2][v1]=1;
    }
    return 1;
}
int visiT(int v)
{
    if(count==0)
        {
        printf("%d",v);
    count++;
        }
        else
        printf(" %d",v);
        return 1;
}
void BFSTraverse(MGraph *G,Status(*Visit)(int v))
{
    int v,w;
    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(w=0;w<G->vexnum;w++)
                    if(!visited[w]&&G->arcs[top][w]==1)
                {
                    visited[w]=1;Visit(w);
                    EnQueue(&Q,w);
                }
            }
        }
}
int main()
{
    int n;
    MGraph G;
    scanf("%d",&n);
    while(n--)
    {
        count=0;
        memset(visited,0,sizeof(visited));
        CreatUDG(&G);
        BFSTraverse(&G,visiT);
        printf("\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猿憨憨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值