1971. Find if Path Exists in Graph Easy

@problem@discussion
#Depth-First Search#Breadth-First Search#Union Find#Graph



1/**
2 * [1971] Find if Path Exists in Graph
3 *
4 * 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.
5 * You want to determine if there is a valid path that exists from vertex source to vertex destination.
6 * Given edges and the integers n, source, and destination, return true if there is a valid path from source to destination, or false otherwise.
7 *  
8 * Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2021/08/14/validpath-ex1.png" style="width: 141px; height: 121px;" />
10 * Input: n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2
11 * Output: true
12 * Explanation: There are two paths from vertex 0 to vertex 2:
13 * - 0 &rarr; 1 &rarr; 2
14 * - 0 &rarr; 2
15 * 
16 * Example 2:
17 * <img alt="" src="https://assets.leetcode.com/uploads/2021/08/14/validpath-ex2.png" style="width: 281px; height: 141px;" />
18 * Input: n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0, destination = 5
19 * Output: false
20 * Explanation: There is no path from vertex 0 to vertex 5.
21 * 
22 *  
23 * Constraints:
24 * 
25 * 	1 <= n <= 2 * 10^5
26 * 	0 <= edges.length <= 2 * 10^5
27 * 	edges[i].length == 2
28 * 	0 <= ui, vi <= n - 1
29 * 	ui != vi
30 * 	0 <= source, destination <= n - 1
31 * 	There are no duplicate edges.
32 * 	There are no self edges.
33 * 
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/find-if-path-exists-in-graph/
38// discuss: https://leetcode.com/problems/find-if-path-exists-in-graph/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43    pub fn valid_path(n: i32, edges: Vec<Vec<i32>>, source: i32, destination: i32) -> bool {
44        false
45    }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_1971() {
56    }
57}
58


Back
© 2025 bowen.ge All Rights Reserved.