PIXELBANKv9.1.0
Menu

Find if Path Exists in Graph

Given n nodes, undirected edges, and two nodes source and destination, return True if a path exists between them.

Input: first line = n, second = edges as u:v comma-separated (or 'none'), third = source destination.

Example:

Input:
3
0:1,1:2,2:0
0 2
Output:
True
Reasoning:
  • The graph is constructed with n=3n=3 nodes and undirected edges between nodes 0 and 1, 1 and 2, and 2 and 0.
  • The edges create a cycle: 0↔1↔2↔00 \leftrightarrow 1 \leftrightarrow 2 \leftrightarrow 0, allowing for a path between any two nodes.
  • A path exists from the source node 0 to the destination node 2, as they are directly connected through node 1 and also through the cycle 0↔20 \leftrightarrow 2.
  • The function returns True because a path is found between the source and destination nodes.

Constraints:

  • 1 <= n <= 2 * 10^5
  • 0 <= edges.length <= 2 * 10^5
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Find if Path Exists in Graph - Easy | PixelBank