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 * 105
0 <= edges.length <= 2 * 105
edges[i].length == 2
0 <= ui, vi <= n - 1
ui != vi
0 <= source, destination <= n - 1
There are no duplicate edges.
There are no self edges.
Solutions
Solution 1: DFS
Thinking
We only need connectivity between two vertices. After building the adjacency lists, DFS with a visited set reports success on reaching the destination, in linear time.
We first convert \(\textit{edges}\) into an adjacency list \(g\), then use DFS to determine whether there is a path from \(\textit{source}\) to \(\textit{destination}\).
During the process, we use an array \(\textit{vis}\) to record the vertices that have already been visited to avoid revisiting them.
The time complexity is \(O(n + m)\), and the space complexity is \(O(n + m)\). Here, \(n\) and \(m\) are the number of nodes and edges, respectively.
Method 1 expands with an explicit stack in depth-first order. A queue expands level by level with the same visited marks: the source is enqueued, and a dequeued vertex that is the destination succeeds. Unvisited neighbors are enqueued. An empty queue means the destination is unreachable.
We can also use BFS to determine whether there is a path from \(\textit{source}\) to \(\textit{destination}\).
Specifically, we define a queue \(q\), initially adding \(\textit{source}\) to the queue. Additionally, we use a set \(\textit{vis}\) to record the vertices that have already been visited to avoid revisiting them.
Next, we continuously take vertices \(i\) from the queue. If \(i = \textit{destination}\), it means there is a path from \(\textit{source}\) to \(\textit{destination}\), and we return \(\textit{true}\). Otherwise, we traverse all adjacent vertices \(j\) of \(i\). If \(j\) has not been visited, we add \(j\) to the queue \(q\) and mark \(j\) as visited.
Finally, if the queue is empty, it means there is no path from \(\textit{source}\) to \(\textit{destination}\), and we return \(\textit{false}\).
The time complexity is \(O(n + m)\), and the space complexity is \(O(n + m)\). Here, \(n\) and \(m\) are the number of nodes and edges, respectively.
When the path itself is unused, union-find merges every edge and compares the two roots. There is no walk over the graph.
Union-Find is a tree-like data structure that, as the name suggests, is used to handle some disjoint set merge and query problems. It supports two operations:
Find: Determine which subset an element belongs to. The time complexity of a single operation is \(O(\alpha(n))\).
Union: Merge two subsets into one set. The time complexity of a single operation is \(O(\alpha(n))\).
For this problem, we can use the Union-Find set to merge the edges in edges, and then determine whether source and destination are in the same set.
The time complexity is \(O(n \log n + m)\) or \(O(n \alpha(n) + m)\), and the space complexity is \(O(n)\). Where \(n\) and \(m\) are the number of nodes and edges, respectively.
We only need connectivity between two vertices. With \(n \le 2 \times 10^5\), an adjacency-list walk with a visited mark reports success on reaching the destination, in linear time.
Recursing from the source into each neighbor reaches depth \(n\) on a chain and overflows the call stack. Connectivity does not depend on the order of neighbors.
An explicit stack therefore returns immediately when the source is the destination. Otherwise a vertex is marked when it is pushed. After a pop, a neighbor that is the destination succeeds, and only unmarked neighbors are pushed. An empty stack means the destination is unreachable.
We first convert \(\textit{edges}\) into an adjacency list \(g\). If \(\textit{source}\) is \(\textit{destination}\), we return \(\textit{true}\) immediately. Otherwise an explicit stack starts at the source: a vertex is recorded in \(\textit{vis}\) when it is pushed, a neighbor that is the destination returns \(\textit{true}\), and only unmarked neighbors are pushed. An empty stack means there is no path.
The time complexity is \(O(n + m)\), and the space complexity is \(O(n + m)\). Here, \(n\) and \(m\) are the number of nodes and edges, respectively.