From 8eaf456f14f79ee4af1901e09bccb3e9ae1bf116 Mon Sep 17 00:00:00 2001 From: Himanshu sahu Date: Wed, 14 Feb 2024 22:20:25 +0530 Subject: [PATCH] Added FindPathsExists --- Graph/FindPathExistsInGraph | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Graph/FindPathExistsInGraph diff --git a/Graph/FindPathExistsInGraph b/Graph/FindPathExistsInGraph new file mode 100644 index 0000000..03d39c9 --- /dev/null +++ b/Graph/FindPathExistsInGraph @@ -0,0 +1,41 @@ +Problem statement is -There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive). The edges in the graph are represented as a 2D integer array edges, where each edges[i] = [ui, vi] denotes a bi-directional edge between vertex ui and vertex vi. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself. + +You want to determine if there is a valid path that exists from vertex source to vertex destination. + +Given edges and the integers n, source, and destination, return true if there is a valid path from source to destination, or false otherwise. + + + +## solotion +class Solution { +public: + bool validPath(int n, vector>& edges, int source, int destination) { + vector>adj(n); + queueq; + vectorvisited(n+1,false); + + for(auto x : edges){ + auto u=x.front(); + auto v=x.back(); + adj[u].push_back(v); + adj[v].push_back(u); + } + q.push(source); + visited[source]=true; + + while(!q.empty()){ + auto temp=q.front(); + q.pop(); + + for(auto vertex : adj[temp]){ + if(!visited[vertex]){ + visited[vertex]=true; + q.push(vertex); + } + + + } + } + return visited[destination]; + } +}; \ No newline at end of file