TIL 004 DFS / BFS

์กฐ์„ฑํ˜„ยท2020๋…„ 12์›” 27์ผ
0

์ •๊ธ€

๋ชฉ๋ก ๋ณด๊ธฐ
5/21

๐Ÿ‘‰ ์‚ฌ์šฉ๋ชฉ์ 

DFS: ๋‹ค์ฐจ์› ๋ฐฐ์—ด์—์„œ ๊ฐ ์นธ์„ ๋ฐฉ๋ฌธํ• ๋•Œ ๊นŠ์ด๋ฅผ ์šฐ์„ ์œผ๋กœ ๋ฐฉ๋ฌธ, ํ˜„์žฌ ์ •์ ์—์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ณณ๊นŒ์ง€ ๊ฐ€๋ณด๋ฉฐ ํƒ์ƒ‰.

BFS: ๋‹ค์ฐจ์› ๋ฐฐ์—ด์—์„œ ๊ฐ ์นธ์„ ๋ฐฉ๋ฌธํ• ๋•Œ ๋„ˆ๋น„๋ฅผ ์šฐ์„ ์œผ๋กœ ๋ฐฉ๋ฌธ, ํ˜„์žฌ ์ •์ ์—์„œ ์—ฐ๊ฒฐ๋œ ์ฃผ๋ณ€์˜ ๊ฐ€๊นŒ์šด ๋…ธ๋“œ๋ถ€ํ„ฐ ํƒ์ƒ‰.


์ถœ์ฒ˜: https://namu.wiki/w/BFS

๐Ÿ‘‰ ๊ตฌํ˜„๋ฐฉ๋ฒ•

๊ธฐ๋ณธ์ ์ธ ํ˜•์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. visited array๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐฉ๋ฌธํ•œ ๊ณณ์€ ๋‹ค์‹œ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š๋„๋ก ํ•ด์•ผํ•œ๋‹ค.

#R = number of row 
#C = number of column

# ์ฃผ์–ด์ง„ array
A=[[0 for _ in range(C)] for _ in range(R)] 

# ๋ฐฉ๋ฌธํ•œ ๊ณณ์„ ํ‘œ๊ธฐํ•˜๊ธฐ ์œ„ํ•œ array
vis=[[0 for _ in range(C)] for _ in range(R)] 

# ํƒ์ƒ‰ ์ปค์„œ (์ƒํ•˜์ขŒ์šฐ)
dr = [1,0,-1,0]
dc = [0,-1,0,1]

DFS: ์Šคํƒ, ์žฌ๊ท€๋กœ ๊ตฌํ˜„

# ์Šคํƒ
while(stack):
    cur = stack.pop()
    for j in range(4)
       c = cur + j
       if vis[c] == 1: continue
       if ๋ฒ”์œ„ ๋ฐ– or ๊ฐˆ ์ˆ˜ ์—†๋Š” ๊ณณ : continue
       stack.append(c)
       vis[c] = 1

# ์žฌ๊ท€
def dfs(i):
    vis[i] = 1;
    for j in range(4)
       c = i + j
       if vis[c] == 1: continue
       if ๋ฒ”์œ„ ๋ฐ– or ๊ฐˆ ์ˆ˜ ์—†๋Š” ๊ณณ : continue
       dfs(c);

BFS: ํ๋กœ ๊ตฌํ˜„

# ํ
while(queue):
    cur = queue.popleft()
    for j in range(4)
       c = cur + j
       if vis[c] == 1: continue
       if ๋ฒ”์œ„ ๋ฐ– or ๊ฐˆ ์ˆ˜ ์—†๋Š” ๊ณณ : continue
       queue.append(c)
       vis[c] = 1

๐Ÿ‘‰ ์œ ํ˜•

ํ•œ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋˜ ์žˆ๋Š” ๋ชจ๋“  ๋…ธ๋“œ ํƒ์ƒ‰ํ•˜๋Š” ์œ ํ˜•์ด ์ „ํ˜•์ ์ด๋‹ค. ์ด๋•Œ๋Š” DFS, BFS ์•„๋ฌด๊ฑฐ๋‚˜ ์จ๋„ ๋˜์ง€๋งŒ, ํŠน๋ณ„ํ•œ ์œ ํ˜•๋“ค๋„ ์žˆ๋‹ค.

