[Algorithm] ๐Ÿšฉ[๋ฐฑ์ค€] 1260 (Feat. DFS์™€ BFS)

myeonjiยท2022๋…„ 2์›” 4์ผ
0

Algorithm

๋ชฉ๋ก ๋ณด๊ธฐ
31/89
post-custom-banner

BFS(๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰) ์•Œ๊ณ ๋ฆฌ์ฆ˜

> DFS(๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰) ์•Œ๊ณ ๋ฆฌ์ฆ˜

๊ทธ๋ž˜ํ”„ ์ „์ฒด๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํŠนํžˆ "๊นŠ์ด"๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ํƒ์ƒ‰
์Šคํƒ ์ž๋ฃŒ๊ตฌ์กฐ(ํ˜น์€ ์žฌ๊ท€ ํ•จ์ˆ˜)๋ฅผ ์ด์šฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ฏ€๋กœ ์‹ค์ œ ๊ตฌํ˜„ํ•  ๋•Œ ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๊ฐ„๊ฒฐํ•œ ์ฝ”๋“œ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
์Šคํƒ์€ ๋ฆฌ์ŠคํŠธ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ append์™€ pop์ด O(1)์ด๋‹ค.

  • ์ฃผ๋กœ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์•Œ๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ
  1. ํƒ์ƒ‰ ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ์Šคํƒ์— ์‚ฝ์ž… ํ›„ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
  2. ์Šคํƒ ๋‚ด ์ตœ์ƒ๋‹จ ๋…ธ๋“œ์— ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ทธ ์ธ์ ‘ ๋…ธ๋“œ๋ฅผ ์Šคํƒ์— ์‚ฝ์ž… ํ›„ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ, ๋งŒ์•ฝ ์ธ์ ‘ ๋…ธ๋“œ๊ฐ€ ๋ชจ๋‘ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ๋˜๋ฉด ์Šคํƒ ๋‚ด ์ตœ์ƒ๋‹จ ๋…ธ๋“œ ๊บผ๋ƒ„
  3. 2๋ฒˆ ๊ณผ์ • ๋๊นŒ์ง€ ๋ฐ˜๋ณต

> BFS(๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰) ์•Œ๊ณ ๋ฆฌ์ฆ˜

๊ทธ๋ž˜ํ”„์—์„œ ๊ฐ€๊นŒ์šด ๋…ธ๋“œ๋ถ€ํ„ฐ ์šฐ์„ ์ ์œผ๋กœ ํƒ์ƒ‰

  • ํ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•˜๋ฉฐ ํŠน์ • ์กฐ๊ฑด์—์„œ์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•  ๋•Œ ์‚ฌ์šฉ
  1. ํƒ์ƒ‰ ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ํ์— ์‚ฝ์ž…ํ•˜๊ณ  ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
  2. ํ์—์„œ ๋…ธ๋“œ๋ฅผ ๊บผ๋‚ธ ๋’ค์— ํ•ด๋‹น ๋…ธ๋“œ์˜ ์ธ์ ‘ ๋…ธ๋“œ ์ค‘์—์„œ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ํ์— ์‚ฝ์ž…ํ•˜๊ณ  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
  3. 2๋ฒˆ ๊ณผ์ • ๋๊นŒ์ง€ ๋ฐ˜๋ณต

BFS์™€ DFS๊ฐ€ ํ—ท๊ฐˆ๋ ค์„œ ๋น„๊ตํ•ด๋ณด์•˜๋‹ค.

- BFS : ์ •์ ๊ณผ ๊ฐ™์€ ๋ ˆ๋ฒจ์— ์žˆ๋Š” ๋…ธ๋“œ๋“ค์„ ๋จผ์ € ํƒ์ƒ‰

- DFS : ์ •์ ์˜ ์ž์‹๋“ค์„ ๋จผ์ € ํƒ์ƒ‰

< BFS >

from collections import deque

# BFS ๋ฉ”์„œ๋“œ ์ •์˜
def bfs(graph, start, visited):
	# ํ(Queue) ๊ตฌํ˜„์„ ์œ„ํ•ด deque ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ
    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

# ๊ฐ ๋…ธ๋“œ๊ฐ€ ์—ฐ๊ฒฐ๋œ ์ •๋ณด๋ฅผ ํ‘œํ˜„ (2์ฐจ์› ๋ฆฌ์ŠคํŠธ)
graph = [
	[],
    [2, 3, 8],
    [1, 7],
    [1, 4, 5],
    [3, 5],
    [3, 4],
    [7],
    [2, 6, 8],
    [1, 7]
]

# ๊ฐ ๋…ธ๋“œ๊ฐ€ ๋ฐฉ๋ฌธ๋œ ์ •๋ณด๋ฅผ ํ‘œํ˜„ (1์ฐจ์› ๋ฆฌ์ŠคํŠธ)
visited=[False]*9

