BFS Algorithm and Visualization Guide
BFS Algorithm and Visualization Guide
FM-SSCT-ACAD-
Repulic of the Philippines
002
SURIGAO DEL NORTE STATE UNIVERSITY
Revision No. 00
Narciso Street, Surigao City 8400, Philippines
Effective Date 20 September 2018
“For Nation’s Greatr
Page No. 1 of 21
COLLEGE OFCOMPUTING & INFORMATION SCIENCES
First Semester, Academic Year 2025-2026
CS 315: ELECTIVE 2 (INTELLIGENT SYSTEM)
Document Code No. FM-SSCT-ACAD-
002
STUDENT INFORMATION
Revision No. 00
CODE SNIPPET:
import tkinter as tk
return None
[Link](cur)
cur = [Link](cur)
if cur is None:
return None
[Link](start)
return list(reversed(path))
q = deque([start])
seen = set([start])
while q:
node = [Link]()
visited_order.append(node)
if node == goal:
[Link](nbr)
Document Code No. FM-SSCT-ACAD-
Repulic of the Philippines
002
SURIGAO DEL NORTE STATE UNIVERSITY
Revision No. 00
Narciso Street, Surigao City 8400, Philippines
Effective Date 20 September 2018
“For Nation’s Greatr
Page No. 2 of 21
parent[nbr] = node
[Link](nbr)
Document Code No. FM-SSCT-ACAD-
return None, visited_order 002
class BFSVisualizer: Revision No. 00
def __init__(self, root):
Effective Date 20 September 2018
[Link] = root Page No. 2 of 21
[Link]("BFS Visualizer")
[Link](left, text="Goal:").pack(anchor="w")
[Link](left, text="Output:").pack(anchor="w")
def reset(self):
self.stop_animation()
[Link]("1.0","end")
self.start_entry.delete(0,"end"); self.start_entry.insert(0,"A")
self.goal_entry.delete(0,"end"); self.goal_entry.insert(0,"L")
self.delay_entry.delete(0,"end"); self.delay_entry.insert(0,str(self.animation_delay))
[Link] = {}
self.node_positions = {}
[Link]("all")
[Link]("end","Reset done.\n")
def build_graph(self):
[Link] = {
"A": ["B","C","D"],
"B": ["E","F"],
"C": [],
"D": ["G","H"],
"E": ["I","J"],
"F": [],
"G": ["K","L"],
"H": [],
self.compute_positions()
Document Code No. FM-SSCT-ACAD-
Repulic of the Philippines
002
SURIGAO DEL NORTE STATE UNIVERSITY
Revision No. 00
Narciso Street, Surigao City 8400, Philippines
Effective Date 20 September 2018
“For Nation’s Greatr
Page No. 3 of 21
self.draw_graph()
y = v_spacing * (i+1)
x = h_spacing * (j+1)
positions[node] = (x,y)
self.node_positions = positions
[Link]("all")
x1,y1 = self.node_positions.get(u,(0,0))
for v in nbrs:
x2,y2 = self.node_positions.get(v,(0,0))
[Link].create_line(x1,y1,x2,y2, arrow=[Link])
self.draw_path(visit_path, "orange")
if final_path:
self.draw_path(final_path, "purple")
color = "lightblue"
color = "orange"
color = "red"
color = "purple"
[Link].create_oval(x-20,y-20,x+20,y+20, fill=color)
[Link].create_text(x,y, text=n)
if label:
pts = []
for n in path:
x,y = self.node_positions[n]
[Link]([x,y])
if [Link]: return
Document Code No. FM-SSCT-ACAD-
self.animation_delay = int(self.delay_entry.get() or self.animation_delay) 002
start = self.start_entry.get().strip(); goal = self.goal_entry.get().strip() Revision No. 00
t = [Link](target=self._animate_bfs, args=(start,goal), daemon=True)
Effective Date 20 September 2018
[Link]() Page No. 4 of 21
def _animate_bfs(self, start, goal):
[Link](self.animation_delay/1000.0)
if path:
self.draw_graph(final_path=path)
else:
[Link] = False
def stop_animation(self):
[Link] = False
self.compute_positions()
self.draw_graph()
if __name__ == "__main__":
root = [Link]()
[Link]("1000x650")
app = BFSVisualizer(root)
[Link]()
OUTPUT:
Depth-First Search (DFS) explores as far as possible along each path before backtracking. It uses a stack (LIFO) and is more memory-efficient than Breadth-
First Search (BFS) but does not guarantee the shortest path.
Key Features:
continue
[Link](node)
visited_order.append(node)
if node == goal:
parent[nbr] = node
[Link](nbr)
class DFSVisualizer:
[Link] = root
[Link]("DFS Visualizer")
[Link] = {}
self.node_positions = {}
[Link] = False
self.animation_delay = 600
self.setup_ui()
self.build_graph()
[Link]("<Configure>", self.on_canvas_resize)
def setup_ui(self):
[Link](left, text="Start:").pack(anchor="w")
[Link](left, text="Goal:").pack(anchor="w")
[Link](left, text="Output:").pack(anchor="w")
Document Code No. FM-SSCT-ACAD-
Repulic of the Philippines
002
SURIGAO DEL NORTE STATE UNIVERSITY
Revision No. 00
Narciso Street, Surigao City 8400, Philippines
Effective Date 20 September 2018
“For Nation’s Greatr
Page No. 6 of 21
[Link] = [Link](left, width=36, height=14); [Link]()
"E": ["I","J"],
"F": [],
"G": ["K","L"],
"H": [],
self.compute_positions()
self.draw_graph()
def compute_positions(self):
levels = []
[Link]([])
levels[depth].append(node)
dfs_level(c, depth+1)
dfs_level("A", 0)
positions = {}
count = len(level)
y = v_spacing * (i+1)
x = h_spacing * (j+1)
positions[node] = (x,y)
self.node_positions = positions
[Link]("all")
x1,y1 = self.node_positions.get(u,(0,0))
Document Code No. FM-SSCT-ACAD-
Repulic of the Philippines
002
SURIGAO DEL NORTE STATE UNIVERSITY
Revision No. 00
Narciso Street, Surigao City 8400, Philippines
Effective Date 20 September 2018
“For Nation’s Greatr
Page No. 7 of 21
for v in nbrs:
x2,y2 = self.node_positions.get(v,(0,0))
Document Code No. FM-SSCT-ACAD-
[Link].create_line(x1,y1,x2,y2, arrow=[Link]) 002
if visit_path and not final_path: Revision No. 00
self.draw_path(visit_path, "orange")
Effective Date 20 September 2018
if final_path: Page No. 7 of 21
self.draw_path(final_path, "purple")
pts = []
for n in path:
x,y = self.node_positions[n]
[Link]([x,y])
def run_dfs(self):
if [Link]: return
[Link]()
[Link] = True
highlight = set()
visit_path = []
visited_str = ""
[Link](node); visit_path.append(node)
self.draw_graph(highlight=highlight, visit_path=visit_path)
[Link](self.animation_delay/1000.0)
if path:
self.draw_graph(final_path=path)
else:
[Link] = False
def stop_animation(self):
[Link] = False
Document Code No. FM-SSCT-ACAD-
Repulic of the Philippines
002
SURIGAO DEL NORTE STATE UNIVERSITY
Revision No. 00
Narciso Street, Surigao City 8400, Philippines
Effective Date 20 September 2018
“For Nation’s Greatr
Page No. 8 of 21
def on_canvas_resize(self, event):
Uniform-Cost Search (UCS) extends Breadth-First Search (BFS) by considering path costs, always expanding the least-cost node first. It guarantees finding
the optimal path when all costs are non-negative.
Key Features:
CODE SNIPPET:
import tkinter as tk
return None
[Link](cur)
cur = [Link](cur)
if cur is None:
return None
[Link](start)
return list(reversed(path))
pq = [(0, start)]
cost_so_far = {start: 0}
seen = set()
while pq:
d, node = [Link](pq)
Document Code No. FM-SSCT-ACAD-
Repulic of the Philippines
002
SURIGAO DEL NORTE STATE UNIVERSITY
Revision No. 00
Narciso Street, Surigao City 8400, Philippines
Effective Date 20 September 2018
“For Nation’s Greatr
Page No. 9 of 21
if node in seen:
continue
Document Code No. FM-SSCT-ACAD-
[Link](node) 002
visited_order.append(node) Revision No. 00
if node == goal:
Effective Date 20 September 2018
return reconstruct_path(parent, start, goal), visited_order, d Page No. 9 of 21
for nbr, w in weighted_graph.get(node, []):
self.node_positions = {}
[Link] = False
self.animation_delay = 600
self.setup_ui()
self.build_graph()
[Link]("<Configure>", self.on_canvas_resize)
def setup_ui(self):
[Link](left, text="Start:").pack(anchor="w")
[Link](left, text="Goal:").pack(anchor="w")
[Link](left, text="Output:").pack(anchor="w")
def reset(self):
self.stop_animation()
[Link]("1.0","end")
self.start_entry.delete(0,"end"); self.start_entry.insert(0,"A")
self.goal_entry.delete(0,"end"); self.goal_entry.insert(0,"L")
self.delay_entry.delete(0,"end"); self.delay_entry.insert(0,str(self.animation_delay))
[Link] = {}
self.weighted_map = {}
self.node_positions = {}
[Link]("all")
[Link]("end","Reset done.\n")
def build_graph(self):
Document Code No. FM-SSCT-ACAD-
Repulic of the Philippines
002
SURIGAO DEL NORTE STATE UNIVERSITY
Revision No. 00
Narciso Street, Surigao City 8400, Philippines
Effective Date 20 September 2018
“For Nation’s Greatr
Page No. 10 of 21
[Link] = {
"A": ["B","C","D"],
Document Code No. FM-SSCT-ACAD-
"B": ["E","F"], 002
"C": [], Revision No. 00
"D": ["G","H"],
Effective Date 20 September 2018
"E": ["I","J"], Page No. 10 of 21
"F": [],
[Link]([])
levels[depth].append(node)
dfs_level(c, depth+1)
dfs_level("A", 0)
positions = {}
count = len(level)
y = v_spacing * (i+1)
x = h_spacing * (j+1)
positions[node] = (x,y)
self.node_positions = positions
[Link]("all")
x1,y1 = self.node_positions.get(u,(0,0))
for v in nbrs:
x2,y2 = self.node_positions.get(v,(0,0))
[Link].create_line(x1,y1,x2,y2, arrow=[Link])
self.draw_path(visit_path, "orange")
if final_path:
self.draw_path(final_path, "purple")
color = "lightblue"
color = "orange"
color = "red"
[Link].create_oval(x-20,y-20,x+20,y+20, fill=color)
Document Code No. FM-SSCT-ACAD-
[Link].create_text(x,y, text=n) 002
if label: Revision No. 00
[Link].create_text(10,10, anchor="nw", text=label, font=("Arial",12,"bold"))
Effective Date 20 September 2018
if self.weighted_map: Page No. 11 of 21
self.draw_edge_weights()
def run_ucs(self):
if [Link]: return
self.weighted_map = {}
self.weighted_map[u] = []
for v in nbrs:
w = [Link](1,10)
self.weighted_map[u].append((v,w))
[Link]("end","Random weights:\n")
[Link]("end")
self.draw_graph()
[Link]()
[Link] = True
highlight = set()
visit_path = []
visited_str = ""
[Link](node); visit_path.append(node)
# UCS doesn't show a path while animating — show visited highlight only
self.draw_graph(highlight=highlight)
if path:
Document Code No. FM-SSCT-ACAD-
self.draw_graph(final_path=path) 002
[Link]("end", f"Total cost: {cost}\n") Revision No. 00
[Link]("end", f"Visit path: {visited_str}\n")
Effective Date 20 September 2018
[Link]("end", f"Path found: {' -> '.join(path)}\n") Page No. 12 of 21
else:
[Link]()
OUTPUT:
return None
[Link](cur)
cur = [Link](cur)
if cur is None:
return None
[Link](start)
return list(reversed(path))
found = [False]
visited_order.append(node)
if node == goal:
found[0] = True
return True
Document Code No. FM-SSCT-ACAD-
Repulic of the Philippines
002
SURIGAO DEL NORTE STATE UNIVERSITY
Revision No. 00
Narciso Street, Surigao City 8400, Philippines
Effective Date 20 September 2018
“For Nation’s Greatr
Page No. 13 of 21
if depth == 0:
return False
Document Code No. FM-SSCT-ACAD-
for nbr in [Link](node, []): 002
if nbr not in parent: Revision No. 00
parent[nbr] = node
Effective Date 20 September 2018
if dfs_limited(nbr, depth - 1): Page No. 13 of 21
return True
self.animation_delay = 600
self.setup_ui()
self.build_graph()
[Link]("<Configure>", self.on_canvas_resize)
def setup_ui(self):
[Link](left, text="Start:").pack(anchor="w")
[Link](left, text="Goal:").pack(anchor="w")
[Link](left, text="Output:").pack(anchor="w")
def reset(self):
self.stop_animation()
[Link]("1.0","end")
self.start_entry.delete(0,"end"); self.start_entry.insert(0,"A")
self.goal_entry.delete(0,"end"); self.goal_entry.insert(0,"L")
self.depth_entry.delete(0,"end"); self.depth_entry.insert(0,"3")
self.delay_entry.delete(0,"end"); self.delay_entry.insert(0,str(self.animation_delay))
[Link] = {}
self.node_positions = {}
[Link]("all")
[Link]("end","Reset done.\n")
def build_graph(self):
Document Code No. FM-SSCT-ACAD-
Repulic of the Philippines
002
SURIGAO DEL NORTE STATE UNIVERSITY
Revision No. 00
Narciso Street, Surigao City 8400, Philippines
Effective Date 20 September 2018
“For Nation’s Greatr
Page No. 14 of 21
[Link] = {
"A": ["B","C","D"],
Document Code No. FM-SSCT-ACAD-
"B": ["E","F"], 002
"C": [], Revision No. 00
"D": ["G","H"],
Effective Date 20 September 2018
"E": ["I","J"], Page No. 14 of 21
"F": [],
[Link]([])
levels[depth].append(node)
dfs_level(c, depth+1)
dfs_level("A", 0)
positions = {}
count = len(level)
y = v_spacing * (i+1)
x = h_spacing * (j+1)
positions[node] = (x,y)
self.node_positions = positions
[Link]("all")
x1,y1 = self.node_positions.get(u,(0,0))
for v in nbrs:
x2,y2 = self.node_positions.get(v,(0,0))
[Link].create_line(x1,y1,x2,y2, arrow=[Link])
self.draw_path(visit_path, "orange")
if final_path:
self.draw_path(final_path, "purple")
color = "lightblue"
color = "orange"
color = "red"
[Link].create_oval(x-20,y-20,x+20,y+20, fill=color)
Document Code No. FM-SSCT-ACAD-
[Link].create_text(x,y, text=n) 002
if label: Revision No. 00
[Link].create_text(10,10, anchor="nw", text=label, font=("Arial",12,"bold"))
Effective Date 20 September 2018
def draw_path(self, path, color): Page No. 15 of 21
if len(path) < 2: return
[Link] = True
highlight = set()
visit_path = []
visited_str = ""
[Link](node); visit_path.append(node)
self.draw_graph(highlight=highlight, visit_path=visit_path)
[Link](self.animation_delay/1000.0)
if path:
self.draw_graph(final_path=path)
else:
[Link] = False
def stop_animation(self):
[Link] = False
self.compute_positions()
self.draw_graph()
if __name__ == "__main__":
root = [Link]()
[Link]("1000x650")
app = DLSVisualizer(root)
[Link]()
OUTPUT:
Document Code No. FM-SSCT-ACAD-
Repulic of the Philippines
002
SURIGAO DEL NORTE STATE UNIVERSITY
Revision No. 00
Narciso Street, Surigao City 8400, Philippines
Effective Date 20 September 2018
“For Nation’s Greatr
Page No. 16 of 21
Iterative Deepening Search (IDS) combines Breadth-First Search (BFS) and Depth First Search (DFS) by running Depth First Search (DFS) with increasing depth limits
until a solution is found.
Key Features:
return None
[Link](cur)
cur = [Link](cur)
if cur is None:
return None
[Link](start)
return list(reversed(path))
found = [False]
visited_order.append(node)
if node == goal:
found[0] = True
return True
if depth == 0:
return False
parent[nbr] = node
return True
Document Code No. FM-SSCT-ACAD-
Repulic of the Philippines
002
SURIGAO DEL NORTE STATE UNIVERSITY
Revision No. 00
Narciso Street, Surigao City 8400, Philippines
Effective Date 20 September 2018
“For Nation’s Greatr
Page No. 17 of 21
return False
dfs_limited(start, limit)
Document Code No. FM-SSCT-ACAD-
if found[0]: 002
return reconstruct_path(parent, start, goal), visited_order Revision No. 00
return None, visited_order
Effective Date 20 September 2018
def ids(graph, start, goal, max_depth=20): Page No. 17 of 21
combined = []
[Link] = root
[Link]("IDS Visualizer")
[Link] = {}
self.node_positions = {}
[Link] = False
self.animation_delay = 600
self.setup_ui()
self.build_graph()
[Link]("<Configure>", self.on_canvas_resize)
def setup_ui(self):
[Link](left, text="Start:").pack(anchor="w")
[Link](left, text="Goal:").pack(anchor="w")
[Link](left, text="Output:").pack(anchor="w")
def reset(self):
self.stop_animation()
[Link]("1.0","end")
self.start_entry.delete(0,"end"); self.start_entry.insert(0,"A")
self.goal_entry.delete(0,"end"); self.goal_entry.insert(0,"L")
self.depth_entry.delete(0,"end"); self.depth_entry.insert(0,"4")
self.delay_entry.delete(0,"end"); self.delay_entry.insert(0,str(self.animation_delay))
Document Code No. FM-SSCT-ACAD-
Repulic of the Philippines
002
SURIGAO DEL NORTE STATE UNIVERSITY
Revision No. 00
Narciso Street, Surigao City 8400, Philippines
Effective Date 20 September 2018
“For Nation’s Greatr
Page No. 18 of 21
[Link] = {}
self.node_positions = {}
Document Code No. FM-SSCT-ACAD-
[Link]("all") 002
[Link]("end","Reset done.\n") Revision No. 00
def build_graph(self):
Effective Date 20 September 2018
[Link] = { Page No. 18 of 21
"A": ["B","C","D"],
def compute_positions(self):
levels = []
[Link]([])
levels[depth].append(node)
dfs_level(c, depth+1)
dfs_level("A", 0)
positions = {}
count = len(level)
y = v_spacing * (i+1)
x = h_spacing * (j+1)
positions[node] = (x,y)
self.node_positions = positions
[Link]("all")
x1,y1 = self.node_positions.get(u,(0,0))
for v in nbrs:
x2,y2 = self.node_positions.get(v,(0,0))
[Link].create_line(x1,y1,x2,y2, arrow=[Link])
self.draw_path(visit_path, "orange")
if final_path:
self.draw_path(final_path, "purple")
color = "lightblue"
Document Code No. FM-SSCT-ACAD-
Repulic of the Philippines
002
SURIGAO DEL NORTE STATE UNIVERSITY
Revision No. 00
Narciso Street, Surigao City 8400, Philippines
Effective Date 20 September 2018
“For Nation’s Greatr
Page No. 19 of 21
if visit_path and n in visit_path and not final_path:
color = "orange"
Document Code No. FM-SSCT-ACAD-
if highlight and n in highlight: 002
color = "red" Revision No. 00
if final_path and n in final_path:
Effective Date 20 September 2018
color = "purple" Page No. 19 of 21
[Link].create_oval(x-20,y-20,x+20,y+20, fill=color)
max_depth = int(self.depth_entry.get() or 4)
[Link]()
[Link] = True
combined_so_far = []
found = False
iter_visit_path = []
iter_visit_path.append(node)
combined_so_far.append(node)
highlight = set([node])
[Link](self.animation_delay/1000.0)
[Link](0.25)
found = True
break
if path:
self.draw_graph(final_path=path)
else:
self.draw_graph()
[Link]("end")
Document Code No. FM-SSCT-ACAD-
Repulic of the Philippines
002
SURIGAO DEL NORTE STATE UNIVERSITY
Revision No. 00
Narciso Street, Surigao City 8400, Philippines
Effective Date 20 September 2018
“For Nation’s Greatr
Page No. 20 of 21
[Link] = False
def stop_animation(self):
Document Code No. FM-SSCT-ACAD-
[Link] = False 002
def on_canvas_resize(self, event): Revision No. 00
if not [Link]: return
Effective Date 20 September 2018
self.compute_positions() Page No. 20 of 21
self.draw_graph()
Accuracy of Algorithm Algorithm works perfectly for all test Minor errors in Multiple errors, incomplete Major errors, does not
Implementation 40% cases and meets all requirements implementation
functional
but overall results, or partial functionality function as intended
Efficiency of Code 30% Code is highly optimized, minimal Acceptable efficiency, minor Code runs but is inefficient, Code is very slow or
complexity, runs fast optimizations possible redundant processes poorly structured.
Clarity of Documentation 20% Well-organized, clear, and detailed Documentation is clear but Documentation is incomplete No documentation or very
documentation with examples lacks minor details or somewhat unclear unclear
Timely Submission 10% Submitted on or before the deadline 1–2 days late 3–4 days late More than 4 days late or
not submitted
TOTAL
Remarks:
Prepared by: