title: Using BFS algorithm to find the shortest path
body: <pre><code>std::list &lt;int&gt; q;
  std::vector&lt;bool&gt; visited(cols + 1);
  for(int i = 1; i &lt;= cols; i++) visited[i] = false;
  visited[x] = true;
  if(!l[x].empty())
  {
      for(std::list&lt;int&gt;::iterator i = l[x].begin(); i != l[x].end(); i++)
      {
          q.push_back(x); q.push_back(* i);
      }
      while(!q.empty())
      {
          y = q.back(); q.pop_back();
          x = q.back(); q.pop_back();
          if(!visited[y])
          {
              visited[y] = true;
              if(!l[y].empty())
              for(std::list&lt;int&gt;::iterator i = l[y].begin(); i != l[y].end(); i++)
              {
                  if(!visited[*i])
                  {q.push_back(y); q.push_back(* i);}
              }
              dfst[x].push_back(y);
              if(flag != 0) dfst[y].push_back(x);
          }
      }
  }
  </code></pre>
  
  <p>This is my DFS algorithm for finding the spanning tree in a graph. I need to convert it to the BFS algorithm finding the shortest path between two vertices. Well...how can I do this? Is the BFS algorithm somewhat similar to the one above? Or do I need to write it from the beginning?</p>
  
  <p>l - adjacency list
  dfst - array holding spanning tree at the end
  x - starting vertex
  y - helper variable</p>