The code below is used to find the shortest path based on the constraints (of maxTotalDist and maxDistOutdoors) given. I have got it mostly working, but the problem I am having is that even though I am able to find a path (on some given problems). I'm not able to confirm that I've found it.
For example, if a path is found, the "visited" list, in the code, should be the full path that I obtained (the path is then tested on whether or not it is good enough to be the "shortest path"). But, any print statements I try to implement to see which nodes I'm hitting aren't being printed. (i.e. Trying to find the path from Node 'A' to Node 'C', Node A has children ['b', 'c']. If I make the sample if statement (if visited[0] == 'c'), the print statement will not go off).
Anyway, I know this is probably really badly written, but any help would contribute. Thanks.
def bruteForceSearch1(digraph, start, end, maxTotalDist, maxDistOutdoors, visited = [])
if not (digraph.hasNode(start) and digraph.hasNode(end)):
raise ValueError('Start or End not in graph')
path = [str(start)]
if start == end:
return path
shortest = None
#This is the iterator talked about in the question
for node in digraph.childrenOf(start):
if (str(node) not in visited):
visited = visited + [str(node)]
#Sample print statement
if visited[0] == 'nodeName':
print "Node is part of search"
newPath = bruteForceSearch1(digraph, node, end, maxTotalDist, maxDistOutdoors, visited)
if newPath == None:
continue
if shortest != None:
totalShort, outdoorShort = digraph.getDistances(shortest)
total, outdoor = digraph.getDistances(path + newPath)
if (total <= maxTotalDist) and (outdoor <= maxDistOutdoors):
if (shortest == None):
shortest = newPath
else:
if (len(newPath) < len(shortest)):
shortest = newPath
elif (len(newPath) == len(shortest)) and (total <= totalShort) and (outdoor <= outdoorShort):
shortest = newPath
if shortest != None:
path = path + shortest
else:
path = None
return path