Understanding Graph Reachability
Given n vertices labeled 0 through n − 1 and undirected edges, return whether a path connects source to destination. The visualization displays adjacency lists and the DFS frontier.
How it works
Reachability needs a visited set because a graph can revisit a vertex through many routes.
Build an undirected adjacency list, mark vertices when pushing them onto a DFS stack, and stop when the destination is popped.
Try an example
Input: n = 6, edges = [[0, 1], [0, 2], [3, 4], [4, 5]], source = 0, destination = 5
Output: false
The source and destination belong to disconnected components.
Constraints
- 1–16 vertices and at most 48 edges.
- Every endpoint must be a valid vertex.
- Self-loops and repeated edges are allowed.
Use the simulator
Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.
Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.