python - ValueError: dictionary update sequence element while implementing Breadth First Search -


i trying port breadth first search input graph dictionary of dictionary values:

input graph:

graph = {'a': {'b':'b', 'c':'c'},          'b': {'a':'a', 'd':'d', 'e':'e'},          'c': {'a':'a', 'f':'f'},          'd': {'b':'b'},          'e': {'b':'b', 'f':'f'},          'f': {'c':'c', 'e':'e'}} 

python program

def dictsub(d1,d2):     return {key: d1[key] - d2.get(key, 0) key in d1.keys()}  def bfs_paths(graph, start, goal):     queue = [(start, [start])]     while queue:         (vertex, path) = queue.pop(0)         next in dictsub(graph[vertex], dict(zip([path,path]))):             # print graph[vertex] - set(path)             print graph[vertex]             print set(path)             print vertex             raw_input()             if next == goal:                 yield path + [next]             else:                 queue.append((next, path + [next]))  def shortest_path(graph, start, goal):     try:         return next(bfs_paths(graph, start, goal))     except stopiteration:         return none  print shortest_path(graph, 'a', 'f') # ['a', 'c', 'f'] 

problem

so source code this tutorial. runs if original source code used. when trying implement ran into:

traceback (most recent call last):   file "./bfstest.py", line 66, in <module>     print shortest_path(graph, 'a', 'f') # ['a', 'c', 'f']   file "./bfstest.py", line 62, in shortest_path     return next(bfs_paths(graph, start, goal))   file "./bfstest.py", line 49, in bfs_paths     next in dictsub(graph[vertex], dict(zip([path,path]))): valueerror: dictionary update sequence element #0 has length 1; 2 required 

what have tried:

  1. dictionary update sequence element #0 has length 3; 2 required

  2. error: "dictionary update sequence element #0 has length 1; 2 required" on django 1.4

please feel free suggest better way achieve this. please don't suggest change data structure (it's requirement).

the error comes dict(zip([path, path])), should dict(zip(path, path))

also, dictsub function should this:

def dictsub(d1,d2):     return {key: d1[key] key in d1.keys() if key not in d2} 

alternatively, simplify code using set arithmetics

def bfs_paths(graph, start, goal):     graph = {k: set(v) k, v in graph.items()}     queue = [(start, [start])]     while queue:         (vertex, path) = queue.pop(0)         next in (graph[vertex] - set(path)):             if next == goal:                 yield path + [next]             else:                 queue.append((next, path + [next])) 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -