K24

Path Exists in Graph

Junior Level Candidate
Target Time: 15 - 20 Minutes
Type: BOOLEAN

Problem Statement & Objective

Given an undirected graph and two vertices source and destination, determine whether there is a valid path from source to destination.

Function Signature (Python)

path_exists_in_graph(n: int, edges: list[list[int]], source: int, target: int) -> bool

Input & Output Contract

Output Rule:True veya False.

System Constraints

1 <= n <= 2 * 10^5
0 <= kenarlar.length <= 2 * 10^5
0 <= kaynak, hedef < n

Example Execution Walkthrough

n = 3, kenarlar = [[0, 1], [1, 2], [2, 0]], kaynak = 0, hedef = 2 -> True

Example Test Scenarios (3)

Example 1: Üçgen Yol
Input:[3,[[0,1],[1,2],[2,0]],0,2]
Expected Output:true
Example 2: Ayrık Bileşenler
Input:[6,[[0,1],[0,2],[3,5],[5,4],[4,3]],0,5]
Expected Output:false
Example 3: Tek Düğüm
Input:[1,[],0,0]
Expected Output:true
Loading Python Editor...
Input Parameters
[
  3,
  [
    [
      0,
      1
    ],
    [
      1,
      2
    ],
    [
      2,
      0
    ]
  ],
  0,
  2
]
Expected Output
true