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

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

1260๋ฒˆ: DFS์™€ BFS

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 1,000), ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ M(1 ≤ M ≤ 10,000), ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•  ์ •์ ์˜ ๋ฒˆํ˜ธ V๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ M๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ„์„ ์ด ์—ฐ๊ฒฐํ•˜๋Š” ๋‘ ์ •์ ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์–ด๋–ค ๋‘ ์ •์  ์‚ฌ

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ๊ทธ๋ž˜ํ”„์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, DFS(Depth - First - Search)์™€ BFS(Breadth - First - Searh)๋ฅผ ํ†ตํ•ด ํƒ์ƒ‰ํ•  ๊ฒฝ์šฐ์— ํƒ์ƒ‰ ์ˆœ์„œ๋ฅผ ๊ฒฐ๊ณผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ๋Š” DFS์™€ BFS์— ๋Œ€ํ•ด ์ดํ•ดํ•˜๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค.

 

DFS ๊ตฌํ˜„

def dfs(depth, cur_node, visited):
    # ์ด๋ฏธ ์‹œ์ž‘ ์ง€์ ์€ ๋ฐฉ๋ฌธํ•˜์˜€์œผ๋ฏ€๋กœ, ํƒ์ƒ‰ ๊ฒฝ๋กœ๋Š” n - 1
    if depth == n - 1:
        return

    for next_node in range(n):
        if graph[cur_node][next_node] and next_node + 1 not in visited:
            visited.append(next_node + 1)
            dfs(depth + 1, next_node, visited)
    return visited

 DFS๋Š” ์žฌ๊ท€ ํ˜ธ์ถœ์ด๋‚˜ Stack์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. Stack์œผ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค๋ฉด, ํŠน์ • ๋…ธ๋“œ์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ๋‹ค๋ฅธ ๋…ธ๋“œ๋“ค์„ ์—ญ์ˆœ์œผ๋กœ ์‚ฝ์ž…ํ•˜์—ฌ์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์žฌ๊ท€ ํ˜ธ์ถœ๋„ Stack๊ณผ ๊ฐ™์€ ์›๋ฆฌ ์ด๋ฏ€๋กœ ์ด๋ฅผ ํ†ตํ•ด DFS๋ฅผ ๊ตฌํ˜„ํ•˜์˜€๋‹ค. depth๊ฐ€ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์€ ๋ฐฉ๋ฌธ ๊ฐ€๋Šฅํ•œ ๋…ธ๋“œ๋“ค์„ ๋‹ค ๋ฐฉ๋ฌธํ•˜์˜€๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค.

 ๋˜ํ•œ ์ˆœ์—ด, ์กฐํ•ฉ๊ณผ ๋‹ฌ๋ฆฌ ์žฌ๊ท€ ํ˜ธ์ถœ์„ ํ•˜๋”๋ผ๋„ ๋ฐฑํŠธ๋ž™ํ‚น์„ ํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ์— ํ•œ ๋ฒˆ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ฅผ ๋‹ค์‹œ ํ•ด์ œํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋งŒ์•ฝ ๋ฐฉ๋ฌธ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋ผ๊ณ  ํ•œ๋‹ค๋ฉด, ๋ฐฑํŠธ๋ž™ํ‚น์„ ์œ„ํ•ด ์žฌ๊ท€ ํ˜ธ์ถœ ํ›„์— visited.pop()์ด ํ•„์š”ํ•˜๋‹ค.

 

BFS ๊ตฌํ˜„

def bfs(start):
    # ์‹œ์ž‘ ์ง€์  ์ถ”๊ฐ€์™€, ๋ฐฉ๋ฌธ ํ‘œ์‹œ
    q = deque([start])
    visited = [start + 1]

    while q:
        cur_node = q.popleft()
        for next_node in range(n):
            if graph[cur_node][next_node] and next_node + 1 not in visited:
                q.append(next_node)
                visited.append(next_node + 1)
    return visited

 BFS๋Š” DFS์™€ ๋‹ฌ๋ฆฌ Queue๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋จผ์ € ํƒ์ƒ‰ํ•œ ๋…ธ๋“œ๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•จ์œผ๋กœ์จ depth๋กœ ๋ป—์–ด๊ฐ€๋Š” ๊ฒƒ์ด ๋‚˜๋‹Œ Breadth๋กœ ๋ป—์–ด ๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

 

์ฝ”๋“œ

from collections import deque


def bfs(start):
    # ์‹œ์ž‘ ์ง€์  ์ถ”๊ฐ€์™€, ๋ฐฉ๋ฌธ ํ‘œ์‹œ
    q = deque([start])
    visited = [start + 1]

    while q:
        cur_node = q.popleft()
        for next_node in range(n):
            if graph[cur_node][next_node] and next_node + 1 not in visited:
                q.append(next_node)
                visited.append(next_node + 1)
    return visited


def dfs(depth, cur_node, visited):
    # ์ด๋ฏธ ์‹œ์ž‘ ์ง€์ ์€ ๋ฐฉ๋ฌธํ•˜์˜€์œผ๋ฏ€๋กœ, ํƒ์ƒ‰ ๊ฒฝ๋กœ๋Š” n - 1
    if depth == n - 1:
        return

    for next_node in range(n):
        if graph[cur_node][next_node] and next_node + 1 not in visited:
            visited.append(next_node + 1)
            dfs(depth + 1, next_node, visited)
    return visited


if __name__ == "__main__":
    n, m, v = map(int, input().split())
    graph = [[False] * n for _ in range(n)]

    for _ in range(m):
        i, j = map(int, input().split())
        # ๊ทธ๋ž˜ํ”„๋Š” ์–‘๋ฐฉํ–ฅ์ด๋ฏ€๋กœ ๋ฐ˜์ „ํ•˜์—ฌ ์ฒดํฌํ•ด์ค˜์•ผ ํ•จ
        graph[i - 1][j - 1] = True
        graph[j - 1][i - 1] = True

    print(*dfs(0, v - 1, [v]))
    print(*bfs(v - 1))

 

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€