0% found this document useful (0 votes)
2 views1 page

DFS Algorithm

The document provides pseudocode for two graph traversal algorithms: Breadth-First Search (BFS) and Depth-First Search (DFS). BFS uses a queue to explore nodes level by level, while DFS utilizes a stack to explore as far as possible along each branch before backtracking. Both algorithms maintain a visited list to track which nodes have already been processed.

Uploaded by

snmahadevaswamy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

DFS Algorithm

The document provides pseudocode for two graph traversal algorithms: Breadth-First Search (BFS) and Depth-First Search (DFS). BFS uses a queue to explore nodes level by level, while DFS utilizes a stack to explore as far as possible along each branch before backtracking. Both algorithms maintain a visited list to track which nodes have already been processed.

Uploaded by

snmahadevaswamy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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)

You might also like