Problem Solving/BOJ & Programmers

[BOJ] 1260๋ฒˆ: DFS์™€ BFS

geum 2021. 6. 27. 00:38

๊ณ„์† ๋ฏธ๋ค„๋†จ๋˜ DFS/BFS ๊ฐœ๋…์„ ๊ณต๋ถ€ํ•œ ํ›„ ๋ฐฑ์ค€์—์„œ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋ฌธ์ œ๋กœ ๋ณด์ด๋Š” ๊ฑธ ํ’€์–ด๋ดค๋Š”๋ฐ ์—ฌ๋Ÿฌ ๋ฒˆ์˜ ์‹œ๋„ ๋์— ์–ด์ฐŒ์ €์ฐŒ ๋งž์•˜๋‹ค. ํ•˜์ง€๋งŒ ์œ„์˜ ์‚ฌ์ง„์ฒ˜๋Ÿผ ์ •๋‹ต๊นŒ์ง€ ๊ฐ€๋Š” ๊ณผ์ •์ด ๊ฝค ํ—˜๋‚œํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ๋ฅผ ๋ณต๊ธฐํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.


๋ฌธ์ œ

 

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

 

์•„์ด๋””์–ด ํ๋ฆ„

์ฝ”๋“œ์˜ ํฐ ํ‹€์€ ์•„๋ž˜์ฒ˜๋Ÿผ ์ƒ๊ฐํ–ˆ๋‹ค.

 

1) ๊ทธ๋ž˜ํ”„ ํ˜•ํƒœ ํ‘œํ˜„ ๋ฐฉ๋ฒ•? 2์ฐจ์› ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ๋งŒ๋“ค๊ธฐ

2) DFS ๊ตฌํ˜„? ์Šคํƒ ๊ตฌ์กฐ๋‚˜ ์žฌ๊ท€ ํ•จ์ˆ˜ ์‚ฌ์šฉ

3) BFS ๊ตฌํ˜„? ํ ์‚ฌ์šฉ

 

 

๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ

1) AttributeError

์•Œ๊ณ ๋ฆฌ์ฆ˜ ์˜ค๋žœ๋งŒ์— ํ’€์—ˆ๋”๋‹ˆ ๋‚˜๋„ ๋ชจ๋ฅด๋Š” ์‚ฌ์ด์— ๊นŒ๋จน์—ˆ๋Š”์ง€ input ํ•จ์ˆ˜์— ๊ด„ํ˜ธ๋ฅผ ์•ˆ ์ผ๋‹ค!^^

 

2) TypeError

์‚ฌ์‹ค ํƒ€์ž…์—๋Ÿฌ๋Š” ์ •ํ™•ํ•˜๊ฒŒ ์–ด๋””์„œ ๋‚œ ๊ฑด์ง€ ์•„์ง๋„ ๋ชจ๋ฅด๊ฒ ๋‹ค. int๋ฅผ ์จ์•ผํ•˜๋Š”๋ฐ char๋ฅผ ์ผ๋‹ค๋˜๊ฐ€ ์ด๋Ÿด ๋•Œ ๋‚˜๋Š” ์—๋Ÿฌ๋ผ๋Š”๋ฐ ์ด์œ ๋Š” ๋ชป ์ฐพ์•˜๊ณ  ํ‹€๋ ธ์œผ๋‹ˆ๊นŒ ๊ณ ์ณ์•ผ๊ฒ ๋‹ค ์ด ์ƒ๊ฐ์ด ์ปธ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

 

์‹œ๊ฐ„ ์ดˆ๊ณผ

# DFS
    for j in range(1, n+1):
        # ์ถœ๋ฐœ์  v์˜ ์ธ์ ‘๋…ธ๋“œ์ด์ง€๋งŒ ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†๋Š” ๋…ธ๋“œ
        if visited[j] == 0 and adjMatrix[v][j] == 1:
            dfs(j)
            
 # BFS
     while q:
        v = q.popleft()

        print(v, end=' ')

        # ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ ๋˜์ง€ ์•Š์€ ์ธ์ ‘ ๋…ธ๋“œ ๋ชจ๋‘ ํ์— ์‚ฝ์ž…
        for k in range(1, n+1):
            if k not in visited and adjMatrix[v][k] == 1:
                q.append(k)
                visited[k] = 1

