title: Traversing the Graph and listing the vertices visited along the given path
body: <p>Graph contains 'n' nodes and 'n-1' edges, hence, a unique path between any two nodes.<br><br>
  OK, so I know how we can find the vertices that lie on a path(given: source, destination) in a graph. It can easily be done by using DFS or BFS.</p>
  
  <p>Example:</p>
  
  <p><a href="https://i.stack.imgur.com/Qdcay.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Qdcay.png" alt="enter image description here"></a></p>
  
  <p><strong>Input</strong><br>
  List of entries, each entry contain Source and Destination vertices<br>
  <strong>Output</strong><br>
  List of entries, each entry containing 'Vertices on path' corresponding to input entry.</p>
  
  <pre><code>Source Destination  Vertices on path
    5         4           5 4
    3         5           3 4 5
    1         3           1 5 4 3
    3         5           3 4 5
    5         4           5 4
  </code></pre>
  
  <p><strong>My current solution</strong><br>
  Currently I am running iterative DFS on each given path and finding the vertices that lie on that path.</p>
  
  <p><strong>Issue/Problem</strong><br>
  When the no of nodes and no of paths given are large(~100,000) then finding the vertices on each given path(for all paths) is time consuming task.(Running DFS on each path for 100,000 paths for graph having 100,000 nodes). I want to know the better method of solving this problem. What data-structure or algorithm I may use instead of my current solution. Sometimes, I am also getting StackOverflow error, which I think is due to deep recursion.</p>