[ํŒŒ์ด์ฌ/python] DFS์™€ BFS

2021. 4. 5. 01:09

๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ DFS์™€ ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ BFS๋Š” ๊ทธ๋ž˜ํ”„๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ฐฉ๋ฒ•์ด๋‹ค.

๊ทธ๋ž˜ํ”„๋ž€ ์ •์  node์™€ ์ •์ ์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ„์„  edge๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค.

 

๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ DFS

DFS๋Š” Depth First Search์˜ ์•ฝ์ž๋กœ, ๋ฃจํŠธ ๋…ธ๋“œ์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ์•„๋ž˜ ๋ฐฉํ–ฅ์œผ๋กœ ํƒ์ƒ‰ํ•œ๋‹ค.

์ตœ๋Œ€ํ•œ ๊นŠ์ด ๋‚ด๋ ค๊ฐ„ ๋’ค ๋”์ด์ƒ ๊ฐˆ ๊ณณ์ด ์—†์„ ๊ฒฝ์šฐ ์˜†์œผ๋กœ ์ด๋™ํ•˜์—ฌ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๊ธฐ ๋•Œ๋ฌธ์—

๋ฃจํŠธ๋…ธ๋“œ์—์„œ ๋‹ค์Œ ๋ถ„๊ธฐ๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „์— ํ•ด๋‹น ๋ถ„๊ธฐ๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ํƒ์ƒ‰ํ•œ๋‹ค. 

๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ ์œ ์šฉํ•˜๋ฉฐ, BFS๋ณด๋‹ค ์ข€ ๋” ๊ฐ„๋‹จํ•˜์ง€๋งŒ ๊ฒ€์ƒ‰์†๋„๋Š” ๋Š๋ฆฌ๋‹ค.

์ž๊ธฐ ์ž์‹ ์„ ํ˜ธ์ถœํ•˜๋Š” ์žฌ๊ท€์  ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํ˜•ํƒœ๋กœ ๊ตฌํ˜„ํ•˜๋ฉฐ ์Šคํƒ์„ ์ด์šฉํ•œ๋‹ค.

 

 

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ BFS

BFS๋Š” Breadth First Search์˜ ์•ฝ์ž๋กœ, ๋ฃจํŠธ ๋…ธ๋“œ์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ์˜ค๋ฅธ์ชฝ ๋ฐฉํ–ฅ์œผ๋กœ ํƒ์ƒ‰ํ•œ๋‹ค.

์ตœ๋Œ€ํ•œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๊ฐ„ ๋’ค ๋”์ด์ƒ ๊ฐˆ ๊ณณ์ด ์—†์„ ๊ฒฝ์šฐ ๋ฐ‘์œผ๋กœ ์ด๋™ํ•˜์—ฌ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๊ธฐ ๋•Œ๋ฌธ์—

์‹œ์ž‘ ์ •์ ์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ€๊นŒ์šด ์ •์ ์„ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๊ณ  ๋ฉ€๋ฆฌ ๋–จ์–ด์ ธ ์žˆ๋Š” ์ •์ ์„ ๋‚˜์ค‘์— ๋ฐฉ๋ฌธํ•˜๊ฒŒ ๋œ๋‹ค.

์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์„ ๊ฒฝ์šฐ ์œ ์šฉํ•˜๋ฉฐ, DFS๋ณด๋‹ค ๊ฒ€์ƒ‰์†๋„๊ฐ€ ๋น ๋ฅด์ง€๋งŒ ์ข€ ๋” ๋ณต์žกํ•˜๋‹ค.

์žฌ๊ท€์ ์œผ๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š์œผ๋ฉฐ ํ๋ฅผ ์ด์šฉํ•˜์—ฌ ์„ ์ž…์„ ์ถœ(FIFO)์˜ ๋ฐฉ์‹์œผ๋กœ ํƒ์ƒ‰ํ•œ๋‹ค. 

 

 

ํŒŒ์ด์ฌ์œผ๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ

๊ตฌํ˜„ํ•  ๋•Œ ์–ด๋–ค ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ–ˆ์—ˆ๋Š”์ง€ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜๋“œ์‹œ ๊ฒ€์‚ฌํ•ด์•ผํ•œ๋‹ค.

๊ฒ€์‚ฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋ฌดํ•œ๋ฃจํ”„์— ๋น ์งˆ ์œ„ํ—˜์ด ์žˆ๋‹ค.

