-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind if path exits in graph.cpp
42 lines (38 loc) · 1.26 KB
/
find if path exits in graph.cpp
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
41
42
class Solution
{
public:
bool validPath(int n, vector<vector<int>> &edges, int source, int destination)
{
// Create an adjacency list representation of the graph
vector<vector<int>> graph(n);
for (auto &edge : edges)
{
graph[edge[0]].push_back(edge[1]);
graph[edge[1]].push_back(edge[0]);
}
// Initialize a queue for BFS traversal and a visited array
queue<int> q;
vector<int> visited(n, 0);
visited[source] = 1;
q.push(source);
// Perform BFS traversal
while (!q.empty())
{
int current = q.front();
q.pop();
if (current == destination)
return true; // Destination reached
// Visit all neighbors of the current vertex
for (int neighbor : graph[current])
{
if (visited[neighbor] == 0)
{
visited[neighbor] = 1;
q.push(neighbor);
}
}
}
return false; // Destination not reached, no valid path exists
}
};
// leetcode - 1971