networkx图论Depth First Search广度优先搜索遍历DFS,基于栈,Python

Posted zhangphil

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了networkx图论Depth First Search广度优先搜索遍历DFS,基于栈,Python相关的知识,希望对你有一定的参考价值。

(1)深度优先搜索遍历,通常使用栈来保存遍历搜索过的节点记录。当搜索到的节点没有子节点,意味着达到了尽头,开始回退。回退的过程其实就是把栈顶的节点弹出(删掉),然后再次在栈中读第一个元素(本例是列表实现的栈,即为0号元素)。

(2)每个节点用一个标志位标记当前节点是否已经访问过,如果访问过,压入栈顶。

(3)每次迭代独取当前顶点V时候,同时要把顶点V的子节点压入栈。

import networkx as nx
import matplotlib.pyplot as plt

# 记录搜索路径
search_path = []


def my_graph():
    # G = nx.gnm_random_graph(n=6, m=8)
    # G = nx.balanced_tree(r=3, h=2)
    G = nx.random_tree(20)

    pos = nx.spring_layout(G)
    nx.draw_networkx(G, pos,
                     node_color='green',
                     node_size=300,
                     font_size=10,
                     font_color='white',
                     edge_color='gray',
                     width=1,
                     with_labels=True)

    print('G.nodes(data=True)', G.nodes(data=True))

    dfs(G)

    plt.show()


# 基于栈实深度优先遍历搜索
def dfs(G):
    for n in G.nodes():
        G.nodes[n]['visited'] = False
    print(G.nodes(data=True))
    print(G.edges(data=True))

    V = 0

    stack = []  # 用列表当作一个栈,只在栈顶操作(数组的第1个位置)
    stack.append(V)

    while True:
        if len(stack) == 0:
            break

        print('-----')
        print('stack-', stack)

        visit_node = stack[0]
        G.nodes[visit_node]['visited'] = True
        print('访问', visit_node)

        neighbors = nx.neighbors(G, visit_node)

        count = 0
        for node in neighbors:
            visited = G.nodes[node]['visited']
            if (visited) or (node in stack) or (node in search_path):
                continue

            stack.insert(0, node)
            count = count + 1

        print('nodes', G.nodes(data=True))

        if count == 0:
            print(visit_node, '走到尽头')
            del (stack[0])
            print('弹出', visit_node)
            search_path.append(visit_node)

        print('stack--', stack)

    list.reverse(search_path)
    print('search_path', search_path)

    print('\\n==对比深度搜索遍历结果==')
    print('networkx dfs',list(nx.dfs_tree(G,V)))


if __name__ == '__main__':
    my_graph()

生成图:

日志输出:

