Find if Path Exists in Graph

There is a bi-directional graph with n vertices, labeled from 0 to n - 1. The edges are represented as a 2D integer array edges, where each edges[i] = [u, v] denotes a bi-directional edge between vertex u and vertex v. No vertex has an edge to itself, and each vertex pair is connected by at most one edge. Given the graph, determine if there is a valid path from a source vertex to a destination vertex.

Constraints

  • 1 <= n <= 2 * 10^5
  • 0 <= edges.length <= 2 * 10^5
  • edges[i].length == 2
  • 0 <= u, v <= n - 1
  • u != v
  • 0 <= source, destination <= n - 1
  • There are no duplicate edges.
  • There are no self edges.
Saving… · 2 lines
class Solution:
def findIfPathExistsInGraph(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool:
Console

Click Run to execute sample tests.