Skip to content

Instantly share code, notes, and snippets.

@mikezink
Created October 17, 2019 03:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikezink/a1defedba99be413d00d0e8ed09c14a0 to your computer and use it in GitHub Desktop.
Save mikezink/a1defedba99be413d00d0e8ed09c14a0 to your computer and use it in GitHub Desktop.
from pythonds.graphs import Graph
def buildGraph(wordFile):
d = {}
g = Graph()
wfile = open(wordFile,'r')
# create buckets of words that differ by one letter
for line in wfile:
word = line[:-1]
# print(word)
for i in range(len(word)):
bucket = word[:i] + '_' + word[i+1:]
if bucket in d:
d[bucket].append(word)
else:
d[bucket] = [word]
# add vertices and edges for words in the same bucket
for bucket in d.keys():
for word1 in d[bucket]:
for word2 in d[bucket]:
if word1 != word2:
g.addEdge(word1,word2)
return g
© 2019 GitHub, Inc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment