๊ด€๋ฆฌ ๋ฉ”๋‰ด

DOing

[์•Œ๊ณ ๋ฆฌ์ฆ˜] BFS with Python ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜

[์•Œ๊ณ ๋ฆฌ์ฆ˜] BFS with Python

mangdo 2021. 5. 22. 13:34

๐Ÿ’ก BFS ์•Œ๊ณ ๋ฆฌ์ฆ˜

Breath-First Search, ๊ทธ๋ž˜ํ”„์—์„œ ๊ฐ€๊นŒ์šด ๋ถ€๋ถ„์„ ์šฐ์„ ์ ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰์ด๋ผ๊ณ ๋„ ํ•œ๋‹ค.

๋ฐ์ดํ„ฐ๊ฐ€ N๊ฐœ ์žˆ์„๋•Œ O(N)์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค. ์ผ๋ฐ˜์ ์ธ ๊ฒฝ์šฐ ์‹ค์ œ ์ˆ˜ํ–‰์‹œ๊ฐ„์€ DFS๋ณด๋‹ค ์ข‹์€ํŽธ์ด๋‹ค.

 

[DFS vs BFS]

  ๋™์ž‘ ๋ฐฉ์‹ ๋™์ž‘ ์›๋ฆฌ ๊ตฌํ˜„ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ๋ผ๋ฉด?
DFS ๋ฉ€๋ฆฌ ์žˆ๋Š”(๊นŠ๊ฒŒ ์žˆ๋Š”)๋…ธ๋“œ ๋ถ€ํ„ฐ ํƒ์ƒ‰ ์Šคํƒ ์žฌ๊ท€ ํ•จ์ˆ˜ ์ด์šฉ ํ•œ๊ฐœ์”ฉ ๋„ฃ์Œ
BFS ๊ฐ€๊นŒ์šด ๋…ธ๋“œ๋ถ€ํ„ฐ ํƒ์ƒ‰ ํ ํ ์ž๋ฃŒ๊ตฌ์กฐ ์ด์šฉ ํ•œ๋ฒˆ์— ๋‹ค ๋„ฃ์Œ

๐Ÿ’ก BFS ๋™์ž‘๊ณผ์ •

1. ํƒ์ƒ‰ ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ํ์— ์‚ฝ์ž…ํ•˜๊ณ  ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.

2. ํ์—์„œ ๋…ธ๋“œ๋ฅผ ๊บผ๋‚ด ํ•ด๋‹น ๋…ธ๋“œ์˜ ์ธ์ ‘๋…ธ๋“œ ์ค‘์—์„œ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ํ์— ์‚ฝ์ž…ํ•˜๊ณ  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค,

3. 2๋ฒˆ๊ณผ์ •์„ ๋”์ด์ƒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

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

1->(2->3->8)->(7->4->5)->6

์ž์„ธํžˆ ๋ณด๋ฉด (2, 3, 4)๋Š” ๋ชจ๋‘ ๊ฑฐ๋ฆฌ๊ฐ€ 1์ด๊ณ , (7, 4, 5) ๋ชจ๋‘ ๊ฑฐ๋ฆฌ๊ฐ€ 2์ด๊ณ , 6๋Š” ๊ฑฐ๋ฆฌ๊ฐ€ 3์ด๋‹ค. ์ฆ‰, ๊ฑฐ๋ฆฌ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ๋‹ค. ์ด๋Ÿฌํ•œ ํŠน์„ฑ์„ ์ด์šฉํ•ด์„œ ๊ฐ ๊ฐ„์„  ๋น„์šฉ์ด ๋ชจ๋‘ ๊ฐ™๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— ์ตœ๋‹จ๊ฑฐ๋ฆฌ ๋ฌธ์ œ์—๋„ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ’ก BFS ๊ตฌํ˜„ ์ฝ”๋“œ

from collections import deque

