Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9eb3232

Browse files
committed
search
1 parent a1e202e commit 9eb3232

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Search Algorithm Visualizer
2+
3+
This Python script runs a search algorithm with live graph traversal updates based on a graph created in a text file. The graph, maze.txt, is made up of:
4+
5+
- 1 for obstacles
6+
- 0 for paths
7+
- 2 for goals
8+
9+
## Types of search algorithms
10+
11+
Currently the script can visualize:
12+
13+
- Breadth-first search
14+
- Depth-first search
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
0000000010101010
2+
1010101010101010
3+
0010001010101010
4+
0000000000000000
5+
1111101000000000
6+
0000000011111111
7+
0000000011111111
8+
0000000010000111
9+
0000000010110111
10+
0000000000100121
11+
0000000011101101
12+
0000000011100001
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import matplotlib.pyplot as plt
2+
import copy
3+
4+
5+
class DFS():
6+
7+
def __init__(self, graph, starting_loc=None):
8+
self.map = graph
9+
self.starting_loc = starting_loc
10+
self.current_loc = starting_loc
11+
self.found_target = False
12+
self.target_loc = None
13+
self.shape = (len(graph), len(graph[0]))
14+
self.fig = None
15+
self.ax = None
16+
17+
def start_map(self):
18+
self.fig = plt.figure()
19+
self.ax = self.fig.gca()
20+
self.fig.show()
21+
22+
def show_map(self):
23+
plt.imshow(self.map)
24+
self.fig.canvas.draw()
25+
plt.pause(0.00000001)
26+
27+
def search_children(self, loc=None):
28+
r, c = loc or self.starting_loc
29+
self.current_loc = (r, c)
30+
self.map[r][c] = 3
31+
self.show_map()
32+
r_dif = -1
33+
c_dif = -1
34+
35+
while (not self.found_target and r_dif < 2):
36+
37+
while(c_dif < 2):
38+
39+
if (abs(c_dif) + abs(r_dif) == 2):
40+
c_dif += 1
41+
continue
42+
43+
new_r = abs(r + r_dif)
44+
new_c = abs(c + c_dif)
45+
46+
if (new_r < self.shape[0] and new_c < self.shape[1]):
47+
48+
if (self.map[new_r][new_c] == 2):
49+
self.found_target = True
50+
self.target_loc = (new_r, new_c)
51+
break
52+
53+
if (self.map[new_r][new_c] == 0):
54+
self.search_children((new_r, new_c))
55+
56+
c_dif += 1
57+
r_dif += 1
58+
c_dif = -1
59+
60+
61+
class BFS():
62+
63+
def __init__(self, graph, starting_loc=None):
64+
self.map = graph
65+
self.starting_loc = starting_loc
66+
self.current_loc = starting_loc
67+
self.found_target = False
68+
self.target_loc = None
69+
self.shape = (len(graph), len(graph[0]))
70+
self.search_queue = []
71+
self.fig = None
72+
self.ax = None
73+
74+
def start_map(self):
75+
self.fig = plt.figure()
76+
self.ax = self.fig.gca()
77+
self.fig.show()
78+
79+
def show_map(self):
80+
plt.imshow(self.map)
81+
self.fig.canvas.draw()
82+
plt.pause(0.00000001)
83+
84+
def push_queue(self, item):
85+
self.search_queue.append(item)
86+
87+
def pull_queue(self):
88+
return self.search_queue.pop(0)
89+
90+
def search_children(self, loc=None):
91+
r, c = loc or self.starting_loc
92+
self.current_loc = (r, c)
93+
self.map[r][c] = 3
94+
self.show_map()
95+
r_dif = -1
96+
c_dif = -1
97+
98+
while (not self.found_target and r_dif < 2):
99+
100+
while(c_dif < 2):
101+
102+
if (abs(c_dif) + abs(r_dif) == 2):
103+
c_dif += 1
104+
continue
105+
106+
new_r = abs(r + r_dif)
107+
new_c = abs(c + c_dif)
108+
109+
if (new_r < self.shape[0] and new_c < self.shape[1]):
110+
111+
if (self.map[new_r][new_c] == 2):
112+
self.found_target = True
113+
self.target_loc = (new_r, new_c)
114+
break
115+
116+
if (self.map[new_r][new_c] == 0 and (new_r, new_c) not in self.search_queue):
117+
self.push_queue((new_r, new_c))
118+
119+
c_dif += 1
120+
r_dif += 1
121+
c_dif = -1
122+
123+
next_child = self.pull_queue()
124+
if (next_child != None):
125+
self.search_children(next_child)
126+
127+
128+
def prep_graph():
129+
# Prepare Graph
130+
131+
def split_chars(string):
132+
return [eval(char) for char in string]
133+
134+
with open("maze.txt", "r") as maze_file:
135+
graph_strings = maze_file.read().split("\n")
136+
maze_file.close()
137+
138+
return [split_chars(row) for row in graph_strings]
139+
140+
141+
if __name__ == "__main__":
142+
choice = input("bfs or dfs\n")
143+
graph_list = prep_graph()
144+
145+
if (choice.lower() == "bfs"):
146+
searcher = BFS(copy.deepcopy(graph_list), (0, 0))
147+
148+
elif (choice.lower() == "dfs"):
149+
searcher = DFS(copy.deepcopy(graph_list), (0, 0))
150+
151+
searcher.start_map()
152+
searcher.search_children()
153+
print("Current location: ", searcher.current_loc)
154+
print("Found target: ", searcher.found_target)
155+
print("Target location: ", searcher.target_loc)

0 commit comments

Comments
 (0)