/*

上机试验5-图的建立和遍历

1)建立【无向】【非连通】图的邻接表存储结构,要求顶点个数不少于15个。

2)用DFS及BFS对此邻接表进行遍历,打印出两种遍历的顶点访问顺序。

3)给定图中任意两个顶点v1和v2及整数k,判断是否存在从v1到v2的路径长度为k的简单路径,若有打印出路径上的顶点序列(要求路径上不含回路)。

进一步:找出从v1到v2的所有路径长度为k的【简单】路径。(简单路径是指:顶点序列中不含【重现】的顶点的路径。)

*/

#include<memory>

#include<cstdio>

#include<malloc.h>

#define PointNum 15

using namespace std;

struct V{

int vertex;

int distance;

int sign;

struct V* next;

}Vertex[PointNum+1];

int P[PointNum];

int union_find(int x) // 并查集

{

while(P[x]!=x)x=P[x];

return x;

}

void DFS(int vertex_);

void BFS(int vertex_);

int DFS(int V1,  int V2, int V3, int kn);

int lujing[PointNum+1],lujing_I;

int main()

{

int times,vertexNumbers,edgeNumbers,i,j,V1,V2,Distance,n=1,v1,v2,kn;

struct V *p,*q,*temp;

printf("请输入您所要进行的测试次数:");

scanf("%d",&times);

while(times--)

{

printf("\n第%d组数据输入.\n",n++);

printf("请输入顶点数:");

scanf("%d",&vertexNumbers); //要保证vertexNumbers <= PointNum

for( i = 0 ; i < vertexNumbers ; i ++ )

{

Vertex[i].vertex = i;//

Vertex[i].distance = 0;

Vertex[i].sign = 0;

Vertex[i].next = NULL;

}

printf("请输入边数:");

scanf("%d",&edgeNumbers); //要保证vertexNumbers <= PointNum

for( i = 0 ; i < edgeNumbers ; i ++ )

{

printf("请输入数据:");

scanf("%d%d%d",&V1,&V2,&Distance);

p = &Vertex[V1];q = Vertex[V1].next;

while( q != NULL ){

p = q;

q = q->next;

}//出来的时候q指向Vertex[V1]邻接表的结尾-NULL,

temp = (struct V *)malloc(sizeof(Vertex[0]));

p->next = temp;  temp->next = NULL;//接上temp

temp->vertex = V2; temp->distance = Distance;temp->sign = 0;   //给temp赋值

p = &Vertex[V2];q = Vertex[V2].next;

while( q != NULL ){

p = q;

q = q->next;

}

temp = (struct V *)malloc(sizeof(Vertex[0]));///temp的问题???????

p->next = temp;  temp->next = NULL;//接上temp

temp->vertex = V1;temp->distance = Distance;temp->sign = 0;   //给temp赋值

}

for(i = 0 ; i < vertexNumbers ; i ++ )

{

printf("\nDFS遍历的顶点访问顺序 :");

DFS(i);

for(int j = 0 ; j < vertexNumbers ; j ++ )

{

Vertex[j].sign = 0;

}

}

printf("\n");

for(i = 0 ; i < vertexNumbers ; i ++ )

{

for(int j = 0 ; j < vertexNumbers ; j ++ )

{

Vertex[j].sign = 0;

}

printf("\nBFS遍历的顶点访问顺序 :");

BFS(i);

}

for(j = 0 ; j < vertexNumbers ; j ++ )

{

Vertex[j].sign = 0;

}

printf("输入v1、v2及整数kn :\n");

scanf("%d%d%d",&v1,&v2,&kn);

for(j = 0 ; j < vertexNumbers ; j ++ )

{

Vertex[j].sign = 0;

}

lujing_I = 0;

DFS(v1,v2,v1,kn);

}

return 0;

}

void BFS(int vertex_)

