Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1782092
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Python/Ruby

2018-04-16 21:49:30

这一篇做这系列文章的结束,Graph是我感觉很难的数据结构,实现数据结构,用数据结构再来解决问题。
前面Tree那一篇里面其实也缺少很多复杂的数据结构,例如AVL, RBTree,Skip-List, 这些复杂的数据结构实现复杂.到Graph我记得原来看DAG想死的心都有,现在仍然是觉得这些数据结构真他娘的复杂。死记硬背可以,让我自己去写个实现,用数据结构和算法解决问题真难。

图这里其实代码就只有DFS和BFS, DFS是用stack, BFS用queue.当然实际代码里面都用了list. 求2个Vertex之间的最短路径用BFS.

点击(此处)折叠或打开

  1. def dfs(graph, start):
  2.     visited = set()
  3.     stack = [start]
  4.     
  5.     while stack:
  6.         vertex = stack.pop()
  7.         
  8.         if vertex not in visited:
  9.             visited.add(vertex)
  10.             stack.extend(graph[vertex] - visited)
  11.     
  12.     return visited

  13. #recursion dfs
  14. def rec_dfs(graph, start, visited = None):
  15.     if visited == None:
  16.         visited = set()
  17.     visited.add(start)
  18.     
  19.     for next in graph[start] - visited:
  20.         rec_dfs(graph, next, visited)
  21.     
  22.     return visited
  23.     

  24. def dfs_paths(graph, start, dest):
  25.     stack = [(start, [start])]
  26.     
  27.     while stack:
  28.         (vertex, path ) = stack.pop()
  29.         for next in graph[vertex] - set(path):
  30.             if next == dest:
  31.                 yield path + [next]
  32.             else:
  33.                 stack.append((next, path + [next]))
  34.                 
  35.         
  36. def test_dfs():
  37.     
  38.     graph = {'A': set(['B', 'C']),
  39.              'B': set(['A', 'D', 'E']),
  40.              'C': set(['A','F']),
  41.              'D': set(['B']),
  42.              'E': set(['B', 'F']),
  43.              'F': set(['C','E'])
  44.              }
  45.     
  46.     print(dfs(graph, 'A'))
  47.     print(rec_dfs(graph, 'A'))
  48.     print(list(dfs_paths(graph, 'A', 'F')))
  49.     

  50. test_dfs()


  51. def bfs(graph, start):
  52.     visited = set()
  53.     stack = [start]
  54.     
  55.     while stack:
  56.         vertex = stack.pop(0)
  57.         
  58.         if vertex not in visited:
  59.             visited.add(vertex)
  60.             stack.extend(graph[vertex] - visited)
  61.     
  62.     return visited
  63.     

  64. def bfs_paths(graph, start, dest):
  65.     stack = [(start, [start])]
  66.     
  67.     while stack:
  68.         (vertex, path ) = stack.pop(0)
  69.         for next in graph[vertex] - set(path):
  70.             if next == dest:
  71.                 yield path + [next]
  72.             else:
  73.                 stack.append((next, path + [next]))
  74.                 
  75.         

  76. def shortest_path(graph, start, dest):
  77.     
  78.     try:
  79.         return next(bfs_paths(graph, start, dest))
  80.     except StopIteration:
  81.         return None
  82.         

  83. def test_bfs():
  84.     
  85.     graph = {'A': set(['B', 'C']),
  86.              'B': set(['A', 'D', 'E']),
  87.              'C': set(['A','F']),
  88.              'D': set(['B']),
  89.              'E': set(['B', 'F']),
  90.              'F': set(['C','E'])
  91.              }
  92.     
  93.     print(bfs(graph, 'A'))
  94.     
  95.     print(list(bfs_paths(graph, 'A', 'F')))
  96.     
  97.     print(shortest_path(graph, 'A','F'))

  98. test_bfs()


阅读(950) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~