-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask3.py
More file actions
38 lines (28 loc) · 787 Bytes
/
Copy pathtask3.py
File metadata and controls
38 lines (28 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#task3
task3_input = open(file='input3_4.txt', mode='r')
task3_output = open(file='output3_4.txt', mode='w')
LIST1 = task3_input.readline().strip().split(" ")
vertices = int(LIST1[0])
edge = int(LIST1[1])
graph= {}
for i in range(edge):
u, v = map(int,task3_input.readline().split())
if u not in graph :
graph[u] = []
graph[u].append(v)
if v not in graph :
graph[v] = []
graph[v].append(u)
def dfs_traversal(graph, vertices, visited=None):
if visited is None:
visited = set()
visited.add(vertices)
dfs.append(vertices)
for i in graph[vertices]:
if i not in visited:
dfs_traversal(graph , i , visited)
dfs = []
dfs_traversal(graph,1)
task3_output.write(' '.join(map(str, dfs)))
task3_input.close()
task3_output.close()