图常见算法-广度优先算法python实现
广度优先算法 广度优先搜索:breadth-first search, BFS 该算法用于解决两类问题: 1、节点A到节点B是否有路径? 2、节点A到节点B的哪条路径最短? 算法实现思想: 图的建立使用了散列表,双端队列使用了deque,为了避免死循环,需要使用一个列表searched_queue记录已经查找过的数据 from collections import deque graph = { } graph [ "you" ] = [ "alice" , "bob" , "claire" ] graph [ "bob" ] = [ "anuj" , "peggy" ] graph [ "alice" ] = [ "peggy" ] graph [ "claire" ] = [ "thom" , "jonny" ] graph [ "anuj" ] = [ ] graph [ "peggy" ] = [ ] graph [ "thom" ] = [ "rico" ] graph [ "rico" ] = [ ] graph [ "jonny" ] = [ ] def bsfSearch ( name ) : search_queue = deque ( ) search_queue += graph [ name ] searched_queue = [ ] while