-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_uninformed_search.py
94 lines (75 loc) · 2.58 KB
/
01_uninformed_search.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from collections import deque
def sort(state):
return ''.join(list(filter(lambda token: token in state, list('👴🥬🐑🐺'))))
def is_valid_move(side):
return not ('🥬🐑' in side or '🐑🐺' in side)
def expand(state):
moves = dict()
_from = 0 if '👴' in state[0] else 1
_to = 1 if '👴' in state[0] else 0
_arrow = '->' if _from == 0 else '<-'
_from_elements = list(state[_from])
for a in _from_elements:
moving = ''.join({'👴', a})
key = _arrow + moving
_new_origin = sort(''.join(list(filter(lambda e: e not in moving, _from_elements))))
_new_destination = sort(state[_to] + moving)
if is_valid_move(_new_origin):
if _from == 0:
moves[key] = (_new_origin, _new_destination)
else:
moves[key] = (_new_destination, _new_origin)
return moves
def build_graph():
graph = dict()
queue = deque()
queue.append(('👴🥬🐑🐺', ''))
while queue:
node = queue.popleft()
if node not in graph:
graph[node] = expand(node)
neighbors = list(graph[node].values())
for neighbor in neighbors:
if neighbor not in graph:
queue.append(neighbor)
return graph
def dfs(graph, initial, is_goal):
stack = deque()
visited = set()
stack.append((initial, []))
visited.add(initial)
while stack:
node, path = stack.pop()
print(node, path)
if is_goal(node):
return path, len(path), len(visited)
node_edges = graph[node]
for edge_label, target_node in node_edges.items():
if target_node not in visited:
visited.add(target_node)
stack.append((target_node, path + [edge_label]))
return None
def bfs(graph, initial, is_goal):
queue = deque()
visited = set()
queue.append((initial, []))
visited.add(initial)
while queue:
node, path = queue.popleft()
print(node, path)
if is_goal(node):
return path, len(path), len(visited)
node_edges = graph[node]
for edge_label, target_node in node_edges.items():
if target_node not in visited:
visited.add(target_node)
queue.append((target_node, path + [edge_label]))
return None
def is_goal(node):
return node == ('', '👴🥬🐑🐺')
# knowledge representation
graph = build_graph()
print('DFS')
print(dfs(graph, ('👴🥬🐑🐺', ''), is_goal))
print('BFS')
print(bfs(graph, ('👴🥬🐑🐺', ''), is_goal))