DAA109 - Editorial

Prerequisites: DFS

Problem: Given a directed graph with N vertices and M directed edges, the task is to output all the vertices that are reachable from the starting vertex 1.

Solution Approach: The solution is quite simple. Use Depth-First Search (DFS) algorithm to traverse the directed graph and mark the nodes that are reachable from the starting vertex 1. At the end print/output only those nodes who were visited from node 1.

Time complexity: O(V + E) as we use DFS.

Space complexity: O(V) for extra boolean array to mark the nodes visited.