Python 最短距离算法
我想创建一个简单的广度优先搜索算法,它返回最短路径Python 最短距离算法,python,breadth-first-search,Python,Breadth First Search,我想创建一个简单的广度优先搜索算法,它返回最短路径 for i in movie_info: actor_info[i] = movie_info[i] def find_shortest_path(graph, start, end, path=[]): path = path + [start] if start == end: return path if not start in graph: return None
for i in movie_info:
actor_info[i] = movie_info[i]
def find_shortest_path(graph, start, end, path=[]):
path = path + [start]
if start == end:
return path
if not start in graph:
return None
shortest = None
for node in graph[start]:
if node not in path:
newpath = find_shortest_path(graph, node, end, path)
if newpath:
if not shortest or len(newpath) < len(shortest):
shortest = newpath
return shortest
L = find_shortest_path(actor_info, 'act1', 'act2')
print len([i for i in L if i in movie_info])
演员信息字典将演员和演员映射到演员出现的电影列表:
actor_info = { "act1" : ["movieC", "movieA"], "act2" : ["movieA", "movieB"],
"act3" :["movieA", "movieB"], "act4" : ["movieC", "movieD"],
"act5" : ["movieD", "movieB"], "act6" : ["movieE"],
"act7" : ["movieG", "movieE"], "act8" : ["movieD", "movieF"],
"KevinBacon" : ["movieF"], "act10" : ["movieG"], "act11" : ["movieG"] }
与此相反,电影将映射到其中出现的演员列表:
movie_info = {'movieB': ['act2', 'act3', 'act5'], 'movieC': ['act1', 'act4'],
'movieA': ['act1', 'act2', 'act3'], 'movieF': ['KevinBacon', 'act8'],
'movieG': ['act7', 'act10', 'act11'], 'movieD': ['act8', 'act4', 'act5'],
'movieE': ['act6', 'act7']}
打个电话
shortest_dictance("act1", "Kevin Bacon", actor_info, movie_info)
我应该得到3
,因为act1
出现在movieC
和Act4
一起出现在movieD
和Act8
一起出现在movief
和KevinBacon
一起。所以最短的距离是3
到目前为止,我有:
def shotest_distance(actA, actB, actor_info, movie_info):
'''Return the number of movies required to connect actA and actB.
If theres no connection return -1.'''
# So we keep 2 lists of actors:
# 1.The actors that we have already investigated.
# 2.The actors that need to be investigated because we have found a
# connection beginning at actA. This list must be
# ordered, since we want to investigate actors in the order we
# discover them.
# -- Each time we put an actor in this list, we also store
# her distance from actA.
investigated = []
to_investigate = [actA]
distance = 0
while actB not in to_investigate and to_investigate!= []:
for actor in to_investigate:
to_investigated.remove(actA)
investigated.append(act)
for movie in actor_info[actor]:
for co_star in movie_info[movie]:
if co_star not in (investigated and to_investigate):
to_investigate.append(co_star)
....
....
return d
我无法找到适当的方法来跟踪在每次代码迭代中发现的距离。此外,代码在时间上似乎非常无效。首先从中创建一个图形来连接所有节点,然后运行最短路径代码(可能有一个高效的图形库来执行此操作,而不是下面提到的函数,不过这是一个优雅的图形库)然后从最短路径中找出所有电影名称的数目
for i in movie_info:
actor_info[i] = movie_info[i]
def find_shortest_path(graph, start, end, path=[]):
path = path + [start]
if start == end:
return path
if not start in graph:
return None
shortest = None
for node in graph[start]:
if node not in path:
newpath = find_shortest_path(graph, node, end, path)
if newpath:
if not shortest or len(newpath) < len(shortest):
shortest = newpath
return shortest
L = find_shortest_path(actor_info, 'act1', 'act2')
print len([i for i in L if i in movie_info])
电影中的i的信息:
演员信息[i]=电影信息[i]
def查找最短路径(图形、开始、结束、路径=[]):
路径=路径+[开始]
如果开始=结束:
返回路径
如果未在图形中启动:
一无所获
最短=无
对于图形[start]中的节点:
如果节点不在路径中:
newpath=查找最短路径(图形、节点、端点、路径)
如果是新路径:
如果不是最短的或len(新路径)
查找最短路径来源:这看起来很有效。它跟踪当前的一组电影。对于每一步,它都会查看所有尚未被考虑(“看过”)的一步之遥电影 输出是
All movies with act1 set(['movieC', 'movieA'])
Movies with actors from those movies set(['movieB', 'movieC', 'movieA', 'movieD'])
New movies with actors from those movies set(['movieB', 'movieD'])
Movies with actors from those movies set(['movieB', 'movieC', 'movieA', 'movieF', 'movieD'])
New movies with actors from those movies set(['movieF'])
3
这是一个版本,它返回构成最小连接的电影列表(无连接为无,如果actA和actB相同,则为空列表)
以下是输出:
The Kevin Bacon Number for act1 is 3
Movies are: movieC, movieD, movieF
编码nit:在图形中使用
start
而不是graph.has_key(start)
。这是面试问题吗?
The Kevin Bacon Number for act1 is 3
Movies are: movieC, movieD, movieF