python - 有向图可以有两个 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
是否正确?
解决方案
推荐阅读
- angular - 将两次 API 调用转换为一次,并在两个组件中使用其数据
- r - 在没有渲染功能的 R Shiny 中显示 Plotly Plot
- sql - 雪花我们如何遍历临时表的每一行并将其值插入到另一个表中,其中每个字段的值都是单行?
- unit-testing - Azure Pipeline:如何保存 Visual Studio“测试结果”以用于管道中的其他任务?
- python - 从嵌套的 json url 获取字段
- javascript - 在角度选择下拉列表中获取名称和 ID 作为值
- python - 通过使用 Python 读取数字 txt 文件进行快速排序
- reactjs - 反应js中未定义handleChange
- javascript - 包括在 Angular 中创建内联数组的检查 component.html 无法编译
- bigcommerce - 我在哪里可以找到 Bigcommerce 中的订阅者?