Ppt
Ppt
Algorithm:
• Start from the initial node and mark it as
visited.
• Explore an unvisited adjacent node and
move deeper.
• Repeat until no unvisited nodes remain,
then backtrack.
• Continue until all reachable nodes are
visited.
EXAMPLE OF DFS
BREADTH FIRST SEARCH(BFS)
Definition: BFS (Breadth First Search) is a graph traversal algorithm that explores all
neighbors of a node before moving to the next level.
Algorithm:
• Start from the initial node and mark it as
visited.
• Enqueue the node and explore all its
unvisited adjacent nodes.
• Dequeue a node, visit its neighbors, and
repeat until all nodes are visited.
EXAMPLE OF BFS
DFS vs BFS
DFS BFS
DFS uses a stack (either BFS uses a queue (FIFO) to
explicit or recursion) to explore all neighbors of a node
explore nodes deeply before before moving to the next
backtracking. level.
The space complexity of DFS The space complexity of BFS
is O(V) in the worst case due is O(V) as it stores all nodes
to recursive calls storing in the queue level-wise.
nodes in the stack.
DFS is ideal for deep exploration tasks like cycle detection, while BFS excels in finding
shortest paths and level-wise searches.
REFERENCES
Graph (abstract data type) – Wikipedia