{

struct V *p = &Vertex[vertex_];

int others[PointNum];

do{

if(Vertex[p->vertex].sign == 0){

printf("%d->",p->vertex);//输出访问节点

Vertex[p->vertex].sign = 1;//代表已经访问过了//

others[p->vertex] = -1;//代表这一个可以继续进行BFS

}

else others[p->vertex] = p->vertex;//记录已有的点

p = p->next;//主要

}while(p != NULL);

p = &Vertex[vertex_];

while( p->next != NULL )//有些没有连接要考虑~~!!!!!!!

{

p = p->next;

if(others[p->vertex] == -1)BFS(p->vertex);

}

}

void DFS(int vertex_)

{

struct V *q = Vertex[vertex_].next;//开始的:p = &Vertex[i];q = Vertex[i].next;

Vertex[vertex_].sign = 1;//代表已经访问过了

printf("%d->",vertex_);//

do{

for(;q != NULL && Vertex[q->vertex].sign == 1;q = q->next);//判断节点是否已经访问过了//原来是q->next != NULL//很大的错误:::::::::::::::Vertex[q->vertex].sign == 1不能用:::q.sign == 1(********************

if(q == NULL )return;//(q->next == NULL && vertex_Sum == vertexNumbers)

else DFS(q->vertex);//从q->vertex继续深搜索 *********************q->sign居然没有赋初值!!!! //很大的错误:::::::::::::::Vertex[q->vertex].sign == 1不能用:::q->sign == 1(**************

}while( q != NULL );//有些没有连接要考虑~~!!!!!!!!

}

int DFS(int V1,  int V2, int V3, int kn)

{

struct V *q = Vertex[V1].next;//开始的

int sum,i;

Vertex[V1].sign = 1;//代表已经访问过了

lujing[lujing_I ++ ] = V1;//printf("%d->",V1);//

if(V1 == V2)

{

for(sum=0,i=0; i < lujing_I; i++)

{

if(i >= 1 )//从第二个点开始

{

for(q = &Vertex[lujing[i-1]]; q != NULL; q = q->next)

{

if( q->vertex == lujing[i])sum += q->distance;

}

}

}

if(kn==sum)

{

printf("%d到%d距离为%d的路径为:",V3,kn,sum);

for(i=0; i < lujing_I; i++)

{

printf("%d->",lujing[i]);

}

printf("%d\n",sum);

}

//return sum;

}

do{

for(;q != NULL && Vertex[q->vertex].sign == 1;q = q->next);//判断节点是否已经访问过了//原来是q->next != NULL//很大的错误:::::::::::::::Vertex[q->vertex].sign == 1不能用:::q.sign == 1(********************

if(q == NULL )return -1;//(q->next == NULL && vertex_Sum == vertexNumbers)

else {

DFS(q->vertex,V2,V3,kn);//从q->vertex继续深搜索 *********************q->sign居然没有赋初值!!!! //很大的错误:::::::::::::::Vertex[q->vertex].sign == 1不能用:::q->sign == 1(**************

Vertex[q->vertex].sign = 0;lujing_I -- ;q = q->next;

}

}while( q != NULL );//有些没有连接要考虑~~!!!!!!!!

return 0;

}

