Algo-dfs-bfs.md


Before DFS/BFS...


Stack

  • LIFO์˜ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๋Š” ์ž๋ฃŒ๊ตฌ์กฐ Stack-TIL-Link

Queue

  • FIFO์˜ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๋Š” ์ž๋ฃŒ๊ตฌ์กฐ Queue-TIL-Link

Recursive Function

์ž๊ธฐ ์ž์‹ ์„ ๋‹ค์‹œ ํ˜ธ์ถœํ•˜๋Š” ํ•จ์ˆ˜

def recursive_func():
    print("recursive")
    recursive_func()

RecursionError: maximum recursion depth exceeded while calling a Python object

  • Python์—์„œ๋Š” ์žฌ๊ท€ ์ œํ•œ ๊นŠ์ด๋ฅผ ๋‘์–ด ํ•จ์ˆ˜๊ฐ€ ์ผ์ • ํšŸ์ˆ˜ ์ด์ƒ ์žฌ๊ท€์ ์œผ๋กœ ํ˜ธ์ถœ๋œ๋‹ค๋ฉด RecursionError๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ๋ฌธ์ œํ’€์ด์—์„œ ์‚ฌ์šฉํ•  ๋•Œ์—๋Š” ์ข…๋ฃŒ์กฐ๊ฑด์„ ๋ฐ˜๋“œ์‹œ ๋ช…์‹œํ•ด์•ผ ํ•œ๋‹ค.

ํŒฉํ† ๋ฆฌ์–ผ ๊ตฌํ˜„ ์˜ˆ์ œ

# ๋‹จ์ˆœ ๋ฐ˜๋ณต
def factorial_iterative(n):
    result = 1
    for i in range(1, n+1):
        result *= i
    return result

# Recursive
def factorial_recursive(n):
    if n <= 1:
        return 1
    return n * factorial_recursive(n-1)

๐Ÿ’ก ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ• - GCD ๊ณ„์‚ฐ

  • GCD: Greatest Common Divisor - ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜

๋‘ ์ž์—ฐ์ˆ˜ A, B์— ๋Œ€ํ•˜์—ฌ (A > B) A๋ฅผ B๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ R์ด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด A์™€ B์˜ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋Š” B์™€ R์˜ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜์™€ ๊ฐ™๋‹ค.

def gcd(a, b):
    if a % b == 0:
        return b
    else:
        gcd(b, a%b)

์ด์ฒ˜๋Ÿผ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์ž˜ ํ™œ์šฉํ•˜๋ฉด ๋ณต์žกํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ ๊ฐ€๋…์„ฑ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ์งœ๊ธฐ์—๋Š” ์ข‹์ง€ ์•Š์€ ๋ฐฉ๋ฒ•์ด๊ธฐ๋•Œ๋ฌธ์— ์ƒํ™ฉ์— ๋งž์ถ”์–ด ์ž˜ ์‚ฌ์šฉํ•˜์ž

โœ” ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ ํ˜„์žฌ ์ •์ ๊ณผ ์—ฐ๊ฒฐ๋œ(์ธ์ ‘ํ•œ) ์ •์ ๋“ค์„ ๊ฐˆ ์ˆ˜ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๊ณ , ํŠน์ • ์ •์ ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋‹ค๋ฉด ๊ทธ ์ •์ ์— ๊ฐ€์„œ ๊ฐ™์€ ํ–‰์œ„๋ฅผ ๋ฐ˜๋ณตํ•˜๋Š” ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜

์žฅ์ 

  • ์ง๊ด€์ ์ด๊ณ  ๊ตฌํ˜„ํ•˜๊ธฐ ์‰ฌ์›€

  • ๊ฐ™์€ ์ •์ ์„ ๋ฐ˜๋ณตํ•˜์ง€ ์•Š๋„๋ก ์ •์ ์— ๋ฐฉ๋ฌธํ–ˆ๋‹ค๋Š” ๊ฒƒ์„ ํ‘œ์‹œํ•ด์ค€๋‹ค.

  • ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ•œ๋‹ค.

๋‹จ์ 

  • ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋น„์šฉ์ด ์ถ”๊ฐ€๋กœ ๋“ค์–ด๊ฐ„๋‹ค.

  • ์žฌ๊ท€ ๊นŠ์ด๊ฐ€ ์ง€๋‚˜์น˜๊ฒŒ ๊นŠ์–ด์ง€๋ฉด ๋ฉ”๋ชจ๋ฆฌ ๋น„์šฉ์„ ์˜ˆ์ธกํ•˜๊ธฐ ์–ด๋ ต๋‹ค.

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

  • ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์•Œ ์ˆ˜ ์—†๋‹ค.

โœ” ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ ์‹œ์ž‘์ ์—์„œ ๊ฐ€๊นŒ์šด ์ •์ ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฉ๋ฌธํ•˜๋Š” ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜

์žฅ์ 

  • ํšจ์œจ์ ์ธ ์šด์˜ ๊ฐ€๋Šฅ

  • ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ณต์žก๋„ ๋ฉด์—์„œ ์•ˆ์ •์ 

  • ๊ฐ„์„ ์˜ ๋น„์šฉ์ด ๋ชจ๋‘ ๊ฐ™์„ ๊ฒฝ์šฐ, ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

    ๊ฐ„์„ ์˜ ๋น„์šฉ์ด ๊ฐ™๋‹ค? ๋ฌธ์ œ์˜ ์กฐ๊ฑด์—์„œ ๊ฒฝ๋กœ๋งˆ๋‹ค ๋น„์šฉ์ด ๋‹ค๋ฅด๊ฒŒ ์ฃผ์–ด์ง€๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”๋ฐ ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋“ฑ์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ™œ์šฉํ•ด์•ผ ํ•œ๋‹ค.

๋‹จ์ 

  • DFS์— ๋น„ํ•ด ์ฝ”๋“œ ๊ตฌํ˜„์ด ์–ด๋ ต๋‹ค.

  • ๋ชจ๋“  ์ง€์ ์„ ํƒ์ƒ‰ํ•  ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•ด, ํ์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ฏธ๋ฆฌ ์ค€๋น„๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

DFS vs BFS

  • ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ => ๊ธฐ๋ณธ์ ์œผ๋กœ ๋“œ๋Š” ๋ฉ”๋ชจ๋ฆฌ ๋น„์šฉ์€ BFS๊ฐ€ ๋งŽ์ง€๋งŒ, DFS๋Š” ์˜ˆ์™ธ์ ์ธ ์ƒํ™ฉ์—์„œ ๋” ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ ๋น„์šฉ์ด ๋“ ๋‹ค.

Last updated