3.4.5 迭代加深的深度优先搜索(iterative-deepening search) --- 实现代码附详细注释

本文主要是介绍3.4.5 迭代加深的深度优先搜索(iterative-deepening search) --- 实现代码附详细注释,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述

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

用链式前向星或者邻接表存图会更加方便的 懒得改了就这样吧 注释之后有时间补上
因为dfs是相同代价搜索 所以路径代价没有用处

import pandas as pd
import sys
from pandas import Series, DataFrame# 城市信息:city1 city2 path_cost
_city_info = None# 按照路径消耗进行排序的FIFO,低路径消耗在前面
# 优先队列
_frontier_priority = []# 节点数据结构
class Node:def __init__(self, state, parent, action, path_cost):self.state = stateself.parent = parentself.action = actionself.path_cost = path_costdef main():global _city_infoimport_city_info()while True:src_city = input('输入初始城市\n')dst_city = input('输入目的城市\n')# result = breadth_first_search(src_city, dst_city)result = iterative_deepening_search(src_city, dst_city) # 搜索路径# print(result.state)if not result:print('从城市: %s 到城市 %s 查找失败' % (src_city, dst_city))else:print('从城市: %s 到城市 %s 查找成功' % (src_city, dst_city))path = [] # 记录路径while True: # 回溯 输出路径path.append(result.state)if result.parent is None:breakresult = result.parentsize = len(path)for i in range(size):if i < size - 1:print('%s->' % path.pop(), end='') # pop 出栈操作else:print(path.pop())def import_city_info(): #初始化数据集global _city_info data = [{'city1': 'Oradea', 'city2': 'Zerind', 'path_cost': 71},{'city1': 'Oradea', 'city2': 'Sibiu', 'path_cost': 151},{'city1': 'Zerind', 'city2': 'Arad', 'path_cost': 75},{'city1': 'Arad', 'city2': 'Sibiu', 'path_cost': 140},{'city1': 'Arad', 'city2': 'Timisoara', 'path_cost': 118},{'city1': 'Timisoara', 'city2': 'Lugoj', 'path_cost': 111},{'city1': 'Lugoj', 'city2': 'Mehadia', 'path_cost': 70},{'city1': 'Mehadia', 'city2': 'Drobeta', 'path_cost': 75},{'city1': 'Drobeta', 'city2': 'Craiova', 'path_cost': 120},{'city1': 'Sibiu', 'city2': 'Fagaras', 'path_cost': 99},{'city1': 'Sibiu', 'city2': 'Rimnicu Vilcea', 'path_cost': 80},{'city1': 'Rimnicu Vilcea', 'city2': 'Craiova', 'path_cost': 146},{'city1': 'Rimnicu Vilcea', 'city2': 'Pitesti', 'path_cost': 97},{'city1': 'Craiova', 'city2': 'Pitesti', 'path_cost': 138},{'city1': 'Fagaras', 'city2': 'Bucharest', 'path_cost': 211},{'city1': 'Pitesti', 'city2': 'Bucharest', 'path_cost': 101},{'city1': 'Bucharest', 'city2': 'Giurgiu', 'path_cost': 90},{'city1': 'Bucharest', 'city2': 'Urziceni', 'path_cost': 85},{'city1': 'Urziceni', 'city2': 'Vaslui', 'path_cost': 142},{'city1': 'Urziceni', 'city2': 'Hirsova', 'path_cost': 98},{'city1': 'Neamt', 'city2': 'Iasi', 'path_cost': 87},{'city1': 'Iasi', 'city2': 'Vaslui', 'path_cost': 92},{'city1': 'Hirsova', 'city2': 'Eforie', 'path_cost': 86}]_city_info = DataFrame(data, columns=['city1', 'city2', 'path_cost'])print(_city_info)def depth_limited_search(src_state, dst_state, limit):"""[Figure 3.17]"""global _city_infodef recursive_dls(node, dst_state, limit):if node.state == dst_state:return nodeelif limit == 0:return 'cutoff'else:cutoff_occurred = Falsefor i in range(len(_city_info)):dst_city = ''if _city_info['city1'][i] == node.state:dst_city = _city_info['city2'][i]elif _city_info['city2'][i] == node.state:dst_city = _city_info['city1'][i]if dst_city == '':continuechild = Node(dst_city, node, 'go', node.path_cost + _city_info['path_cost'][i])result = recursive_dls(child, dst_state, limit - 1)if result == 'cutoff':cutoff_occurred = Trueelif result is not None:return resultreturn 'cutoff' if cutoff_occurred else None# Body of depth_limited_search:return recursive_dls(Node(src_state, None, None, 0), dst_state, limit)def iterative_deepening_search(src_state, dst_state):"""[Figure 3.18]"""global _city_infofor depth in range(sys.maxsize):print("%s\n" % depth);result = depth_limited_search(src_state, dst_state, depth)if result != 'cutoff':return resultif __name__ == '__main__':main()

这篇关于3.4.5 迭代加深的深度优先搜索(iterative-deepening search) --- 实现代码附详细注释的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

pandas中位数填充空值的实现示例

《pandas中位数填充空值的实现示例》中位数填充是一种简单而有效的方法,用于填充数据集中缺失的值,本文就来介绍一下pandas中位数填充空值的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是中位数填充?为什么选择中位数填充?示例数据结果分析完整代码总结在数据分析和机器学习过程中,处理缺失数

Golang HashMap实现原理解析

《GolangHashMap实现原理解析》HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持高效的插入、查找和删除操作,:本文主要介绍GolangH... 目录HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持

Pandas使用AdaBoost进行分类的实现

《Pandas使用AdaBoost进行分类的实现》Pandas和AdaBoost分类算法,可以高效地进行数据预处理和分类任务,本文主要介绍了Pandas使用AdaBoost进行分类的实现,具有一定的参... 目录什么是 AdaBoost?使用 AdaBoost 的步骤安装必要的库步骤一:数据准备步骤二:模型

使用Pandas进行均值填充的实现

《使用Pandas进行均值填充的实现》缺失数据(NaN值)是一个常见的问题,我们可以通过多种方法来处理缺失数据,其中一种常用的方法是均值填充,本文主要介绍了使用Pandas进行均值填充的实现,感兴趣的... 目录什么是均值填充?为什么选择均值填充?均值填充的步骤实际代码示例总结在数据分析和处理过程中,缺失数

Java对象转换的实现方式汇总

《Java对象转换的实现方式汇总》:本文主要介绍Java对象转换的多种实现方式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java对象转换的多种实现方式1. 手动映射(Manual Mapping)2. Builder模式3. 工具类辅助映

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

如何为Yarn配置国内源的详细教程

《如何为Yarn配置国内源的详细教程》在使用Yarn进行项目开发时,由于网络原因,直接使用官方源可能会导致下载速度慢或连接失败,配置国内源可以显著提高包的下载速度和稳定性,本文将详细介绍如何为Yarn... 目录一、查询当前使用的镜像源二、设置国内源1. 设置为淘宝镜像源2. 设置为其他国内源三、还原为官方