[Python/ํŒŒ์ด์ฌ][๐Ÿฅˆ2] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 1260 - DFS์™€ BFS

keyneneยท2022๋…„ 11์›” 4์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
22/26
post-custom-banner

๐Ÿ“–[Python/ํŒŒ์ด์ฌ][๐Ÿฅˆ2] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 1260 - DFS์™€ BFS

๐Ÿ“œ๋ฌธ์ œ



DFS(๊นŠ์ด์šฐ์„ ํƒ์ƒ‰)

๊ทธ๋ž˜ํ”„์—์„œ ๊นŠ์€ ๋ถ€๋ถ„์„ ์šฐ์„ ์ ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
#Stack #์Šคํƒ #์žฌ๊ท€ #์ˆœํ™˜ #๋ฌดํ•œ๋ฃจํ”„ํƒˆ์ถœ #Back-Tracking

BFS(๋„“์ด์šฐ์„ ํƒ์ƒ‰)

๊ทธ๋ž˜ํ”„์—์„œ ๊ฐ€๊นŒ์šด ๋…ธ๋“œ๋ถ€ํ„ฐ ์šฐ์„ ์ ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
#Queue #ํ


DFS์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ˆ˜์ฐจ๋ก€ ์—ฐ์Šต์œผ๋กœ ์ด์ œ ์ต์ˆ™ํ•ด์กŒ๊ณ , BFS์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด๋ฒˆ ๋ฌธ์ œ๋กœ ์ฒ˜์Œ ์ ‘ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.
์šฐ์„ , ๋‹ค๋ฅธ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ์—ฌ๋Ÿฌ ์ฐจ๋ก€ ์‚ดํŽด๋ณด๋‹ˆ 2๊ฐ€์ง€ ํ’€์ด๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ๋˜์—ˆ๋‹ค.

1. MAPํ˜•์‹์œผ๋กœ ์ธ๋ฑ์Šค ๊ธฐ์ค€์œผ๋กœ ๊ฐ ๋…ธ๋“œ์˜ ์—ฐ๊ฒฐ์„ ์ง๊ด€์ ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๊ธฐ

๐Ÿ‘‰๐Ÿป ์ˆ˜์—ด+DFS/BFS ๋“ฑ ์ง๊ด€์ ์œผ๋กœ ์—ฐ๊ฒฐ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ๋ฅผ ์ด์šฉํ•  ๋•Œ
๐Ÿ‘‰๐Ÿป CYCLE ์—ฌ๋ถ€ ๋ฅผ ํ™•์ธํ•  ๋•Œ

 ex) 1-2-3 cycle๋กœ ์—ฐ๊ฒฐ๋˜์–ด์žˆ์œผ๋ฉด
    amap = [[2,3], [1,3], [1,2]]
    amap[1] = [2,3]  #1๋ฒˆ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๋…ธ๋“œ๋“ค (idx == 1)
    amap[2] = [1,3]  #2๋ฒˆ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๋…ธ๋“œ๋“ค (idx == 2)
    amap[3] = [1,2]  #3๋ฒˆ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๋…ธ๋“œ๋“ค (idx == 3)

2. MATRIX์ฒ˜๋Ÿผ ๊ฐ ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๊ฐ„์„ ์„ ์ธ์ ‘ํ–‰๋ ฌ ํ˜•ํƒœ๋กœ ๋‚˜ํƒ€๋‚ด๊ธฐ

๐Ÿ‘‰๐Ÿป ๋ฏธ๋กœ์ฐพ๊ธฐ, ์˜์—ญ๊ตฌํ•˜๊ธฐ ๋“ฑ ๋งคํŠธ๋ฆญ์Šค ํ˜•์‹์„ ์ด์šฉํ•  ๋•Œ

 ex) 1-2-3 cycle๋กœ ์—ฐ๊ฒฐ๋˜์–ด์žˆ์œผ๋ฉด
    matrix = [[0,0,0,0], [0,0,1,1], [0,1,0,1], [0,1,1,0]]
    matrix[1] = [0,0,1,1]  #1๋ฒˆ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๋…ธ๋“œ๋“ค์˜ ๊ฐ„์„  (idx == 1)
    matrix[2] = [0,1,0,1]  #2๋ฒˆ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๋…ธ๋“œ๋“ค์˜ ๊ฐ„์„  (idx == 2)
    matrix[2] = [0,1,1,0]  #2๋ฒˆ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๋…ธ๋“œ๋“ค์˜ ๊ฐ„์„  (idx == 2)

