Determine whether two vertices in an undirected graph are connected by any path.
You are given an undirected graph with n vertices labeled from 0 to n - 1, a list of edges, a source vertex source, and a destination vertex destination.
Return true if there exists any path from source to destination; otherwise return false.
A path may use any number of intermediate vertices, but it must follow the edges of the graph.
n: number of verticesedges: list of undirected edges, where each edge is [u, v]source: starting vertexdestination: target vertextrue if destination is reachable from source, otherwise return false.0 <= source, destination < nn vertices labeled 0..n-1edges[i] = [u, v] represents an undirected edge between u and vn and edges.length are expected to be within typical interview-sized limits for graph traversalsource, can you tell whether destination is among them?n: number of verticesedges: undirected edge list [[u1, v1], [u2, v2], ...]source: start vertexdestination: target vertextrue if a path exists, otherwise false.0 to n - 1source and destination are valid vertex labelsExample 1
Input
n = 3 edges = [[0,1],[1,2],[2,0]] source = 0 destination = 2
Output
true
Explanation
There is a direct edge from 0 to 2, so a path exists.
Example 2
Input
n = 6 edges = [[0,1],[0,2],[3,5],[5,4],[4,3]] source = 0 destination = 5
Output
false
Explanation
Vertices 0 and 5 belong to different connected components, so no path exists.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.