{"id":484,"date":"2023-09-14T16:37:05","date_gmt":"2023-09-14T08:37:05","guid":{"rendered":"http:\/\/lingbo.online\/?p=484"},"modified":"2023-09-14T16:37:05","modified_gmt":"2023-09-14T08:37:05","slug":"%e6%b7%b1%e5%ba%a6%e4%bc%98%e5%85%88%e6%90%9c%e7%b4%a2%ef%bc%88depth-first-searchdfs%ef%bc%89%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/lingbo.online\/index.php\/uncategorized\/%e6%b7%b1%e5%ba%a6%e4%bc%98%e5%85%88%e6%90%9c%e7%b4%a2%ef%bc%88depth-first-searchdfs%ef%bc%89%e8%af%a6%e8%a7%a3\/","title":{"rendered":"\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff08Depth-First Search,DFS\uff09\u8be6\u89e3"},"content":{"rendered":"<h3>\u7b97\u6cd5\u6982\u8ff0<\/h3>\n<p>\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff08Depth-First Search\uff0cDFS\uff09\u662f\u5341\u5206\u5e38\u89c1\u7684\u56fe\u641c\u7d22\u65b9\u6cd5\u4e4b\u4e00\u3002\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u4f1a\u6cbf\u7740\u4e00\u6761\u8def\u5f84\u4e00\u76f4\u641c\u7d22\u4e0b\u53bb\uff0c\u5728\u65e0\u6cd5\u641c\u7d22\u65f6\uff0c\u56de\u9000\u5230\u521a\u521a\u8bbf\u95ee\u8fc7\u7684\u8282\u70b9\u3002\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u6309\u7167\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u7684\u65b9\u5f0f\u5bf9\u56fe\u8fdb\u884c\u904d\u5386\u3002\u5e76\u4e14\u6bcf\u4e2a\u8282\u70b9\u53ea\u80fd\u8bbf\u95ee\u4e00\u6b21\u3002<br \/>\n\u6df1\u641c\u4f18\u5148\u641c\u7d22\u7684\u672c\u8d28\u4e0a\u5c31\u662f\u6301\u7eed\u641c\u7d22\uff0c\u904d\u5386\u4e86\u6240\u6709\u53ef\u80fd\u7684\u60c5\u51b5\uff0c\u5fc5\u7136\u80fd\u5f97\u5230\u89e3\u3002DFS\u641c\u7d22\u7684\u6d41\u7a0b\u662f\u4e00\u4e2a\u6811\u7684\u5f62\u5f0f\uff0c\u6bcf\u6b21\u4e00\u6761\u8def\u8d70\u5230\u9ed1\u3002<br \/>\n\u6df1\u641c\u4f18\u5148\u641c\u7d22\u6700\u65e9\u662f\u4f7f\u7528\u5728\u5f00\u53d1\u722c\u866b\u65e9\u671f\u4f7f\u7528\u8f83\u591a\u7684\u65b9\u6cd5\u3002\u5b83\u7684\u76ee\u7684\u4e3b\u8981\u662f\u8fbe\u5230\u88ab\u641c\u7d22\u7ed3\u6784\u7684\u53f6\u7ed3\u70b9\uff1a\u5373\u90a3\u4e9b\u4e0d\u5305\u542b\u4efb\u4f55\u8d85\u94fe\u7684HTML\u6587\u4ef6)\u3002\u6cbf\u7740HTML\u76ee\u5f55\u5c42\u7ea7\u4e0b\u63a2\uff0c\u76f4\u5230\u6700\u540e\u4e00\u5c42\uff0c\u7136\u540e\u56de\u9000\u5230\u4e0a\u5c42\uff0c\u88ab\u8bbf\u95ee\u8fc7\u7684\u8282\u70b9\u4f1a\u88ab\u6807\u8bb0\uff0c\u7136\u540e\u67e5\u770b\u662f\u5426\u6709\u5176\u4ed6\u8282\u70b9\uff0c\u5982\u679c\u6709\u5219\u7ee7\u7eed\u4e0b\u7a81\u7136\uff0c\u76f4\u5230\u6700\u540e\u4e00\u5c42\u3002\u4e00\u6b21\u7c7b\u63a8\u76f4\u5230\u6240\u6709\u8282\u70b9\u90fd\u88ab\u67e5\u627e\u3002<\/p>\n<h3>\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u7b97\u6cd5\u601d\u60f3<\/h3>\n<p>\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u79d8\u7c4d\uff1a\u540e\u8bbf\u95ee\u7684\u8282\u70b9\uff0c\u5176\u90bb\u63a5\u70b9\u5148\u88ab\u8bbf\u95ee\u3002\u6839\u636e\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u79d8\u7c4d\uff0c\u540e\u6765\u7684\u5148\u670d\u52a1\uff0c\u8fd9\u53ef\u4ee5\u501f\u52a9\u201c\u6808\u201d\u6765\u5b9e\u73b0\u3002\u9012\u5f52\u672c\u8eab\u5c31\u662f\u4f7f\u7528\u201c\u6808\u201d\u5b9e\u73b0\u7684\uff0c\u56e0\u6b64\u4f7f\u7528\u9012\u5f52\u7684\u65b9\u6cd5\u66f4\u65b9\u4fbf\u3002<\/p>\n<p>\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u56fe\u7684\u65b9\u6cd5\u662f\uff0c\u4ece\u56fe\u4e2d\u67d0\u9876\u70b9v\u51fa\u53d1\uff1a<\/p>\n<ol>\n<li>\u8bbf\u95ee\u9876\u70b9v\uff1b<\/li>\n<li>\u4f9d\u6b21\u4ecev\u7684\u672a\u88ab\u8bbf\u95ee\u7684\u90bb\u63a5\u70b9\u51fa\u53d1\uff0c\u5bf9\u56fe\u8fdb\u884c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\uff1b\u76f4\u81f3\u56fe\u4e2d\u548cv\u6709\u8def\u5f84\u76f8\u901a\u7684\u9876\u70b9\u90fd\u88ab\u8bbf\u95ee\uff1b<\/li>\n<li>\u82e5\u6b64\u65f6\u56fe\u4e2d\u5c1a\u6709\u9876\u70b9\u672a\u88ab\u8bbf\u95ee\uff0c\u5219\u4ece\u4e00\u4e2a\u672a\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\u51fa\u53d1\uff0c\u91cd\u65b0\u8fdb\u884c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\uff0c\u76f4\u5230\u56fe\u4e2d\u6240\u6709\u9876\u70b9\u5747\u88ab\u8bbf\u95ee\u8fc7\u4e3a\u6b62\u3002<\/li>\n<\/ol>\n<h3>\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u7b97\u6cd5\u6b65\u9aa4<\/h3>\n<ul>\n<li>\u521d\u59cb\u5316\u56fe\u4e2d\u7684\u6240\u6709\u8282\u70b9\u5747\u672a\u88ab\u8bbf\u95ee\uff1b<\/li>\n<li>\u4ece\u56fe\u4e2d\u7684\u67d0\u4e2a\u8282\u70b9v\u51fa\u53d1\uff0c\u8bbf\u95eev\u5e76\u6807\u8bb0\u5176\u5df2\u7ecf\u88ab\u8bbf\u95ee\uff1b<\/li>\n<li>\u4f9d\u6b21\u68c0\u67e5v\u7684\u6240\u6709\u90bb\u63a5\u70b9w\uff0c\u5982\u679cw\u672a\u88ab\u8bbf\u95ee\uff0c\u5219\u4ecew\u51fa\u53d1\u8fdb\u884c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\uff08\u9012\u5f52\u8c03\u7528\uff0c\u91cd\u590d\u6b65\u9aa42\u548c\u6b65\u9aa43\uff09<\/li>\n<\/ul>\n<h3>\u7b97\u6cd5\u5b9e\u73b0<\/h3>\n<h4>\u56fe\u7684DFS\u904d\u5386<\/h4>\n<pre><code class=\"language-python\">def dfs(graph,start,visited):\n    # \u8bbf\u95ee\u5f53\u524d\u8282\u70b9\n    pirnt(start,end=&#039;&#039;)\n    # \u6807\u8bb0\u5f53\u524d\u8282\u70b9\u4e3a\u5df2\u8bbf\u95ee\n    visited[start]=True\n    # \u904d\u5386\u5f53\u524d\u8282\u70b9\u7684\u90bb\u5c45\u8282\u70b9\n    for neighbor in graph[start]:\n        # \u5982\u679c\u90bb\u5c45\u8282\u70b9\u672a\u88ab\u8bbf\u95ee\uff0c\u5219\u7ee7\u7eed\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\n        if not visited[neighbor]:\n        dfs(graph,neighbor,visited)\n\ngraph = {\n    &#039;A&#039;: [&#039;B&#039;, &#039;C&#039;],\n    &#039;B&#039;: [&#039;A&#039;, &#039;D&#039;, &#039;E&#039;],\n    &#039;C&#039;: [&#039;A&#039;, &#039;F&#039;, &#039;G&#039;],\n    &#039;D&#039;: [&#039;B&#039;],\n    &#039;E&#039;: [&#039;B&#039;],\n    &#039;F&#039;: [&#039;C&#039;],\n    &#039;G&#039;: [&#039;C&#039;]\n}\n\n# \u6807\u8bb0\u8282\u70b9\u662f\u5426\u5df2\u8bbf\u95ee\u7684\u5217\u8868\nvisited = {node: False for node in graph}\n\n# \u4ece\u8282\u70b9A\u5f00\u59cb\u8fdb\u884cDFS\u904d\u5386\nprint(&quot;DFS\u904d\u5386\u7ed3\u679c\uff1a&quot;)\ndfs(graph, &#039;A&#039;, visited)<\/code><\/pre>\n<h3>\u4e8c\u53c9\u6811\u7684DFS\u904d\u5386<\/h3>\n<pre><code class=\"language-python\"># \u4e8c\u53c9\u6811\u8282\u70b9\u5b9a\u4e49\nclass TreeNode:\n    def __init__(self, val):\n        self.val = val\n        self.left = None\n        self.right = None\n\n# \u4e8c\u53c9\u6811\u7684DFS\u904d\u5386\ndef dfs_binary_tree(root):\n    if root is None:\n        return\n    print(root.val, end=&#039; &#039;)\n    dfs_binary_tree(root.left)\n    dfs_binary_tree(root.right)\n\n# \u6784\u9020\u4e8c\u53c9\u6811\nroot = TreeNode(1)\nroot.left = TreeNode(2)\nroot.right = TreeNode(3)\nroot.left.left = TreeNode(4)\nroot.left.right = TreeNode(5)\n\n# \u4e8c\u53c9\u6811\u7684DFS\u904d\u5386\nprint(&quot;\u4e8c\u53c9\u6811\u7684DFS\u904d\u5386\u7ed3\u679c\uff1a&quot;)\ndfs_binary_tree(root)<\/code><\/pre>\n<h3>Leecode.2062 \u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u5143\u97f3\u5b50\u5b57\u7b26\u4e32<\/h3>\n<p>\u9898\u76ee\u94fe\u63a5\uff1a<a href=\"https:\/\/leetcode.cn\/problems\/count-vowel-substrings-of-a-string\/description\/\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/leetcode.cn\/problems\/count-vowel-substrings-of-a-string\/description\/<\/a><\/p>\n<p>\u5b50\u5b57\u7b26\u4e32 \u662f\u5b57\u7b26\u4e32\u4e2d\u7684\u4e00\u4e2a\u8fde\u7eed\uff08\u975e\u7a7a\uff09\u7684\u5b57\u7b26\u5e8f\u5217\u3002<br \/>\n\u5143\u97f3\u5b50\u5b57\u7b26\u4e32 \u662f \u4ec5 \u7531\u5143\u97f3\uff08'a'\u3001'e'\u3001'i'\u3001'o' \u548c 'u'\uff09\u7ec4\u6210\u7684\u4e00\u4e2a\u5b50\u5b57\u7b26\u4e32\uff0c\u4e14\u5fc5\u987b\u5305\u542b \u5168\u90e8\u4e94\u79cd \u5143\u97f3\u3002<br \/>\n\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32 word \uff0c\u7edf\u8ba1\u5e76\u8fd4\u56de word \u4e2d \u5143\u97f3\u5b50\u5b57\u7b26\u4e32\u7684\u6570\u76ee \u3002<\/p>\n<pre><code class=\"language-python\">class Solution:\n    def countVowelSubstrings(self, word: str) -&gt; int:\n        cnt=0\n        for i in range(len(word)-4):\n            for j in range(i,len(word)+1):\n                if set(word[i:j])==set(&#039;aeiou&#039;):\n                    cnt+=1\n        return cnt<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7b97\u6cd5\u6982\u8ff0 \u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff08Depth-First Search\uff0cDFS\uff09\u662f\u5341\u5206\u5e38\u89c1\u7684\u56fe\u641c\u7d22\u65b9\u6cd5\u4e4b\u4e00\u3002\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u4f1a\u6cbf\u7740\u4e00\u6761\u8def\u5f84\u4e00\u76f4\u641c &#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":[1],"tags":[],"class_list":["post-484","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts\/484","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=484"}],"version-history":[{"count":1,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts\/484\/revisions"}],"predecessor-version":[{"id":486,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts\/484\/revisions\/486"}],"wp:attachment":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/media?parent=484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/categories?post=484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/tags?post=484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}