2 ๋ถ„ ์†Œ์š”

๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰

๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰์ด๋ž€, ํ•˜๋‚˜์˜ ์ •์ ์œผ๋กœ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ์ฐจ๋ก€๋Œ€๋กœ ๋ชจ๋“  ์ •์ ๋“ค์„ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธํ•˜๋Š” ๋™์ž‘์„ ๋งํ•œ๋‹ค.
๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰์— ์“ฐ์ด๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋งŽ์ง€๋งŒ, ๊ทธ ์ค‘ ๊ฐ€์žฅ DFS์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•œ๋‹ค.

DFS(Depth First Search) - ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰

DFS๋ž€, ๋ฃจํŠธ๋…ธ๋“œ์—์„œ ์‹œ์ž‘ํ•ด, ๋‹ค์Œ ๋ถ„๊ธฐ(branch)๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „์— ํ•ด๋‹น ๋ถ„๊ธฐ๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋งํ•œ๋‹ค.

image

  • ๋ฏธ๋กœ๋ฅผ ํƒ์ƒ‰ํ•  ๋•Œ, ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ์„ ๋•Œ๊นŒ์ง€ ๊ฐ€๋‹ค๊ฐ€ ๋” ์ด์ƒ ๊ฐˆ ์ˆ˜ ์—†๊ฒŒ ๋˜๋ฉด ๋‹ค์‹œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ฐˆ๋ฆผ๊ธธ๋กœ ๋Œ์•„์™€์„œ ๋‹ค๋ฅธ ๋ฐฉํ–ฅ์œผ๋กœ ํƒ์ƒ‰์„ ๋‹ค์‹œ ํ•˜๋Š” ๊ฒƒ๊ณผ ์œ ์‚ฌ -> ์žฌ๊ท€์™€ ๋™์ผํ•œ ํ˜•ํƒœ์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • ์ฆ‰, ๋„“๊ฒŒ ํƒ์ƒ‰ํ•˜๊ธฐ ์ „์— ๊นŠ๊ฒŒ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ์‹
  • ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ: ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ
  • BFS๋ณด๋‹ค ์กฐ๊ธˆ ๋” ๊ฐ„๋‹จํ•˜์ง€๋งŒ, ์†๋„๋Š” ๋Š๋ฆผ

DFS ํŠน์ง•

  • ์ž๊ธฐ ์ž์‹ ์„ ํ˜ธ์ถœํ•˜๋Š” ์ˆœํ™˜(์žฌ๊ท€) ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํ˜•ํƒœ๋ฅผ ๊ฐ–๋Š”๋‹ค.
  • ์ „์œ„ ์ˆœํšŒ(Pre-Order Traversals)๋ฅผ ํฌํ•จํ•œ ๋‹ค๋ฅธ ํ˜•ํƒœ์˜ ํŠธ๋ฆฌ ์ˆœํšŒ๋Š” ๋ชจ๋‘ DFS์˜ ํ•œ ์ข…๋ฅ˜
  • ์–ด๋–ค ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ–ˆ์—ˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜๋“œ์‹œ ๊ฒ€์‚ฌํ•ด์•ผํ•œ๋‹ค. ์•ˆ๊ทธ๋Ÿฌ๋ฉด ๋ฌดํ•œ๋ฃจํ”„ ์œ„ํ—˜

image

  1. a ๋…ธ๋“œ(๋ฃจํŠธ ๋…ธ๋“œ)๋ฅผ ๋ฐฉ๋ฌธ
    • ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋Š” ํ‘œ์‹œ
  2. a์™€ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋“ค์„ ์ฐจ๋ก€๋กœ ์ˆœํšŒ
    • ์ธ์ ‘ํ•œ ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฉด ์ข…๋ฃŒ
  3. a์™€ ์ด์›ƒํ•œ ๋…ธ๋“œ b๋ฅผ ๋ฐฉ๋ฌธํ–ˆ๋‹ค๋ฉด, a์™€ ์ธ์ ‘ํ•œ ๋˜ ๋‹ค๋ฅธ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ธฐ ์ „์— b์˜ ์ด์›ƒ ๋…ธ๋“œ๋“ค์„ ์ „๋ถ€ ๋ฐฉ๋ฌธํ•ด์•ผ ํ•จ
    • b๋ฅผ ๋ฃจํŠธ ๋…ธ๋“œ๋กœ DFS๋ฅผ ๋‹ค์‹œ ์‹œ์ž‘ -> b์˜ ์ด์›ƒ ๋…ธ๋“œ ๋ฐฉ๋ฌธ
  4. b์˜ ๋ถ„๊ธฐ๋ฅผ ์ „๋ถ€ ์™„๋ฒฝ ํƒ์ƒ‰ ํ–ˆ๋‹ค๋ฉด, ๋‹ค์‹œ a์— ์ธ์ ‘ํ•œ ์ด์›ƒ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ
    • b์˜ ๋ถ„๊ธฐ๋ฅผ ์ „๋ถ€ ์™„๋ฒฝ ํƒ์ƒ‰ํ•œ ๋’ค์—์•ผ a์˜ ๋‹ค๋ฅธ ์ด์›ƒ ๋…ธ๋“œ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ
    • ์•„์ง ๋ฐฉ๋ฌธ ์•ˆ๋œ ์ •์ ์ด ์—†์œผ๋ฉด ์ข…๋ฃŒ
    • ๋ฐฉ๋ฌธ ์•ˆํ•œ ์ ์ด ์žˆ์œผ๋ฉด ๊ทธ ์ ์„ ์‹œ์ž‘์œผ๋กœ ๋‹ค์‹œ DFS

