Graph Traversal & Tree Traversal
Graph Traversal Graph traversal means visiting all the vertices (nodes) of a graph in a specific order, usually to process or search data. It helps in finding paths, connectivity, and other properties in graphs. There are two main traversal methods: Depth First Search (DFS) Breadth First Search (BFS) 1. Depth First Search (DFS) DFS explores as far as possible along each branch before backtracking. It uses a stack (can be implemented using recursion). Algorithm (Using Recursion): DFS(G, v): mark v as visited for each neighbor u of v: if u is not visited: DFS(G, u) Steps: Start from any vertex. Visit and mark it as visited. Move to an adjacent unvisited vertex. Repeat until no unvisited adjacent vertex remains. Backtrack to the previous verte...