๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 1260_DFS์™€ BFS

๋ฟŒ์•ผ._. 2022. 1. 6. 22:50

Silver II

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/1260)

<DFS์™€ BFS>

๋ฌธ์ œ 

 

๊ทธ๋ž˜ํ”„๋ฅผ DFS๋กœ ํƒ์ƒ‰ํ•œ ๊ฒฐ๊ณผ์™€ BFS๋กœ ํƒ์ƒ‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋‹จ, ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ •์ ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” ์ •์  ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ๊ฒƒ์„ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๊ณ , ๋” ์ด์ƒ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ ์ด ์—†๋Š” ๊ฒฝ์šฐ ์ข…๋ฃŒํ•œ๋‹ค. ์ •์  ๋ฒˆํ˜ธ๋Š” 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€์ด๋‹ค.

 

 

์ž…๋ ฅ

 

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 1,000), ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ M(1 ≤ M ≤ 10,000), ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•  ์ •์ ์˜ ๋ฒˆํ˜ธ V๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ M๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ„์„ ์ด ์—ฐ๊ฒฐํ•˜๋Š” ๋‘ ์ •์ ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์–ด๋–ค ๋‘ ์ •์  ์‚ฌ์ด์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ„์„ ์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ๊ฐ„์„ ์€ ์–‘๋ฐฉํ–ฅ์ด๋‹ค.

 

์ถœ๋ ฅ 

 

์ฒซ์งธ ์ค„์— DFS๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ, ๊ทธ๋‹ค์Œ ์ค„์—๋Š” BFS๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. V๋ถ€ํ„ฐ ๋ฐฉ๋ฌธ๋œ ์ ์„ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

 

 

๋ฌธ์ œ ํ’€์ด

   - ์‹œ๊ฐ„ ์ดˆ๊ณผ ์ฝ”๋“œ

#์‹œ๊ฐ„์ดˆ๊ณผ
import sys

if __name__=='__main__':
    N,M,V=map(int,sys.stdin.readline().split())

    arr={}
    check=[False]*N # ๋‹ค ๋ฐฉ๋ฌธํ–ˆ๋Š”์ง€ ํ™•์ธ
    for i in range(M):
        a,b=map(int,sys.stdin.readline().split())
        if a in arr:
            temp=arr.get(a)
            temp.append(b)
            arr[a]=temp
        else:
            arr[a]=[b]
        if b in arr:
            temp = arr.get(b)
            temp.append(a)
            arr[b]=temp
        else:
            arr[b]=[a]

    for key,value in arr.items():
        value=sorted(value)
        arr[key]=value


    # DFS - ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰
    X=V
    result=[X]
    check[X-1]=True
    while False in check:
        for i in arr.get(X):
            if check[i-1]==False:
                result.append(i)
                X=i
                check[i-1]=True
                break

    print(' '.join(map(str,result)))

    # BFS - ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰
    check=[False]*N
    result=[V]
    check[V-1] = True
    temp=[V]
    while False in check:
        V=temp.pop(0) # ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰์ด๋ฏ€๋กœ
        for i in arr.get(V):
            if check[i-1]==False:
                temp.append(i)
                result.append(i)
                check[i-1]=True

    print(' '.join(map(str,result)))

 

  • DFS์™€ BFS๋ฅผ ๊ตฌํ•˜๋Š”๋ฐ while๋ฌธ for๋ฌธ์„ ๋‹ค ์‚ฌ์šฉํ•œ Magic

ํ’€๋ฉด์„œ๋„ DFS, BFS ๊ตฌํ•˜๋Š”๋ฐ ์ฝ”๋“œ๊ฐ€ ์ด๋ ‡๊ฒŒ ๊ธด๊ฐ€ ํ•˜๋ฉด์„œ ๋ˆˆ๋ฌผ ๋‚ฌ๋˜ ๋ฌธ์ œ์ด๋‹ค.

๋‚ด๊ฐ€ DFS, BFS๋„ ๋ชจ๋ฅด๋Š”๊ฐ€? ๊ทธ๋Ÿด์ง€๋„.. ์ด๋ฒˆ ๊ธฐํšŒ๋ฅผ ํ†ตํ•ด ์ดํ•ดํ•œ ๊ฒƒ ๊ฐ™๋‹ค!

์ด ๋ฌธ์ œ๋Š” ๊ฐœ๋… ๋ฌธ์ œ๋ผ DFS, BFS ๊ฐœ๋…๋ถ€ํ„ฐ ์ฐพ์•„๋ดค๋‹ค. ์‚ฌ์‹ค ๊ฐœ๋… ์ฝ”๋“œ๋ฅผ ๋ณธ ๊ฒƒ์ด ์ด ๋ฌธ์ œ์˜ ์ „๋ถ€๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค..

 

 

