do i need to call Graph.find_path ?
'B': ['C', 'D'],
'C': ['D'],
'D': ['C'],
'E': ['F'],
'F': ['C']})
<__main__.Gra ph instance at 0x01D74378>
Traceback (most recent call last):
File "<pyshell#1 0>", line 1, in <module>
g.find_all_path s('A', 'C')
File "C:/Python25/Progs/pygameProgs/visualgraph/graph.py", line 26,
in find_all_paths
newpaths = find_all_paths( self.dictionary , node, end, path)
NameError: global name 'find_all_paths ' is not defined
Traceback (most recent call last):
File "<pyshell#1 1>", line 1, in <module>
g.find_path('A' , 'C')
File "C:/Python25/Progs/pygameProgs/visualgraph/graph.py", line 13,
in find_path
newpath = find_path(self. dictionary, node, end, path)
NameError: global name 'find_path' is not defined
class Graph:
def __init__(self, dictionary):
self.dictionary = dictionary
def find_path(self, start, end, path=[]):
path = path + [start]
if start == end:
return path
if not self.dictionary .has_key(start) :
return None
for node in self.dictionary[start]:
if node not in path:
newpath = find_path(self. dictionary, node, end, path)
if newpath: return newpath
return None
def find_all_paths( self, start, end, path=[]):
path = path + [start]
if start == end:
return [path]
if not self.dictionary .has_key(start) :
return []
paths = []
for node in self.dictionary[start]:
if node not in path:
newpaths = find_all_paths( self.dictionary , node, end,
path)
for newpath in newpaths:
paths.append(ne wpath)
return paths
def find_shortest_p ath(self, start, end, path=[]):
path = path + [start]
if start == end:
return path
if not self.dictionary .has_key(start) :
return None
shortest = None
for node in self.dictionary[start]:
if node not in path:
newpath = find_shortest_p ath(self.dictio nary, node,
end, path)
if newpath:
if not shortest or len(newpath) < len(shortest):
shortest = newpath
return shortest
>>g = Graph({'A': ['B', 'C'],
'C': ['D'],
'D': ['C'],
'E': ['F'],
'F': ['C']})
>>g
>>g.find_all_pa ths('A', 'C')
File "<pyshell#1 0>", line 1, in <module>
g.find_all_path s('A', 'C')
File "C:/Python25/Progs/pygameProgs/visualgraph/graph.py", line 26,
in find_all_paths
newpaths = find_all_paths( self.dictionary , node, end, path)
NameError: global name 'find_all_paths ' is not defined
>>g.find_path(' A', 'C')
File "<pyshell#1 1>", line 1, in <module>
g.find_path('A' , 'C')
File "C:/Python25/Progs/pygameProgs/visualgraph/graph.py", line 13,
in find_path
newpath = find_path(self. dictionary, node, end, path)
NameError: global name 'find_path' is not defined
>>>
class Graph:
def __init__(self, dictionary):
self.dictionary = dictionary
def find_path(self, start, end, path=[]):
path = path + [start]
if start == end:
return path
if not self.dictionary .has_key(start) :
return None
for node in self.dictionary[start]:
if node not in path:
newpath = find_path(self. dictionary, node, end, path)
if newpath: return newpath
return None
def find_all_paths( self, start, end, path=[]):
path = path + [start]
if start == end:
return [path]
if not self.dictionary .has_key(start) :
return []
paths = []
for node in self.dictionary[start]:
if node not in path:
newpaths = find_all_paths( self.dictionary , node, end,
path)
for newpath in newpaths:
paths.append(ne wpath)
return paths
def find_shortest_p ath(self, start, end, path=[]):
path = path + [start]
if start == end:
return path
if not self.dictionary .has_key(start) :
return None
shortest = None
for node in self.dictionary[start]:
if node not in path:
newpath = find_shortest_p ath(self.dictio nary, node,
end, path)
if newpath:
if not shortest or len(newpath) < len(shortest):
shortest = newpath
return shortest
Comment