Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Path Existence Between Two Nodes

Easy Acceptance 80.12% Points 20

You are given an undirected graph and two vertices, source and destination. Your task is to determine whether a path exists between them.

Return true if a path exists; otherwise, return false.

Examples
Example 1

image Input: V = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0, destination = 5
Output: false
Explanation: There is no path between vertices 0 and 5.

Example 2

image Input: V = 3, edges = [[0,1],[1,2]], source = 0, destination = 2
Output: true
Explanation: A path exists from vertex 0 to vertex 2.

Example 3

image Input: V = 1, edges = [], source = 0, destination = 0
Output: true
Explanation: The source and destination are the same vertex.

Constraints
    • 1 = V = 105
    • 0 = E = 2 × 105
Topics
Graph
Companies
Cisco BNY Mellon
Added 15 Aug 2026 80.12% acceptance
Ln 1, Col 1Sign in to save your code
6 5
0 1
0 2
3 5
5 4
4 3
0 5
false