Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Path Existence Between Two Nodes

Easy Acceptance 80.12% Points 20.00

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
Companies
Cisco BNY Mellon
Topics
Graph
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions