Find if Path Exists in Graph

Easy
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Find if Path Exists in Graph

Approach

Build undirected adjacency lists, then BFS until destination is reached or queue exhausts.

Complexity Analysis

Time
O(V + E)
Space
O(V + E)

Pattern

Reachability by BFS

Why It Works

BFS explores every vertex reachable from source. If destination is visited, a path exists; if traversal ends otherwise, no path exists.

Updated Feb 2026