Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1789808
  • 博文数量: 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

2016-06-21 10:37:05

本文内容基本上出自: Grokking.Algorithms.An.illustrated.guide.for.programmers.and.other.curious.people

BFS:解决什么问题?
1. Is there a path from node A to node B?
2. What is the shortest path from node A to node B?
适用于无权重的图,以前的帖子里面的wordladder问题,也是BFS的应用。
wordladder: http://blog.chinaunix.net/uid-7713641-id-5695587.html

下面的例子从“you"开始寻找第一个字符串结束是"m"的字符串,将每个节点的out edge 放入双端队列,然后最左端的出队,依次处理每个element, 是则打印,否则加入一个
叫searched的列表,以后不再来查询同样的element.  

点击(此处)折叠或打开

  1. graph={"you":["alice","bob","claire"],"bob":["anuj","peggy"],"alice":["peggy"],"claire":["thom","jonny"],"anuj":["thom","jonny"],"peggy":[],"thom":[],"jonny":[]}

  2. from collections import deque

  3. def bfs(name):
  4.     search_queue=deque()
  5.     #search_queue+=graph["you"]
  6.     search_queue.extend(graph[name])
  7.     searched=[]
  8.     while search_queue: #while the queue is not empty
  9.         person=search_queue.popleft() #left out
  10.         if not person in searched:
  11.             if person_is_seller(person):
  12.                 print("{} is a mango seller".format(person))
  13.                 return True
  14.             else:
  15.                 #search_queue+=graph[person]
  16.                 search_queue.extend(graph[person])
  17.                 searched.append(person)         #mark as searched
  18.     return False

  19. def person_is_seller(name):
  20.     #return name.startswith("m")
  21.     return name[-1]=='m'

  22. bfs("you")

2. Dijkstra 算法解决什么问题:   
What is the shortest path from node A to node B in a weighted graph(DAG),仅仅适用于有向无环图。

代码在这里:
从"start" 到"fin" 最短的路径是什么

点击(此处)折叠或打开

  1. graph={"start":{"a":6,"b":2},"a":{"fin":1},"b":{"a":3,"fin":5},"fin":{}}

  2. infinity=float("inf")

  3. costs={}
  4. costs["a"]=6
  5. costs["b"]=2
  6. costs["fin"]=infinity

  7. parents={}
  8. parents["a"]="start"
  9. parents["b"]="start"
  10. parents["fin"]=None

  11. processed=[]

  12. def find_lowest_cost_node(costs):
  13.     lowest_cost=float("inf")
  14.     lowest_cost_node=None
  15.     for node in costs: #go throught each node
  16.         cost=costs[node]
  17.         if cost < lowest_cost and node not in processed: #if it's lowest cost so far and has not been processed yet
  18.             lowest_cost=cost #set it as the new lowest-cost node
  19.             lowest_cost_node=node
  20.     return lowest_cost_node

  21. node=find_lowest_cost_node(costs) #find the lowest-cost node that you have not processed yet
  22. while node is not None: #if you have processed all the nodes,this while loop is done
  23.     cost=costs[node]
  24.     neighbors=graph[node]
  25.     for n in neighbors.keys(): #go throught all the neighbors of this node
  26.         new_cost=cost+neighbors[n] #if it's cheaper to get to this neightbor
  27.         if costs[n] > new_cost: #by going through this node
  28.             costs[n]=new_cost #update cost for this node
  29.             parents[n]=node #this node become the new parent for this neighbor
  30.     processed.append(node) #mark the node as procesed
  31.     node=find_lowest_cost_node(costs)


  32. print(costs)

3. Dijkstra只有在权重为正数的时候才适用,如果权重有负数,则应该使用Bellman-Ford algorithm
阅读(1427) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~