Skip to content

Commit

Permalink
BFS simple search added
Browse files Browse the repository at this point in the history
  • Loading branch information
Danial-Kord committed Feb 26, 2021
1 parent 87abac9 commit 0c49d8c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 20 deletions.
67 changes: 49 additions & 18 deletions BFS.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

scale = 0.5

LEFT_X_AREA = -4
RIGHT_X_AREA = 4




class graph_search(MovingCameraScene):
def construct(self):
LEFT_X_AREA = -4
RIGHT_X_AREA = 4
# grid = ScreenGrid()
camera = self.camera_frame
# camera.set_width(12)
Expand All @@ -20,22 +23,50 @@ def construct(self):
path = "C:\Danial\Projects\Danial\AI teaching assistance stuff\Artificial-Intelligence-Visualization\input.txt"
graph = GraphSystem.Graph(scale)
graph.read_from_file(path)
graph.show_complete_graph(self,RIGHT_X_AREA,LEFT_X_AREA)
# root = graph.root
# radius = graph.root.visual_shape.radius

# current_y_point = 3
# delta_x = 0
# if graph.branching_factors[1] > 1:
# delta_x = (RIGHT_X_AREA - LEFT_X_AREA) / (graph.branching_factors[1]-1)
# root.set_pos(0,current_y_point)
# current_y_point-=scale*2 + scale
# start_x_point = LEFT_X_AREA
# for i in graph.map[root]:
# i.set_pos(start_x_point,current_y_point)
# self.add(i.graphics,root.graphics,Arrow([root.x,root.y,0],[start_x_point,i.y,0],stroke_width=1,buff= i.scale))
# start_x_point += delta_x


# showing sample graph
sample_graph = graph.show_complete_graph(self,RIGHT_X_AREA,LEFT_X_AREA)
self.play(sample_graph.animate.scale(0.5))
self.wait(1)
self.play(sample_graph.animate.to_edge(UL))
self.wait(1)
self.play(Write(Line([0,20,0],[0,-20,0]).next_to(sample_graph)))


# Start BFS
draw_root = graph.root.graphics.copy()
LEFT_X_AREA = sample_graph.get_width() + sample_graph.get_center()[0] + 0.5
RIGHT_X_AREA = 5.5
self.play(draw_root.animate.move_to(graph.get_node_relative_pos(graph.root,RIGHT_X_AREA,LEFT_X_AREA)))
draw_root.set_color(BLUE_A)
self.play(draw_root.animate.scale(1.5))

frontier = []
shape_frontier = []
explored = []
shape_frontier.append(draw_root)
frontier.append(graph.root)
while(True):
if len(frontier) == 0:
break
expand_node = frontier.pop()
expand_shape = shape_frontier.pop()
explored.append(expand_node)
if graph.map.__contains__(expand_node):
for i in graph.map[expand_node]:
if i in explored:
continue
frontier.append(i)
draw_root = i.graphics.copy()
shape_frontier.append(draw_root)
pos = graph.get_node_relative_pos(i,RIGHT_X_AREA,LEFT_X_AREA)
self.play(draw_root.animate.move_to(pos))
draw_root.set_color(BLUE_A)
self.play(draw_root.animate.scale(1.5))
arrow = Arrow(expand_shape.get_center(),draw_root.get_center(),stroke_width=1,buff=draw_root.get_width()/2,color=YELLOW)
self.play(Write(arrow))
v = VGroup

# c1 = Circle()
# c1.radius = CIRCLE_RADIUS
# c1.move_to([0,0,0])
Expand Down
17 changes: 15 additions & 2 deletions GraphSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,24 @@ def show_complete_graph(self,scene,RIGHT_X_AREA,LEFT_X_AREA):

node.set_pos(current_x_point,current_y_point)
group.add(node.graphics)
scene.add(node.graphics)

scene.add(node.graphics)

# adding edges
for i in self.map:
nodes = self.map[i]
for j in nodes:
group.add(Arrow([i.x,i.y,0],[j.x,j.y,0],stroke_width=1,buff= self.scale))
scene.add(group)
arrow = Arrow([i.x,i.y,0],[j.x,j.y,0],stroke_width=1,buff= self.scale)
group.add(arrow)
scene.add(arrow)
return group

def get_node_relative_pos(self,node,RIGHT_X_AREA,LEFT_X_AREA):
y_bias = 3
current_y_point = float(y_bias - (self.scale*2 + self.scale) * node.depth)

current_x_point = (RIGHT_X_AREA + LEFT_X_AREA)/2
if self.branching_factors[node.depth] > 1:
current_x_point = float(LEFT_X_AREA + ((RIGHT_X_AREA - LEFT_X_AREA) / (self.branching_factors[node.depth]-1))*node.order)
return [current_x_point,current_y_point,0]

0 comments on commit 0c49d8c

Please sign in to comment.