图的深度优先遍历(递归与非递归C语言)

4 篇文章 0 订阅
3 篇文章 0 订阅

图的深度优先遍历(递归与非递归C语言)

递归:

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

#define MaxVertexNum 10  /* 最大顶点数设为10 */
#define INFINITY 65535   /* ∞设为双字节无符号整数的最大值65535*/
typedef int Vertex;      /* 用顶点下标表示顶点,为整型 */
typedef int WeightType;  /* 边的权值设为整型 */

typedef struct GNode *PtrToGNode;
struct GNode{
	Vertex V[MaxVertexNum];//定义一个顶点数组,方便之后用数组下标来取顶点的数据 
    int Nv;  /* 顶点数 */
    int Ne;  /* 边数   */
    WeightType g[MaxVertexNum][MaxVertexNum]; /* 邻接矩阵 */
};
typedef PtrToGNode MGraph; /* 以邻接矩阵存储的图类型 */
bool visited[MaxVertexNum]; /* 顶点的访问标记 */

MGraph CreateGraph(); /* 创建图并且将Visited初始化为false;裁判实现,细节不表 */

void Visit( Vertex V )
{
    printf(" %d", V);
}

void DFS( MGraph G, Vertex V);


int main()
{
    MGraph G;
    Vertex V;
	
    G = CreateGraph();
    printf("请输入从哪一个顶点开始深度优先遍历");
    scanf("%d", &V);
    printf("DFS from %d:", V);
    
	for(int i = 0;i < G->Nv;i++){// 初始化visited,用来标识是否已经访问过
		visited[i] = false; 
	}
	for(int j = 0;j < G->Nv;j++){
		if(!(visited[j]))
		{
			DFS(G, V);
		}
	}
    

    return 0;
}
MGraph CreateGraph(){
	MGraph G;
	G = (MGraph)malloc(sizeof(GNode));
	G->Ne = 0;	//初始化一下 
	G->Nv = 0;
	int N; //表示图中顶点数 
	printf("输入图含有几个顶点"); 
	scanf("%d",&N);
	G->Nv = N;
	printf("输入这N个顶点是什么");
	for(int k = 0;k < N;k++){
		scanf("%d",&G->V[k]);
	}
	int tag;//来标识有没有边 
	for(int i = 0;i < N;i++){
		printf("输入邻接矩阵第%d行是什么",i);
		for(int j = 0;j < N;j++){
			scanf("%d",&tag);
			if(tag != 0){
				tag = 1;
				G->Ne  = G->Ne + 1;
			}
			G->g[i][j] = tag;
		}
	}
	return G;
}

void DFS( MGraph G, Vertex V){
	
	//找到V这个顶点的下标
	int j;
	for(j = 0;G->V[j] != V;j++);
	Visit(V);
	visited[j] = true;
	for(int k = 0;k < G->Nv;k++){
		if(visited[k] == false && G->g[j][k] == 1){
			DFS(G,G->V[k]);
		}
	}
}


递归算法中的核心代码块我认为是visited[k] == false && G->g[j][k] == 1,这条语句套在if语句中来判断是不是要递归,这条语句的意思是,如果该行没有被访问过,并且存在边的时候,进行递归,因为图的邻接矩阵是一个方阵假设是A,假如从0到1有边,那么A(0,1)那个位置就是1,要是有权值A(0,1)就是权值,是无向图的话A(1,0)也是同理的,所以在判断点有没有访问过可以直接利用列上的值就可以,递归传回的顶点也是,例如是A(0,1),则传回去1就可以,然后再用从1顶点继续递归。

非递归:

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

#define MaxVertexNum 10  /* 最大顶点数设为10 */
#define INFINITY 65535   /* ∞设为双字节无符号整数的最大值65535*/
typedef int Vertex;      /* 用顶点下标表示顶点,为整型 */
typedef int WeightType;  /* 边的权值设为整型 */

