我通过重新创建路径寻找算法来自我教学AI(所以不,它不是任务或任何东西),更具体地说是Dijkstra,并设法让一切开始工作(算法扩展完全像Dijkstra和can确定最短路径的权重)。然而,我正在努力提出回溯部分,它采用最短的路径并返回顶点
我经常阅读并研究了很多关于Dijkstra表并且在概念上完全理解它(我可以正确地解决Dijkstra表中的许多问题)但是我无法翻译它返回最短顶点的回溯部分路径。
有人介意给我一些指导吗?
代码:
from heapq import heappop, heappush, heappushpop
import collections
import copy
def min_val(destVal, marked, target_edge):
if(destVal > marked + target_edge):
destVal = marked + target_edge
return destVal
else:
return destVal
def weighted(graph, start, end):
true_graph = copy.deepcopy(graph)
current_min_distance = 0
trail = []
trail_value =[0]
to_expand = collections.deque()
to_expand_list = []
to_expand_list_value = []
new_node = start
trail.append(start)
destVal_node = []
destVal_value = []
full_destVal = {}
parents = {}
print "Starting point: ", graph[start]
lowest_distance = 0;
while True:
mindistance = float("inf")
table_check = []
table_check_values = []
print "____________________________________"
to_remove = []
starting_node = new_node
minigraph = graph[new_node]
print "Starting point: ", new_node
print "Graph of current node: ", graph[new_node]
print "To expand beginning: ", to_expand
table_check = full_destVal.keys()
table_check_values = full_destVal.values()
for node in to_expand:
if node not in minigraph:
minigraph[node] = full_to_expand.get(node)
full_trail = dict(zip(trail, trail_value))
full_to_expand = dict(zip(to_expand_list, to_expand_list_value))
if new_node == end:
print ""
print ""
print "**********************************Soulution Found**********************************"
print ""
print "Trail: ", trail
print "Parents: ", parents
print "Length of shortest path: ", lowest_distance
solution = [end]
n = end
while n in parents and parents[n]:
solution.append(parents[n])
n = parents[n]
return solution[::-1]
#Goes through each node in the minigraph
for node in minigraph:
full_to_expand = dict(zip(to_expand_list, to_expand_list_value))
if node not in to_expand and node not in trail:
to_expand.append(node)
to_expand_list.append(node)
print minigraph
to_expand_list_value.append(minigraph[node])
if node not in trail and to_expand:
parents[node] = new_node
mindistance = float("inf")
if node in full_destVal:
mindistance = full_destVal[node]
else:
minidistance = float("inf")
heappush(q, minigraph[node])
full_to_expand = dict(zip(to_expand_list, to_expand_list_value))
to_compare = min_val(mindistance, int(full_trail.get(starting_node)), int(minigraph[node]))
table_check.append(node)
table_check_values.append(to_compare)
full_table_check = dict(zip(table_check, table_check_values))
to_expand_list_value[to_expand_list.index(node)] = to_compare
mindistance = min(table_check_values)
new_node = table_check[table_check_values.index(min(table_check_values))]
if new_node not in trail:
trail_value.append(min(table_check_values))
trail.append(new_node)
full_trail = dict(zip(trail, trail_value))
full_destVal = dict(zip(table_check, table_check_values))
lowest_distance = mindistance
print "Chosen node:", mindistance
print "Node to visit: ", new_node
print "Complete trail of nodes visited ",full_trail
print "Full to_expand_list: ", full_to_expand
print "Current minigraph",minigraph
full_destVal.pop(new_node, None)
table_check = full_destVal.keys()
table_check_values = full_destVal.values()
full_to_expand.pop(new_node, None)
to_expand_list = full_to_expand.keys()
to_expand_list_value = full_to_expand.values()
#Sample weighted graphs
graph = {'A': {'B':4, 'C':3, 'D': 6},
'B': {'A':3, 'D':8},
'C': {'D':7, 'E':5},
'D': {'E':1},
'E':{}}
grid = {'A': {'B':16, 'C':9, 'D': 35},
'B': {'A':16, 'D':12, 'E':25},
'C': {'A':9, 'D':15, 'F':22},
'D': {'A':35,'B':12,'C':15,'E':14,'F':17, 'G':19},
'E':{'B':25, 'D':14, 'G':8,},
'F': {'C':22,'D':17,'G':14},
'G': {'D':19,'E':8,'F':14},
}
jim = {
'B': {'A': 5, 'D': 1, 'G': 2},
'A': {'B': 5, 'D': 3, 'E': 12, 'F' :5},
'D': {'B': 1, 'G': 1, 'E': 1, 'A': 3},
'G': {'B': 2, 'D': 1, 'C': 2},
'C': {'G': 2, 'E': 1, 'F': 16},
'E': {'A': 12, 'D': 1, 'C': 1, 'F': 2},
'F': {'A': 5, 'E': 2, 'C': 16}}
#print weighted(graph, 'A','E')
print weighted(grid, 'A','G')
#print weighted(jim, 'A','G')