# BFS
def bfs(graph, start, visited):
  # ํ ๊ตฌํ˜„์„ ์œ„ํ•ด deque๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ
  queue = deque([start])

  # ํƒ์ƒ‰ ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
  visited[start] = True

  # ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
  while queue:
    # ํ์—์„œ ํ•˜๋‚˜์˜ ์›์†Œ๋ฅผ ๊บผ๋‚ด์„œ ์ถœ๋ ฅ
    n = queue.popleft()
    print(n, end='')

    # ๊บผ๋‚ธ ์›์†Œ์™€ ์ธ์ ‘๋…ธ๋“œ ํ™•์ธ
    for i in graph[n]:
      # ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๋ผ๋ฉด
      if not visited[i]:
        queue.append(i)
        visited[i]=True

# 2์ฐจ์› ๋งต์ •๋ณด ์ž…๋ ฅ๋ฐ›๊ธฐ
graph=[
  [], # 0๋ฒˆ ๋…ธ๋“œ ๋น„์šฐ๊ธฐ
  [2,3,8], #1๋ฒˆ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ 2,3,8๋…ธ๋“œ
  [1,7],
  [1,4,5],
  [3,5],
  [3,4],
  [7],
  [2,6,8],
  [1,7]
]

# ๋ฐฉ๋ฌธ ์ •๋ณด
visited = [False]*(8+1) #(์ด ๋…ธ๋“œ์˜ ๊ฐฏ์ˆ˜)+์ธ๋ฑ์Šค 0
# bfsํ˜ธ์ถœ
bfs(graph, 1, visited)

 

๐Ÿ’ก BFS ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ

Q1. ๋ฏธ๋กœ ํƒˆ์ถœ

 ๋™๋นˆ์ด๋Š” N*M ํฌ๊ธฐ์˜ ์ง์‚ฌ๊ฐํ˜• ํ˜•ํƒœ์˜ ๋ฏธ๋กœ์— ๊ฐ‡ํ˜€์žˆ๋‹ค. ๋ฏธ๋กœ์—๋Š” ์—ฌ๋Ÿฌ ๋งˆ๋ฆฌ์˜ ๊ดด๋ฌผ์ด ์žˆ์–ด ์ด๋ฅผ ํ”ผํ•ด ํƒˆ์ถœํ•ด์•ผํ•œ๋‹ค. ๋™๋นˆ์ด์˜ ์œ„์น˜๋Š” (1,1)์ด๊ณ  ๋ฏธ๋กœ์˜ ์ถœ๊ตฌ๋Š” (N,M)์˜ ์œ„์น˜์— ์กด์žฌํ•˜๋ฉฐ ํ•œ๋ฒˆ์— ํ•œ์นธ์”ฉ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ ๊ดด๋ฌผ์ด ์žˆ๋Š” ๋ถ€๋ถ„์€ 0์œผ๋กœ, ๊ดด๋ฌผ์ด ์—†๋Š” ๋ถ€๋ถ„์€ 1๋กœ ํ‘œ์‹œ๋˜์–ด์žˆ๋‹ค. ๋ฏธ๋กœ๋Š” ๋ฐ˜๋“œ์‹œ ํƒˆ์ถœํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ๋กœ ์ œ์‹œ๋œ๋‹ค. ์ด๋•Œ ๋™๋นˆ์ด๊ฐ€ ํƒˆ์ถœํ•˜๊ธฐ ์œ„ํ•ด ์›€์ง์—ฌ์•ผํ•˜๋Š” ์ตœ์†Œ ์นธ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜์‹œ์˜ค. ์นธ์„ ์…€๋•Œ๋Š” ์‹œ์ž‘ ์นธ๊ณผ ๋งˆ์ง€๋ง‰ ์นธ์„ ๋ชจ๋‘ ํฌํ•จํ•ด์„œ ๊ณ„์‚ฐํ•œ๋‹ค.

* ์ž…๋ ฅ ์กฐ๊ฑด

- ์ฒซ์งธ์ค„์— ๋‘ ์ •์ˆ˜ N,M(4<= N,M <= 200)์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ๊ฐ M์˜ ์ •์ˆ˜(0ํ˜น์€ 1)๋กœ ๋ฏธ๋กœ์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ๊ฐ์˜ ์ˆ˜๋“ค์€ ๊ณต๋ฐฑ ์—†์ด ๋ถ™์–ด์„œ ์ž…๋ ฅ์œผ๋กœ ์ œ์‹œ๋œ๋‹ค. ๋˜ํ•œ ์‹œ์ž‘ ์นธ๊ณผ ๋งˆ์ง€๋ง‰ ์นธ์€ ํ•ญ์ƒ 1์ด๋‹ค.

