This article describes how to use python to implement dict layout traversal. For more information, see
The code is as follows:
# _ * _ Coding: utf_8 _
Import sys
Import OS
Class Graph ():
Def _ init _ (self, V, E ):
Self. V = V
Self. E = E
Self. visited = []
Self. dict = {}
Self. fd = open ("input.txt ")
Def initGraph (self ):
Self. visited = [0 for I in range (self. V + 1)]
For I in range (self. E ):
F, t = map (int, self. fd. readline (). split ())
# F, t = map (int, sys. stdin. readline (). split ())
If self. dict. has_key (f) = False:
L = []
L. append (t)
Self. dict [f] = l
Else:
L = self. dict [f]
L. append (t)
Self. dict [f] = l
Def dfsGraph (self, src ):
Self. visited [src] = 1
Print src,
If self. dict. get (src): # self. dict [src], an exception occurs.
For u in self. dict [src]:
If self. visited [u] = 0:
Self. dfsGraph (u)
Graph = Graph (6, 10)
Graph. initGraph ()
Graph. dfsGraph (1)
Nput.txt
The code is as follows:
1 2
1 3
1 4
3 2
2 6
4 3
3 5
4 5
6 5
3 6
Output:
The code is as follows:
1 2 6 5 3 4