-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.python
More file actions
167 lines (132 loc) · 3.68 KB
/
tempCodeRunnerFile.python
File metadata and controls
167 lines (132 loc) · 3.68 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
from collections import deque
import heapq
# ---------------- GRAPH ----------------
graph = {
'A': [('B', 1), ('C', 4)],
'B': [('D', 2), ('E', 5)],
'C': [('F', 1)],
'D': [],
'E': [('F', 1)],
'F': []
}
# For BFS / DFS / DLS / IDDFS / Bidirectional (ignore weights)
simple_graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['F'],
'F': []
}
# ---------------- BFS ----------------
def bfs(start, goal):
q = deque([[start]])
visited = set([start])
while q:
path = q.popleft()
node = path[-1]
if node == goal:
return path
for neigh in simple_graph[node]:
if neigh not in visited:
visited.add(neigh)
q.append(path + [neigh])
return None
# ---------------- DFS ----------------
def dfs(start, goal):
stack = [(start, [start])]
visited = set()
while stack:
node, path = stack.pop()
if node == goal:
return path
if node not in visited:
visited.add(node)
for neigh in reversed(simple_graph[node]):
stack.append((neigh, path + [neigh]))
return None
# ---------------- UNIFORM COST SEARCH ----------------
def uniform_cost(start, goal):
pq = [(0, start, [start])]
visited = set()
while pq:
cost, node, path = heapq.heappop(pq)
if node == goal:
return path, cost
if node not in visited:
visited.add(node)
for neigh, w in graph[node]:
heapq.heappush(pq, (cost + w, neigh, path + [neigh]))
return None
# ---------------- DEPTH LIMITED SEARCH ----------------
def dls(node, goal, limit, path, visited):
if node == goal:
return path
if limit == 0:
return None
visited.add(node)
for neigh in simple_graph[node]:
if neigh not in visited:
res = dls(neigh, goal, limit - 1, path + [neigh], visited)
if res:
return res
return None
# ---------------- IDDFS ----------------
def iddfs(start, goal, max_depth):
for depth in range(max_depth + 1):
visited = set()
res = dls(start, goal, depth, [start], visited)
if res:
return res
return None
# ---------------- BIDIRECTIONAL BFS ----------------
def bidirectional(start, goal):
if start == goal:
return [start]
q1 = deque([start])
q2 = deque([goal])
p1 = {start: None}
p2 = {goal: None}
while q1 and q2:
meet = expand(q1, p1, p2, simple_graph)
if meet:
return build_path(meet, p1, p2)
meet = expand(q2, p2, p1, reverse_graph(simple_graph))
if meet:
return build_path(meet, p1, p2)
return None
def expand(queue, parents, other_parents, g):
node = queue.popleft()
for neigh in g[node]:
if neigh not in parents:
parents[neigh] = node
queue.append(neigh)
if neigh in other_parents:
return neigh
return None
def build_path(meet, p1, p2):
path1 = []
n = meet
while n:
path1.append(n)
n = p1[n]
path1.reverse()
path2 = []
n = p2[meet]
while n:
path2.append(n)
n = p2[n]
return path1 + path2
def reverse_graph(g):
rg = {k: [] for k in g}
for u in g:
for v in g[u]:
rg[v].append(u)
return rg
# ---------------- TEST ----------------
print("BFS:", bfs('A', 'F'))
print("DFS:", dfs('A', 'F'))
print("UCS:", uniform_cost('A', 'F'))
print("DLS (limit=3):", dls('A', 'F', 3, ['A'], set()))
print("IDDFS:", iddfs('A', 'F', 5))
print("Bidirectional:", bidirectional('A', 'F'))