DFS ๊ตฌํ˜„

DFS์˜ ๊ตฌํ˜„ ๋ฐฉ์‹์€ 2๊ฐ€์ง€๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.

  1. ์ˆœํ™˜ ํ˜ธ์ถœ ์‚ฌ์šฉ
  2. ๋ช…์‹œ์ ์ธ ์Šคํƒ ์‚ฌ์šฉ: ๋ช…์‹œ์ ์ธ ์Šคํƒ์„ ์‚ฌ์šฉํ•ด ๋ฐฉ๋ฌธํ•œ ์ •์ ๋“ค์„ ์Šคํƒ์— ์ €์žฅํ–ˆ๋‹ค๊ฐ€ ๋‹ค์‹œ ๊บผ๋‚ด์„œ ์ž‘์—…

์Šคํƒ/ํ๋ฅผ ํ™œ์šฉํ•œ DFS ๊ตฌํ˜„

  1. ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™œ์šฉํ•œ ๊ตฌํ˜„
    def dfs(graph, start_node):
     
     ## ๊ธฐ๋ณธ์€ ํ•ญ์ƒ ๋‘๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณ„๋„๋กœ ๊ด€๋ฆฌํ•ด์ฃผ๋Š” ๊ฒƒ
     need_visited, visited = list(), list()
     
     ## ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ์‹œ์ •ํ•˜๊ธฐ 
     need_visited.append(start_node)
        
     ## ๋งŒ์•ฝ ์•„์ง๋„ ๋ฐฉ๋ฌธ์ด ํ•„์š”ํ•œ ๋…ธ๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด,
     while need_visited:
     
         ## ๊ทธ ์ค‘์—์„œ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœ (์Šคํƒ ๊ตฌ์กฐ์˜ ํ™œ์šฉ)
         node = need_visited.pop()
            
         ## ๋งŒ์•ฝ ๊ทธ ๋…ธ๋“œ๊ฐ€ ๋ฐฉ๋ฌธํ•œ ๋ชฉ๋ก์— ์—†๋‹ค๋ฉด
         if node not in visited:
     
             ## ๋ฐฉ๋ฌธํ•œ ๋ชฉ๋ก์— ์ถ”๊ฐ€ํ•˜๊ธฐ 
             visited.append(node)
     
             ## ๊ทธ ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋ฅผ 
             need_visited.extend(graph[node])
                
     return visited
    
       - list์—์„œ pop์„ ํ™œ์šฉํ•˜๋ฉด ์„ฑ๋Šฅ์ด ๋–จ์–ด์ง€๋Š” ๋‹จ์   
    
  2. deque๋ฅผ ํ™œ์šฉํ•œ ๊ตฌํ˜„
    def dfs2(graph, start_node):
     ## deque ํŒจํ‚ค์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
     from collections import deque
     visited = []
     need_visited = deque()
        
     ##์‹œ์ž‘ ๋…ธ๋“œ ์„ค์ •ํ•ด์ฃผ๊ธฐ
     need_visited.append(start_node)
        
     ## ๋ฐฉ๋ฌธ์ด ํ•„์š”ํ•œ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์•„์ง ์กด์žฌํ•œ๋‹ค๋ฉด
     while need_visited:
         ## ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ์ง€์ •ํ•˜๊ณ 
         node = need_visited.pop()
     
         ##๋งŒ์•ฝ ๋ฐฉ๋ฌธํ•œ ๋ฆฌ์ŠคํŠธ์— ์—†๋‹ค๋ฉด
         if node not in visited:
     
             ## ๋ฐฉ๋ฌธ ๋ฆฌ์ŠคํŠธ์— ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€
             visited.append(node)
             ## ์ธ์ ‘ ๋…ธ๋“œ๋“ค์„ ๋ฐฉ๋ฌธ ์˜ˆ์ • ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€
             need_visited.extend(graph[node])
                    
     return visited
    
       - ์Šคํƒ/ํ๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ๋Š” collections ํŒจํ‚ค์ง€์—์„œ deque๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด list๋ณด๋‹ค ์„ฑ๋Šฅ์ด ๋›ฐ์–ด๋‚จ  
    

์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ†ตํ•œ DFS ๊ตฌํ˜„

def dfs_recursive(graph, start, visited = []):
## ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ช…๋ น์–ด / ์žฌ๊ท€๊ฐ€ ์ด๋ฃจ์–ด์ง 
    visited.append(start)
 
    for node in graph[start]:
        if node not in visited:
            dfs_recursive(graph, node, visited)
    return visited
  • visited ์ž๋ฃŒํ˜•์„ ๊ธฐ๋ณธ ํ•จ์ˆ˜ ์ธ์ž๋กœ ํฌํ•จ์‹œํ‚ค๊ณ , ๋ฐฉ๋ฌธํ•œ ๋ฆฌ์ŠคํŠธ๋“ค์„ ์žฌ๊ท€๋ฅผ ํ†ตํ•ด ๊ณ„์† visited์— ๋‹ด๋Š” ๋ฐฉ์‹

์ฐธ๊ณ ์ž๋ฃŒ

DFS ์™„๋ฒฝ ๊ตฌํ˜„ํ•˜๊ธฐ[Python]

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS)์ด๋ž€

DFS/BFS ๊ฐœ๋… ๋ฐ ๋ฌธ์ œ ์ ‘๊ทผ ๋ฐฉ๋ฒ•

ํƒœ๊ทธ: , ,

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