DFS --- Depth First Search 深度优先搜索算法

2024-06-06 09:38

本文主要是介绍DFS --- Depth First Search 深度优先搜索算法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Depth First Search 




                   原理还是去看《DSAA》,这里着重分析实现策略。



如果对于图这种数据结构不熟悉,这个BFS一般是搞不定的...

下面分别是无向图的邻接表实现和邻接矩阵实现

http://blog.csdn.net/cinmyheart/article/details/41381845

http://blog.csdn.net/cinmyheart/article/details/41370465


-----------------------------------------------------------


/************************************************************
code file	: dfs.h
code writer	: EOF
code date	: 2014.11.22
e-mail		: jasonleaster@gmail.comcode description:This file is a header file for out test program.
We abstract the data structure -- Graph here. And we also
declare some useful API to construct out naive graph :)************************************************************/#ifndef _DFS_H_
#define _DFS_H_#include <stdio.h>#include <stdlib.h>#define CONNECTED    1#define DISCONNECTED 0#define SUCCESS  0#define FAILED  -1#define UN_VISITED 0#define VISITED   1#define ENTRY_POINT 3struct visited{int array[0];};struct vertex{int value;struct vertex* next;struct vertex* end;};struct graph{int num_vertex;int num_edge;struct vertex adjacent[0];};void dfs(struct graph* p_graph,struct vertex* p_vertex,struct visited* p_visited);struct graph* init_graph(int vertex,int edge);void   release_graph(struct graph* p_graph);int add_edge(struct graph* p_graph,char from_v,char to_v);int print_graph(struct graph* p_graph);#endif


关键的,对于DFS的实现

这个函数我设计接受三个参数(指针),p_graph指向图,p_vertex指向节点,p_visited指向标记“数组”(动态分配得的结构体)

/*********************************************************
code writer	:	EOF
code date 	: 	2014.11.24
code file	:	dfs.c
e-mail		:	jasonleaster@gmail.comCode description:This function @dfs() is a implementation of 
"Depth first search" which is based on recursion.**********************************************************/#include "dfs.h"void dfs(struct graph* p_graph,struct vertex* p_vertex,struct visited* p_visited)
{if(!p_vertex){return;}p_visited->array[p_vertex->value] = VISITED;printf("%d->",p_vertex->value);for(p_vertex = p_vertex->next;!!p_vertex;p_vertex = p_vertex->next){if(p_visited->array[p_vertex->value] == UN_VISITED){dfs(p_graph,&(p_graph->adjacent[p_vertex->value]),p_visited);}}printf("\n");
}

既然是深度优先搜索,而且对于已经检索到的数据会有“全局标记”(这里采用的是同一动态内存分配所得内存区域)。

那么我们使可以利用圈图进行测试的

比方说

节点链接是个圈: 1->2->3->4->1,这种,首尾相连的圈。

从任何节点进入,都可以停下并且检索完所有的节点。


我们的测试程序:

/****************************************************************
code file	: test_graph.c
code writer	: EOF
code date	: 2014.11.22
e-mail		: jasonleaster@gmail.comcode description:Here , we use this program to call some API which would 
construct a ADT--graph and test it.*****************************************************************/
#include "dfs.h"int main()
{struct graph* p_graph = NULL;FILE* fp = fopen("./text.txt","r+");if(!fp){printf("fopen() failed!\n");return 0;}int ret    = 0;int vertex = 0;int edge   = 0;int from_v = 0;int to_v   = 0;fscanf(fp,"%d",&vertex);fscanf(fp,"%d",&edge);p_graph = init_graph(vertex,edge);int temp = 0;for(temp = 0;temp < edge;temp++){/***	I think it's necessary to check the returned value** of scanf() family.*/ret = fscanf(fp,"%d %d",&from_v,&to_v);if(ret != 2){break;}add_edge(p_graph,from_v,to_v);}struct visited* p_visited = (struct visited*)malloc(sizeof(struct visited) +\sizeof(int)*(p_graph->num_vertex));for(temp = 0;temp < p_graph->num_vertex;temp++){p_visited->array[temp] = UN_VISITED;}	print_graph(p_graph);/***	Here, we start to DFS.*/dfs(p_graph,&(p_graph->adjacent[ENTRY_POINT]),p_visited);release_graph(p_graph);free(p_visited);p_visited = NULL;fclose(fp);return 0;
}


