1
我正在尝试深入遍历Python中的图形,其中有11个节点。深度遍历Python中的图形
graph = {'A': ['B', 'C'],
'B': ['A', 'F'],
'C': ['D', 'E'],
'D': ['H', 'J', 'C'],
'E': ['C', 'F'],
'F': ['B', 'G'],
'G': ['F', 'H'],
'H': ['G', 'D', 'I'],
'I': ['H', 'K'],
'J': ['D', 'E', 'K'],
'K': ['I', 'J']}
current_node = []
viewed_nodes = []
for i in graph.keys():
print("I'm at the " + str(i) + " node." + " The nodes connected to " +str(i) + " are " + str(graph[i]))
print("I'm going to mark the " + str(i) + " node as visited.")
viewed_nodes.append(str(i))
这是我的代码。我试图弄清楚如何进行深度遍历,意思是在返回并沿着不同的路径前完成所有操作。
你看着图形库?他们可能已经实施了您正在尝试解决的问题。例如。 https://github.com/pmatiello/pythongraph,但可能有其他人。 – Randrian
在所有应有的尊重,这不是代码。这是一个字典和两个空列表。到目前为止,你到底尝试过什么? – SiHa
为我在graph.keys(): 打印(“我在”+ str(i)+“节点。”) print我想弄清楚如何使它线性下降,然后去回填并填写它遗漏的内容 –