DFS

  1. ๊ฒฝ๋กœ ์ฐพ๊ธฐ -> ์—ฌ๋Ÿฌ ๊ฒฝ๋กœ๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ• ๋•Œ, ์žฌ๊ท€ ์‚ฌ์šฉ

    def dfs(i):
        for j in range(4)
           c = i + j
           if vis[c] == 1: continue
           if ๋ฒ”์œ„ ๋ฐ– or ๊ฐˆ ์ˆ˜ ์—†๋Š” ๊ณณ : continue
           vis[c] = 1
           dfs(c);
           vis[c] = 0      
  2. ์‚ฌ์ดํด ์ฐพ๊ธฐ -> ์‹œ์ž‘์ ์„ ๋”ฐ๋กœ ์ €์žฅํ•ด๋‘”๋‹ค.

    def dfs(i):
        vis[i] = 1;
        for j in range(4)
           c = i + j
           if vis[c] == 1 (์‹œ์ž‘์ ์ธ์ง€ ํ™•์ธ):
               return "cycle"
           dfs(c);
  3. ์œ„์ƒ์ •๋ ฌ -> ์ฐจ์ˆ˜๋ฅผ ์ €์žฅํ•˜๊ณ , ์ง„์ž… ์ฐจ์ˆ˜๊ฐ€ 0์ธ node ๋ถ€ํ„ฐ ๋„ฃ๋Š”๋‹ค.

     for i in range(N):
         if order[i] == 0:
             stack.append(i)
             ans.append(i)
     while(stack):
         cur = stack.pop()
         order[cur] = -1 # ๋ฐฉ๋ฌธํ–ˆ๋‹ค๋Š” ํ‘œ์‹œ
         for i in range(len(A[cur])):
             order[A[cur][i][0]] -= 1
             if order[i] == 0:
                stack.append(A[cur])

BFS

  1. ์ตœ๋‹จ๊ฑฐ๋ฆฌ -> BFS๋ฅผ ์“ฐ๋ฉด ์ฃผ๋ณ€์„ ๋จผ์ € ํƒ์ƒ‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋จผ์ € ํƒ์ƒ‰๋˜๋Š” ๊ฑฐ๋ฆฌ๊ฐ€ ์ž๋™์œผ๋กœ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋‹ค.

    # ํ
    while(queue):
        cur = queue.popleft()
        ๊ฑฐ๋ฆฌ = cur[1]
        for j in range(4)
           c = cur + j
           if vis[c] == 1: continue
           if ๋ฒ”์œ„ ๋ฐ– or ๊ฐˆ ์ˆ˜ ์—†๋Š” ๊ณณ : continue
           stack.append([c, ๊ฑฐ๋ฆฌ+1])
           vis[c] = 1
  2. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ -> while ๋ฌธ์„ ์ถ”๊ฐ€ํ•ด ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ํƒ์ƒ‰

        while(1):
            cursize = len(queue)
            while(cursize):
                cur = queue.popleft()
                for i in range(len(A)):
                    newcur = cur + A[i]
                    if newcur > K: continue
                    if vis[newcur]==True: continue
                    queue.append(newcur)
                    vis[newcur]=True
                cursize-=1
            cnt += 1

๐Ÿ‘‰ ํŒ

  1. ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ ์กฐ๊ฑด์„ ์ž˜ ์ •๋ฆฌํ•œ๋‹ค.
  2. BFS,DFS๋ฅผ ๋จผ์ € ์กฐ๊ฑด์— ๋งž๋Š”์ง€ ๊ตฌํ˜„ํ•œ ๋‹ค์Œ, ์–ด๋–ค ๋…ธ๋“œ๊ฐ€ append ๋  ์ง€ outer shell์„ ๊ตฌํ˜„ํ•œ๋‹ค.
  3. ์ฃผ์–ด์ง„ ์กฐ๊ฑด์— ๋”ฐ๋ผ ์žฌ๊ท€๋ฅผ ์“ธ ๊ฒƒ์ธ์ง€ ์Šคํƒ, ํ๋ฅผ ์“ธ ๊ฒƒ์ธ์ง€ ๊ฒฐ์ •ํ•œ๋‹ค.

๐Ÿ‘‰ ๋งํฌ

๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰
https://devuna.tistory.com/32

์ฝ”๋”ฉ ๋ฌธ์ œ ์œ ํ˜• ์ •๋ฆฌ
https://myeongmy.tistory.com/55

BFS,DFS ๊ตฌํ˜„
https://blog.encrypted.gg/729

์œ„์ƒ์ •๋ ฌ
https://m.blog.naver.com/ndb796/221236874984

profile
Jazzing๐Ÿ‘จโ€๐Ÿ’ป

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