[Algorithm] DFS & BFS | ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰
๐Ÿ–ฅ CS/Algorithm

[Algorithm] DFS & BFS | ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰

โ€ปํƒ์ƒ‰: ๋งŽ์€ ๋ฐ์ดํ„ฐ์—์„œ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ๋Š” ๊ณผ์ •

 

๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰์„ ์œ„ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ

 

(1) ์Šคํƒ

(2) ํ

(3) ์žฌ๊ท€ํ•จ์ˆ˜

(4) ๊ทธ๋ž˜ํ”„

 

 

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

: ๊นŠ์€ ๋ถ€๋ถ„์„ ์šฐ์„ ์ ์œผ๋กœ ํƒ์ƒ‰

 

๐Ÿ‘‰ ํŠน์ • ๊ฒฝ๋กœ๋กœ ํƒ์ƒ‰ํ•˜๋‹ค๊ฐ€ ํŠน์ • ์ƒํ™ฉ์—์„œ ์ตœ๋Œ€ํ•œ ๊นŠ์ˆ™์ด ๋“ค์–ด๊ฐ€์„œ ๋…ธ๋“œ ๋ฐฉ๋ฌธ ํ›„ ๋‹ค์‹œ ๋Œ์•„๊ฐ€ ๋‹ค๋ฅธ ๋…ธ๋“œ ๋ฐฉ๋ฌธ

    

(1) ํƒ์ƒ‰ ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ์Šคํƒ์— ์‚ฝ์ž…ํ•˜๊ณ  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
โ€ป ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ: ์Šคํƒ์— ํ•œ๋ฒˆ ์‚ฝ์ž…๋˜์–ด ์ฒ˜๋ฆฌ๋œ ๋…ธ๋“œ๊ฐ€ ๋‹ค์‹œ ์‚ฝ์ž…๋˜์ง€ ์•Š๊ฒŒ check
(2) ์Šคํƒ์˜ ์ตœ์ƒ๋‹จ ๋…ธ๋“œ์— ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ทธ ์ธ์ ‘ ๋…ธ๋“œ๋ฅผ ์Šคํƒ์— pushํ•˜๊ณ  ๋‹ค์‹œ ๋Œ์•„๊ฐ€ ๋‹ค๋ฅธ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•œ๋‹ค.
(3) (2) ๊ณผ์ •์„ ๋ฐ˜๋ณต ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

 

๊ฒฐ๊ณผ์ ์œผ๋กœ ๋…ธ๋“œ์˜ ํƒ์ƒ‰ ์ˆœ์„œ๋Š” ์Šคํƒ์— ๋“ค์–ด๊ฐ„ ์ˆœ์„œ์™€ ๊ฐ™๋‹ค.

 1 → 2 → 5 →6 → 3 → 7 → 4 → 8 

 

์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•.

N,M,V = map(int,input().split())
graph = [[] for _ in range(N+1)]
visited = [False] * (N + 1)

for i in range(M):
    start, end  = map(int, input().split())
    graph[start].append(end)
    graph[end].append(start)
    graph[start].sort()
    graph[end].sort()
    

def DFS(graph, v, visited):
    visited[v] = True
    print(v, end = ' ')

    for i in graph[v]:
        if not visited[i]:
            DFS(graph, i, visited)
            
DFS(graph, V, visited)

 

๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•.

from sys import stdin

N, M, V = map(int, stdin.readline().split())
matrix = [[0]*(N+1) for i in range(N+1)]

for i in range(M):
    a, b = map(int, stdin.readline().split())
    matrix[a][b] = matrix[b][a] = 1
visit_list = [0]*(N+1)


def DFS(v):
    visit_list[v] = 1
    print(v, end=' ')
    for i in range(1, N+1):
        if(visit_list[i] == 0 and matrix[v][i] == 1):
            DFS(i)


DFS(V)

 

 

 

BFS | ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰

: ๊ฐ€๊นŒ์šด ๋…ธ๋“œ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜

 

๐Ÿ‘‰ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ํ์— ๋„ฃ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด ๊ตฌํ˜„ํ•˜๋ฉด ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋จผ์ € ๋“ค์–ด์˜จ ๊ฒƒ์ด ๋จผ์ € ๋‚˜๊ฐ

 

(1) ํƒ์ƒ‰ ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ํ์— ์‚ฝ์ž…ํ•˜๊ณ  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
โ€ป ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ: ์Šคํƒ์— ํ•œ๋ฒˆ ์‚ฝ์ž…๋˜์–ด ์ฒ˜๋ฆฌ๋œ ๋…ธ๋“œ๊ฐ€ ๋‹ค์‹œ ์‚ฝ์ž…๋˜์ง€ ์•Š๊ฒŒ check
(2) ํ์—์„œ ๋…ธ๋“œ๋ฅผ ๊บผ๋‚ด ํ•ด๋‹น ๋…ธ๋“œ์˜ ์ธ์ ‘ ๋…ธ๋“œ ์ค‘ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ํ์— ์‚ฝ์ž…ํ•˜๊ณ  ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
(3) (2) ๊ณผ์ •์„ ๋ฐ˜๋ณต ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

 

 

 

๊ฒฐ๊ณผ์ ์œผ๋กœ ๋…ธ๋“œ์˜ ํƒ์ƒ‰ ์ˆœ์„œ๋Š” ํ์— ๋“ค์–ด๊ฐ„ ์ˆœ์„œ์™€ ๊ฐ™๋‹ค.

 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 

 

 ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•.

from collections import deque

N,M,V = map(int,input().split())
graph = [[] for _ in range(N+1)]
visited = [False] * (N + 1)

for i in range(M):
    start, end  = map(int, input().split())
    graph[start].append(end)
    graph[end].append(start)
    graph[start].sort()
    graph[end].sort()
    
def BFS(graph, start, visited):
    queue = deque([start])
    visited[start] = True

    while queue:
        v = queue.popleft()
        print(v, end=' ')    
        for i in graph[v]:
            if not visited[i]:
                queue.append(i)
                visited[i] = True
                
BFS(graph,V,visited)

 

๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•.

N,M,V=map(int,input().split())
matrix=[[0]*(N+1) for i in range(N+1)]
for i in range(M):
    a,b = map(int,input().split())
    matrix[a][b]=matrix[b][a]=1
visit_list=[0]*(N+1)

def bfs(V):
    queue=[V] 
    visit_list[V]=1
    while queue:
        V=queue.pop(0)
        print(V, end=' ')
        for i in range(1, N+1):
            if(visit_list[i]==0 and matrix[V][i]==0):
                queue.append(i)
                visit_list[i]=1


bfs(V)