typedef struct GNode *PtrToGNode;
struct GNode{
	Vertex V[MaxVertexNum];//定义一个顶点数组,方便之后用数组下标来取顶点的数据 
    int Nv;  /* 顶点数 */
    int Ne;  /* 边数   */
    WeightType g[MaxVertexNum][MaxVertexNum]; /* 邻接矩阵 */
};

typedef PtrToGNode MGraph; /* 以邻接矩阵存储的图类型 */

typedef struct{//定义一个栈 
	int index; //记录每个顶点的下标 
	int data; //记录值 
}stuck;
bool visited[MaxVertexNum]; /* 顶点的访问标记 */

MGraph CreateGraph(); /* 创建图并且将Visited初始化为false;裁判实现,细节不表 */

void Visit( Vertex V )
{
    printf(" %d", V);
}

void DFS( MGraph G, Vertex V);


int main()
{
    MGraph G;
    Vertex V;
	
    G = CreateGraph();
    printf("请输入从哪一个顶点开始深度优先遍历");
    scanf("%d", &V);
    printf("DFS from %d:", V);
    
	for(int i = 0;i < G->Nv;i++){// 初始化visited,用来标识是否已经访问过
		visited[i] = false; 
	}
	for(int j = 0;j < G->Nv;j++){
		if(!(visited[j]))
		{
			DFS(G, V);
		}
	}
    return 0;
}
MGraph CreateGraph(){
	MGraph G;
	G = (MGraph)malloc(sizeof(GNode));
	G->Ne = 0;	//初始化一下 
	G->Nv = 0;
	int N; //表示图中顶点数 
	printf("输入图含有几个顶点"); 
	scanf("%d",&N);
	G->Nv = N;
	printf("输入这N个顶点是什么");
	for(int k = 0;k < N;k++){
		scanf("%d",&G->V[k]);
	}
	int tag;//来标识有没有边 
	for(int i = 0;i < N;i++){
		printf("输入邻接矩阵第%d行是什么",i);
		for(int j = 0;j < N;j++){
			scanf("%d",&tag);
			if(tag != 0){
				tag = 1;
				G->Ne  = G->Ne + 1;
			}
			G->g[i][j] = tag;
		}
	}
	return G;
}

void DFS( MGraph G, Vertex V){
	//栈初始化 
	stuck s[MaxVertexNum];
	int top = -1;
	//找这一点的下标
	int i; 
	for(i = 0;G->V[i] != V;i++);
	visited[i] = true;
	top = top + 1;
	s[top].data = V;
	s[top].index = i;
	Visit(s[top].data);
	int m;int k;
	while(top != -1){
		for(m = 0; m < G->Nv;m++){
			if((G->g[s[top].index][m] == 1)&&(visited[m] == false)){
				break;
			}
		}
		if(m == G->Nv){//相等说明遍历了一行没有1或者都遍历过了,要出栈 
			top = top -1;
		} 
		else{//不相等的话这个j就是下一个要遍历的下标 
			//入栈操作 
			top = top + 1;
			s[top].data = G->V[m];
			s[top].index = m;
			visited[m] = true;
			//访问
			Visit(s[top].data); 
			i = m;
			
		}		
	}
}

算法思想:递归算法都是用栈来完成的,所以非递归一定要创建一个栈,我的代码中创建了一个栈,栈中包含两个变量,一个是data(数据)一个是index(在数组中的下标),当有节点时就进栈,没有可访问节点时就出栈,该代码的核心代码我认为是((G->g[s[top].index][m] == 1)&&(visited[m] == false))其功能是为了找到顶点的下标,就这样一直循环,到栈空的时候循环结束

运行结果:

