-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectedPath.cc
52 lines (43 loc) · 1.05 KB
/
directedPath.cc
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
43
44
45
46
47
48
49
50
51
52
/**
* Check whether there is a path from the first given vertex to the second
* in a given directed graph.
*/
#include <bits/stdc++.h>
using namespace std;
bool checkPath(auto& adj, vector<bool>& visited, int src, int dest) {
queue<int> q;
visited[src] = true;
q.push(src);
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto it = adj[u].begin(); it != adj[u].end(); ++it) {
// If an adjacent vertex is destination node, return true.
if (u == dest)
return true;
if (visited[*it] == false) {
visited[*it] = true;
q.push(*it);
}
}
}
return false;
}
int main() {
int nv, ne, src, dest;
cin >> nv >> ne >> src >> dest;
vector<vector<int>> adj(nv);
for (int i = 0; i < ne; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
}
vector<bool> visited(nv, false);
// Using simple BFS. Could use DFS as well.
if (!checkPath(adj, visited, src, dest))
cout << "No path found...\n";
else
cout << "Path do exists from "<< src << " to " << dest << "\n";
return 0;
}
// Time Complexity: O(V+E) i.e. same as DFS or BFS