--- nfo/python/scripts/sixdegrees/sixdegrees.py 2008/02/06 01:45:22 1.2 +++ nfo/python/scripts/sixdegrees/sixdegrees.py 2008/02/06 22:58:00 1.3 @@ -1,4 +1,4 @@ -# $Id: sixdegrees.py,v 1.2 2008/02/06 01:45:22 joko Exp $ +# $Id: sixdegrees.py,v 1.3 2008/02/06 22:58:00 joko Exp $ # (c) 2008 Andreas Motl @@ -15,18 +15,24 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from sets import Set +#from sets import Set class Node: def __init__(self, id): self.id = id - self.children = Set() + self.children = [] def addChild(self, child): - self.children.add(child) + # with Set + #self.children.add(child) + + # with list + if not child in self.children: + self.children.append(child) def __repr__(self): + #print "repr-node" children_ids = [] for child in self.children: child_id = str(child.id) @@ -73,6 +79,10 @@ # extended "Depth Limited search (DLS)" implementation # see http://en.wikipedia.org/wiki/Depth-limited_search def DLS(self, node, goal, current_depth, max_depth, result, path=[]): + + # break recursion if node already visited to prevent looping + if node in path: return + path.append(node) #print "checking node %s on level %s" % (node, current_depth) #print "path:", path @@ -84,8 +94,6 @@ mynode = stack.pop() if current_depth < max_depth: self.DLS(mynode, goal, current_depth + 1, max_depth, result, path) - else: - pass path.pop() def DLS_orig(self, node, goal, current_depth, max_depth): @@ -102,6 +110,7 @@ pass def __repr__(self): + #print "repr-graph" payload = '' for key, node in self.index.iteritems(): payload += str(node) + "\n"