/*

输入:

10

4

4

0 1 34

0 2 55

1 3 55

2 3 34

0 3 89

5

5

0 1 1

0 3 1

0 4 1

1 2 1

1 3 1

7

10

0 1 12

0 4 34

1 2 43

1 5 543

2 3 54

2 4 54

2 5 54

3 6 64

4 5 64

5 6 654

15

16

0 1 1

1 2 1

1 10 1

2 3 1

3 4 1

4 5 1

5 6 1

6 7 1

6 14 1

7 8 1

8 9 1

9 10 1

10 11 1

11 12 1

12 13 1

13 14 1

5

5

5 4 12

0 1 12

0 2 32

3 0 43

4 0 43

输出:

DFS遍历的顶点访问顺序 :0 -> 1 -> 2 -> 3 -> 4 ->

DFS遍历的顶点访问顺序 :1 -> 0 -> 3 -> 4 -> 2 ->

DFS遍历的顶点访问顺序 :2 -> 1 -> 0 -> 3 -> 4 ->

DFS遍历的顶点访问顺序 :3 -> 0 -> 1 -> 2 -> 4 ->

DFS遍历的顶点访问顺序 :4 -> 0 -> 1 -> 2 -> 3 ->

BFS遍历的顶点访问顺序 :0 -> 1 -> 3 -> 4 -> 2 ->

BFS遍历的顶点访问顺序 :1 -> 0 -> 2 -> 3 -> 4 ->

BFS遍历的顶点访问顺序 :2 -> 1 -> 0 -> 3 -> 4 ->

BFS遍历的顶点访问顺序 :3 -> 0 -> 1 -> 4 -> 2 ->

BFS遍历的顶点访问顺序 :4 -> 0 -> 1 -> 3 -> 2 ->

DFS遍历的顶点访问顺序 :0 -> 1 -> 2 -> 3 -> 6 -> 5 -> 4 ->

DFS遍历的顶点访问顺序 :1 -> 0 -> 4 -> 2 -> 3 -> 6 -> 5 ->

DFS遍历的顶点访问顺序 :2 -> 1 -> 0 -> 4 -> 5 -> 6 -> 3 ->

DFS遍历的顶点访问顺序 :3 -> 2 -> 1 -> 0 -> 4 -> 5 -> 6 ->

DFS遍历的顶点访问顺序 :4 -> 0 -> 1 -> 2 -> 3 -> 6 -> 5 ->

DFS遍历的顶点访问顺序 :5 -> 1 -> 0 -> 4 -> 2 -> 3 -> 6 ->

DFS遍历的顶点访问顺序 :6 -> 3 -> 2 -> 1 -> 0 -> 4 -> 5 ->

BFS遍历的顶点访问顺序 :0 -> 1 -> 4 -> 2 -> 5 -> 3 -> 6 ->

BFS遍历的顶点访问顺序 :1 -> 0 -> 2 -> 5 -> 4 -> 3 -> 6 ->

BFS遍历的顶点访问顺序 :2 -> 1 -> 3 -> 4 -> 5 -> 0 -> 6 ->

BFS遍历的顶点访问顺序 :3 -> 2 -> 6 -> 1 -> 4 -> 5 -> 0 ->

BFS遍历的顶点访问顺序 :4 -> 0 -> 2 -> 5 -> 1 -> 3 -> 6 ->

BFS遍历的顶点访问顺序 :5 -> 1 -> 2 -> 4 -> 6 -> 0 -> 3 ->

BFS遍历的顶点访问顺序 :6 -> 3 -> 5 -> 2 -> 1 -> 4 -> 0 ->

第3组数据输入.

请输入顶点数:

*/

转载于:https://www.cnblogs.com/Xbingbing/p/8505276.html