๐Ÿ“•ํ’€์ด๋ฐฉํ–ฅ

์•ž์„œ ์„ค๋ช…ํ•œ 2๊ฐ€์ง€ ํ’€์ด๋ฐฉ๋ฒ•์œผ๋กœ ๋‹ค ํ’€์–ด๋ณด์•˜๋‹ค.
1. MAP์„ ์ด์šฉํ•œ ํ’€์ด
2. MATRIX๋ฅผ ์ด์šฉํ•œ ํ’€์ด


๐Ÿ“์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์ˆœ์„œ

1๋ฒˆํ’€์ด(MAP)

  1. n,m,v ์ž…๋ ฅ๋ฐ›๊ณ  ์ฃผ์–ด์ง€๋Š” ๋…ธ๋“œ node(list)๋ฅผ, mapํ˜•์‹์œผ๋กœ ์ €์žฅ ํ›„ ์ •๋ ฌํ•˜์ž
  2. DFS, BFS ํ•จ์ˆ˜ ์ •์˜ ํ›„ ํ˜ธ์ถœ

2๋ฒˆํ’€์ด(MATRIX)

  1. n,m,v ์ž…๋ ฅ๋ฐ›๊ณ  ์ฃผ์–ด์ง€๋Š” ๋…ธ๋“œ node(list)๋ฅผ, matrixํ˜•์‹์œผ๋กœ 0, 1๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ €์žฅํ•˜์ž
  2. DFS, BFS ํ•จ์ˆ˜ ์ •์˜ ํ›„ ํ˜ธ์ถœ

๐Ÿ’ป๊ฒฐ๊ณผ์ฝ”๋“œ

โœ๏ธ1๋ฒˆํ’€์ด(MAP)

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

n,m,v = map(int, input().split())
node = [list(map(int, input().split())) for _ in range(m)]
amap = [[] for _ in range(n+1)]

#amap์— ๋งคํ•‘
for x,y in node:
    amap[x].append(y)
    amap[y].append(x)

#amap ์ •๋ ฌ
for i in range(1,n+1):
    amap[i].sort()

#DFS
d_visited = [False]*(n+1)
def dfs(v):
    print(v, end=' ')
    d_visited[v] = True   #ํ˜ธ์ถœ๋œ v ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
    for i in amap[v]:   #amap[v]์— ์ €์žฅ๋œ ๋…ธ๋“œ๋“ค ํ•˜๋‚˜์”ฉ
        if not d_visited[i]:   #๋ฐฉ๋ฌธ๊ฒ€์‚ฌํ•˜๋ฉด์„œ
            dfs(i)             #DFS ํ˜ธ์ถœ

#BFS
b_visited = [False]*(n+1)
def bfs(v):
    queue = deque([v])   #ํ์— v ๋„ฃ๊ธฐ
    b_visited[v] = True   #v ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ

    while queue:   #ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
        v = queue.popleft()   #๊ฐ€์žฅ ๋จผ์ € ๋“ค์–ด์˜จ ๋…ธ๋“œ ๊บผ๋‚ด๊ธฐ pop
        print(v, end=' ')   #์„ ์ž…์„ ์ถœ์ด๋ฏ€๋กœ, pop๋œ ๋…ธ๋“œ ์ถœ๋ ฅ

        for i in amap[v]:   #amap[v]์— ์ €์žฅ๋œ ๋…ธ๋“œ๋“ค ํ•˜๋‚˜์”ฉ
            if not b_visited[i]:   #๋ฐฉ๋ฌธ๊ฒ€์‚ฌํ•˜๋ฉด์„œ
                queue.append(i)   #ํ์— ์‚ฝ์ž…ํ•˜๊ณ 
                b_visited[i] = True   #๋ฐฉ๋ฌธ์ฒ˜๋ฆฌํ•˜๊ธฐ
        #์ด ๋•Œ ํ๊ฐ€ ๋น„์ง€ ์•Š์•˜์œผ๋ฉด ๋‹ค์‹œ while queue์—์„œ ๋ฐ˜๋ณต!!

