BFS Pseudocode:
BFS(graph, start):
# Create a queue
queue = []
# Create a visited list to track visited nodes
visited = set()
# Start with the source node
[Link](start)
[Link](start)
while queue:
# Dequeue a node
node = [Link](0)
# Process the node (e.g., print it)
print(node)
# Explore its neighbors
for neighbor in graph[node]:
if neighbor not in visited:
[Link](neighbor)
[Link](neighbor)
DFS Pseudocode:
DFS(graph, start):
# Create a stack
stack = []
# Create a visited list to track visited nodes
visited = set()
# Start with the source node
[Link](start)
while stack:
# Pop a node from the stack
node = [Link]()
# If the node has not been visited, process it
if node not in visited:
print(node) # Process the node
[Link](node)
# Push all unvisited neighbors onto the stack
for neighbor in graph[node]:
if neighbor not in visited:
[Link](neighbor)