-
-
Save mikezink/a1defedba99be413d00d0e8ed09c14a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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