title: Get path between 2 vertices using BFS in C++
body: <p>I have written a code for this but it gives <strong>segmentation fault for disconnected graphs</strong>. It works fine for graphs that are connected.
  How can I overcome this error?</p>
  
  <pre><code>vector&lt;int&gt; getPathBFS(int V, int** edges,int v1, int v2, int* visited, unordered_map&lt;int,int&gt; t)
  {
      queue&lt;int&gt; q;
      q.push(v1);
      visited[v1]=1;
      int done=0;
      while(!q.empty() &amp;&amp; done==0)
      {
          for(int i=0;i&lt;V;i++)
          {
              int front=q.front();
              q.pop();
              if(edges[front][i]==1 &amp;&amp; visited[i]!=1)
              {
                  q.push(i);
                  t[i]=front;
                  visited[i]=1;
                  if(i==v2)
                  {
                      done=1;
                      break;
                  }
              }
          }
      }
      vector&lt;int&gt; a;
      if(done==0)
          return a;
      else
      {
          int k=v2;
          a.push_back(v2);
          while(k!=v1)
          {
              k=t[k];
              a.push_back(k);
          }
          return a;
      }
  }
  
  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 v1,v2;
      cin&gt;&gt;v1&gt;&gt;v2;
  
      int* visited=new int[V];
      for(int i=0;i&lt;V;i++)
          visited[i]=0;
      unordered_map&lt;int,int&gt; t;
      t[v2]=0;
      vector&lt;int&gt; ans=getPathBFS(V,edges,v1,v2,visited,t);
      for(int i=0;i&lt;ans.size();i++ &amp;&amp; !ans.empty())
      {
          cout&lt;&lt;ans[i]&lt;&lt;" ";
      }
  
      delete [] visited;
      for(int i=0;i&lt;V;i++)
      {
          delete [] edges[i];
      }
  
      delete [] edges;
  
      return 0;
  }
  </code></pre>
  
  <p>I did a dry run of the code. It will first create adjacency matrix edges and mark all the edges in it. Visited array is used to keep track of all the vertices that have been visited till now so that there is no infinite loop. 
  For the test case given below it will work till the queue contains 1 then it will pop 1 and the loop will end because there is no edge left that is connected to 1 and is not visited. After this the while loop should ideally break and as done==0 it should return an empty vector. I can't understand why the segmentation fault is coming.</p>
  
  <p>The map is being used to keep track of which vertex was put in the queue by which vertex.</p>
  
  <p>Doesn't work for the test case:</p>
  
  <pre><code>6 3
  5 3
  0 1
  3 4
  0 3
  </code></pre>
  
  <p>Below is the image of the graph for the above test case:
  <a href="https://i.stack.imgur.com/hnxa2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hnxa2.png" alt="enter image description here"></a></p>
  
  <p>Here we need to find the path from vertex 0 to 3.
  The input format is :
  Number of Vertices in the graph, Number of edges</p>
  
  <p>Edges between the vertices (for E lines),</p>
  
  <p>Vertices between which we need to find the path.</p>