dfs(v)
print()
bfs(v)

โœ๏ธ2๋ฒˆํ’€์ด(MATRIX)

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

n,m,v = map(int, input().split())
node = [list(map(int, input().split())) for _ in range(m)]
matrix = [[0]*(n+1) for _ in range(n+1)] #์ธ์ ‘ํ–‰๋ ฌ

#๋…ธ๋“œ๋“ค ๊ฐ„ ๊ฐ„์„ ์„ 1๋กœ ๋งคํ•‘
for x,y in node:
    matrix[y][x] = matrix[x][y] = 1

#DFS
d_visited = [False]*(n+1)
def dfs(v):
    print(v, end=' ')
    d_visited[v] = True

    for i in range(1, n+1):
    	#i๋…ธ๋“œ์— ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๊ณ , ํ˜„์žฌ๋…ธ๋“œ(v)์™€ i๋…ธ๋“œ ๊ฐ„์˜ ์„ ์ด ์žˆ์„ ๊ฒฝ์šฐ dfsํ˜ธ์ถœ
        if not d_visited[i] and matrix[v][i]:
            dfs(i)
            
            
##########################################################################
โ“visited๋ฅผ 2์ฐจ์›์ด ์•„๋‹ˆ๋ผ 1์ฐจ์›๋ฐฐ์—ด๋กœ ์ง€์ •ํ•˜๋Š” ์ด์œ 

  Map์€ '๋…ธ๋“œ'๊ฐ€ ์ค‘์ ์ธ ๋ฐ˜๋ฉด, Matrix๋Š” 0๊ณผ 1์ด ๋ชจ๋‘ '๊ฐ„์„ '์ด ์ฃผ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ!
  
  visited๋ฅผ 2์ฐจ์›๋ฐฐ์—ด๋กœ ์ •์˜ํ•ด๋„ ์กฐ๊ฑด๋ฌธ์„ ์ž˜ ์ž‘์„ฑํ•˜์—ฌ ๊ฒ€์‚ฌํ•˜๋ฉด ๋˜๊ฒ ์ง€๋งŒ,
  1์ฐจ์›๋ฐฐ์—ด๋กœ ์ •์˜ํ•˜๋ฉด, 
  "if not visited[1]: " ๋Š” '๋…ธ๋“œ'๊ฐ€ ์ฃผ์ฒด๊ฐ€ ๋˜์–ด, 
  1๋ฒˆ '๋…ธ๋“œ'์— ๋ฐฉ๋ฌธํ•œ ์ ์ด ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๊ฒŒ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ„ํŽธํ•จ
##########################################################################



#BFS
b_visited = [False]*(n+1)
def bfs(v):
    queue = deque([v])
    b_visited[v] = True

    while queue:
        v = queue.popleft()
        print(v, end=' ')

        for i in range(1, n+1):
            if not b_visited[i] and matrix[v][i] == 1:
                queue.append(i)
                b_visited[i] = True

dfs(v)
print()
bfs(v)

๐Ÿ“š์ •๋ฆฌ

๋™์ž‘ ์›๋ฆฌ๋Š” ์œ„ ์ฝ”๋“œ์—์„œ ์„ค๋ช…ํ–ˆ์œผ๋‹ˆ ์ƒ๋žตํ•œ๋‹ค.

  1. DFS/BFS์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ชจ๋‘ ๋‹ค์–‘ํ•œ ํ’€์ด๋ฒ•์ด ์žˆ๋‹ค. (Map, Matrix ์ด์™ธ์—๋„ ๋งŽ์„ ๊ฒƒ)
  2. ๊นŠ์ด๊ธฐ์ค€์œผ๋กœ ํƒ์ƒ‰ํ•ด์•ผํ•˜๋ฉด DFS, ๋„“์ด(๊ฐ€๊นŒ์šด)๊ธฐ์ค€์œผ๋กœ ํƒ์ƒ‰ํ•˜๋ฉด BFS๋กœ ํ’€์ดํ•  ๊ฒƒ
profile
keynene
post-custom-banner

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