* ์ถœ๋ ฅ ์กฐ๊ฑด

- ์ฒซ์งธ์ค„์— ์ตœ์†Œ ์ด๋™ ์นธ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

์ž…๋ ฅ ์˜ˆ์‹œ ์ถœ๋ ฅ ์˜ˆ์‹œ
5 6
101010
111111
000001
111111
111111
10

 

[๋ฌธ์ œ ์•„์ด๋””์–ด]

BFS๋Š” ๊ฐ€๊นŒ์šด ๊ฑฐ๋ฆฌ์ˆœ์œผ๋กœ ํƒ์ƒ‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๋ฌธ์ œ์—๋„ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค.

๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ์˜ ๊ฐ’์„ ๊ฑฐ๋ฆฌ ์ •๋ณด๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ํ•œ๋ฒˆ๋„ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ณณ์„ 1, ๊ฐ€์ง€ ๋ชปํ•  ๊ณณ์€ 0, ์ด๋ฏธ ๊ฐ„ ๊ณณ์€ 1๋ณด๋‹ค ํฐ๊ฐ’์ด๋‹ค.

๋ฐฉ๋ฌธ ๋…ธ๋“œ์˜ ๊ฐ’์„ ๊ฑฐ๋ฆฌ ์ •๋ณด๋กœ ๋ณ€๊ฒฝ

[์ฝ”๋“œ]

๊ตฌํ˜„์‹œ, visited ๋ฆฌ์ŠคํŠธ๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค.

๋ฏธ๋กœ์˜ ์ •๋ณด๋ฅผ ๋‹ด์€ graph๋ฅผ ํ•œ๋ฒˆ๋„ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ณณ์„ 1, ๊ฐ€์ง€ ๋ชปํ•  ๊ณณ์€ 0์œผ๋กœ ์œ ์ง€ํ•˜๊ณ  ์ด๋ฏธ ๊ฐ„ ๊ณณ์€ 1๋ณด๋‹ค ํฐ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

from collections import deque

# n๊ฐœ์˜ ์ค„, m๊ฐœ์˜ ์—ด
n, m = map(int,input().split())

# ๋ฏธ๋กœ์˜ ์ •๋ณด ์ž…๋ ฅ๋ฐ›๊ธฐ
graph=[]
for i in range(n):
  graph.append(list(map(int,input())))

# ๋ฐฉํ–ฅ ์ •๋ณด(์ƒํ•˜์ขŒ์šฐ)
dx = [-1,1,0,0]
dy = [0,0,-1,1]

# ์ถœ๋ฐœ ๋…ธ๋“œ
queue = deque()
deque.append((0,0))
# ํ˜น์€ deque([(0,0)])

while queue :
  # ํ์—์„œ ํ•˜๋‚˜์˜ ์›์†Œ๋ฅผ ๊บผ๋‚ด์„œ ์ถœ๋ ฅ
  x, y = queue.popleft()

  # ๊บผ๋‚ธ ์›์†Œ์™€ ์ธ์ ‘๋…ธ๋“œ ํ™•์ธ
  for i in range(len(dx)):
    nx = x+dx[i]
    ny = y+dy[i]

    # ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ ๋…ธ๋“œ ๋ฌด์‹œ
    if nx>n-1 or ny>m-1 or nx<0 or ny<0 :
      continue

    # ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๋ผ๋ฉด
    # ์ฒ˜์Œ ๋ฐฉ๋ฌธํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐ”๋กœ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋‹ค
    if graph[nx][ny]==1:
      queue.append((nx,ny))
      graph[nx][ny] = graph[x][y]+1 # ์ง์ „ ๋…ธ๋“œ+1


print(graph[n-1][m-1])

 

 

์ถœ์ฒ˜ : 

์ด๊ฒƒ์ด ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋‹ค(์ด๋™๋นˆ ์ €)