The total number of distinct nodes visited, considering all possible paths.

Given a undirected graph, source and destination, write the code to find the total number of distinct nodes visited, considering all possible paths.

I tried to do it with dfs .But unable to mark nodes in recursion . Please suggest a good algorithm.

Simply maintain another array. But you will have to solve the following problem. When you have graph

     B
    / \
-> A   D ->
    \ /
     C

Where A is source and D is destination, the result is 2, not 1. With DFS it stops when first path is found, but this is not a big deal…