ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

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
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
๊ธ€ ๋ณด๊ด€ํ•จ