[Algorithm] DFS์™€ BFS

mjยท2024๋…„ 5์›” 8์ผ
0
post-custom-banner


๐ŸŽฏ DFS (๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜)


Depth First Search
์Šคํƒ๊ณผ ์žฌ๊ท€ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„

  1. ํƒ์ƒ‰ ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ์Šคํƒ์— ์‚ฝ์ž…ํ•˜๊ณ  ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.
  2. ์Šคํƒ์˜ ์ตœ์ƒ๋‹จ ๋…ธ๋“œ์— ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ธ์ ‘ ๋…ธ๋“œ๊ฐ€ ์žˆ์œผ๋ฉด ๊ทธ ์ธ์ ‘ ๋…ธ๋“œ๋ฅผ ์Šคํƒ์— ๋„ฃ๊ณ  ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค. ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ธ์ ‘ ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฉด ์Šคํƒ์—์„œ ์ตœ์ƒ๋‹จ ๋…ธ๋“œ๋ฅผ ๊บผ๋‚ธ๋‹ค.
  3. 2๋ฒˆ์˜ ๊ณผ์ •์„ ๋” ์ด์ƒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.
# DFS ํ•จ์ˆ˜ ์ •์˜ - graph๋Š” ์ธ์ ‘๋ฆฌ์ŠคํŠธ 
def dfs(graph, v, visited):

    # ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
    visited[v] = True
    
    print(v, end=' ')
    
    # ํ˜„์žฌ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๋‹ค๋ฅธ ๋…ธ๋“œ๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ๋ฐฉ๋ฌธ
    for i in graph[v]:
        if not visited[i]: #๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด 
            dfs(graph, i, visited)



๐ŸŽฏ BFS (๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜)


Breadth First Search
while๋ฌธ๊ณผ deque๋กœ ๊ตฌํ˜„

  1. ํƒ์ƒ‰ ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ํ์— ์‚ฝ์ž…ํ•˜๊ณ  ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.
  2. ํ์—์„œ ๋…ธ๋“œ๋ฅผ ๊บผ๋‚ด(popleft()) ํ•ด๋‹น ๋…ธ๋“œ์˜ ์ธ์ ‘ ๋…ธ๋“œ ์ค‘์—์„œ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ํ์— ์‚ฝ์ž…ํ•˜๊ณ  ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.
  3. 2๋ฒˆ์˜ ๊ณผ์ •์„ ๋” ์ด์ƒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†์„ ๋•Œ๊นŒ์ง€(ํ๊ฐ€ ๋น„์–ด์žˆ์„ ๋•Œ ๊นŒ์ง€) ๋ฐ˜๋ณต (while)ํ•œ๋‹ค.

[์ฐธ๊ณ ]๋ฏธ๋กœํƒ์ƒ‰ ๋“ฑ ์ขŒํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ

  • ๊ทธ๋ž˜ํ”„(๋งต)์˜ ๋ฒ”์œ„ ๋‚ด์— ์žˆ๊ณ  ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋งŒ ํ์— ์‚ฝ์ž…ํ•˜๊ธฐ
  • ์ค‘๋ณตํƒ์ƒ‰์„ ํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌํ•ด์ฃผ๊ธฐ.
  • ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ์˜ ๊ฒฝ์šฐ ๋”ฐ๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด ํ‘œ์‹œํ•ด๋‘˜์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๊ทธ๋ž˜ํ”„(๋งต) ๋‚ด์— ํ‘œ์‹œํ•˜๋Š” ๊ฒƒ์ด ํšจ์œจ์ ์ž„.
  • ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ์˜ ์œ„์น˜ : 1. ๋งจ ์ฒ˜์Œ ์‹œ์ž‘ ์œ„์น˜(๋…ธ๋“œ) / 2. ํ์— ์‚ฝ์ž…ํ•˜๊ธฐ ๋ฐ”๋กœ ์ „ํ›„๋กœ.

# BFS ํ•จ์ˆ˜ ์ •์˜ - graph๋Š” ์ธ์ ‘๋ฆฌ์ŠคํŠธ 
def bfs(graph, start, visited):

    # ํ(Queue) ๊ตฌํ˜„์„ ์œ„ํ•ด deque ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ
    queue = deque([start])
    
    # ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
    visited[start] = True
    
    # ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
    while queue:
        # ํ์—์„œ ํ•˜๋‚˜์˜ ์›์†Œ๋ฅผ ๋ฝ‘์•„ ์ถœ๋ ฅ
        v = queue.popleft() 
        print(v, end=' ')
       
        # ํ•ด๋‹น ์›์†Œ์™€ ์—ฐ๊ฒฐ๋œ, ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์›์†Œ๋“ค์„ ํ์— ์‚ฝ์ž…
        for i in graph[v]:
            if not visited[i]:
                queue.append(i)
                visited[i] = True



๐Ÿ“Œ ๊ทธ๋ž˜ํ”„ ๊ตฌํ˜„ : ์ธ์ ‘ ํ–‰๋ ฌ, ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ


  • ์ธ์ ‘ ํ–‰๋ ฌ (Adjacency Matrix) : 2์ฐจ์› ๋ฐฐ์—ด๋กœ ๊ทธ๋ž˜ํ”„์˜ ์—ฐ๊ฒฐ ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„
#์ธ์ ‘ ํ–‰๋ ฌ
[[0,7,5], [7,0,999999999], [5,999999999,0]]

  • ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ (Adjacency List) : ๋ฆฌ์ŠคํŠธ๋กœ ๊ทธ๋ž˜ํ”„์˜ ์—ฐ๊ฒฐ ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„
#์ธ์ ‘ ๋ฆฌ์ŠคํŠธ 
[[(1,7),(2,5)], [(0,7)], [(0,5)]]



๐Ÿ“Œ [Python] ์Šคํƒ๊ณผ ํ ์‚ฌ์šฉ๋ฒ•


๐ŸŒ€ Stack ์‚ฌ์šฉ๋ฒ•

stack = []
stack.append(5)
stack.pop()

๐ŸŒ€ Deque ์‚ฌ์šฉ๋ฒ•

from collections import deque

#Deque ์ดˆ๊ธฐํ™”
# ๋น„์–ด์žˆ๋Š” ํ ๋งŒ๋“ค๊ธฐ
deque = deque()

# ์›์†Œ๊ฐ€ ์žˆ๋Š” ํ ๋งŒ๋“ค๊ธฐ
deque = deque([1, 2, 3])

# ํ ์ตœ๋Œ€ ๊ธธ์ด ๋ช…์‹œํ•˜๊ธฐ(์›์†Œ๋ฅผ maxlen๋ณด๋‹ค ๋” ๋งŽ์ด ๋„ฃ์œผ๋ฉด maxlen์ด ์ž๋™ ๊ฐฑ์‹ ๋จ)
deque = deque(maxlen=5)


# deque์— ์ถ”๊ฐ€
deque.append(i)

# deque์—์„œ ๊บผ๋‚ด๊ธฐ
deque.popleft()

์ฐธ๊ณ )

profile
์ผ๋‹จ ํ•  ์ˆ˜ ์žˆ๋Š”๊ฑธ ํ•˜์ž.
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€