0% found this document useful (0 votes)
11 views4 pages

BFS Traversal

The document outlines various graph traversal algorithms including BFS and DFS, detailing their procedures for traversing nodes, detecting cycles, and finding connected components. It also describes methods for finding the shortest path in unweighted graphs, performing topological sorting, and finding all paths between two nodes. Additionally, it covers BFS and DFS techniques for level order and binary tree traversals.

Uploaded by

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

BFS Traversal

The document outlines various graph traversal algorithms including BFS and DFS, detailing their procedures for traversing nodes, detecting cycles, and finding connected components. It also describes methods for finding the shortest path in unweighted graphs, performing topological sorting, and finding all paths between two nodes. Additionally, it covers BFS and DFS techniques for level order and binary tree traversals.

Uploaded by

ummeaymen2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

BFS Traversal

1. Start with the initial node (source).


2. Create an empty queue to manage the order of exploration.
3. Mark the source node as visited.
4. Add the source node to the queue.
5. While there are nodes in the queue:
o Remove a node from the queue.
o For each neighbor of this node:
 If the neighbor hasn’t been visited:
 Mark it as visited.
 Add it to the queue.

2. DFS Traversal (Recursive)

1. Start with the initial node.


2. Mark the current node as visited.
3. For each neighbor of the current node:
o If the neighbor hasn’t been visited:
 Recursively perform DFS on the neighbor.

3. BFS Cycle Detection in an Undirected Graph

1. Start with a source node and create an empty queue.


2. Mark the source node as visited and add it to the queue, along with its parent (initially -
1).
3. While there are nodes in the queue:
o Remove a node and its parent from the queue.
o For each neighbor of this node:
 If the neighbor hasn’t been visited:
 Mark it as visited and add it to the queue, with the current node as
its parent.
 If the neighbor is already visited and isn’t the parent of the current node:
 A cycle exists.

4. DFS Cycle Detection in an Undirected Graph

1. Start with a source node.


2. Mark the current node as visited.
3. For each neighbor of the current node:
o If the neighbor hasn’t been visited:
 Recursively check for a cycle using DFS.
 If a cycle is detected in recursion, return true.
o If the neighbor is already visited and isn’t the parent of the current node:
 A cycle exists.
4. If no cycle is found after exploring all neighbors, return false.

5. Find Connected Components using BFS

1. Create an empty list to store connected components.


2. For each node in the graph:
o If the node hasn’t been visited:
 Start a BFS from this node, marking all reachable nodes as visited and
storing them in a list (the current component).
 Add the list to the connected components.

6. Find Connected Components using DFS

1. Create an empty list to store connected components.


2. For each node in the graph:
o If the node hasn’t been visited:
 Start a DFS from this node, marking all reachable nodes as visited and
storing them in a list (the current component).
 Add the list to the connected components.

7. BFS to Find the Shortest Path in an Unweighted Graph

1. Start with the source node.


2. Create a queue to store nodes, along with their current distance from the source.
3. Mark the source node as visited and add it to the queue with distance 0.
4. While there are nodes in the queue:
o Remove a node and its distance from the queue.
o If the node is the destination, return its distance.
o For each neighbor of this node:
 If the neighbor hasn’t been visited:
 Mark it as visited and add it to the queue with a distance one
greater than the current distance.
5. If the destination isn’t reached, return -1.
8. DFS for Topological Sorting

1. Create an empty stack to store the result.


2. For each node in the graph:
o If the node hasn’t been visited:
 Perform a recursive DFS from this node.
 During DFS, once all neighbors of a node have been explored, add
the node to the stack.
3. After exploring all nodes, reverse the stack to get the topological order.

9. DFS to Find All Paths Between Two Nodes

1. Start with the source node and initialize an empty list to store paths.
2. Use a helper function that:
o Adds the current node to the current path.
o If the current node is the destination:
 Save the current path.
o Otherwise:
 For each neighbor of the current node:
 If the neighbor hasn’t been visited:
 Recursively call the helper function for the neighbor.
o After exploring all neighbors, backtrack by removing the current node from the
path.

10. BFS for Level Order Traversal of a Binary Tree

1. Start with the root of the tree.


2. Create a queue to store nodes.
3. Add the root to the queue.
4. While there are nodes in the queue:
o Remove a node from the queue.
o Print its value.
o Add its left and right children (if they exist) to the queue.

11. DFS for Binary Tree Traversals

Preorder Traversal:

1. Visit the current node.


2. Recursively traverse the left subtree.
3. Recursively traverse the right subtree.

Inorder Traversal:

1. Recursively traverse the left subtree.


2. Visit the current node.
3. Recursively traverse the right subtree.

Postorder Traversal:

1. Recursively traverse the left subtree.


2. Recursively traverse the right subtree.
3. Visit the current node.

You might also like