# ์ •์˜๋œ BFS ํ•จ์ˆ˜ ํ˜ธ์ถœ
bfs(graph, 1, visited)

< DFS >

# DFS ๋ฉ”์„œ๋“œ ์ •์˜
def dfs(graph,v,visited):
  #ํ˜„์žฌ ๋…ธ๋“œ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
  visited[v]=[True]
  print(v, end=' ')
  # ํ˜„์žฌ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๋‹ค๋ฅธ ๋…ธ๋“œ๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ๋ฐฉ๋ฌธ
  for i in graph[v]:
    if not visited[i]:
      dfs(graph,i,visited)

# ๊ฐ ๋…ธ๋“œ๊ฐ€ ์—ฐ๊ฒฐ๋œ ์ •๋ณด๋ฅผ ํ‘œํ˜„ (2์ฐจ์› ๋ฆฌ์ŠคํŠธ)
graph = [
	[],
    [2, 3, 8],
    [1, 7],
    [1, 4, 5],
    [3, 5],
    [3, 4],
    [7],
    [2, 6, 8],
    [1, 7]
]

# ๊ฐ ๋…ธ๋“œ๊ฐ€ ๋ฐฉ๋ฌธ๋œ ์ •๋ณด๋ฅผ ํ‘œํ˜„ (1์ฐจ์› ๋ฆฌ์ŠคํŠธ)
visited=[False]*9

# ์ •์˜๋œ DFS ํ•จ์ˆ˜ ํ˜ธ์ถœ
dfs(graph, 1, visited)

> ๋ฌธ์ œํ’€์ด

DFS์™€ BFS๋Š” ํŠธ๋ฆฌ๋‚˜ ๊ทธ๋ž˜ํ”„ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ์„œ๋กœ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋ฅผ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ“Œ ํ•˜์ง€๋งŒ ์ด ๋ฌธ์ œ์—์„œ๋Š” ์ธ์ ‘ ํ–‰๋ ฌ ๋ฐฉ์‹์œผ๋กœ ํ–‰๊ณผ ์—ด์„ ํ†ตํ•ด์„œ ๊ฐ’์„ ํ•ด๋‹น ์ˆซ์ž๋“ค์ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์–ด์•ผ ํ•œ๋‹ค.
์ธ๋ฑ์Šค์™€ ๊ฐ’์„ ์ผ์น˜์‹œํ‚ค๊ธฐ ์œ„ํ•ด (n+1) * (n+1)์˜ ํ–‰๋ ฌ์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค๊ณ  0์„ ์ฑ„์šด๋‹ค.
์—ฐ๊ฒฐ๋œ ์ˆซ์ž๋“ค ๊ฐ’์„ m๊ฐœ ์ž…๋ ฅ ๋ฐ›์•„์„œ ํ•ด๋‹น ํ–‰๊ณผ ์—ด์„ ์—ฐ๊ฒฐ์‹œ์ผœ์•ผ ํ•˜๋ฏ€๋กœ 1๋กœ ๋ฐ”๊พผ๋‹ค.
์ฆ‰, ํ–‰์˜ ์ˆซ์ž์™€ ์—ด์˜ ์ˆซ์ž๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์Œ์„ ํ‘œ์‹œํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

from collections import deque

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

graph = [[0] * (n+1) for _ in range(n+1)]

for _ in range(m):
    m1, m2 = map(int, input().split())
    # ๋…ธ๋“œ ์—ฐ๊ฒฐํ•˜๊ธฐ
    graph[m1][m2] = graph[m2][m1] = 1

## ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ (BFS)
def bfs(start_v):
    visited = [start_v]
    # ๋ฆฌ์ŠคํŠธ๋ฅผ ์จ์„œ pop(0)ํ•˜๋ฉด ์‹œ๊ฐ„ ๋ณต์žก๋„ O(N)
    # deque๋ฅผ ์จ์„œ popleft()ํ•˜๋ฉด ์‹œ๊ฐ„ ๋ณต์žก๋„ O(1)
    queue = deque()
    queue.append(start_v)

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

        for w in range(len(graph[start_v])):
            if graph[v][w] == 1 and (w not in visited):
                visited.append(w)
                queue.append(w)


## ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ (DFS)
def dfs(start_v, visited=[]):
    visited.append(start_v)
    print(start_v, end=' ')

    for w in range(len(graph[start_v])):
        if graph[start_v][w] == 1 and (w not in visited):
            dfs(w, visited)

dfs(v)
print()
bfs(v)
profile
DBA, ๊ฒฝ์ œ ๊ทธ๋ฆฌ๊ณ  ๊ณ ๋ƒฅ์ด
post-custom-banner

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