DFS์™€ BFS์˜ ์ฐจ์ด๋Š” ์ธ์ ‘ํ•œ ๋…ธ๋“œ์˜ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋ฅผ ๋จผ์ € ๋ณด๋Š๋ƒ, ํ˜„์žฌ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ๋ณด๊ณ  ๋„˜์–ด๊ฐ€๋Š๋ƒ์˜ ์ฐจ์ด๋‹ค.

์ธ์ ‘ํ•œ ๋…ธ๋“œ์˜ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋ฅผ ๋จผ์ € ๋ณธ๋‹ค๋ฉด, ๊ฐ€์žฅ ์ตœ๊ทผ์— ์Œ“์ธ ์ˆœ์„œ๋Œ€๋กœ ๋ด์•ผํ•˜๋ฏ€๋กœ stack์„ ํ™œ์šฉํ•˜๊ณ 

ํ˜„์žฌ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ๋ณด๊ณ  ๋„˜์–ด๊ฐ„๋‹ค๋ฉด, ๋จผ์ € ๋“ค์–ด์˜จ ๊ฒƒ์„ ๋จผ์ € ๋ด์•ผํ•˜๋ฏ€๋กœ queue๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

graph_list = {1: [3, 4],
              2: [3, 4, 5],
              3: [1, 5],
              4: [1],
              5: [2, 6],
              6: [3, 5]}
root_node = 1

๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ์˜ graph_list์—๋Š” key๊ฐ€ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ณณ, ์ฆ‰ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋“ค์„ value๋กœ ์ €์žฅํ•ด๋‘์—ˆ๋‹ค.

 

DFS ๊ตฌํ˜„ํ•˜๊ธฐ

def DFS(graph, root):
    visited = []
    stack = [root]

    while stack:
        n = stack.pop()
        if n not in visited:
            visited.append(n)
            stack.extend(sorted(graph[n], reverse=True))
    return visited

print(DFS(graph_list, root_node))

๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ ๊ฒฝ๋กœ๋ฅผ ์ €์žฅํ•  visted์™€ ์ธ์ ‘ํ•œ ๋…ธ๋“œ ์ค‘ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋˜ ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•  stack๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

 

๋ฐฉ๋ฌธ ์˜ˆ์ •์ธ stack์— ๊ฐ’์ด ์žˆ์„ ๋™์•ˆ ๋ฐ˜๋ณตํ•œ๋‹ค.

๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ๋กœ ๋ฐฉ๋ฌธํ•  ๋…ธ๋“œ stack.pop()์„ n์— ์ €์žฅํ•œ๋‹ค.

n์„ ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†๋‹ค๋ฉด visited์— ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฐฉ๋ฌธํ–ˆ์Œ์„ ํ‘œ์‹œํ•˜๊ณ 

stack์— n๊ณผ ์ธ์ ‘ํ•œ ๋…ธ๋“œ graph[n]์„ ์ถ”๊ฐ€ํ•˜๋Š”๋ฐ,

๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ์ถ”๊ฐ€ํ•ด์•ผ pop()ํ–ˆ์„ ๋•Œ ์ฒ˜์Œ ๋…ธ๋“œ๋ถ€ํ„ฐ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

BFS ๊ตฌํ˜„ํ•˜๊ธฐ

from collections import deque

def BFS(graph, root):
    visited = []
    queue = deque([root])

    while queue:
        n = queue.popleft()
        if n not in visited:
            visited.append(n)
            queue.extend(graph[n])
    return visited
  
print(BFS(graph_list, root_node))

๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ ๊ฒฝ๋กœ๋ฅผ ์ €์žฅํ•  visted์™€ ์ธ์ ‘ํ•œ ๋…ธ๋“œ ์ค‘ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋˜ ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•  queue๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

์ด๋•Œ popleft()๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด queue๋ฅผ collections์˜ deque๋กœ ์ƒ์„ฑํ•œ๋‹ค.

 

๋ฐฉ๋ฌธ ์˜ˆ์ •์ธ queue์— ๊ฐ’์ด ์žˆ์„ ๋™์•ˆ ๋ฐ˜๋ณตํ•œ๋‹ค.

๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ๋กœ ๋ฐฉ๋ฌธํ•  ๋…ธ๋“œ queue.popleft()๋ฅผ n์— ์ €์žฅํ•œ๋‹ค.

n์„ ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†๋‹ค๋ฉด visited์— ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฐฉ๋ฌธํ–ˆ์Œ์„ ํ‘œ์‹œํ•˜๊ณ 

queue์— n๊ณผ ์ธ์ ‘ํ•œ ๋…ธ๋“œ graph[n]์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

728x90

BELATED ARTICLES

more