Algorithm:
visited = zeros(n)
Explore(v,visited)
function Explored(v,visited)
visited[v] = 1;
for all u neighbours of v do
if not visited[i] then
Explore(u,visited)
Now I had to prove the following:
- If $u$ is not reachable from the given vertex then visited[u] = 0.
- If u is reachable from the given vertex then visited[u] = 1.
Proof (2): I proved the second one by using induction on the path length. Now the path length between $u$ and $v$ is 1 that means u is a neighbour of $v$ and we call explore on all the neighbours of $v$ hence $visited[u] = 1$. Now let us suppose the algorithm gives correct output for any vertex $u$ which is connected to $v$ by a path of length $k$. Now any vertex which has path length of $k+1$ from v must have a neighbour which has path of length $k$ and which would have been marked $visited[u] = 1$. Now when the function is called on that all the neighbours will be marked 1. Hence the vertex which is at length of $k+1$ will also be marked 1.
Proof(1): To prove this I used proof by contrapositive. So if $visited[u] = 1$ then $u$ is reachable from $v$. I don't know how to progress forward from this.
The other way I tried to prove this is proof by contradiction. Let us assume $v$ is not reachable from $v$ and it is still marked 1. Then as it is 1 one all of the vertices reachable from v must be marked 1. (From the second statement). And I get stuck here. Don't know how to continue with the proof.