Graph Theory37 sections · 1633 units
Open in Course

Practice Problem 3

(Reachability check)

Practice Problem: Check if there is a directed path from node aa to node bb. Input: nn nodes, mm edges, and a query (a,b)(a, b). Output: "YES" if aa can reach bb, "NO" otherwise. Hint: Run SCC detection. If aa and bb are in the same SCC, answer is "YES" (mutual reachability).

If they are in different SCCs, check if there is a path from scc[a] to scc[b] in the condensation DAG using BFS or DFS. This is faster than running BFS from aa every time if you have multiple queries.