- ํ†ต๊ณผํ•œ ์ฝ”๋“œ

import sys

def dfs(arr, start): # ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰
    visit=[]
    temp=[start]

    while temp:
        x=temp.pop(0)
        if x not in visit: # ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด
            visit.append(x)
            temp=arr.get(x)+temp
    return visit

def bfs(arr, start): # ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰
    visit=[]
    temp=[start]

    while temp:
        x=temp.pop(0)
        if x not in visit: # ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด
            visit.append(x)
            temp+=arr.get(x)
    return visit

if __name__=='__main__':
    N,M,V=map(int,sys.stdin.readline().split())

    arr={}
    for i in range(N): #init
        arr[i+1]=[]

    for i in range(M): # ์–‘๋ฐฉํ–ฅ ๊ฐ„์„ 
        a,b=map(int,sys.stdin.readline().split())
        arr[a].append(b)
        arr[b].append(a)

    for key,value in arr.items(): #์ž‘์€ ๊ฒƒ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๊ธฐ ์œ„ํ•ด ์ •๋ ฌ
        value=sorted(value)
        arr[key]=value

    print(' '.join(map(str, dfs(arr,V))))
    print(' '.join(map(str, bfs(arr, V))))

 

  • dfs
    - visit list ์„ ์–ธ, temp list์— ์‹œ์ž‘ํ•  ์ •์  ๋ฒˆํ˜ธ ๋„ฃ์–ด์„œ ์„ ์–ธ
    - temp list๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต: temp์—์„œ ๋งจ ์•ž ๊ฐ’์„ ๋ฝ‘์•„์™€ ์•„์ง ๋ฐฉ๋ฌธํ•œ ์ •์ ์ด ์•„๋‹ˆ๋ผ๋ฉด visit์— ์ถ”๊ฐ€ํ•˜๊ณ  temp list ์•ž์— ๊ทธ ์ •์ ๊ณผ ์—ฐ๊ฒฐ๋œ ์ •์ ๋“ค์„ ์ถ”๊ฐ€
  • bfs
    - visit list ์„ ์–ธ, temp list์— ์‹œ์ž‘ํ•  ์ •์  ๋ฒˆํ˜ธ ๋„ฃ์–ด์„œ ์„ ์–ธ
    - temp list๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต: temp์—์„œ ๋งจ ์•ž ๊ฐ’์„ ๋ฝ‘์•„์™€ ์•„์ง ๋ฐฉ๋ฌธํ•œ ์ •์ ์ด ์•„๋‹ˆ๋ผ๋ฉด visit์— ์ถ”๊ฐ€ํ•˜๊ณ  temp list์— ๊ทธ ์ •์ ๊ณผ ์—ฐ๊ฒฐ๋œ ์ •์ ๋“ค์„ ์ถ”๊ฐ€
  • main
    - ์ •์  ์ˆ˜, ๊ฐ„์„  ์ˆ˜, ์‹œ์ž‘ ์ •์  ์ž…๋ ฅ
    - ์ •์ ๊ณผ ์—ฐ๊ฒฐ๋œ ์ •์ ์„ dict๋กœ ํ‘œํ˜„ -> key ๊ฐ’๋“ค ๋‹ค ์„ ์–ธ
    - ์–‘๋ฐฉํ–ฅ ๊ฐ„์„  ์ด๋ฏ€๋กœ ์–‘์ชฝ key์— value ์ถ”๊ฐ€
    - ์ •์  ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ๊ฒƒ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๊ธฐ ์œ„ํ•ด ์ •๋ ฌ
    - dfs, bfs ํ˜ธ์ถœ

์ƒ๊ฐ๐Ÿค”

 

dfs์™€ bfs๋ฅผ ์ดํ•ดํ•˜๊ณ  ๋‚˜๋‹ˆ ๋ฌธ์ œ๋ฅผ ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค. ์•„.. ์•„๋‹Œ๊ฐ€ dfs ์ฝ”๋“œ๊ฐ€ ์ž˜๋ชป๋œ ๊ฑฐ ๊ฐ™๊ธฐ๋„ ํ•˜๊ณ  ์•„๋‹Œ ๊ฑฐ ๊ฐ™๊ธฐ๋„ ํ•˜๊ณ  ๋ชจ๋ฅด๊ฒ ๋‹ค ใ…œ_ใ…œ

์ด์ œ ๊ฒ๋จน์ง€ ๋ง๊ณ  dfs์™€ bfs ๊ด€๋ จ๋œ ๋ฌธ์ œ๋ฅผ ๋„์ „ํ•ด๋ด์•ผ๊ฒ ๋‹ค ๐Ÿ˜‰