do i need to call Graph.find_path ?
>>g = Graph({'A': ['B', 'C'],
'B': ['C', 'D'],
'C': ['D'],
'D': ['C'],
'E': ['F'],
'F': ['C']})
>>g
<__main__.Gra ph instance at 0x01D74378>
>>g.find_all_pa ths('A', 'C')
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
>>g.find_path(' A', 'C')
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 4 6330
Wrong:
newpath = find_path(self. dictionary, node, end, path)
newpaths = find_all_paths( self.dictionary , node, end, path)
newpath = find_shortest_p ath(self.dictio nary, node, end, path)
Correct;
newpath = self.find_path( self.dictionary , node, end, path)
newpaths = self.find_all_p aths(self.dicti onary, node, end, path)
newpath = self.find_short est_path(self.d ictionary, node, end, path)
Regards
Roopesh
klant a écrit :
do i need to call Graph.find_path ?
>>>g = Graph({'A': ['B', 'C'],
'B': ['C', 'D'],
'C': ['D'],
'D': ['C'],
'E': ['F'],
'F': ['C']})
>>>g
<__main__.Gra ph instance at 0x01D74378>
>>>g.find_all_p aths('A', 'C')
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
>>>g.find_path( 'A', 'C')
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:
Unless you need to ensure compat with ages-old Python versions, better
to use newstyle classes:
class Graph(object):
def __init__(self, dictionary):
self.dictionary = dictionary
def find_path(self, start, end, path=[]):
http://effbot.org/pyfaq/why-are-defa...en-objects.htm
Unless you exactly what you're doing (and given your current problem, I
can tell it's not the case), *don't* use mutable containers as default
function argument values.
def find_path(self, start, end, path=None):
if path is None:
path = []
path = path + [start]
path.append(sta rt)
if start == end:
return path
if not self.dictionary .has_key(start) :
if start not in self.dictionnar y:
return None
for node in self.dictionary[start]:
if node not in path:
newpath = find_path(self. dictionary, node, end, path)
newpath = self.find_path( ...)
(snip remaining code - same problems, same solutions...)
class Graph(object):
where does anyone write like that? I've seen only examples like i have
written.
is the object then passed to init?
class Graph(object):
def __init__(self, dictionary):
self.structure = dictionary
or
class Graph(object):
def __init__(self, object):
self.structure = object
i dont get it.
and "mutable containers", do you refer to "path=[]" as a parameter.
>
if start == end:
return path
if not self.dictionary .has_key(start) :
if start not in self.dictionnar y:
return None
for node in self.dictionary[start]:
if node not in path:
newpath = find_path(self. dictionary, node, end, path)
newpath = self.find_path( ...)
(snip remaining code - same problems, same solutions...)
it is to incoherent fo follow what you mean here(not meaning to sound
rude i appreciate the help). This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: da Vinci |
last post by:
Greetings.
I want to get everyone's opinion on the use of recursion.
We covered it in class tonight and I want a good solid answer from
people in the "know" on how well recursion is accepted in modern day
applications.
Should we readily use it when we can or only when absolutly forced to
use it?
|
by: Csaba Gabor |
last post by:
I suppose spring fever has hit at alt.math.undergrad since I didn't get
any rise from
them when I posted this a week ago, so I am reposting this to some of
my favorite
programming groups:
I'm looking for problems that have double (or more) recursion, where
the split is not
clean (ie. where there may be overlap). Let's call this overlapped
recursion, where the
|
by: Csaba Gabor |
last post by:
I suppose spring fever has hit at alt.math.undergrad since I didn't get
any rise from
them when I posted this a week ago, so I am reposting this to some of
my favorite
programming groups:
I'm looking for problems that have double (or more) recursion, where
the split is not
clean (ie. where there may be overlap). Let's call this overlapped
recursion, where the
|
by: NOO Recursion |
last post by:
Hi everyone! I am trying to write a program that will search a 12x12 for a
thing called a "blob". A blob in the grid is made up of asterisks. A blob
contains at least one asterisk. If an asterisk is in a blob, an asterisk
that is contiguous to it is in the same blob. If a blob has more than two
asterisks, then each asterisk in the blob is contiguous to at least one
other asterisk in the blob. For example this 12x12 grid has 6 blobs. ...
|
by: Muzammil |
last post by:
int harmonic(int n) {
if (n=1) {
return 1;
}
else {
return harmonic(n-1)+1/n;
}
}
can any help me ??
| |
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |