{
  "id": 2005,
  "title": "BFS Benchmark error",
  "url": "/competitions/FacebookRecruiting/discussion/2005",
  "author_name": "",
  "post_date": "2012-06-07T09:47:18.237Z",
  "votes": 2,
  "comment_count": 1,
  "views": 1810,
  "content": "<p>In the BFS benchmark code, next_node should be added to looked_at at some point in the while loop, eg:</p>\r\n<pre class=\"x_p1\"><span style=\"font-family:'courier new',courier\">&nbsp; &nbsp;&nbsp;<span class=\"x_s1\">while</span> queue <span class=\"x_s1\">and</span> len(visited)&lt;num_nodes:</span></pre>\r\n<pre class=\"x_p1\"><span style=\"font-family:'courier new',courier\">&nbsp; &nbsp; &nbsp; &nbsp; next_node = queue.popleft()</span></pre>\r\n<pre class=\"x_p1\"><strong><span style=\"font-family:'courier new',courier\">&nbsp; &nbsp; &nbsp; &nbsp; looked_at.add(next_node)</span></strong></pre>\r\n<pre class=\"x_p1\"><span style=\"font-family:'courier new',courier\">&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"x_s1\">if</span> next_node <span class=\"x_s1\">not</span> <span class=\"x_s1\">in</span> neighbors:</span></pre>\r\n<pre class=\"x_p1\"><span style=\"font-family:'courier new',courier\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; visited.append(next_node)</span></pre>\r\n<pre class=\"x_p1\"><span style=\"font-family:'courier new',courier\">&nbsp; &nbsp; &nbsp; &nbsp; queue.extend(n <span class=\"x_s1\">for</span> n <span class=\"x_s1\">in</span> graph[next_node] <span class=\"x_s1\">if</span> n <span class=\"x_s1\">not</span> <span class=\"x_s1\">in</span> looked_at)</span></pre>\r\n<p>Otherwise the BFS will continue to visit nodes it has already seen. This won't make much difference in this case because we are stopping after visiting 10 nodes anyway, but in an unbounded BFS it would be pretty inefficient. You can see the consequences\r\n of this error by, for example</p>\r\n<pre>grep 1861969 bfs_benchmark_web.csv <br>1861969,1106237 1562455 338173 1106237 200587 244377 421994 1106237 611500 1562455</pre>\r\n<p>1106237 and 1562455 are listed twice.</p>",
  "messages": [
    {
      "id": "11438",
      "postDate": "06/07/2012 09:47:18",
      "content": "<p>In the BFS benchmark code, next_node should be added to looked_at at some point in the while loop, eg:</p>\r\n<pre class=\"x_p1\"><span style=\"font-family:'courier new',courier\">&nbsp; &nbsp;&nbsp;<span class=\"x_s1\">while</span> queue <span class=\"x_s1\">and</span> len(visited)&lt;num_nodes:</span></pre>\r\n<pre class=\"x_p1\"><span style=\"font-family:'courier new',courier\">&nbsp; &nbsp; &nbsp; &nbsp; next_node = queue.popleft()</span></pre>\r\n<pre class=\"x_p1\"><strong><span style=\"font-family:'courier new',courier\">&nbsp; &nbsp; &nbsp; &nbsp; looked_at.add(next_node)</span></strong></pre>\r\n<pre class=\"x_p1\"><span style=\"font-family:'courier new',courier\">&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"x_s1\">if</span> next_node <span class=\"x_s1\">not</span> <span class=\"x_s1\">in</span> neighbors:</span></pre>\r\n<pre class=\"x_p1\"><span style=\"font-family:'courier new',courier\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; visited.append(next_node)</span></pre>\r\n<pre class=\"x_p1\"><span style=\"font-family:'courier new',courier\">&nbsp; &nbsp; &nbsp; &nbsp; queue.extend(n <span class=\"x_s1\">for</span> n <span class=\"x_s1\">in</span> graph[next_node] <span class=\"x_s1\">if</span> n <span class=\"x_s1\">not</span> <span class=\"x_s1\">in</span> looked_at)</span></pre>\r\n<p>Otherwise the BFS will continue to visit nodes it has already seen. This won't make much difference in this case because we are stopping after visiting 10 nodes anyway, but in an unbounded BFS it would be pretty inefficient. You can see the consequences\r\n of this error by, for example</p>\r\n<pre>grep 1861969 bfs_benchmark_web.csv <br>1861969,1106237 1562455 338173 1106237 200587 244377 421994 1106237 611500 1562455</pre>\r\n<p>1106237 and 1562455 are listed twice.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "11442",
      "postDate": "06/07/2012 15:47:55",
      "content": "<p>Thanks for pointing this out - the bug's been fixed in the github repo. It had a very small impact on performance since the BFS terminated after only 10 nodes.</p>",
      "rawMarkdown": "",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 11442,
      "author_name": "benhamner",
      "author_url": "",
      "post_date": "06/07/2012 15:47:55",
      "content": "<p>Thanks for pointing this out - the bug's been fixed in the github repo. It had a very small impact on performance since the BFS terminated after only 10 nodes.</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "11438": "",
    "11442": ""
  },
  "source": "meta"
}