Given this code ...
import Queue def breadthFirstSearch(graph, start, end): q = Queue.Queue() path = [start] q.put(path) while not q.empty(): path = q.get() lastNode = path[len(path) - 1] if lastNode == end: return path for linkNode in graph[lastNode]: if linkNode not in path: newPath = [] newPath = path + [linkNode] q.put(newPath)
Where graph is a dictionary representing a directed graph, for example, {'stack':['overflow'], 'foo':['bar']} , i.e. the stack indicates an overflow, and foo indicates a bar.
Is it possible to optimize this search in width more? Because I plan to use it in a very large dictionary.
source share