def build_graph(file): d = {} g = LinkGraph() with open(file) as f: for line in f: word = line[:-1] for i in range(len(word)): bucket = word[:i] + '_' + word[i+1:] if bucket in d: d[bucket].append(word) else: d[bucket] = [word] for bucket, values in d.items(): for word1 in values: for word2 in values: if word1 != word2: g.add_edge(word1, word2) return g
def test_link_bfs(): lg = LinkGraph() lg.add_edge('A', 'B') lg.add_edge('A', 'C') lg.add_edge('A', 'E') lg.add_edge('B', 'A') lg.add_edge('B', 'C') lg.add_edge('B', 'D') lg.add_edge('C', 'D') lg.add_edge('D', 'C') lg.add_edge('E', 'D') lg.add_edge('E', 'F') lg.add_edge('F', 'C') for i in lg.bfs(lg, 'B'): print i
def build_bfs_graph(): g = LinkGraph() g.add_edge('s', 'a') g.add_edge('s', 'c') g.add_edge('s', 'd') g.add_edge('a', 's') g.add_edge('a', 'c') g.add_edge('a', 'e') g.add_edge('b', 'c') g.add_edge('b', 'd') g.add_edge('b', 'g') g.add_edge('c', 's') g.add_edge('c', 'a') g.add_edge('c', 'b') g.add_edge('d', 's') g.add_edge('d', 'b') g.add_edge('e', 'a') g.add_edge('e', 'f') g.add_edge('e', 'g') g.add_edge('f', 'e') g.add_edge('f', 'g') g.add_edge('g', 'b') g.add_edge('g', 'e') g.add_edge('g', 'f') return g