Find if Path Exists in Graph

Try to solve the Find if Path Exists in Graph problem.

Statement

Given a 2D list, edges,  which represents a bidirectional graph. Each vertex is labeled from 00 to n−1n-1, and each edge in the graph is represented as a pair, [xi,yi][x_i, y_i], showing a bidirectional edge between xix_i and yiy_i. Each pair of vertices is connected by at most one edge, and no vertex is connected to itself.

Determine whether a valid path exists from the source vertex to the destination vertex. If it exists, return TRUE; otherwise, return FALSE.

Constraints:

  • 1≤1\leq n ≤102\leq 10^2

  • 0≤0 \leq edges.length ≤n(n−1)/2\leq n(n-1)/2

  • edges[i].length =2 = 2

  • 0≤xi,yi≤n−10 \leq x_i,y_i \leq n-1

  • xi≠yi x_i\ne y_i

  • 0≤0\leq source, destination ≤n−1\leq n - 1

  • There are no duplicate edges.

  • There are no self-edges.

Examples

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.