C语言实现的图的深度搜索与广度搜索程序相关推荐

  1. 深度搜索问题c语言,C语言实现的图的深度搜索与广度搜索程序.doc

    C语言实现的图的深度搜索与广度搜索程序 C语言实现的图的深度搜索与广度搜索程序 /* 上机试验5-图的建立和遍历 1)建立[无向][非连通]图的邻接表存储结构,要求顶点个数不少于15个. 2)用DFS ...

  2. 万能的搜索——深度搜索和广度搜索

    搜索分为深度优先搜索(dfs)和广度优先搜索(bfs) 深度搜索和广度搜索的区别是: 深度搜索是往深度方向进行搜索的,先选一条路走到底,再选另一条路: 广度搜索是一层一层的,把一层上的所有情况都搜索到 ...

  3. 图的深度遍历和广度遍历算法

    图的深度遍历和广度遍历算法 图的深度遍历可以简单理解为一条道走到黑,首先访问图中任一起始顶点v,再访问与v顶点邻接且未被访问过的顶点w1,再访问与w1邻接且未被访问过的顶点w2,重复上述操作,若不能继 ...

  4. python来进行图的深度遍历和广度遍历

    python来进行图的深度遍历和广度遍历 # -*- coding: utf-8 -*- """ Created on Sat Sep 14 18:01:27 2019@ ...

  5. 图的深度遍历和广度遍历

    理论部分 图的深度遍历和广度遍历都不算很难像极了二叉树的前序遍历和层序遍历,如下面的图,可以用右边的邻接矩阵进行表示,假设以顶点0开始对整幅图进行遍历的话,两种遍历方式的思想如下: 1. 深度优先遍历 ...

  6. 【图】图的深度搜索和广度搜索

    深度优先搜索 图的深度优先遍历(Depth First Search,DFS)是一种遍历图的算法,其基本思想是从起始顶点开始,不断访问邻接顶点,直到无法继续访问为止,然后回溯到之前的顶点,继续访问其未 ...

  7. 深度搜索和广度搜索领接表实现_数据结构与算法--图的搜索(深度优先和广度优先)...

    数据结构与算法--图的搜索(深度优先和广度优先) 有时候我们需要系统地检查每一个顶点或者每一条边来获取图的各种性质,为此需要从图的某个顶点出发,访遍图中其余顶点,且使得每一个顶点只被访问一次,这个过程 ...

  8. 深度搜索和广度搜索特点的深刻理解

    问题提出: 考虑如下图所示的简单图所表示的缅因州的道路系统.在冬天里保持道路通路通畅的唯一方式就是经常扫雪.高速公路部分希望只扫尽可能少的道路上的雪,而确保总是存在连接任何两个乡镇的干净道路.如何才能 ...

  9. 深度搜索和广度搜索领接表实现_算法基础04-深度优先搜索、广度优先搜索、二分查找、贪心算法...

    深度优先搜索DFS.广度优先搜索BFS 比较 拿谚语打比方的话,深度优先搜索可以比作打破砂锅问到底.不撞南墙不回头:广度优先搜索则对应广撒网,多敛鱼 两者没有绝对的优劣之分,只是适用场景不同 当解决方 ...

最新文章

  1. 转:在线框架引用 bootstrap/jq/jqmobile/css框架
  2. 解决vue项目eslint校验 Do not use ‘new‘ for side effects 的两种方法
  3. android倒计时录制视频下载,android录制视屏(预览,倒计时)
  4. HDU1407 测试你是否和LTC水平一样高 暴力、二分、hash
  5. Ubuntu 安装Jenkins报错
  6. 在Ubuntu中搭建NFS服务器
  7. 数学建模-非线性优化模型
  8. 机器学习读书笔记:样本降维
  9. php代码写一串新年祝福,新年祝福QQ留言代码_把幸福装的满满的
  10. 服务器usb驱动安装系统安装失败怎么办,usb驱动安装不成功,详细教您usb驱动安装失败的解决方法...
  11. FBReader导入eclipse 和Androidstudio相关问题
  12. lay和lied_辨析:lie, lay, lain, lied, laid
  13. 性能测试--03企业级性能测试与分析
  14. css3 calc的使用
  15. 【DSP】【第一篇】开始DSP学习
  16. Hive SQL查询效率提升之Analyze方案的实施
  17. [DAY003]考研数学极限的计算知识点与题目总结(三)
  18. Abaqus中C3D8R单元和C3D8I单元的区别
  19. webview加载的页面和浏览器渲染的页面不一致_QQ音乐Android客户端Web页面通用性能优化实践...
  20. 谷歌浏览器插件开发接口自动打码【2022年07月05日更新一下修改的点】

热门文章

  1. [Ajax] 实现跨域访问
  2. Python_生成器Generator
  3. 【Vue】路由Router传参的两种方式(详解)
  4. 查看端口命令及端口功能详解
  5. 机器学习实战11-训练深层神经网络
  6. VMware15设置快照回到指定时间的状态
  7. java轮训算法_负载均衡轮询算法实现疑问
  8. mysql像plsql一样删除提交_MySQL学习-MySQL内置功能_事务操作
  9. 1041. Robot Bounded In Circle
  10. java开发过程中几种常用算法