Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 1260๋ฒˆ - DFS์™€ BFS

๊ฐ์ž ๐Ÿฅ” 2022. 5. 14. 22:36
๋ฐ˜์‘ํ˜•

 

๋ฌธ์ œ ๋งํฌ

https://www.acmicpc.net/problem/1260

 

1260๋ฒˆ: DFS์™€ BFS

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

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

https://didu-story.tistory.com/98

 

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ DFS, BFS

-- ๋ณธ ํฌ์ŠคํŒ…์€ ์ด๊ฒƒ์ด ์ทจ์—…์„ ์œ„ํ•œ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋‹ค with ํŒŒ์ด์ฌ ์ฑ…์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 1. DFS Depth First Search, ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ ๊ทธ๋ž˜ํ”„์˜ ๊นŠ์€ ๋ถ€๋ถ„์„ ์šฐ์„ ์ ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฃผ๋กœ ์Šค

didu-story.tistory.com

  • dfs์™€ bfs ์ฝ”๋“œ๋ฅผ ๋”ฐ๋กœ function์œผ๋กœ ๊ตฌ์„ฑํ•ด์„œ ํ’€์—ˆ๋‹ค.
  • visited์— True False ๋กœ ๋…ธ๋“œ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ–ˆ๋‹ค. ๋‹จ, DFS๋จผ์ € ์ถœ๋ ฅํ•˜๊ณ , BFS๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— BFS์—์„œ๋Š” ๋ฐฉ๋ฌธํ•œ๋…ธ๋“œ๊ฐ€ true๋กœ ๋ฐ”๋€Œ์–ด์ ธ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๊ฑธ false๋กœ ๋ฐ”๊พธ๋ฉด์„œ ํ’€์—ˆ๋‹ค.

 

โœ”๏ธ DFS ๊ตฌํ˜„

func dfs(_ v: Int) {
    visited[v] = true
    print(v, terminator: " ")
    for i in 1..<n+1 {
        if visited[i] == false && matrix[v][i] == 1 {
            dfs(i)
        }
    }
}
  • v ๋Š” start ๋…ธ๋“œ์ด๋‹ค. ๊ทธ๋ž˜์„œ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•˜์ž๋งˆ์ž ๋…ธ๋“œ v ๋Š” ๋ฐ”๋กœ ๋ฐฉ๋ฌธ์ฒดํฌ๋ฅผ ํ•ด์ค€๋‹ค. 
  • ๋ฌธ์ œ์—์„œ๋Š” ํƒ์ƒ‰์„ ํ•˜๋ฉด์„œ, ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋“ค์„ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•ด์•ผํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ๋ฐ”๋กœ v๋ฅผ ์ถœ๋ ฅํ•ด์ค€๋‹ค.
  • print์—์„œ terminator๋Š” ์ค„๋ฐ”๊ฟˆ์„ ํ•˜์ง€ ์•Š๊ณ , "" ์•ˆ์— ์žˆ๋Š” ๋ฌธ์ž๋ฅผ ํ†ตํ•ด ์—ฐ๊ฒฐ์‹œ์ผœ์„œ(?) ์ถœ๋ ฅํ•ด์ค€๋‹ค.
  • ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด์„œ dfs ์žฌ๊ท€๋ฅผ ํ™œ์šฉํ•  ๊ฒƒ์ด๋‹ค. (๋‚ด๊ฐ€ ๊ทธ๋ž˜ํ”„๋ฅผ ์–ด๋–ป๊ฒŒ ๊ทธ๋ ธ๋Š๋ƒ์— ๋”ฐ๋ผ ํ•จ์ˆ˜๊ฐ€ ๋‹ฌ๋ผ์งˆ ๊ฒƒ์ด๋‹ค)
    • 1๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๋ฐฉ๋ฌธ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜๋ฉด์„œ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆฐ๋‹ค.
    • ๋งŒ์•ฝ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๊ณ , ๊ทผ๋ฐ ๊ทธ๊ฒŒ ์‹œ์ž‘๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š” ๋…ธ๋“œ๋ผ๋ฉด, dfs์— ๋„ฃ์–ด์„œ ์žฌ๊ท€๋กœ ๋Œ๋ ค์ค€๋‹ค.

 

โœ”๏ธ BFS ๊ตฌํ˜„

