'''常见数据结构-图'''
'''a指向b,a指向d,依次类推'''
charts = {'a':['b','d'],'c':['e'],'d':['c','e']}
'''遍历图中的路径'''
def path(chart,x,y,pathd=[]):
pathd = pathd + [x]
if x == y:
return pathd
if not chart.has_key(x):
return None
for jd in chart[x]:
if jd not in pathd:
newjd =path(chart,jd,y,pathd)
if newjd:
return newjd
print(path(charts,'a','e'))转载于:https://blog.51cto.com/freshair/1896337
本文介绍了一种图数据结构的表示方法,并提供了一个简单的Python函数来遍历图中的路径。通过实例展示了如何查找从一个节点到另一个节点的路径。

被折叠的 条评论
为什么被折叠?



