K24
Path Exists in Graph
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) -> boolInput & Output Contract
Output Rule:True veya False.
System Constraints
1 <= n <= 2 * 10^5
0 <= kenarlar.length <= 2 * 10^5
0 <= kaynak, hedef < nExample Execution Walkthrough
n = 3, kenarlar = [[0, 1], [1, 2], [2, 0]], kaynak = 0, hedef = 2 -> TrueExample Test Scenarios (3)
Example 1: Üçgen Yol
Input:
[3,[[0,1],[1,2],[2,0]],0,2]Expected Output:
trueExample 2: Ayrık Bileşenler
Input:
[6,[[0,1],[0,2],[3,5],[5,4],[4,3]],0,5]Expected Output:
falseExample 3: Tek Düğüm
Input:
[1,[],0,0]Expected Output:
true