- '''常见数据结构-图'''
- '''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'))
来源: