-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind if Path Exists in Graph.cpp
More file actions
40 lines (29 loc) · 880 Bytes
/
Find if Path Exists in Graph.cpp
File metadata and controls
40 lines (29 loc) · 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public:
bool answer = 0;
void dfs(vector<int>adj[],vector<int>& visited,int source,int destination)
{
visited[source] = true;
if(source == destination)
{
answer = 1;
return;
}
for(auto V : adj[source])
{
if(!visited[V])
dfs(adj,visited,V,destination);
}
}
bool validPath(int n, vector<vector<int>>& edges, int start, int destination) {
vector<int>adj[n];
for(auto edge : edges)
{
adj[edge[0]].push_back(edge[1]);
adj[edge[1]].push_back(edge[0]);
}
vector<int>visited(n,0);
dfs(adj,visited,start,destination);
return answer;
}
};