์žฌ๊ท€ ๋•Œ๋ฌธ์— ๊ทธ๋Ÿฐ๊ฐ€ ์‹ถ์–ด์„œ ์žฌ๊ท€ ๊นŠ์ด๋ฅผ ๋” ๊นŠ๊ฒŒ ์„ค์ •ํ•˜๋ ค๋‹ค๊ฐ€ https://hjp845.tistory.com/32 ์ด ๊ธ€์„ ๋ณด๊ณ  ๋…ธ๋“œ ํ™•์ธ, ๋ฐฉ๋ฌธ ์ฒดํฌ ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ”์ฃผ์—ˆ๋‹ค.

 

์ถœ๋ ฅ ์ดˆ๊ณผ

BFS์—์„œ ๋ฌดํ•œ๋ฃจํ”„ ๊ฑธ๋ ธ์„ ๋•Œ ์ถœ๋ ฅ ์ดˆ๊ณผ์˜€๋‹ค.

 

์™„์ „ํžˆ ํ‹€๋ฆฐ ๊ฒฝ์šฐ

์ด๊ฒƒ ๋•Œ๋ฌธ์— ๊ณ ๋ฏผ์„ ์ •๋ง ๋งŽ์ด ํ–ˆ๋‹ค. ์˜ˆ์ œ ์ž…๋ ฅ 2๋ฅผ ๋„ฃ์œผ๋ฉด BFS ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” 3 1 4 2 5๊ฐ€ ๋‚˜์™€์•ผ ํ•˜๋Š”๋ฐ ๊ณ„์† 3 1 4๊นŒ์ง€๋งŒ ๋‚˜์™€์„œ ์™œ ๋๊นŒ์ง€ ํƒ์ƒ‰์„ ๋ชปํ• ๊นŒ ์ด ์ƒ๊ฐ์„ ๊ณ„์† ํ–ˆ๋‹ค.

 

์ตœ์ข… ์ฝ”๋“œ

์œ„์— ์ ์€ ์—๋Ÿฌ๋“ค์„ ํ•˜๋‚˜์”ฉ ํ•ด๊ฒฐํ•˜๋ฉด์„œ ์ œ์ถœํ•œ ์ตœ์ข… ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

import sys

from collections import deque

n, m, v = map(int, input().split())

# ์ธ๋ฑ์Šค ํ—ท๊ฐˆ๋ฆฌ์ง€ ์•Š๊ฒŒ ํ•˜๋ ค๊ณ  (n+1)*(n+1) ํฌ๊ธฐ์˜ ์ธ์ ‘ํ–‰๋ ฌ ์ƒ์„ฑ
adjMatrix = [[0]*(n+1) for _ in range(n+1)]

# ๋ฐฉ๋ฌธ ์ฒดํฌ์šฉ ๋ฐฐ์—ด ์ƒ์„ฑ
visited = [0]*(n+1)

for i in range(m):
    a, b = map(int, sys.stdin.readline().split())
    
    # ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ์„ ์˜๋ฏธ
    adjMatrix[a][b] = adjMatrix[b][a] = 1

def dfs(v):
    visited[v] = 1

    print(v, end=' ')

    for j in range(1, n+1):
        # ์ถœ๋ฐœ์  v์˜ ์ธ์ ‘๋…ธ๋“œ์ด์ง€๋งŒ ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†๋Š” ๋…ธ๋“œ
        if adjMatrix[v][j] == 1 and visited[j] == 0 :
            dfs(j)


