Find if Path Exists in Graph

Graphs, problem 2 of 8

Find if Path Exists in Graph

Easy

LC #1971

dfsbfsconnectivity

Not attempted yet

You get an undirected graph with n nodes labelled 0..n-1 as a list of edges ([u, v] pairs). Return True if there is a path from source to destination, otherwise False.

Example 1

Input: n = 3, edges = [[0,1],[1,2],[2,0]],
       source = 0, destination = 2
Output: True

Example 2

Input: n = 6,
       edges = [[0,1],[0,2],[3,5],[5,4],[4,3]],
       source = 0, destination = 5
Output: False

{0, 1, 2} and {3, 4, 5} are separate components.

Constraints

  • 1 <= n <= 2 * 10^4
  • 0 <= len(edges) <= 2 * 10^4
  • no duplicate edges and no self-loops

Python

Loading draft…

Test results

6 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

2 examples, 4 hidden

Run examples, then submit all tests.