测试文本数据text.txt:

5
5
1 2
2 3
3 4
4 1

测试结果:



同样的,我们可以用其他的图进行测试:

测试文本:

8个节点,12条边的图

8
12
3 6
3 1
1 4
1 2
4 3
4 6
4 7
4 5
7 6
2 4
2 5
5 7

测试结果:






这篇关于DFS --- Depth First Search 深度优先搜索算法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1035744

相关文章

深度解析Java DTO(最新推荐)

《深度解析JavaDTO(最新推荐)》DTO(DataTransferObject)是一种用于在不同层(如Controller层、Service层)之间传输数据的对象设计模式,其核心目的是封装数据,... 目录一、什么是DTO?DTO的核心特点:二、为什么需要DTO?(对比Entity)三、实际应用场景解析

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

深度解析Python装饰器常见用法与进阶技巧

《深度解析Python装饰器常见用法与进阶技巧》Python装饰器(Decorator)是提升代码可读性与复用性的强大工具,本文将深入解析Python装饰器的原理,常见用法,进阶技巧与最佳实践,希望可... 目录装饰器的基本原理函数装饰器的常见用法带参数的装饰器类装饰器与方法装饰器装饰器的嵌套与组合进阶技巧

深度解析Spring Boot拦截器Interceptor与过滤器Filter的区别与实战指南

《深度解析SpringBoot拦截器Interceptor与过滤器Filter的区别与实战指南》本文深度解析SpringBoot中拦截器与过滤器的区别,涵盖执行顺序、依赖关系、异常处理等核心差异,并... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现

深度解析Spring AOP @Aspect 原理、实战与最佳实践教程

《深度解析SpringAOP@Aspect原理、实战与最佳实践教程》文章系统讲解了SpringAOP核心概念、实现方式及原理,涵盖横切关注点分离、代理机制(JDK/CGLIB)、切入点类型、性能... 目录1. @ASPect 核心概念1.1 AOP 编程范式1.2 @Aspect 关键特性2. 完整代码实

SpringBoot开发中十大常见陷阱深度解析与避坑指南

《SpringBoot开发中十大常见陷阱深度解析与避坑指南》在SpringBoot的开发过程中,即使是经验丰富的开发者也难免会遇到各种棘手的问题,本文将针对SpringBoot开发中十大常见的“坑... 目录引言一、配置总出错?是不是同时用了.properties和.yml?二、换个位置配置就失效?搞清楚加

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

Python中文件读取操作漏洞深度解析与防护指南

《Python中文件读取操作漏洞深度解析与防护指南》在Web应用开发中,文件操作是最基础也最危险的功能之一,这篇文章将全面剖析Python环境中常见的文件读取漏洞类型,成因及防护方案,感兴趣的小伙伴可... 目录引言一、静态资源处理中的路径穿越漏洞1.1 典型漏洞场景1.2 os.path.join()的陷

Spring Boot拦截器Interceptor与过滤器Filter深度解析(区别、实现与实战指南)

《SpringBoot拦截器Interceptor与过滤器Filter深度解析(区别、实现与实战指南)》:本文主要介绍SpringBoot拦截器Interceptor与过滤器Filter深度解析... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现与实

MyBatis分页插件PageHelper深度解析与实践指南

《MyBatis分页插件PageHelper深度解析与实践指南》在数据库操作中,分页查询是最常见的需求之一,传统的分页方式通常有两种内存分页和SQL分页,MyBatis作为优秀的ORM框架,本身并未提... 目录1. 为什么需要分页插件?2. PageHelper简介3. PageHelper集成与配置3.