# Explores the network of words D starting at the source word. If the exploration ends # without reaching the target word, then there is no path between the source and the target. # In this case the function returns False. Otherwise, there is a path and the function # returns True. def searchWordNetwork(source, target, D): # Initialization: processed and reached are two dictionaries that will help in the # exploration. # reached: contains all words that have been reached but not processed. # processed: contains all words that have been reached and processed, i.e., their neighbors have also been explored. # The values of keys are not useful at this stage of the program and so we use 0 as dummy values. processed = {source:0} reached = {} for e in D[source]: reached[e] = source # the value in the dictionary of a key k is the "parent" of k # Repeat until reached set becomes empty or target is reached while reached: # Check if target is in reached; this would imply there is path from source to target if target in reached: processed.update({target:reached[target]}) return processed # Pick an item in reached and process it item = reached.popitem() # returns an arbitrary key-value pair as a tuple newWord = item[0] parent = item[1] # Find all neighbors of this item and add new neighbors to reached processed[newWord] = parent for neighbor in D[newWord]: if neighbor not in reached and neighbor not in processed: reached[neighbor] = newWord return {}