每个节点都与邻近节点相连,但是这种链接不一定是一对一,而是多种多样,也可能出现一对多或者多对多或者一对多?
书上说能用散列表表示这种关系?散列表的确可以将键映射到值,但是每个值不是只能映射到一个键吗?注意 我们在这里只关注当前一个节点以及此节点对应的下面的,也就是说只考虑一对多模式,一 代表 键(unique),多 代表 值。如下:
graph={}
graph['you']=['alice','bob','clarire']
graph['bob']=['peggy']
graph['alice']=[]
graph['clarire']=[]
以上就是一个典型的有向图 的基础知识 那么深度优先算法是怎么实现的呢?
实现算法:
创建一个队列 用于储存要检查的人
从队列中弹出一个人
检查这个人是不是我们想要的结果
如果是 那么就大功告成 如果不是 就将这个人的所有邻居加入队列
回到第二步:从队列中弹出一个人 这样一直循环下去 直到大功告成
如果最后已经没有什么可以弹出的人了 那么就是说明图中根本没有目标路径
下面是实现算法的具体
from collections import deque
search_queue = deque()
search_queue += graph['you']
while search_queue:
person = search_queue.popleft()
if person_is_seller(person):
return True
else:
search_queue += graph[person]
return False
def person_is_seller(name):
return name[-1] == 'm'
在多对一的情况时 某元素会被添加了两次 因此除了第一次之外都是做无用功,所以每次检查完一个人后,要标记为已检查,否则会导致无限循环。考虑到这一点,最终代码如下:
def search(name):
search_queue = deque()
search_queue += graph[name]
searched = []
while search_queue:
person = search_queue.popleft()
if not person in searched:
if person_is_seller(person):
return True
else:
search_queue += graph[person]
searched.append(person)
return False
infinity = float('inf')
costs={}
costs['a'] = 6
costs['b'] = 2
costs['fin'] = infinity
parents = {}
parents['a'] = 'start'
parents['b'] = 'start'
parents['fin'] = None
processed =[]
node = find_lowest_cost_node(costs)
while node is not None:
cost = costs[node]
neighbors = graph[node]
for n in neighbors.keys():
new_cost = cost + neighbors[n]
if costs[n] > new_cost:
costs[n] = new_cost
parents[n] = node
processed.append(node)
node = find_lowest_cost_node(costs)
def find_lowest_cost_node(costs):
lowest_cost = float('inf')
lowest_cost_node = node
for node in costs:
cost = costs[node]
if cost < lowest_cost and node not in processed:
lowest_cost = cost
lowest_cost_node = node
return lowest_cost_node
arr1 = [1,2,3,4,5]
arr2 = map(lambda x: 2*x ,arr1)
arr1 =
arr2 = map(download_page, arr1)
arr1 = [1,2,3,4,5]
refuce(lambda x,y: x+y, arr1)