title: Check if there is a path that connects 2 vertices in C++
body: <p>I'm trying to write a code for checking a path exists from vertex <code>v1</code> to <code>v2</code> in a given graph. 
  It <strong>works for certain test cases and gives runtime error (time limit exceeded) for others</strong>.</p>
  
  <pre><code>bool HasPath(int V, int** edges, int* visited, int v1, int v2)
  {
      if (edges[v1][v2] == 1)
      {
          return true;
      }
  
      for (int i = 0; i&lt;V; i++)
      {
          if (visited[i] == 1)
              continue;
  
          if (edges[v1][i] == 1)
          {
              bool sa = HasPath(V, edges, visited, i, v2);
              visited[i] = 1;
              if (sa == false)
                  continue;
              else
                  return true;
          }
      }
      return false;
  }
  
  int main() {
      int V, E;
      cin &gt;&gt; V &gt;&gt; E;
  
      int** edges = new int*[V];
      for (int i = 0; i&lt;V; i++)
      {
          edges[i] = new int[V];
          for (int j = 0; j&lt;V; j++)
          {
              edges[i][j] = 0;
          }
      }
  
      for (int i = 0; i&lt;E; i++)
      {
          int f, s;
          cin &gt;&gt; f &gt;&gt; s;
          edges[f][s] = 1;
          edges[s][f] = 1;
      }
  
      int* visited = new int[V];
      for (int i = 0; i&lt;V; i++)
          visited[i] = 0;
  
      int v1, v2;
      cin &gt;&gt; v1 &gt;&gt; v2;
      bool ans = HasPath(V, edges, visited, v1, v2);
      if (ans == 1)
          cout &lt;&lt; "true";
      else
          cout &lt;&lt; "false";
      return 0;
  }
  </code></pre>
  
  <p>Edges is the adjacency matrix. And the graph is bidirectional. The aim is to find if path exists from <code>v1</code> to <code>v2</code>.
  Input Format:<br>
  Line 1: <code>V</code> and <code>E</code> <br>
  Next <code>E</code> lines : Two integers <code>a</code> and <code>b</code>, denoting that there exists an edge between vertex <code>a</code> and vertex <code>b</code> (separated by space)<br>
  Line <code>(E+2)</code> : Two integers <code>v1</code> and <code>v2</code> (separated by space)</p>
  
  <p>Sample Test case that fails:</p>
  
  <pre><code>6 3
  5 3
  0 1
  3 4
  0 3 (these are the vertices between which we need to find path)
  </code></pre>
  
  <p>The graph for the above test case:</p>
  
  <p><a href="https://i.stack.imgur.com/K2Vm8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/K2Vm8.png" alt="Here is the image of the graph for the above test case"></a></p>
  
  <p>Sample Test case that passes:</p>
  
  <pre><code>4 4
  0 1
  0 3
  1 2
  2 3
  1 3
  </code></pre>