-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworklink.py
More file actions
65 lines (51 loc) · 1.54 KB
/
worklink.py
File metadata and controls
65 lines (51 loc) · 1.54 KB
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
# 欧拉路关系,要使得一个图形可以一笔画完,必须满足如下两个条件:
# 图形必须是连通的不能有孤立的点。
# 图中拥有奇数连接边的点必须是0或2。
from collections import defaultdict
from backtrack import Problem,BackTrack
def circle(wordlist):
"""建立邻接表"""
worddict = defaultdict(list)
worddict1 = defaultdict(list)
for word in wordlist:
worddict[word[0]].append(word)
worddict[word].append(word[-1])
for word in wordlist:
worddict1[word] = worddict[word[-1]]
return worddict1
# 有向有环带权图
class GraphBackTrack(BackTrack):
"""
回溯法作图
"""
def __init__(self, **kwargs):
super(GraphBackTrack, self).__init__(**kwargs)
self._solution.append(self.problem.start)
def conflict(self,k):
# 第k个节点,是否前面已经走过
if k < self.solution_size and self._solution[k] in self._solution[:k]:
return True
# 回到出发点
if k == self.solution_size and self._solution[k] != self._solution[0]:
return True
return False
def dfs(self,k=1):
if k > self.solution_size:
print(self._solution)
self.best_solution = self._solution[:]
else:
for node in self.problem.graph[self._solution[-1]]:
self._solution.append(node)
if not self.conflict(k):
self.dfs(k+1)
self._solution.pop()
wordlist = ['abc','cde','efg','ghi','ija']
worddict = circle(wordlist)
print(worddict)
p = Problem()
p.graph = worddict
p.start = wordlist[0]
n = len(wordlist)
bt = GraphBackTrack(problem=p,solution_size=n)
bt.dfs()
print(bt.best_solution)