Find longest path with BFS
<p>I have a list with 794 three-letter long words. My task is to find the word(s) with the longest path to a given word.  </p>

<p><strong>Definition (children):</strong><br>
Children to a parent word are the parent word with one and only one letter replaced. </p>

<p><strong>Example:</strong><br>
'can', 'run', 'rap' etcetera are children to the word 'ran' (given that those words exist in the list).</p>

<p><strong>Definition (path):</strong><br>
A path is a series of words where each word is generated by exchanging a single letter in the previous.</p>

<pre><code>#Pseudo code
Given a word 
    Put the word in a queue
    Keep a list with all visited words
    As long as the queue is not empty
        Get the first word in the queue
        Generate a list with all the children to this word
        For every children in that list
            If the child is not in the list of visited words
                Append the child to the list of visited words
                Put the child in the queue
    Print the path to the last grandchild.
</code></pre>

<p>My idea is that this will give the longest path since we continue to generate new children until we run out of possible children (that is, children that hasn't already been visited). </p>

<p><strong>Question:</strong><br>
Is my idea valid? How can I test if it actually works?   </p>

<hr>

<p>The actual code can be viewed below, but it might not make any sense without comments. </p>

<p><strong>Edit</strong><br>
Since trees and lists can be a bit slow, I replaced them with sets. </p>

<pre><code>from Queue import Queuenode; import Bintree
with open('word3',encoding='utf-8') as f:
    lista=f.readlines()
    lista=[x.strip('\n') for x in lista]
alfabet='abcdefghijklmnopqrstuvwxyzÃ¥Ã¤Ã¶'
class Word:
    def __init__(self, w, f = None):
        self.word = w
        self.parent = f
children=Bintree.Bintree()
for i in lista:
    children.put(i)
def barn(far):
    barnlista=[]
    for i in range(3):
        for j in range(29):
            empty=''
            empty+=far[0:i]+alfabet[j]+far[i+1:]
            if empty!=far and children._exists(empty):
                barnlista+=[empty]
    return barnlista
ko=Queuenode()
def vag(item):
    start=item
    counter=0
    while start!=None:
        counter+=1
        print(start.word)
        start=start.parent
    print(counter)
def bfsx(startord):
    ko.put(Word(startord))
    besokta=[startord]
    while not ko.isempty():
        first=ko.get()
        lista=barn(first.word)
        for child in lista:
            if child not in besokta:
                besokta+=[child]
                ko.put(Word(child,first))
    vag(first)
</code></pre>

<p>IIUC, this is not guaranteed to work (in fact, you can build cases where it doesn't).</p>

<p>Suppose you start at node <em>a</em>; there is a direct path <em>a &rarr; b</em>; there are also a direct path <em>a &rarr; c</em> and  an indirect path <em>c &rArr; b</em>. </p>

<p>Suppose that when you iterate over the children of <em>a</em>, you encounter <em>b</em> before <em>c</em>. You deal with <em>b</em> and mark it as visited. At some point later you encounter <em>c</em>, and eventually reconsider <em>b</em> again. At that point, however, <em>b</em> is already considered visited, so your algorithm will consider the shorter subpath <em>a &rarr; b</em> rather than the longer one <em>a &rarr; c &rArr; b</em>. </p>

<p>You cannot also get rid of the "visited" mark, as the story behind your graph makes it clear it is not a DAG. If you remove the "visited" logic, you will encounter infinite loops. </p>

python python-3.x python-3.5