ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ง๐ป๐ป ์๊ณ ๋ฆฌ์ฆ/๊ทธ๋ํ
[๊ทธ๋ํ]BFS(๋๋น ์ฐ์ ํ์) ๊ตฌํ ๋ฐฉ๋ฒ์ ์์๋ณด์
10000COW 2022. 10. 23. 19:33728x90
BFS๋ ํ๋ฅผ ์ด์ฉํ ๋ฐ๋ณต ๊ตฌ์กฐ๋ก ๊ตฌํํ๋ค.
๋จผ์ ํ๋ฅผ ์ด์ฉํ BFS Pseudo code๋ฅผ ๋ณด๋ฉด์ ์ด๋ป๊ฒ ๊ตฌํํด์ผํ ์ง ์๊ฐํด๋ณด์.
1) ํ๋ฅผ ์ด์ฉํ BFS Pseudo code
BFS(G, start_v)
let Q be a queue
label start_v as discovered
Q.enqueue(start_v)
while Q is not empty do
v := Q.dequeue()
if v is the goal then
return v
for all edges from v to w in G.adjacentEdges(v) do
if w is not labeled as discovered then
label w as discovered
w.parent := v
Q.enqueue(w)
2) ํ๋ฅผ ์ด์ฉํ Python code
def iterative_bfs(start_v):
discovered = [start_v]
queue = [start_v]
while queue:
v = queue.pop(0)
for w in graph[v]:
if w not in discovered:
discovered.append(w)
queue.append(w)
return discovered
BFS๋ ์ฌ๊ท์ ์ผ๋ก ๋์ํ์ง ์๋๋ค. ๋ฐ๋ผ์ ํ๋ฅผ ์ด์ฉํ ๋ฐ๋ณต ๊ตฌํ๋ง ๊ฐ๋ฅํ๋ค.
์ฝ๋ฉ ํ ์คํธ์์ ์ฌ๊ท๋ก ์๋ชป ์ ๊ทผํ์ง ์๋๋ก ์ฃผ์๊ฐ ํ์ํ๋ค.
728x90
'๐ง๐ปโ๐ป ์๊ณ ๋ฆฌ์ฆ > ๊ทธ๋ํ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๊ณต์ง์ฌํญ
์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
- Total
- Today
- Yesterday
๋งํฌ
TAG
- React Query
- ์๋ฃ๊ตฌ์กฐ
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- github
- ๋ธ๋ผ์ฐ์
- ์ ๋ ฌ
- DB
- Component
- JavaScript
- Browser
- BOJ
- DOM
- ๊ทธ๋ํ
- zustand
- mdn
- git
- ์๊ณ ๋ฆฌ์ฆ
- ํจ์
- leetcode
- ๋ฆฌ์กํธ
- ํ์ด์ฌ
- react
- error
- CSS
- useState
- ์๋ฐ์คํฌ๋ฆฝํธ
- state
- ์๋ฌ
- Context API
- Python
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
๊ธ ๋ณด๊ดํจ