Watch 10 video solutions for Find if Path Exists in Graph, a easy level problem involving Depth-First Search, Breadth-First Search, Union Find. This walkthrough by Greg Hogg has 26,125 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive). The edges in the graph are represented as a 2D integer array edges, where each edges[i] = [ui, vi] denotes a bi-directional edge between vertex ui and vertex vi. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself.
You want to determine if there is a valid path that exists from vertex source to vertex destination.
Given edges and the integers n, source, and destination, return true if there is a valid path from source to destination, or false otherwise.
Example 1:
Input: n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2 Output: true Explanation: There are two paths from vertex 0 to vertex 2: - 0 → 1 → 2 - 0 → 2
Example 2:
Input: n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0, destination = 5 Output: false Explanation: There is no path from vertex 0 to vertex 5.
Constraints:
1 <= n <= 2 * 1050 <= edges.length <= 2 * 105edges[i].length == 20 <= ui, vi <= n - 1ui != vi0 <= source, destination <= n - 1Problem Overview: You are given an undirected graph with n nodes and a list of edges. The task is simple: determine whether a valid path exists between a given source node and a destination node. In other words, check if the two nodes belong to the same connected component in the graph.
Approach 1: Depth-First Search (DFS) Traversal (Time: O(V + E), Space: O(V))
The most direct solution treats the graph as an adjacency list and performs a traversal starting from the source. Using Depth-First Search, you recursively explore all neighbors of each node until either the destination is found or all reachable nodes are visited. A visited array prevents revisiting nodes and getting stuck in cycles. If the traversal ever reaches the destination, a path exists.
This works because DFS explores every vertex reachable from the starting node. If the destination belongs to the same connected component, DFS will eventually reach it. Building the adjacency list takes O(E) time, and the traversal visits each node and edge at most once. This approach is intuitive and commonly used for connectivity checks in a graph.
Many implementations also use Breadth-First Search instead of DFS. BFS uses a queue instead of recursion but has the same time complexity O(V + E) and space complexity O(V). Both approaches work equally well for reachability problems.
Approach 2: Union-Find (Disjoint Set Union) (Time: O(E · α(V)), Space: O(V))
Union-Find models the graph as a collection of connected components. Initially, each node belongs to its own set. For every edge (u, v), perform a union operation to merge the sets containing those nodes. After processing all edges, nodes that are connected by any path will belong to the same root set.
To check if a path exists, simply compare the roots of source and destination. If they share the same representative parent, they are connected. With path compression and union by rank, each operation runs in nearly constant time, represented as the inverse Ackermann function α(V).
This approach avoids explicit traversal and is extremely efficient when you need to process many connectivity queries or build components incrementally. It’s a classic use case of the Union-Find data structure.
Recommended for interviews: DFS or BFS is typically the expected solution because it directly demonstrates graph traversal skills and understanding of connected components. Union-Find is also highly valued, especially if the interviewer wants to test knowledge of disjoint-set data structures. Showing DFS first proves you understand graph traversal fundamentals, while mentioning Union-Find demonstrates deeper algorithmic knowledge.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Depth-First Search (DFS) | O(V + E) | O(V) | Best general approach for checking connectivity using graph traversal |
| Breadth-First Search (BFS) | O(V + E) | O(V) | Preferred when iterative traversal with a queue is easier than recursion |
| Union-Find (Disjoint Set Union) | O(E · α(V)) | O(V) | Efficient for connectivity checks when processing many edges or multiple queries |