-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFS와 BFS.py
60 lines (49 loc) · 1.44 KB
/
DFS와 BFS.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import sys
from collections import deque
N, M, V = map(int,sys.stdin.readline().split())
num_dict = {}
for _ in range(M):
num1, num2 = map(int, sys.stdin.readline().split())
if num1 not in num_dict:
num_dict[num1] = {
'dfs_used' : False,
'bfs_used' : False,
'link' : []
}
num_dict[num1]['link'].append(num2)
if num2 not in num_dict:
num_dict[num2] = {
'dfs_used' : False,
'bfs_used' : False,
'link' : []
}
num_dict[num2]['link'].append(num1)
stack = [V]
dfs_result = []
while stack:
poped = stack.pop()
if poped not in num_dict:
dfs_result.append(poped)
continue
if not num_dict[poped]['dfs_used']:
dfs_result.append(poped)
num_dict[poped]['dfs_used'] = True
num_dict[poped]['link'].sort()
for num in num_dict[poped]['link'][::-1]:
if num_dict[num]['dfs_used'] : continue
stack.append(num)
print(*dfs_result)
q = deque([V])
bfs_result = []
while q:
polled = q.popleft()
if polled not in num_dict:
bfs_result.append(polled)
continue
if not num_dict[polled]['bfs_used']:
bfs_result.append(polled)
num_dict[polled]['bfs_used'] = True
for num in num_dict[polled]['link']:
if num_dict[num]['bfs_used']: continue
q.append(num)
print(*bfs_result)