用C语言实现 图的邻接表和邻接矩阵数据结构的定义、创建;图的深度优先遍历、广度优先遍历。 200

用C语言,百度到的程序运行不正确,最好亲自运行一下再贴过来,求大神相助!... 用C语言,百度到的程序运行不正确,最好亲自运行一下再贴过来,求大神相助! 展开
 我来答
Ben779608658
2014-11-04 · TA获得超过101个赞
知道答主
回答量:54
采纳率:100%
帮助的人:53.1万
展开全部
/*
                程序1:邻接表的dfs,bfs
                其中n是点的个数,m是边的个数,你需要输入m条有向边,如果要无向只需要反过来多加一遍即可。
*/
#include <stdio.h>
#include <string.h>
#define MAXM 100000
#define MAXN 10000
int next[MAXM],first[MAXN],en[MAXM],n,m,flag[MAXN],pd,dl[MAXN],head,tail;

void input_data()
{
                scanf("%d%d",&n,&m);
                int i,x,y;
                for (i=1;i<=m;i++)
                                {
                                                int x,y;
                                                scanf("%d%d",&x,&y);
                                                next[i]=first[x];
                                                first[x]=i;
                                                en[i]=y;
                                }
}

void pre()
{
                memset(flag,0,sizeof(flag));
                pd=0;
}

void dfs(int x)
{
                flag[x]=1;
                if (!pd)
                                {
                                                pd=1;
                                                printf("%d",x);
                                }else
                                                printf(" %d",x);
                int p=first[x];
                while (p!=0)
                                {
                                                int y=en[p];
                                                if (!flag[y])   dfs(y);
                                                p=next[p];
                                }
}

void bfs(int k)
{
                head=0;tail=1;
                flag[k]=1;dl[1]=k;
                while (head<tail)
                                {
                                                int x=dl[++head];
                                                if (!pd)
                                                                {
                                                                                pd=1;
                                                                                printf("%d",x);
                                                                }else printf(" %d",x);
                                                int p=first[x];
                                                while (p!=0)
                                                                {
                                                                                int y=en[p];
                                                                                if (!flag[y])
                                                                                                {
                                                                                                                flag[y]=1;
                                                                                                                dl[++tail]=y;
                                                                                                }
                                                                                p=next[p];
                                                                }
                                }
}

int main()
{
                input_data();//读入图信息。
                pre();//初始化
                printf("图的深度优先遍历结果:");
                int i;
                for (i=1;i<=n;i++)//对整张图进行dfs; 加这个for主要是为了防止不多个子图的情况
                                if (!flag[i])
                                                dfs(i);
                printf("\n-------------------------------------------------------------\n");
                pre();//初始化
                printf("图的广度优先遍历结果为:");
                for (i=1;i<=n;i++)
                                if (!flag[i])
                                                bfs(i);
                printf("\n----------------------end------------------------------------\n");
                return 0;
}
/*
                程序2:邻接矩阵
                图的广度优先遍历和深度优先遍历
*/
#include <stdio.h>
#include <string.h>
#define MAXN 1000

int n,m,w[MAXN][MAXN],flag[MAXN],pd,dl[MAXN];

void input_data()
{
                scanf("%d%d",&n,&m);
                int i;
                for (i=1;i<=m;i++)
                                {
                                                int x,y;
                                                scanf("%d%d",&x,&y);
                                                w[x][0]++;
                                                w[x][w[x][0]]=y;
                                }
}

void pre()
{
                memset(flag,0,sizeof(flag));
                pd=0;
}

void dfs(int x)
{
                flag[x]=1;
                if (!pd)
                                {
                                                pd=1;
                                                printf("%d",x);
                                }else printf(" %d",x);
                int i;
                for (i=1;i<=w[x][0];i++)
                                {
                                                int y=w[x][i];
                                                if (!flag[y])   dfs(y);
                                }
}

void bfs(int t)
{
                int head=0,tail=1;
                dl[1]=t;flag[t]=1;
                while (head<tail)
                {
                                int x=dl[++head];
                                if (!pd)
                                                {
                                                                pd=1;
                                                                printf("%d",x);
                                                }else printf(" %d",x);
                                int i;
                                for (i=1;i<=w[x][0];i++)
                                                {
                                                                int y=w[x][i];
                                                                if (!flag[y])
                                                                                {
                                                                                                flag[y]=1;
                                                                                                dl[++tail]=y;
                                                                                }
                                                }
                }
}

int main()
{
                input_data();
                printf("图的深度优先遍历结果:");
                pre();
                int i;
                for (i=1;i<=n;i++)
                                if (!flag[i])
                                                dfs(i);
                printf("\n---------------------------------------------------------------\n");
                printf("图的广度优先遍历结果:");
                pre();
                for (i=1;i<=n;i++)
                                if (!flag[i])
                                                bfs(i);
                printf("\n-----------------------------end--------------------------------\n");
                return 0;
}
光点科技
2023-08-15 广告
通常情况下,我们会按照结构模型把系统产生的数据分为三种类型:结构化数据、半结构化数据和非结构化数据。结构化数据,即行数据,是存储在数据库里,可以用二维表结构来逻辑表达实现的数据。最常见的就是数字数据和文本数据,它们可以某种标准格式存在于文件... 点击进入详情页
本回答由光点科技提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式