本文主要是介绍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) --- 实现代码附详细注释的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!