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 n1n-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:

  • 11\leq n 102\leq 10^2

  • 00 \leq edges.length n(n1)/2\leq n(n-1)/2

  • edges[i].length =2 = 2

  • 0xi,yin10 \leq x_i,y_i \leq n-1

  • xiyi x_i\ne y_i

  • 00\leq source, destination n1\leq n - 1

  • There are no duplicate edges.

  • There are no self-edges.

Examples

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