java.lang.Object
g1901_2000.s1971_find_if_path_exists_in_graph.Solution

public class Solution extends Object
1971 - Find if Path Exists in Graph.<p>Easy</p> <p>There is a <strong>bi-directional</strong> graph with <code>n</code> vertices, where each vertex is labeled from <code>0</code> to <code>n - 1</code> ( <strong>inclusive</strong> ). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself.</p> <p>You want to determine if there is a <strong>valid path</strong> that exists from vertex <code>source</code> to vertex <code>destination</code>.</p> <p>Given <code>edges</code> and the integers <code>n</code>, <code>source</code>, and <code>destination</code>, return <code>true</code> <em>if there is a <strong>valid path</strong> from</em> <code>source</code> <em>to</em> <code>destination</code><em>, or</em> <code>false</code> <em>otherwise</em>_._</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/08/14/validpath-ex1.png" alt="" /></p> <p><strong>Input:</strong> n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> There are two paths from vertex 0 to vertex 2: - 0 \u2192 1 \u2192 2 - 0 \u2192 2</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/08/14/validpath-ex2.png" alt="" /></p> <p><strong>Input:</strong> n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0, destination = 5</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong> There is no path from vertex 0 to vertex 5.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= n <= 2 * 10<sup>5</sup></code></li> <li><code>0 <= edges.length <= 2 * 10<sup>5</sup></code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li><code>0 <= source, destination <= n - 1</code></li> <li>There are no duplicate edges.</li> <li>There are no self edges.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • validPath

      public boolean validPath(int n, int[][] edges, int start, int end)