{"id":236,"date":"2023-08-16T11:20:41","date_gmt":"2023-08-16T03:20:41","guid":{"rendered":"http:\/\/lingbo.online\/?p=236"},"modified":"2023-08-21T15:00:14","modified_gmt":"2023-08-21T07:00:14","slug":"bfs","status":"publish","type":"post","link":"https:\/\/lingbo.online\/index.php\/uncategorized\/bfs\/","title":{"rendered":"\u5bbd\u5ea6\u4f18\u5148\u641c\u7d22\u7b97\u6cd5\uff08BFS\uff09\u8be6\u89e3"},"content":{"rendered":"<h3>\u57fa\u672c\u6982\u5ff5<\/h3>\n<p>\u5bbd\u5ea6\u4f18\u5148\u641c\u7d22\u7b97\u6cd5\uff08Breadth First Search\uff09\uff0c\u662f\u56fe\u641c\u7d22\u4e2d\u6700\u57fa\u7840\u3001\u6700\u91cd\u8981\u7684\u7b97\u6cd5\u4e4b\u4e00\u3002Dijkstra\u5355\u5143\u6700\u77ed\u8def\u5f84\u7b97\u6cd5\u548cPrim\u6700\u5c0f\u751f\u6210\u6811\u7b97\u6cd5\u90fd\u91c7\u7528\u4e86\u548c\u5bbd\u5ea6\u641c\u7d22\u7c7b\u4f3c\u7684\u601d\u60f3\u3002\u5bbd\u5ea6\u4f18\u5148\u641c\u7d22\u5c5e\u4e8e\u4e00\u79cd\u76f2\u76ee\u641c\u7d22\u7b97\u6cd5\uff0c\u76ee\u7684\u662f\u7cfb\u7edf\u5730\u5c55\u5f00\u5e76\u68c0\u67e5\u56fe\u4e2d\u7684\u6240\u6709\u8282\u70b9\uff0c\u4ee5\u5bfb\u627e\u7ed3\u679c\uff0c\u5373\uff0c\u8fd9\u79cd\u7b97\u6cd5\u4e0d\u8003\u8651\u7ed3\u679c\u7684\u53ef\u80fd\u4f4d\u7f6e\uff0c\u5f7b\u5e95\u5730\u641c\u7d22\u6574\u5f20\u56fe\uff0c\u76f4\u5230\u627e\u5230\u7ed3\u679c\u4e3a\u6b62\u3002<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u4e00\u5f00\u59cb\u4f4d\u4e8e\u67d0\u4e2a\u9876\u70b9\uff08\u5373\u8d77\u70b9\uff09\uff0c\u7531\u4e8e\u6b64\u65f6\u4e0d\u77e5\u9053\u56fe\u7684\u6574\u4f53\u7ed3\u6784\uff0c\u53ea\u80fd\u4ece\u8d77\u70b9\u5f00\u59cb\u987a\u7740\u8fb9\u641c\u7d22\uff0c\u76f4\u5230\u5230\u8fbe\u6307\u5b9a\u9876\u70b9\uff08\u7ec8\u70b9\uff09\u3002\u518d\u6b21\u8fc7\u7a0b\u4e2d\uff0c\u6bcf\u7ecf\u8fc7\u4e00\u4e2a\u9876\u70b9\uff0c\u90fd\u9700\u8981\u5224\u65ad\u5b83\u662f\u5426\u4e3a\u7ec8\u70b9\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5bbd\u5ea6\u4f18\u5148\u641c\u7d22\u4f1a\u4f18\u5148\u4ece\u79bb\u8d77\u70b9\u8fd1\u7684\u9876\u70b9\u5f00\u59cb\u641c\u7d22\u3002<\/p>\n<h3>\u56fe\u89e3\u7b97\u6cd5<\/h3>\n<p>\u672c\u8282\u4ee5\u7ed8\u56fe\u7684\u65b9\u5f0f\u5c55\u793a\u5bbd\u5ea6\u4f18\u5148\u641c\u7d22\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u76f8\u5173\u56fe\u7247\u6765\u81ea<a href=\"https:\/\/blog.csdn.net\/aliyonghang\/article\/details\/128724989\" title=\"CSDN\" target=\"_blank\"  rel=\"nofollow\" >CSDN<\/a>\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2023\/08\/1-1.png\" alt=\"\" \/><\/p>\n<p>\u5047\u8bbeA\u4e3a\u8d77\u70b9\uff0cG\u4e3a\u7ec8\u70b9\u3002\u6700\u521d\u6211\u4eec\u4f4d\u4e8eA\u70b9\uff0c\u6b64\u65f6\u4e0d\u77e5\u9053G\u70b9\u5728\u54ea\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2023\/08\/2.png#pic_center\" alt=\"\" \/><\/p>\n<p>\u7531\u4e8eB\u3001C\u3001D\u70b9\u4e0eA\u70b9\u76f4\u63a5\u76f8\u8fde\uff0c\u6545\u5c06\u5176\u8bbe\u4e3a\u4e0b\u4e00\u6b65\u7684\u540e\u8865\u9876\u70b9\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2023\/08\/3.png#pic_center\" alt=\"\" \/><\/p>\n<p>\u4ece\u5019\u8865\u9876\u70b9\u4e2d\u9009\u51fa\u4e00\u4e2a\u9876\u70b9\u524d\u8fdb\u3002<strong>\u4f18\u5148\u9009\u62e9\u6700\u65e9\u6210\u4e3a\u540e\u8865\u7684\u90a3\u4e2a\u9876\u70b9<\/strong>\uff08\u5728Dijkstra\u7b97\u6cd5\u4e2d\uff0c\u9009\u62e9\u8ddd\u79bb\u6700\u77ed\u7684\u9876\u70b9\uff09\uff0c\u5982\u679c\u591a\u4e2a\u9876\u70b9\u540c\u65f6\u79f0\u4e3a\u540e\u8865\uff0c\u90a3\u4e48\u53ef\u4ee5\u968f\u673a\u9009\u62e9\u5176\u4e2d\u4e00\u4e2a\uff08\u4f18\u5148\u9009\u62e9\u6700\u65e9\u79f0\u4e3a\u5019\u8865\u7684\u9876\u70b9\uff0c\u7b26\u5408\u961f\u5217\u201c\u5148\u8fdb\u5148\u51fa\u201d\u7684\u601d\u60f3\uff09<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2023\/08\/4.png\" alt=\"\" \/><\/p>\n<p>\u4f9d\u636e\u8fd9\u4e00\u9009\u62e9\u601d\u60f3\uff0c\u9009\u62e9\u6700\u5de6\u8fb9\u7684\u9876\u70b9B\uff0c\u4eceA\u70b9\u79fb\u52a8\u5230B\u4e0a\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2023\/08\/5.png\" alt=\"\" \/><\/p>\n<p>\u5c06B\u70b9\u53d8\u4e3a\u7ea2\u8272\uff0c\u5df2\u7ecf\u641c\u7d22\u8fc7\u7684\u9876\u70b9\u53d8\u4e3a\u6a59\u8272\u3002<br \/>\n\u6b64\u65f6\uff0c\u5019\u8865\u9876\u70b9\u7528<strong>\u201c\u5148\u5165\u5148\u51fa\u201d\uff08FIFO\uff09<\/strong>\u7684\u65b9\u5f0f\u6765\u7ba1\u7406\uff0c\u56e0\u6b64\u4f7f\u7528<strong>\u201c\u961f\u5217\u201d<\/strong>\u8fd9\u4e2a\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u7ba1\u7406\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2023\/08\/6.png\" alt=\"\" \/><\/p>\n<p>\u5c06\u4eceB\u70b9\u76f4\u8fbe\u7684\u4e24\u4e2a\u9876\u70b9E\u548cF\u8bbe\u4e3a\u5019\u8865\u9876\u70b9\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2023\/08\/7.png\" alt=\"\" \/><\/p>\n<p>\u6b64\u65f6\uff0c\u6700\u65e9\u6210\u4e3a\u5019\u8865\u9876\u70b9\u7684\u662fC\u548cD\uff0c\u9009\u62e9\u6700\u5148\u6210\u4e3a\u5019\u8865\u9876\u70b9\u7684C\u70b9<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2023\/08\/8.png\" alt=\"\" \/><\/p>\n<p>\u79fb\u52a8\u5230C\u70b9\u7ee7\u7eed\u8fdb\u884c\u8ba1\u7b97<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2023\/08\/9.png\" alt=\"\" \/><\/p>\n<p>\u5c06\u53ef\u4ee5\u4eceC\u70b9\u76f4\u8fbe\u7684\u9876\u70b9H\u8bbe\u4e3a\u5019\u8865\u9876\u70b9\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2023\/08\/10.png\" alt=\"\" \/><\/p>\n<p>\u91cd\u590d\u4e0a\u8ff0\u64cd\u4f5c\u76f4\u5230\u5230\u8fbe\u7ec8\u70b9G\uff0c\u6216\u8005\u6240\u6709\u9876\u70b9\u90fd\u88ab\u904d\u5386\u4e3a\u6b62\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2023\/08\/11.png\" alt=\"\" \/><\/p>\n<p>\u8fd9\u4e2a\u793a\u4f8b\u4e2d\u7684\u641c\u7d22\u987a\u5e8f\u4e2d\u4e3aA\u3001B\u3001C\u3001D\u3001E\u3001F\u3001H\u3001I\u3001J\u3001K\u3001G<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2023\/08\/12.png\" alt=\"\" \/><\/p>\n<p>\u5b8c\u6210\u4eceA\u5230I\u7684\u641c\u7d22\uff0c\u73b0\u5728\u5728\u9876\u70b9J\u5904<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2023\/08\/13.png\" alt=\"\" \/><\/p>\n<p>\u5230\u8fbe\u7ec8\u70b9G\uff0c\u7ed3\u675f\u641c\u7d22\u3002<br \/>\n\u7531\u8be5\u8ba1\u7b97\u6d41\u7a0b\u53ef\u4ee5\u770b\u51fa\uff0c\u5bbd\u5ea6\u4f18\u5148\u641c\u7d22\u7684\u7279\u5f81\u4e3a\u4ece\u8d77\u70b9\u5f00\u59cb\uff0c<strong>\u7531\u8fd1\u53ca\u8fdc<\/strong>\u8fdb\u884c\u5e7f\u6cdb\u7684\u641c\u7d22\u3002\u56e0\u6b64\uff0c\u76ee\u6807\u9876\u70b9\u8ddd\u79bb\u8d77\u70b9\u8d8a\u8fd1\uff0c\u641c\u7d22\u7ed3\u675f\u7684\u5c31\u8d8a\u5feb\u3002<\/p>\n<h3>\u4ee3\u7801\u6a21\u677f<\/h3>\n<pre><code class=\"language-python\">from collections import deque\n\ndef bfs(graph,start,target):\n&quot;&quot;&quot;\n\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u7b97\u6cd5\n:param graph: \u56fe\u7684\u90bb\u63a5\u8868\u8868\u793a\n:param start: \u8d77\u59cb\u8282\u70b9\n:param target: \u76ee\u6807\u8282\u70b9\n:return: \u662f\u5426\u627e\u5230\u76ee\u6807\u8282\u70b9\n&quot;&quot;&quot;\nqueue=deque() #\u521b\u5efa\u961f\u5217\nqueue.append(start) # \u5c06\u8d77\u59cb\u8282\u70b9\u5165\u961f\nvisited=set() # \u521b\u5efa\u96c6\u5408\u7528\u4e8e\u5b58\u50a8\u5df2\u8bbf\u95ee\u7684\u8282\u70b9\n\nwhile queue:\n    node=popleft() # \u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u4e00\u4e2a\u8282\u70b9\n    if node==target:\n        return True # \u627e\u5230\u76ee\u6807\u8282\u70b9\n\n    visited.add(node) # \u6807\u8bb0\u8282\u70b9\u4e3a\u5df2\u8bbf\u95ee\n\n    for neighbor in graph[node]:\n        if neighbor not in visited:\n            queue.append(neighbor) # \u5c06\u672a\u8bbf\u95ee\u7684\u76f8\u90bb\u8282\u70b9\u5165\u961f\n\n    return False # \u672a\u627e\u5230\u76ee\u6807\u8282\u70b9\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u672c\u6982\u5ff5 \u5bbd\u5ea6\u4f18\u5148\u641c\u7d22\u7b97\u6cd5\uff08Breadth First Search\uff09\uff0c\u662f\u56fe\u641c\u7d22\u4e2d\u6700\u57fa\u7840\u3001\u6700\u91cd\u8981\u7684\u7b97\u6cd5\u4e4b\u4e00\u3002Dijkstra\u5355\u5143\u6700 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[32,1,2],"tags":[35,34,10],"class_list":["post-236","post","type-post","status-publish","format-standard","hentry","category-data_structure","category-uncategorized","category-algorithm_learning","tag-bfs","tag-34","tag-10"],"_links":{"self":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts\/236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/comments?post=236"}],"version-history":[{"count":3,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts\/236\/revisions"}],"predecessor-version":[{"id":252,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts\/236\/revisions\/252"}],"wp:attachment":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/media?parent=236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/categories?post=236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/tags?post=236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}