11周 项目3
来源:互联网 发布:外贸邦海关数据 编辑:程序博客网 时间:2024/05/27 03:29
问题及描述:
/* 烟台大学计算机学院 文件名称:aa.cpp 作者:李金朴 完成日期:2017年11月12日 问题描述:实现图遍历算法,分别输出如下图结构的深度优先(DFS)遍历序列和广度优先遍历(BFS)序列。 输入描述:无 输出描述:输出广度与深度的遍历结果 用到了算法库graph.h */ BFS: [cpp] view plain copy #include <stdio.h> #include <malloc.h> #include "../graph.h" int visited[MAXV]={}; void DFS(ALGraph *G, int v) { ArcNode *p; int w; visited[v]=1; printf("%d ", v); p=G->adjlist[v].firstarc; while (p!=NULL) { w=p->adjvex; if (visited[w]==0) DFS(G,w); p=p->nextarc; } } int main() { int i; ALGraph *G; int A[6][6]= { {0,1,1,1,0,0}, {1,0,1,0,1,0}, {1,1,0,0,0,1}, {1,0,0,0,0,1}, {0,1,0,0,0,1}, {0,0,1,1,1,0} }; ArrayToList(A[0], 6, G); printf(" 由2开始深度遍历:"); DFS(G, 2); printf("\n"); for(i=0; i<MAXV; i++) visited[i]=0; printf(" 由0开始深度遍历:"); DFS(G, 0); printf("\n"); return 0; } DFS: [cpp] view plain copy #include <stdio.h> #include <malloc.h> #include "../graph.h" void BFS(ALGraph *G, int v) { ArcNode *p; int w,i; int queue[MAXV],front=0,rear=0; //定义循环队列 int visited[MAXV]; //定义存放节点的访问标志的数组 for (i=0; i<G->n; i++) visited[i]=0; //访问标志数组初始化 printf("%2d",v); //输出被访问顶点的编号 visited[v]=1; //置已访问标记 rear=(rear+1)%MAXV; queue[rear]=v; //v进队 while (front!=rear) //若队列不空时循环 { front=(front+1)%MAXV; w=queue[front]; //出队并赋给w p=G->adjlist[w].firstarc; //找w的第一个的邻接点 while (p!=NULL) { if (visited[p->adjvex]==0) { printf("%2d",p->adjvex); //访问之 visited[p->adjvex]=1; rear=(rear+1)%MAXV; //该顶点进队 queue[rear]=p->adjvex; } p=p->nextarc; //找下一个邻接顶点 } } printf("\n"); } int main() { ALGraph *G; int A[6][6]= { {0,1,1,1,0,0}, {1,0,1,0,1,0}, {1,1,0,0,0,1}, {1,0,0,0,0,1}, {0,1,0,0,0,1}, {0,0,1,1,1,0} }; ArrayToList(A[0], 6, G); printf(" 由2开始广度遍历:"); BFS(G, 2); printf(" 由0开始广度遍历:"); BFS(G, 0); return 0; }运行结果:
阅读全文
0 0
- 11周项目3
- 11周 项目3
- 11周 项目3
- 第11周-项目3
- 第11周项目3
- 第11周项目3
- 第11周 【项目3
- 第11周【项目3
- 第11周 【项目3
- 第5周项目项目3
- 第11周项目3(3)
- 第11周项目3-Cperson
- 第11周项目3-计算个人所得税
- 第11周项目1(3)
- 第11周项目3:回文,素数
- 第11周项目3(2)
- 第11周项目3(4)
- 第11周项目3(5)
- 第十三周项目三——是否二叉排序树?
- Java追加文件内容的三种方法
- 被选中的商品价格计入总金额
- Intellij IDEA配置springMVC时提示"context:component-scan" is not bound
- 第十三周项目1验证算法(2)
- 11周 项目3
- net WebApi中使用swagger
- OSN博士必须掌握的必杀技(更新至2017/12/15)
- iOS11 解决MBProgressHUD弹框不显示问题
- bzoj1426 收集邮票(递推+概率期望)
- ceph (luminous 版) primary affinity 管理
- sqlnet.ora
- MVP+断点续传
- java多种方式读文件,追加文件内容,对文件的各种操作