G.nodes(data=True) [(0, ), (1, ), (2, ), (3, ), (4, ), (5, ), (6, ), (7, ), (8, ), (9, ), (10, ), (11, ), (12, ), (13, ), (14, ), (15, ), (16, ), (17, ), (18, ), (19, )]
[(0, 'visited': False), (1, 'visited': False), (2, 'visited': False), (3, 'visited': False), (4, 'visited': False), (5, 'visited': False), (6, 'visited': False), (7, 'visited': False), (8, 'visited': False), (9, 'visited': False), (10, 'visited': False), (11, 'visited': False), (12, 'visited': False), (13, 'visited': False), (14, 'visited': False), (15, 'visited': False), (16, 'visited': False), (17, 'visited': False), (18, 'visited': False), (19, 'visited': False)]
[(0, 7, ), (1, 4, ), (1, 10, ), (1, 5, ), (1, 19, ), (2, 8, ), (2, 17, ), (3, 7, ), (5, 17, ), (6, 16, ), (7, 8, ), (8, 15, ), (9, 18, ), (9, 14, ), (11, 15, ), (12, 16, ), (13, 18, ), (14, 16, ), (14, 17, )]
-----
stack- [0]
访问 0
nodes [(0, 'visited': True), (1, 'visited': False), (2, 'visited': False), (3, 'visited': False), (4, 'visited': False), (5, 'visited': False), (6, 'visited': False), (7, 'visited': False), (8, 'visited': False), (9, 'visited': False), (10, 'visited': False), (11, 'visited': False), (12, 'visited': False), (13, 'visited': False), (14, 'visited': False), (15, 'visited': False), (16, 'visited': False), (17, 'visited': False), (18, 'visited': False), (19, 'visited': False)]
stack-- [7, 0]
-----
stack- [7, 0]
访问 7
nodes [(0, 'visited': True), (1, 'visited': False), (2, 'visited': False), (3, 'visited': False), (4, 'visited': False), (5, 'visited': False), (6, 'visited': False), (7, 'visited': True), (8, 'visited': False), (9, 'visited': False), (10, 'visited': False), (11, 'visited': False), (12, 'visited': False), (13, 'visited': False), (14, 'visited': False), (15, 'visited': False), (16, 'visited': False), (17, 'visited': False), (18, 'visited': False), (19, 'visited': False)]
stack-- [8, 3, 7, 0]
-----
stack- [8, 3, 7, 0]
访问 8
nodes [(0, 'visited': True), (1, 'visited': False), (2, 'visited': False), (3, 'visited': False), (4, 'visited': False), (5, 'visited': False), (6, 'visited': False), (7, 'visited': True), (8, 'visited': True), (9, 'visited': False), (10, 'visited': False), (11, 'visited': False), (12, 'visited': False), (13, 'visited': False), (14, 'visited': False), (15, 'visited': False), (16, 'visited': False), (17, 'visited': False), (18, 'visited': False), (19, 'visited': False)]
stack-- [2, 15, 8, 3, 7, 0]
-----
stack- [2, 15, 8, 3, 7, 0]
访问 2
nodes [(0, 'visited': True), (1, 'visited': False), (2, 'visited': True), (3, 'visited': False), (4, 'visited': False), (5, 'visited': False), (6, 'visited': False), (7, 'visited': True), (8, 'visited': True), (9, 'visited': False), (10, 'visited': False), (11, 'visited': False), (12, 'visited': False), (13, 'visited': False), (14, 'visited': False), (15, 'visited': False), (16, 'visited': False), (17, 'visited': False), (18, 'visited': False), (19, 'visited': False)]
stack-- [17, 2, 15, 8, 3, 7, 0]
-----
stack- [17, 2, 15, 8, 3, 7, 0]
访问 17
nodes [(0, 'visited': True), (1, 'visited': False), (2, 'visited': True), (3, 'visited': False), (4, 'visited': False), (5, 'visited': False), (6, 'visited': False), (7, 'visited': True), (8, 'visited': True), (9, 'visited': False), (10, 'visited': False), (11, 'visited': False), (12, 'visited': False), (13, 'visited': False), (14, 'visited': False), (15, 'visited': False), (16, 'visited': False), (17, 'visited': True), (18, 'visited': False), (19, 'visited': False)]
stack-- [5, 14, 17, 2, 15, 8, 3, 7, 0]
-----
stack- [5, 14, 17, 2, 15, 8, 3, 7, 0]
访问 5
nodes [(0, 'visited': True), (1, 'visited': False), (2, 'visited': True), (3, 'visited': False), (4, 'visited': False), (5, 'visited': True), (6, 'visited': False), (7, 'visited': True), (8, 'visited': True), (9, 'visited': False), (10, 'visited': False), (11, 'visited': False), (12, 'visited': False), (13, 'visited': False), (14, 'visited': False), (15, 'visited': False), (16, 'visited': False), (17, 'visited': True), (18, 'visited': False), (19, 'visited': False)]
stack-- [1, 5, 14, 17, 2, 15, 8, 3, 7, 0]
-----
stack- [1, 5, 14, 17, 2, 15, 8, 3, 7, 0]
访问 1
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': False), (5, 'visited': True), (6, 'visited': False), (7, 'visited': True), (8, 'visited': True), (9, 'visited': False), (10, 'visited': False), (11, 'visited': False), (12, 'visited': False), (13, 'visited': False), (14, 'visited': False), (15, 'visited': False), (16, 'visited': False), (17, 'visited': True), (18, 'visited': False), (19, 'visited': False)]
stack-- [19, 10, 4, 1, 5, 14, 17, 2, 15, 8, 3, 7, 0]
-----
stack- [19, 10, 4, 1, 5, 14, 17, 2, 15, 8, 3, 7, 0]
访问 19
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': False), (5, 'visited': True), (6, 'visited': False), (7, 'visited': True), (8, 'visited': True), (9, 'visited': False), (10, 'visited': False), (11, 'visited': False), (12, 'visited': False), (13, 'visited': False), (14, 'visited': False), (15, 'visited': False), (16, 'visited': False), (17, 'visited': True), (18, 'visited': False), (19, 'visited': True)]
19 走到尽头
弹出 19
stack-- [10, 4, 1, 5, 14, 17, 2, 15, 8, 3, 7, 0]
-----
stack- [10, 4, 1, 5, 14, 17, 2, 15, 8, 3, 7, 0]
访问 10
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': False), (5, 'visited': True), (6, 'visited': False), (7, 'visited': True), (8, 'visited': True), (9, 'visited': False), (10, 'visited': True), (11, 'visited': False), (12, 'visited': False), (13, 'visited': False), (14, 'visited': False), (15, 'visited': False), (16, 'visited': False), (17, 'visited': True), (18, 'visited': False), (19, 'visited': True)]
10 走到尽头
弹出 10
stack-- [4, 1, 5, 14, 17, 2, 15, 8, 3, 7, 0]
-----
stack- [4, 1, 5, 14, 17, 2, 15, 8, 3, 7, 0]
访问 4
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': True), (5, 'visited': True), (6, 'visited': False), (7, 'visited': True), (8, 'visited': True), (9, 'visited': False), (10, 'visited': True), (11, 'visited': False), (12, 'visited': False), (13, 'visited': False), (14, 'visited': False), (15, 'visited': False), (16, 'visited': False), (17, 'visited': True), (18, 'visited': False), (19, 'visited': True)]
4 走到尽头
弹出 4
stack-- [1, 5, 14, 17, 2, 15, 8, 3, 7, 0]
-----
stack- [1, 5, 14, 17, 2, 15, 8, 3, 7, 0]
访问 1
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': True), (5, 'visited': True), (6, 'visited': False), (7, 'visited': True), (8, 'visited': True), (9, 'visited': False), (10, 'visited': True), (11, 'visited': False), (12, 'visited': False), (13, 'visited': False), (14, 'visited': False), (15, 'visited': False), (16, 'visited': False), (17, 'visited': True), (18, 'visited': False), (19, 'visited': True)]
1 走到尽头
弹出 1
stack-- [5, 14, 17, 2, 15, 8, 3, 7, 0]
-----
stack- [5, 14, 17, 2, 15, 8, 3, 7, 0]
访问 5
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': True), (5, 'visited': True), (6, 'visited': False), (7, 'visited': True), (8, 'visited': True), (9, 'visited': False), (10, 'visited': True), (11, 'visited': False), (12, 'visited': False), (13, 'visited': False), (14, 'visited': False), (15, 'visited': False), (16, 'visited': False), (17, 'visited': True), (18, 'visited': False), (19, 'visited': True)]
5 走到尽头
弹出 5
stack-- [14, 17, 2, 15, 8, 3, 7, 0]
-----
stack- [14, 17, 2, 15, 8, 3, 7, 0]
访问 14
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': True), (5, 'visited': True), (6, 'visited': False), (7, 'visited': True), (8, 'visited': True), (9, 'visited': False), (10, 'visited': True), (11, 'visited': False), (12, 'visited': False), (13, 'visited': False), (14, 'visited': True), (15, 'visited': False), (16, 'visited': False), (17, 'visited': True), (18, 'visited': False), (19, 'visited': True)]
stack-- [9, 16, 14, 17, 2, 15, 8, 3, 7, 0]
-----
stack- [9, 16, 14, 17, 2, 15, 8, 3, 7, 0]
访问 9
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': True), (5, 'visited': True), (6, 'visited': False), (7, 'visited': True), (8, 'visited': True), (9, 'visited': True), (10, 'visited': True), (11, 'visited': False), (12, 'visited': False), (13, 'visited': False), (14, 'visited': True), (15, 'visited': False), (16, 'visited': False), (17, 'visited': True), (18, 'visited': False), (19, 'visited': True)]
stack-- [18, 9, 16, 14, 17, 2, 15, 8, 3, 7, 0]
-----
stack- [18, 9, 16, 14, 17, 2, 15, 8, 3, 7, 0]
访问 18
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': True), (5, 'visited': True), (6, 'visited': False), (7, 'visited': True), (8, 'visited': True), (9, 'visited': True), (10, 'visited': True), (11, 'visited': False), (12, 'visited': False), (13, 'visited': False), (14, 'visited': True), (15, 'visited': False), (16, 'visited': False), (17, 'visited': True), (18, 'visited': True), (19, 'visited': True)]
stack-- [13, 18, 9, 16, 14, 17, 2, 15, 8, 3, 7, 0]
-----
stack- [13, 18, 9, 16, 14, 17, 2, 15, 8, 3, 7, 0]
访问 13
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': True), (5, 'visited': True), (6, 'visited': False), (7, 'visited': True), (8, 'visited': True), (9, 'visited': True), (10, 'visited': True), (11, 'visited': False), (12, 'visited': False), (13, 'visited': True), (14, 'visited': True), (15, 'visited': False), (16, 'visited': False), (17, 'visited': True), (18, 'visited': True), (19, 'visited': True)]
13 走到尽头
弹出 13
stack-- [18, 9, 16, 14, 17, 2, 15, 8, 3, 7, 0]
-----
stack- [18, 9, 16, 14, 17, 2, 15, 8, 3, 7, 0]
访问 18
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': True), (5, 'visited': True), (6, 'visited': False), (7, 'visited': True), (8, 'visited': True), (9, 'visited': True), (10, 'visited': True), (11, 'visited': False), (12, 'visited': False), (13, 'visited': True), (14, 'visited': True), (15, 'visited': False), (16, 'visited': False), (17, 'visited': True), (18, 'visited': True), (19, 'visited': True)]
18 走到尽头
弹出 18
stack-- [9, 16, 14, 17, 2, 15, 8, 3, 7, 0]
-----
stack- [9, 16, 14, 17, 2, 15, 8, 3, 7, 0]
访问 9
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': True), (5, 'visited': True), (6, 'visited': False), (7, 'visited': True), (8, 'visited': True), (9, 'visited': True), (10, 'visited': True), (11, 'visited': False), (12, 'visited': False), (13, 'visited': True), (14, 'visited': True), (15, 'visited': False), (16, 'visited': False), (17, 'visited': True), (18, 'visited': True), (19, 'visited': True)]
9 走到尽头
弹出 9
stack-- [16, 14, 17, 2, 15, 8, 3, 7, 0]
-----
stack- [16, 14, 17, 2, 15, 8, 3, 7, 0]
访问 16
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': True), (5, 'visited': True), (6, 'visited': False), (7, 'visited': True), (8, 'visited': True), (9, 'visited': True), (10, 'visited': True), (11, 'visited': False), (12, 'visited': False), (13, 'visited': True), (14, 'visited': True), (15, 'visited': False), (16, 'visited': True), (17, 'visited': True), (18, 'visited': True), (19, 'visited': True)]
stack-- [12, 6, 16, 14, 17, 2, 15, 8, 3, 7, 0]
-----
stack- [12, 6, 16, 14, 17, 2, 15, 8, 3, 7, 0]
访问 12
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': True), (5, 'visited': True), (6, 'visited': False), (7, 'visited': True), (8, 'visited': True), (9, 'visited': True), (10, 'visited': True), (11, 'visited': False), (12, 'visited': True), (13, 'visited': True), (14, 'visited': True), (15, 'visited': False), (16, 'visited': True), (17, 'visited': True), (18, 'visited': True), (19, 'visited': True)]
12 走到尽头
弹出 12
stack-- [6, 16, 14, 17, 2, 15, 8, 3, 7, 0]
-----
stack- [6, 16, 14, 17, 2, 15, 8, 3, 7, 0]
访问 6
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': True), (5, 'visited': True), (6, 'visited': True), (7, 'visited': True), (8, 'visited': True), (9, 'visited': True), (10, 'visited': True), (11, 'visited': False), (12, 'visited': True), (13, 'visited': True), (14, 'visited': True), (15, 'visited': False), (16, 'visited': True), (17, 'visited': True), (18, 'visited': True), (19, 'visited': True)]
6 走到尽头
弹出 6
stack-- [16, 14, 17, 2, 15, 8, 3, 7, 0]
-----
stack- [16, 14, 17, 2, 15, 8, 3, 7, 0]
访问 16
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': True), (5, 'visited': True), (6, 'visited': True), (7, 'visited': True), (8, 'visited': True), (9, 'visited': True), (10, 'visited': True), (11, 'visited': False), (12, 'visited': True), (13, 'visited': True), (14, 'visited': True), (15, 'visited': False), (16, 'visited': True), (17, 'visited': True), (18, 'visited': True), (19, 'visited': True)]
16 走到尽头
弹出 16
stack-- [14, 17, 2, 15, 8, 3, 7, 0]
-----
stack- [14, 17, 2, 15, 8, 3, 7, 0]
访问 14
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': True), (5, 'visited': True), (6, 'visited': True), (7, 'visited': True), (8, 'visited': True), (9, 'visited': True), (10, 'visited': True), (11, 'visited': False), (12, 'visited': True), (13, 'visited': True), (14, 'visited': True), (15, 'visited': False), (16, 'visited': True), (17, 'visited': True), (18, 'visited': True), (19, 'visited': True)]
14 走到尽头
弹出 14
stack-- [17, 2, 15, 8, 3, 7, 0]
-----
stack- [17, 2, 15, 8, 3, 7, 0]
访问 17
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': True), (5, 'visited': True), (6, 'visited': True), (7, 'visited': True), (8, 'visited': True), (9, 'visited': True), (10, 'visited': True), (11, 'visited': False), (12, 'visited': True), (13, 'visited': True), (14, 'visited': True), (15, 'visited': False), (16, 'visited': True), (17, 'visited': True), (18, 'visited': True), (19, 'visited': True)]
17 走到尽头
弹出 17
stack-- [2, 15, 8, 3, 7, 0]
-----
stack- [2, 15, 8, 3, 7, 0]
访问 2
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': True), (5, 'visited': True), (6, 'visited': True), (7, 'visited': True), (8, 'visited': True), (9, 'visited': True), (10, 'visited': True), (11, 'visited': False), (12, 'visited': True), (13, 'visited': True), (14, 'visited': True), (15, 'visited': False), (16, 'visited': True), (17, 'visited': True), (18, 'visited': True), (19, 'visited': True)]
2 走到尽头
弹出 2
stack-- [15, 8, 3, 7, 0]
-----
stack- [15, 8, 3, 7, 0]
访问 15
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': True), (5, 'visited': True), (6, 'visited': True), (7, 'visited': True), (8, 'visited': True), (9, 'visited': True), (10, 'visited': True), (11, 'visited': False), (12, 'visited': True), (13, 'visited': True), (14, 'visited': True), (15, 'visited': True), (16, 'visited': True), (17, 'visited': True), (18, 'visited': True), (19, 'visited': True)]
stack-- [11, 15, 8, 3, 7, 0]
-----
stack- [11, 15, 8, 3, 7, 0]
访问 11
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': True), (5, 'visited': True), (6, 'visited': True), (7, 'visited': True), (8, 'visited': True), (9, 'visited': True), (10, 'visited': True), (11, 'visited': True), (12, 'visited': True), (13, 'visited': True), (14, 'visited': True), (15, 'visited': True), (16, 'visited': True), (17, 'visited': True), (18, 'visited': True), (19, 'visited': True)]
11 走到尽头
弹出 11
stack-- [15, 8, 3, 7, 0]
-----
stack- [15, 8, 3, 7, 0]
访问 15
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': True), (5, 'visited': True), (6, 'visited': True), (7, 'visited': True), (8, 'visited': True), (9, 'visited': True), (10, 'visited': True), (11, 'visited': True), (12, 'visited': True), (13, 'visited': True), (14, 'visited': True), (15, 'visited': True), (16, 'visited': True), (17, 'visited': True), (18, 'visited': True), (19, 'visited': True)]
15 走到尽头
弹出 15
stack-- [8, 3, 7, 0]
-----
stack- [8, 3, 7, 0]
访问 8
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': False), (4, 'visited': True), (5, 'visited': True), (6, 'visited': True), (7, 'visited': True), (8, 'visited': True), (9, 'visited': True), (10, 'visited': True), (11, 'visited': True), (12, 'visited': True), (13, 'visited': True), (14, 'visited': True), (15, 'visited': True), (16, 'visited': True), (17, 'visited': True), (18, 'visited': True), (19, 'visited': True)]
8 走到尽头
弹出 8
stack-- [3, 7, 0]
-----
stack- [3, 7, 0]
访问 3
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': True), (4, 'visited': True), (5, 'visited': True), (6, 'visited': True), (7, 'visited': True), (8, 'visited': True), (9, 'visited': True), (10, 'visited': True), (11, 'visited': True), (12, 'visited': True), (13, 'visited': True), (14, 'visited': True), (15, 'visited': True), (16, 'visited': True), (17, 'visited': True), (18, 'visited': True), (19, 'visited': True)]
3 走到尽头
弹出 3
stack-- [7, 0]
-----
stack- [7, 0]
访问 7
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': True), (4, 'visited': True), (5, 'visited': True), (6, 'visited': True), (7, 'visited': True), (8, 'visited': True), (9, 'visited': True), (10, 'visited': True), (11, 'visited': True), (12, 'visited': True), (13, 'visited': True), (14, 'visited': True), (15, 'visited': True), (16, 'visited': True), (17, 'visited': True), (18, 'visited': True), (19, 'visited': True)]
7 走到尽头
弹出 7
stack-- [0]
-----
stack- [0]
访问 0
nodes [(0, 'visited': True), (1, 'visited': True), (2, 'visited': True), (3, 'visited': True), (4, 'visited': True), (5, 'visited': True), (6, 'visited': True), (7, 'visited': True), (8, 'visited': True), (9, 'visited': True), (10, 'visited': True), (11, 'visited': True), (12, 'visited': True), (13, 'visited': True), (14, 'visited': True), (15, 'visited': True), (16, 'visited': True), (17, 'visited': True), (18, 'visited': True), (19, 'visited': True)]
0 走到尽头
弹出 0
stack-- []
search_path [0, 7, 3, 8, 15, 11, 2, 17, 14, 16, 6, 12, 9, 18, 13, 5, 1, 4, 10, 19]

==对比深度搜索遍历结果==
networkx dfs [0, 7, 3, 8, 15, 11, 2, 17, 14, 16, 6, 12, 9, 18, 13, 5, 1, 4, 10, 19]

以上是关于networkx图论Depth First Search广度优先搜索遍历DFS,基于栈,Python的主要内容,如果未能解决你的问题,请参考以下文章

图论DFS(Depth First Search)Algorithm深度优先搜索遍历空间平面图选择路径,networkx,Python

networkx图论Depth First Search广度优先搜索遍历DFS,基于栈,Python

networkx图论Breadth First Search广度优先搜索遍历BFS,基于队列,Python

图论BFS(Breath First Search)Algorithm广度优先搜索遍历空间平面网格图路径选择,networkx,Python

NetworkX是一个图论与复杂网络建模工具

networkx图论最短路径Dijkstra Algorithm,Python