首页 > 解决方案 > 有向图可以有两个 DFS 遍历吗?

问题描述

这是我的输入图

这是我的 DFS 遍历算法(递归方法):

def dfs(v,visited) :
    for i in Graph[v] :
        if i not in visited :
            visited.append(i)
            print(i)
            dfs(i,visited)

n = int(input())
Graph = {}
    for i in range(n) :
    name= input(print("Enter ",i+1," vertex name"))

    list_of_ver = list(map(int,input(print("Enter the vertices connected to ",name," vertex")).split()))

    Graph.update({int(name) : list_of_ver })
visited=[0]
print(Graph)
print("0")
dfs(0,visited)

上图是我的输入图。我的算法给了我不同的 DFS 遍历顺序:0 1 2 3 4 5
是否正确?

标签: pythonalgorithmgraph-theorydepth-first-search

解决方案


推荐阅读