C语言实现的图的深度搜索与广度搜索程序

/*

上机试验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(" 第%d组数据输入. ",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(" DFS遍历的顶点访问顺序 :");

DFS(i);

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

{

Vertex[j].sign = 0;

}

}

printf(" ");

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

{

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

{

Vertex[j].sign = 0;

}

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

BFS(i);

}

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

{

Vertex[j].sign = 0;

}

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

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 ",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