{"id":1076,"date":"2023-03-24T09:47:07","date_gmt":"2023-03-24T01:47:07","guid":{"rendered":""},"modified":"2023-03-24T09:47:07","modified_gmt":"2023-03-24T01:47:07","slug":"Python \u56fe\u7b97\u6cd5","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1076.html","title":{"rendered":"Python \u56fe\u7b97\u6cd5"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Python \u56fe\u7b97\u6cd5<\/h1>\n

Python \u56fe\u7b97\u6cd5\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n

\n \u5728\u89e3\u51b3\u8bb8\u591a\u91cd\u8981\u7684\u6570\u5b66\u96be\u9898\u65f6\uff0c\u56fe\u5f62\u662f\u975e\u5e38\u6709\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002\u4f8b\u5982\u8ba1\u7b97\u673a\u7f51\u7edc\u62d3\u6251\u6216\u5206\u6790\u5316\u5408\u7269\u7684\u5206\u5b50\u7ed3\u6784\u3002\u5b83\u4eec\u8fd8\u7528\u4e8e\u57ce\u5e02\u4ea4\u901a\u6216\u8def\u7ebf\u89c4\u5212\uff0c\u751a\u81f3\u8fd8\u7528\u4e8e\u4eba\u7c7b\u8bed\u8a00\u53ca\u5176\u8bed\u6cd5\u3002\u6240\u6709\u8fd9\u4e9b\u5e94\u7528\u7a0b\u5e8f\u90fd\u6709\u4e00\u4e2a\u5171\u540c\u7684\u6311\u6218\uff0c\u5373\u4f7f\u7528\u5b83\u4eec\u7684\u8fb9\u7f18\u904d\u5386\u56fe\u5e76\u786e\u4fdd\u8bbf\u95ee\u56fe\u7684\u6240\u6709\u8282\u70b9\u3002\u6709\u4e24\u79cd\u5e38\u89c1\u7684\u5efa\u7acb\u65b9\u6cd5\u53ef\u4ee5\u8fdb\u884c\u904d\u5386\uff0c\u4e0b\u9762\u5c06\u5bf9\u6b64\u8fdb\u884c\u4ecb\u7ecd\u3002\n <\/div>\n

\u6df1\u5ea6\u4f18\u5148\u904d\u5386\uff1a<\/h2>\n
\n \u4e5f\u79f0\u4e3a\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff08DFS\uff09\uff0c\u5f53\u4efb\u4f55\u8fed\u4ee3\u4e2d\u51fa\u73b0\u6b7b\u89d2\u65f6\uff0c\u8be5\u7b97\u6cd5\u90fd\u4f1a\u5728\u6df1\u5ea6\u75c5\u623f\u8fd0\u52a8\u4e2d\u904d\u5386\u56fe\u5f62\uff0c\u5e76\u4f7f\u7528\u5806\u6808\u8bb0\u4f4f\u4e0b\u4e00\u4e2a\u9876\u70b9\u4ee5\u5f00\u59cb\u641c\u7d22\u3002\u6211\u4eec\u4f7f\u7528\u8bbe\u7f6e\u7684\u6570\u636e\u7c7b\u578b\u4e3apython\u4e2d\u7684\u56fe\u5f62\u5b9e\u73b0DFS\uff0c\u56e0\u4e3a\u5b83\u4eec\u63d0\u4f9b\u4e86\u5fc5\u9700\u7684\u529f\u80fd\u6765\u8ddf\u8e2a\u5df2\u8bbf\u95ee\u548c\u672a\u8bbf\u95ee\u7684\u8282\u70b9\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-15<\/span>
class <\/span>graph:
    def <\/span>__init__(self,gdict=None):
        if <\/span>gdict is <\/span>None:
            gdict = {}
        self.gdict = gdict
# Check for <\/span><\/span>the visisted and <\/span>unvisited nodes
<\/span> def <\/span>dfs(graph, start, visited = None):
    if <\/span>visited is <\/span>None:
        visited = set()
    visited.add<\/span>(start)
    print(start)
    for <\/span><\/span>next in <\/span>graph[start] - visited:
        dfs(graph, next, visited)
    return <\/span>visited
gdict = { \"a\"<\/span><\/span><\/span><\/span> : set([\"b\"<\/span><\/span>,\"c\"<\/span><\/span>]),
                \"b\" : set([\"a\", \"d\"<\/span><\/span><\/span>]),
                \"c\" : set([\"a\", \"d\"]),
                \"d\" : set([\"e\"<\/span><\/span>]),
                \"e\" : set([\"a\"])
                }
dfs(gdict, 'a'<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-15<\/span>
a b d e c
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u5e7f\u5ea6\u4f18\u5148\u904d\u5386<\/h2>\n
\n \u4e5f\u79f0\u4e3a\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff08BFS\uff09\uff0c\u8be5\u7b97\u6cd5\u904d\u5386\u56fe\u5e7f\u5ea6\u79fb\u52a8\uff0c\u5e76\u5728\u4efb\u4f55\u8fed\u4ee3\u51fa\u73b0\u6b7b\u89d2\u65f6\u4f7f\u7528\u961f\u5217\u8bb0\u4f4f\u4e0b\u4e00\u4e2a\u9876\u70b9\u6765\u5f00\u59cb\u641c\u7d22\u3002\u8bf7\u8bbf\u95ee\u6211\u4eec\u7f51\u7ad9\u4e0a\u7684\u6b64\u94fe\u63a5\u4ee5\u4e86\u89e3\u56fe\u8868\u7684BFS\u6b65\u9aa4\u7684\u8be6\u7ec6\u4fe1\u606f\u3002
\n
\u6211\u4eec\u4f7f\u7528\u524d\u9762\u8ba8\u8bba\u7684\u961f\u5217\u6570\u636e\u7ed3\u6784\u4e3apython\u4e2d\u7684\u56fe\u5f62\u5b9e\u73b0BFS\u3002\u5f53\u6211\u4eec\u7ee7\u7eed\u8bbf\u95ee\u76f8\u90bb\u7684\u672a\u8bbf\u95ee\u8282\u70b9\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u961f\u5217\u4e2d\u65f6\u3002\u7136\u540e\uff0c\u6211\u4eec\u4ec5\u5f00\u59cb\u4f7f\u6ca1\u6709\u672a\u8bbf\u95ee\u8282\u70b9\u7684\u5269\u4f59\u8282\u70b9\u51fa\u961f\u3002\u5f53\u6ca1\u6709\u4e0b\u4e00\u4e2a\u76f8\u90bb\u8282\u70b9\u8981\u8bbf\u95ee\u65f6\uff0c\u6211\u4eec\u5c06\u505c\u6b62\u7a0b\u5e8f\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-15<\/span>
import <\/span>collections
class <\/span>graph:
    def <\/span>__init__(self,gdict=None):
        if <\/span>gdict is <\/span>None:
            gdict = {}
        self.gdict = gdict
def <\/span>bfs(graph, startnode):
# Track the visited and <\/span>unvisited nodes using queue
<\/span>         seen, queue = set([startnode]), collections.deque<\/span>([startnode])
        while <\/span>queue:
            vertex = queue.popleft<\/span>()
            marked(vertex)
            for <\/span><\/span>node in <\/span>graph[vertex]:
                if <\/span>node not <\/span>in <\/span>seen:
                    seen.add<\/span>(node)
                    queue.append<\/span>(node)
def <\/span>marked(n):
    print(n)
# The graph dictionary
<\/span> gdict = { \"a\"<\/span><\/span><\/span><\/span><\/span> : set([\"b\"<\/span><\/span>,\"c\"<\/span><\/span>]),
                \"b\" : set([\"a\", \"d\"<\/span><\/span><\/span>]),
                \"c\" : set([\"a\", \"d\"]),
                \"d\" : set([\"e\"<\/span><\/span>]),
                \"e\" : set([\"a\"])
                }
bfs(gdict, \"a\")
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n <\/div>\n
\n
a c b d e 
<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python \u56fe\u7b97\u6cd5zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[],"class_list":["post-1076","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1076"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1076"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1076\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}