forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path8.py
More file actions
28 lines (25 loc) ยท 644 Bytes
/
8.py
File metadata and controls
28 lines (25 loc) ยท 644 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
# DFS ํจ์ ์ ์
def dfs(graph, v, visited):
# ํ์ฌ ๋
ธ๋๋ฅผ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
visited[v] = True
print(v, end=' ')
# ํ์ฌ ๋
ธ๋์ ์ฐ๊ฒฐ๋ ๋ค๋ฅธ ๋
ธ๋๋ฅผ ์ฌ๊ท์ ์ผ๋ก ๋ฐฉ๋ฌธ
for i in graph[v]:
if not visited[i]:
dfs(graph, i, visited)
# ๊ฐ ๋
ธ๋๊ฐ ์ฐ๊ฒฐ๋ ์ ๋ณด๋ฅผ ๋ฆฌ์คํธ ์๋ฃํ์ผ๋ก ํํ(2์ฐจ์ ๋ฆฌ์คํธ)
graph = [
[],
[2, 3, 8],
[1, 7],
[1, 4, 5],
[3, 5],
[3, 4],
[7],
[2, 6, 8],
[1, 7]
]
# ๊ฐ ๋
ธ๋๊ฐ ๋ฐฉ๋ฌธ๋ ์ ๋ณด๋ฅผ ๋ฆฌ์คํธ ์๋ฃํ์ผ๋ก ํํ(1์ฐจ์ ๋ฆฌ์คํธ)
visited = [False] * 9
# ์ ์๋ DFS ํจ์ ํธ์ถ
dfs(graph, 1, visited)