在这里插入图片描述
在这里插入图片描述

  • 10
    点赞
  • 74
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 以下是的邻接表深度优先的C语言代码: ```c #include<stdio.h> #include<stdlib.h> #define MAX_VERTEX_NUM 20 // 最大顶点数 #define MAX_STACK_SIZE 20 // 最大栈深度 typedef int VertexType; // 顶点的数据类型 // 定义边表结构体 typedef struct ArcNode { int adjvex; // 邻接点在顶点数组中的下标 struct ArcNode *nextarc; // 指向下一个邻接点的指针 } ArcNode; // 定义顶点表结构体 typedef struct { VertexType data; // 顶点的数据 ArcNode *firstarc; // 指向第一个邻接点的指针 } VNode; // 定义邻接表结构体 typedef struct { VNode vertex[MAX_VERTEX_NUM]; // 顶点数组 int vexnum, arcnum; // 顶点数和边数 } ALGraph; // 初始化邻接表 void init(ALGraph *G) { int i; G->vexnum = G->arcnum = 0; for (i = 0; i < MAX_VERTEX_NUM; i++) { G->vertex[i].firstarc = NULL; } } // 添加边 void addEdge(ALGraph *G, int i, int j) { ArcNode *p = (ArcNode *)malloc(sizeof(ArcNode)); p->adjvex = j; p->nextarc = G->vertex[i].firstarc; G->vertex[i].firstarc = p; G->arcnum++; } // 深度优先 void DFS(ALGraph *G, int v) { int visited[MAX_VERTEX_NUM]; int top = -1; // 栈顶指针 int stack[MAX_STACK_SIZE]; // 栈 ArcNode *p; int i; // 初始化visited数组 for (i = 0; i < MAX_VERTEX_NUM; i++) { visited[i] = 0; } // 将起点v入栈并标记为已访问 stack[++top] = v; visited[v] = 1; // 遍栈中的元素 while (top != -1) { // 取出栈顶元素 i = stack[top--]; printf("%d ", G->vertex[i].data); // 遍邻接点 p = G->vertex[i].firstarc; while (p != NULL) { // 如果邻接点未被访问,则入栈并标记为已访问 if (visited[p->adjvex] == 0) { stack[++top] = p->adjvex; visited[p->adjvex] = 1; } p = p->nextarc; } } } int main() { ALGraph G; init(&G); addEdge(&G, 0, 1); addEdge(&G, 0, 2); addEdge(&G, 1, 2); addEdge(&G, 2, 0); addEdge(&G, 2, 3); addEdge(&G, 3, 3); printf("深度优先结果:"); DFS(&G, 2); return 0; } ``` ### 回答2: 下面是的邻接表深度优先的C语言代码。首先,我们需要定义一个的结构体,包含顶点数量和邻接表数组。 ```c #include <stdio.h> #include <stdlib.h> // 定义的结构体 typedef struct GraphNode{ int vertex; struct GraphNode* next; } GraphNode; typedef struct Graph{ int vertices; GraphNode** adjList; } Graph; // 创建的函数 Graph* createGraph(int vertices){ Graph* graph = (Graph*) malloc(sizeof(Graph)); graph->vertices = vertices; graph->adjList = (GraphNode**) malloc(vertices * sizeof(GraphNode*)); for(int i = 0; i < vertices; i++){ graph->adjList[i] = NULL; } return graph; } // 添加边的函数 void addEdge(Graph* graph, int src, int dest){ GraphNode* newNode = (GraphNode*) malloc(sizeof(GraphNode)); newNode->vertex = dest; newNode->next = graph->adjList[src]; graph->adjList[src] = newNode; // 若为无向,则也需添加反向边 newNode = (GraphNode*) malloc(sizeof(GraphNode)); newNode->vertex = src; newNode->next = graph->adjList[dest]; graph->adjList[dest] = newNode; } // 深度优先函数 void DFS(Graph* graph, int startVertex){ int* visited = (int*) malloc(graph->vertices * sizeof(int)); for(int i = 0; i < graph->vertices; i++){ visited[i] = 0; } // 使用栈来实现归遍 int stack[graph->vertices]; int top = -1; stack[++top] = startVertex; visited[startVertex] = 1; while(top != -1){ int currentVertex = stack[top--]; printf("%d ", currentVertex); GraphNode* temp = graph->adjList[currentVertex]; while(temp){ int adjVertex = temp->vertex; if(!visited[adjVertex]){ stack[++top] = adjVertex; visited[adjVertex] = 1; } temp = temp->next; } } } // 主函数 int main(){ int vertices = 6; Graph* graph = createGraph(vertices); addEdge(graph, 0, 1); addEdge(graph, 0, 2); addEdge(graph, 1, 3); addEdge(graph, 1, 4); addEdge(graph, 2, 4); addEdge(graph, 3, 4); addEdge(graph, 3, 5); printf("深度优先结果: "); DFS(graph, 0); return 0; } ``` 代码中的`createGraph`函数用于创建具有指定数量顶点的。`addEdge`函数用于在两个顶点之间添加一条边。`DFS`函数用于执行深度优先。在`main`函数中,我们创建了一个具有6个顶点的,并添加了一些边。最后,我们调用`DFS`函数从第一个顶点开始进行深度优先,并打印结果。 ### 回答3: 深度优先(Depth First Search,简称DFS)是的一种遍算法,其中邻接表是一种的表示方式。下面是一个归的深度优先的C语言代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_VERTEX_NUM 100 // 定义的邻接表节点 typedef struct ArcNode { int adjvex; // 邻接顶点的下标 struct ArcNode *nextarc; // 指向下一个邻接表节点的指针 } ArcNode; // 定义的顶点节点 typedef struct VNode { int data; // 顶点的数据 ArcNode *firstarc; // 指向第一个邻接表节点的指针 } VNode, AdjList[MAX_VERTEX_NUM]; // 定义 typedef struct { AdjList vertices; // 顶点数组 int vexnum; // 顶点数 } Graph; // 归DFS遍 void DFS(Graph G, int v) { int visited[MAX_VERTEX_NUM] = {0}; // 记录顶点是否被访问过的数组 ArcNode *stack[MAX_VERTEX_NUM]; // 用数组实现栈 int top = -1; // 栈顶指针 int i; printf("DFS遍顺序: "); stack[++top] = &(G.vertices[v]); // 将起始顶点的邻接表节点入栈 visited[v] = 1; // 标记起始顶点已经被访问 while (top != -1) { // 栈不为空时 ArcNode *p = stack[top--]; // 弹出栈顶元素 printf("%d ", p->data); // 访问该顶点 for (ArcNode *q = p->firstarc; q != NULL; q = q->nextarc) { // 遍该顶点的邻接点 if (visited[q->adjvex] == 0) { // 若邻接点未被访问过 stack[++top] = &(G.vertices[q->adjvex]); // 入栈 visited[q->adjvex] = 1; // 标记已访问 } } } } int main() { Graph G; G.vexnum = 4; // 假设有4个顶点 for (int i = 0; i < G.vexnum; i++) { G.vertices[i].data = i; // 顶点数据 G.vertices[i].firstarc = NULL; // 初始化邻接表节点指针为空 } // 添加边 ArcNode *arc0 = (ArcNode *)malloc(sizeof(ArcNode)); arc0->adjvex = 1; arc0->nextarc = NULL; G.vertices[0].firstarc = arc0; ArcNode *arc1 = (ArcNode *)malloc(sizeof(ArcNode)); arc1->adjvex = 2; arc1->nextarc = NULL; G.vertices[1].firstarc = arc1; ArcNode *arc2 = (ArcNode *)malloc(sizeof(ArcNode)); arc2->adjvex = 3; arc2->nextarc = NULL; G.vertices[2].firstarc = arc2; // 顶点0的邻接点为1 // 顶点1的邻接点为2 // 顶点2的邻接点为3 DFS(G, 0); return 0; } ``` 此代码采用邻接表的形式存储,并使用归的深度优先算法遍中的顶点和边。首先定义了邻接表节点和顶点节点的数据结构,然后定义了的数据结构,包括顶点数组和顶点数。接下来,使用归的深度优先算法实现了DFS()函数,在DFS()函数中,使用栈来记录待访问的节点,并使用visited数组来标记节点是否被访问过。最后,在主函数中创建了一个具有4个顶点的,并添加了相应的边,然后调用DFS()函数进行遍

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值