We will find a way, we always have.

-interstellar

Problem Solving/๋ฐฑ์ค€

[๋ฐฑ์ค€] 1260๋ฒˆ: DFS์™€ BFS - ํŒŒ์ด์ฌ

Redddy 2022. 6. 25. 01:03

๐Ÿ”ˆ ๋ฌธ์ œ

๊ทธ๋ž˜ํ”„๋ฅผ DFS๋กœ ํƒ์ƒ‰ํ•œ ๊ฒฐ๊ณผ์™€ BFS๋กœ ํƒ์ƒ‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋‹จ, ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ •์ ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” ์ •์  ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ๊ฒƒ์„ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๊ณ , ๋” ์ด์ƒ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ ์ด ์—†๋Š” ๊ฒฝ์šฐ ์ข…๋ฃŒํ•œ๋‹ค. ์ •์  ๋ฒˆํ˜ธ๋Š” 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€์ด๋‹ค.

 

๐Ÿ“์ž…๋ ฅ

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

 

๐Ÿ“‘์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— DFS๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ, ๊ทธ ๋‹ค์Œ ์ค„์—๋Š” BFS๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. V๋ถ€ํ„ฐ ๋ฐฉ๋ฌธ๋œ ์ ์„ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

 

๐Ÿ“š ๋ฌธ์ œ ํ•ด์„

ํ•ด์„์ด๋ผ๊ณ  ํ• ๋งŒํ•œ๊ฒŒ ์—†๋Š”,,ใ…Žใ…Ž

DFS์™€ BFS์˜ ๊ฐœ๋…๋งŒ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค.

DFS์˜ ๋…ธ๋“œ ๋ฐฉ๋ฌธ์ˆœ์„œ์™€ BFS์˜ ๋ฐฉ๋ฌธ์ˆœ์„œ๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

๋งŒ์•ฝ DFS์™€ BFS์˜ ๊ฐœ๋…์ด ์•„์ง ์•ˆ ์žกํ˜€ ์žˆ๋‹ค๋ฉด ์ •๋ฆฌํ•ด ๋‘” ๊ธ€์ด ์žˆ์œผ๋‹ˆ ์ฝ์–ด๋ณด์‹œ๊ธธใ…Ž.ใ…Ž

https://lazypazy.tistory.com/95

 

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ํƒ์ƒ‰์•Œ๊ณ ๋ฆฌ์ฆ˜ DFS/BFS

โ€ป ๊ทธ๋ž˜ํ”„ ์‚ฌ์ง„์€ ์ง์ ‘ ๊ทธ๋ ค์„œ ์ถ”๊ฐ€์˜ˆ์ •! ๊ฐค๋Ÿญ์‹œ ํƒญ์ด ์˜ค๋ฉด ๊ทธ๊ฑธ๋กœ ๊ทธ๋ ค์•ผ์ง€ใ…Ž.ใ…Ž DFS์™€ BFS๋Š” ๋ชจ๋‘ ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋ฅผ ์ค€๋น„ํ•˜๋‹ค๋ณด๋ฉด ์–ด๋Š ์ˆœ๊ฐ„ ๋งŒ๋‚  ์ˆ˜ ๋ฐ–์— ์—†๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜

lazypazy.tistory.com

 

๐Ÿ’ป ์ฝ”๋“œ

import sys
from collections import deque
input = sys.stdin.readline

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

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

n, m, start = map(int, input().rstrip().split()) # ์ •์ , ๊ฐ„์„ , ์‹œ์ž‘์ •์ 

graph = [[] for j in range(n+1)] # ๊ทธ๋ ค์งˆ ๊ทธ๋ž˜ํ”„ ์ดˆ๊ธฐํ™”
for k in range(m):
    a, b = map(int, input().rstrip().split()) # ์ด์›ƒํ•œ ์ •์  ์—ฐ๊ฒฐ์‹œํ‚ด
    graph[a].append(b)
    graph[b].append(a)


visited = [False] * (n+1)
# dfs ํ•จ์ˆ˜ ์‹คํ–‰
dfs(graph, start, visited)

print()

visited = [False] * (n+1)
# bfs ํ•จ์ˆ˜ ์‹คํ–‰
bfs(graph, start, visited)

 

๐Ÿ”— ๋ฌธ์ œ๋งํฌ DFS์™€ BFS