๐ง๐ป๐ป ์๊ณ ๋ฆฌ์ฆ/๊ทธ๋ํ
[๊ทธ๋ํ]BFS(๋๋น ์ฐ์ ํ์) ๊ตฌํ ๋ฐฉ๋ฒ์ ์์๋ณด์
10000COW
2022. 10. 23. 19:33
728x90
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