def bfs(V):
    bfsVisited = [V]

    q = deque() # ํ ์ƒ์„ฑ
    q.append(V) # ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌํ•œ ๋…ธ๋“œ๋Š” ํ์— ์‚ฝ์ž…

    while q:
        pop = q.popleft()
        print(pop, end=' ')

        # ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ ๋˜์ง€ ์•Š์€ ์ธ์ ‘ ๋…ธ๋“œ ๋ชจ๋‘ ํ์— ์‚ฝ์ž…
        for k in range(len(adjMatrix[V])):
            if adjMatrix[pop][k] == 1 and (k not in bfsVisited):
                q.append(k)
                bfsVisited.append(k)

dfs(v)
print()
bfs(v)

 

์ •๋ฆฌ

๋ชจ๋“  ์ฝ”๋“œ๋“ค์˜ ์•„์ด๋””์–ด ํ๋ฆ„์€ ๋™์ผํ–ˆ๊ณ  ์ตœ์ข… ์ฝ”๋“œ์™€ ์ด์ „ ์ฝ”๋“œ๋“ค์˜ ํฐ ์ฐจ์ด์ ์„ ์ •๋ฆฌํ•ด๋ณด๋ฉด

 

1) ๋ฐฉ๋ฌธ ์ฒดํฌ์šฉ ๋ฐฐ์—ด ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

์ „์—ญ ๋ณ€์ˆ˜๋กœ ๋ฐฉ๋ฌธ ๊ฒ€์‚ฌ๋ฅผ ์œ„ํ•œ ๋ฐฐ์—ด์„ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ  DFS์—์„œ๋Š” ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ ๋ฒˆํ˜ธ์™€ ๋™์ผํ•œ ์ธ๋ฑ์Šค ๊ฐ’์„ 1๋กœ, BFS์—์„œ๋Š” ์ด๋ฏธ 1๋กœ ๋ฐ”๋€ ์ธ๋ฑ์Šค๋“ค์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฉ๋ฌธํ–ˆ์„ ๊ฒฝ์šฐ 0์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ–ˆ์—ˆ๋‹ค. ๊ทผ๋ฐ ์ด ๋ฐฉ๋ฒ•์œผ๋กœ ํ•˜๋‹ˆ๊นŒ ๋‚ด๊ฐ€ ํ—ท๊ฐˆ๋ ค์„œ BFS๋Š” ํ•จ์ˆ˜ ์•ˆ์—์„œ ๋ฐฉ๋ฌธ ์ฒดํฌ์šฉ ๋ฐฐ์—ด์„ ๋”ฐ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค.

 

2) for k in range(len(adjMatrix[V]))

for k in range(1, n+1) ์ด๊ฑธ๋กœ ๊ณ„์† ์ฝ”๋“œ๋ฅผ ์งœ๋‹ค๊ฐ€ BFS์—์„œ๋งŒ ๊ฐ’์ด ์ œ๋Œ€๋กœ ์•ˆ ๋‚˜์˜ค๋‹ˆ๊นŒ ๋‹ค๋ฅธ ๋ถ„๋“ค์˜ BFS ์ฝ”๋“œ์™€ ๋‚ด ๊ฑธ ํ•˜๋‚˜ํ•˜๋‚˜ ๋น„๊ตํ•˜๋ฉด์„œ ๊ณ ์นœ ๋ถ€๋ถ„์ด๋‹ค. ๋‚ด๊ฐ€ ์ดํ•ดํ•œ ๋ฐฉ์‹์€ '๋…ธ๋“œ V์™€ ์—ฐ๊ฒฐ๋œ ์ธ์ ‘ ๋…ธ๋“œ ์ˆ˜๋งŒํผ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆฐ๋‹ค' ๋Š” ๊ฒƒ์ธ๋ฐ ๋งž๊ฒŒ ์ดํ•ดํ•œ๊ฑด์ง€๋Š” ํ™•์‹ ์ด ์—†๋‹ค ๐Ÿ˜…

 

์•ž์œผ๋กœ ๋ฌธ์ œ๋ฅผ ๋งŽ์ด ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค.