Notice
Recent Posts
Recent Comments
Link
μΌ | μ | ν | μ | λͺ© | κΈ | ν |
---|---|---|---|---|---|---|
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 |
Tags
- νλ‘κ·Έλλ¨Έμ€
- node.js
- HTML
- λλ¦Όμ½λ©
- νλ‘ νΈμλ
- 그리λ
- ν¬λ‘€λ§
- λͺ¨κ°μ½
- μ½λ©
- λ Έλ§λμ½λ
- μ½λ©μ ν
- heapq
- error
- Til
- JS
- λ°±μ€
- Python
- KDT
- κ΅λΉμ§μ
- νλ‘μ νΈ
- μκ³ λ¦¬μ¦
- javascript
- κ°λ°
- μ½λ©ν μ€νΈ
- CSS
- fe
- react
- ν μ΄νλ‘μ νΈ
- νμ΄μ¬
- mongodb
Archives
- Today
- Total
π± → π³
Algorithm) DFS/BFS λ³Έλ¬Έ
728x90
κ·Έλν νμ μκ³ λ¦¬μ¦: DFS/BFS
νμ(search)μ΄λ λ§μ μμ λ°μ΄ν° μ€μμ μνλ λ°μ΄ν°λ₯Ό μ°Ύλ κ³Όμ
λνμ μΈ κ·Έλν νμ μκ³ λ¦¬μ¦μΌλ‘ DFS/BFSκ° μ‘΄μ¬
μλ£κ΅¬μ‘°
μ€ν(μ μ νμΆ)
λ¨Όμ λ€μ΄ μ¨ λ°μ΄ν°κ° λμ€μ λκ°λ νμμ μλ£κ΅¬μ‘°
μμ) λ°μ€ μκΈ°
DFS method μ μ
def dfs(graph, v, visited):
visited[v] = True
print(v, end=' ')
for i in graph[v]:
if not visited[i]:
dfs(graph, i, visited)
graph = [
[],
[2,3,8],
[1,7],
[1,4,5],
[3,5],
[3,4],
[7],
[2,6,8],
[1,7]
]
visited = [False] * 9
dfs(graph, 1, visited)
BFS method μ μ
κ°μ λΉμ©μ΄ λͺ¨λ κ°μ λ μ΅λ¨κ±°λ¦¬λ₯Ό νμνκΈ° μν΄ μ°μΌ μ μλ μκ³ λ¦¬μ¦
from collections import deque
# BFS λ©μλ μ μ
def bfs(graph, start, visited):
queue = deque([start])
visited[start] = True
while queue:
v = queue.popleft()
print(v, end=' ')
for i in graph[v]:
if not visited[i]:
queue.append(i)
visited[i] = True
graph = [
[],
[2,3,8],
[1,7],
[1,4,5],
[3,5],
[3,4],
[7],
[2,6,8],
[1,7]
]
visited = [False] * 9
bfs(graph, 1, visited)
728x90
'Algorithms' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Algorithm) Dynamic Programming (1) | 2022.10.25 |
---|---|
Algorithm) Binary Search(μ΄μ§νμ) (0) | 2022.10.25 |
Algorithm) Greedy (0) | 2022.10.25 |
Algorithm) μκ³ λ¦¬μ¦ μ±λ₯ (0) | 2022.10.25 |
[λ°±μ€] 18870λ²: μ’ν μμΆ python (0) | 2022.10.10 |