Issues with iteration in a recursive function
<p>The code below is used to find the shortest path based on the constraints (of maxTotalDist and maxDistOutdoors) given. I have got it <em>mostly</em> 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 <em>confirm</em> that I've found it. </p>

<p>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 <em>if</em> statement (if visited[0] == 'c'), the print statement will not go off).</p>

<p>Anyway, I know this is probably really badly written, but any help would contribute. Thanks.</p>

<pre><code>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 &lt;= maxTotalDist) and (outdoor &lt;= maxDistOutdoors):
                if (shortest == None):
                    shortest = newPath
                else:
                    if (len(newPath) &lt; len(shortest)):
                        shortest = newPath
                    elif (len(newPath) == len(shortest)) and (total &lt;= totalShort) and (outdoor &lt;= outdoorShort):
                        shortest = newPath
    if shortest != None:
        path = path + shortest
    else:
        path = None
    return path
</code></pre>

<p>All of the recursive calls will see the same value as <code>visited[0]</code>. It will always be the first child of the original <code>start</code> value. Since you're appending all other values to the end of the list, nothing will ever replace that child as the first value in <code>visited</code>.</p>

<p>I think you probably want to check your specific name against <code>node</code> instead of against <code>visited[0]</code>.</p>

<p>There may be some other issues with your code. For instance, you'll never consider a path <code>a-&gt;c-&gt;b</code> if the path <code>a-&gt;b</code> exists (since <code>b</code> will be in <code>visited</code> after <code>a-&gt;b</code> is checked). In some kinds of graphs (where the triangle inequality doesn't hold), the first path might be shorter than the second. If it's not possible in your graph, you could be fine, but it's not something you should assume for all graphs.</p>

python recursion iteration graph-theory