BFS/DFS is the simplest way. I'll sketch up DFS solution because it's less memory hungry.
Presuming you have adjacency matrix adj
of size N x N
(N
being number of vertices in a graph) with:
1
in adj[i][j]
if you have edge going from i
vertex to j
vertex,
0
otherwise.
In that case you could have something like this:
// doing DFS...
bool isPath(int src, int dest) {
bool visited[N] = {false};
visited[src] = true;
std::stack<int> next;
next.push(src);
while(!next.empty()) {
int cv = next.top();
next.pop();
for (int nv = 0; nv < N; ++nv) {
if (!visited[nv] && adj[cv][nv] == 1) {
visited[nv] = true;
next.push(nv);
}
}
}
// dest was reached from src?
return visited[dest];
}