-
Notifications
You must be signed in to change notification settings - Fork 0
/
词梯问题.py
72 lines (60 loc) · 1.87 KB
/
词梯问题.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
! 如何将大量的单词放入到图中:
1.将单词作为顶点的key,如果两个单词之间只相差一个字母,就在他们之间设置一条边
2.无向图==顶点之间没有权重
"""
from pythonds.graphs.adjGraph import Graph
from pythonds.basic.queue import Queue
import os
def buildGraph(wordFile):
d = {}
g = Graph()
# 获取四字母单词集
wfile = open(wordFile, "r")
for line in wfile:
word = line.strip()
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 in d.keys():
for word1 in d[bucket]:
for word2 in d[bucket]:
if word1 != word2:
g.addEdge(word1, word2)
return g
# BFS算法
def bfs(g, start):
start.setDistance(0)
start.setPred(None)
# 设置队列
vertQueue = Queue()
vertQueue.enqueue(start)
while vertQueue.size() > 0:
# 取队首作为当前顶点
currentVert = vertQueue.dequeue()
for nbr in currentVert.getConnections():
# 遍历临街顶点
if nbr.getColor() == "white":
nbr.setColor("gray")
nbr.setDistance(currentVert.getDistance() + 1)
nbr.setPred(currentVert)
vertQueue.enqueue(nbr)
# 设置当前顶点为黑色
currentVert.setColor("black")
def traverse(y):
x = y
while x.getPred():
print(x.getId())
x = x.getPred()
print(x.getId())
if __name__ == "__main__":
xpth = os.path.dirname(os.path.abspath(__file__))
os.chdir(xpth)
wordgraph = buildGraph("./word.txt")
bfs(wordgraph, wordgraph.getVertex("FOOL"))
traverse(wordgraph.getVertex("SAGE"))