func bfs(_ v: Int) {
    var queue: [Int] = []
    visited[v] = false // dfs ๋Œ์•„๊ฐ€๋ฉด์„œ true ๋กœ ๋ฐ”๋€๊ฑฐ false ๋กœ ๋Œ๋ฆฌ๋ฉด์„œ go
    queue.append(v)
    
    while !queue.isEmpty {
        var newV = queue.removeFirst()
        print(newV, terminator: " ")
        
        for i in 1..<n+1 {
            if visited[i] == true && matrix[newV][i] == 1 {
                queue.append(i)
                visited[i] = false
            }
        }
        
    }
    
}
  • ์ฃผ์˜ ) ์ด ํ•จ์ˆ˜์—์„œ๋Š” ๋ฐฉ๋ฌธํ–ˆ๋‹ค๋Š”๊ฒŒ false, ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๊ฐ€ true 
  • ๋จผ์ € queue๋ฅผ ๊ตฌ์„ฑํ•ด์ฃผ์—ˆ๋‹ค. queue๋Š” ์›๋ž˜ ํŒŒ์ด์ฌ์—์„œ dequeue๋กœ ๋งŒ๋“ค์–ด์ฃผ๋ฉด์„œ,  pop ํ•˜๋ฉด์„œ ์‰ฝ๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฝ‘์„ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์Šค์œ„ํ”„ํŠธ๋Š” dequeue ๊ธฐ๋Šฅ๊ณผ pop ๊ธฐ๋Šฅ์ด ๋”ฐ๋กœ ์—†์–ด์„œ, removeFirst() ๋กœ ํ•ด์•ผํ•œ๋‹ค. 
    • ํ•˜์ง€๋งŒ removeFirst๋Š” O(n) ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค. ๊ทธ๋ž˜์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜๋„ ์žˆ๋‹ค. (ํ•ด๋‹น ๋ฌธ์ œ์—์„œ๋Š” ๋ฐœ์ƒํ•˜์ง€ ์•Š์•˜์ง€๋งŒ, ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๊ฝค๋‚˜ ํฌ๊ธฐ ๋•Œ๋ฌธ์— ์กฐ์‹ฌํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.)
  • ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ์ด๋ฉด์„œ, ์ง€๊ธˆ ์ณ๋‹ค๋ณด๊ณ ์žˆ๋Š” ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š” ๋…ธ๋“œ๋ผ๋ฉด ํƒ์ƒ‰ ๋Œ€์ƒ์ด๊ธฐ ๋•Œ๋ฌธ์— queue์— ๋„ฃ์–ด์ค€๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ๋ฐฉ๋ฌธ์ฒดํฌ๋ฅผ ๋‹ค์‹œ ํ•ด์ฃผ๊ณ ,,, ๋ฐ˜๋ณตํ•œ๋‹ค.

 

๐Ÿ’ป ์ „์ฒด์ฝ”๋“œ

https://github.com/deslog/Algorithm/blob/main/Algorithm/Boj/1260_DFS%EC%99%80%20BFS/main.swift

 

GitHub - deslog/Algorithm

Contribute to deslog/Algorithm development by creating an account on GitHub.

github.com

var nmv = readLine()!.split(separator: " ").map { Int(String($0))! }
let n = nmv[0]
let m = nmv[1]
let v = nmv[2]

var visited = Array(repeating: false, count: n+1)
var matrix = Array(repeating: Array(repeating: 0, count: n+1), count: n+1)

for i in 0..<m {
    let nums = readLine()!.split(separator: " ").map{ Int(String($0))! }
    matrix[nums[0]][nums[1]] = 1
    matrix[nums[1]][nums[0]] = 1
}

func dfs(_ v: Int) {
    visited[v] = true
    print(v, terminator: " ")
    for i in 1..<n+1 {
        if visited[i] == false && matrix[v][i] == 1 {
            dfs(i)
        }
    }
}


func bfs(_ v: Int) {
    var queue: [Int] = []
    visited[v] = false // dfs ๋Œ์•„๊ฐ€๋ฉด์„œ true ๋กœ ๋ฐ”๋€๊ฑฐ false ๋กœ ๋Œ๋ฆฌ๋ฉด์„œ go
    queue.append(v)
    
    while !queue.isEmpty {
        var newV = queue.removeFirst()
        print(newV, terminator: " ")
        
        for i in 1..<n+1 {
            if visited[i] == true && matrix[newV][i] == 1 {
                queue.append(i)
                visited[i] = false
            }
        }
        
    }
    
}

dfs(v)
print("")
bfs(v)
๋ฐ˜์‘ํ˜•