用链式前向星或者邻接表存图会更加方便的 懒得改了就这样吧 注释之后有时间补上
因为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 = state
self.parent = parent
self.action = action
self.path_cost = path_cost
def main():
global _city_info
import_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:
break
result = result.parent
size = 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_info
def recursive_dls(node, dst_state, limit):
if node.state == dst_state:
return node
elif limit == 0:
return 'cutoff'
else:
cutoff_occurred = False
for 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 == '':
continue
child = 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 = True
elif result is not None:
return result
return '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_info
for depth in range(sys.maxsize):
print("%s\n" % depth);
result = depth_limited_search(src_state, dst_state, depth)
if result != 'cutoff':
return result
if __name__ == '__main__':
main()