Advertisement
colinm86

Untitled

Dec 8th, 2019
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. from graph import Graph
  2. from cs260.data_structures.ch_three_hw.queue import Queue
  3.  
  4. def buildGraph(wordFile,length = 4):
  5.     """
  6.    Arguments:
  7.        wordFile {txt file} -- text file of words
  8.        length {int} -- length of word to graph (default: {4})
  9.    
  10.    Returns:
  11.        [Graph] -- build graph
  12.    """    
  13.     d = {}
  14.     graph = Graph()
  15.     wfile = open(wordFile,'r')
  16.     # create buckets of words that differ by one letter
  17.     for line in wfile:
  18.         word = line[:-1]
  19.         if len(word) == length:
  20.             for i in range(len(word)):
  21.                 bucket = word[:i] + '_' + word[i+1:]
  22.                 if bucket in d:
  23.                     d[bucket].append(word)
  24.                 else:
  25.                     d[bucket] = [word]
  26.     # add vertices and edges for words in the same bucket
  27.     for bucket in d.keys():
  28.         for word1 in d[bucket]:
  29.             for word2 in d[bucket]:
  30.                 if word1 != word2:
  31.                     graph.addEdge(word1,word2)
  32.     return graph
  33.  
  34. def breadth_first_search(graph,start):
  35.     """Creates connections between starting word and all
  36.    subsuquent words
  37.    Arguments:
  38.        graph {Graph} -- graph
  39.        start {Vertex} -- starting vertex
  40.    """    
  41.     start.distance = 0
  42.     start.predecessor = None
  43.     vertQueue = Queue()
  44.     vertQueue.enqueue(start)
  45.     while vertQueue.size() > 0:
  46.         currentVert = vertQueue.dequeue()
  47.         for neighbor in currentVert.getConnections():
  48.             if neighbor.color == 'white':
  49.                 neighbor.color = 'gray'
  50.                 neighbor.distance = currentVert.distance + 1
  51.                 neighbor.predecessor = currentVert
  52.                 vertQueue.enqueue(neighbor)
  53.         currentVert.color = 'black'
  54.  
  55. def traverse(final_word):
  56.     """
  57.    Arguments:
  58.        final_word {Vertex} -- Vertex to search back from
  59.    """    
  60.     while final_word.predecessor:
  61.         print(final_word.getId())
  62.         final_word = final_word.predecessor
  63.     print(final_word.getId())
  64.  
  65. def show_path(start_word,end_word):
  66.     """Shows path graph takes to get from start_word to end_word
  67.    Arguments:
  68.        start_word {String} -- [description]
  69.        to_word {String} -- [description]
  70.    """    
  71.     graph = buildGraph("c:/Users/Colin McCullough/Desktop/Colin/CS160/Repos/ColinMcCullough.github.io/cs260/data_structures/ch-seven-hw/wordladder.txt",len(start_word))
  72.     breadth_first_search(graph,graph.vertList[start_word])
  73.     traverse(graph.vertList[end_word])
  74.  
  75. show_path('funny','aaron')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement