234cfds1 发表于 2018-8-15 11:47:24

python数据结构之图

'''常见数据结构-图'''  

  
'''a指向b,a指向d,依次类推'''
  
charts = {'a':['b','d'],'c':['e'],'d':['c','e']}
  

  
'''遍历图中的路径'''
  
def path(chart,x,y,pathd=[]):
  
    pathd = pathd +
  
    if x == y:
  
      return pathd
  
    if not chart.has_key(x):
  
      return None
  

  
    for jd in chart:
  
      if jd not in pathd:
  
            newjd =path(chart,jd,y,pathd)
  
            if newjd:
  
                return newjd
  

  
print(path(charts,'a','e'))
页: [1]
查看完整版本: python数据结构之图