๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ–ฅ Computer Science/Algorithm

[Algorithm] ๋„ˆ๋น„์šฐ์„ ํƒ์ƒ‰(BFS,Breadth-First-Search)๋ž€?(feat.Swift)

by Fomagran ๐Ÿ’ป 2021. 6. 27.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ๐Ÿ‘Ÿ ์ž…๋‹ˆ๋‹ค!

์˜ค๋Š˜์€ ํŠธ๋ฆฌ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š”๋ฐ ์“ฐ์ด๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ ๋„ˆ๋น„์šฐ์„ ํƒ์ƒ‰(BFS)์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


BFS๋ž€?

 

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰์€ ๋งน๋ชฉ์  ํƒ์ƒ‰๋ฐฉ๋ฒ•์˜ ํ•˜๋‚˜๋กœ ์‹œ์ž‘ ์ •์ ์„ ๋ฐฉ๋ฌธํ•œ ํ›„ ์‹œ์ž‘ ์ •์ ์— ์ธ์ ‘ํ•œ ๋ชจ๋“  ์ •์ ๋“ค์„ ์šฐ์„  ๋ฐฉ๋ฌธํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๋” ์ด์ƒ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ •์ ์ด ์—†์„ ๋•Œ๊นŒ์ง€ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋ชจ๋“  ์ •์ ๋“ค์— ๋Œ€ํ•ด์„œ๋„ ๋„ˆ๋น„ ์šฐ์„  ๊ฒ€์ƒ‰์„ ์ ์šฉํ•œ๋‹ค. ํ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ๋งŒ ๋ ˆ๋ฒจ ์ˆœ์„œ๋Œ€๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. - ์œ„ํ‚ค ๋ฐฑ๊ณผ -

 

์‰ฝ๊ฒŒ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด์„œ ์„ค๋ช…๋“œ๋ฆด๊ฒŒ์š”!

 

์•„๋ž˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ์ง€๊ตฌ ์•ˆ์— ์„ธ ๋‚˜๋ผ๋งŒ ์žˆ๊ณ  ๋ชจ๋“  ๋‚˜๋ผ์™€ ๋‚˜๋ผ์˜ ๋„์‹œ๋“ค์„ ํƒ์ƒ‰ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ๊ฒ ์ง€๋งŒ

 

๋จผ์ € ์ง€๊ตฌ์— ์žˆ๋Š” ๋‚˜๋ผ๋ฅผ ์™ผ์ชฝ๋ถ€ํ„ฐ ์–ด๋–ค ๋„์‹œ๋“ค์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ๋„˜์–ด๊ฐ€๋Š” ๋ฐฉ๋ฒ•์€ DFS์ž…๋‹ˆ๋‹ค.

 

(ํ˜น์‹œ DFS๋ฅผ ๋ชจ๋ฅด์‹œ๋Š” ๋ถ„๋“ค์€ ์—ฌ๊ธฐ ์—์„œ ํ™•์ธํ•ด์ฃผ์„ธ์š”!)

 

BFS๋Š” ๋จผ์ € ์ง€๊ตฌ์—์„œ ์‹œ์ž‘ํ•ด์„œ ๋„ˆ๋น„(๊ฐ€๋กœ)๋ถ€ํ„ฐ ์šฐ์„ ์ ์œผ๋กœ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.

 

์ง€๊ตฌ -> ํ•œ๊ตญ -> ๋ฏธ๊ตญ -> ์˜๊ตญ ... ์ˆœ์œผ๋กœ ํƒ์ƒ‰์„ ํ•˜๋Š” ๊ฒƒ์ด์ฃ !

 

์ฆ‰, ๋ฃจํŠธ  ๋…ธ๋“œ์—์„œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด ์ž์‹ ๊ณผ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜๊ณ  ๊ทธ ์•„๋ž˜ ๋ ˆ๋ฒจ๋กœ ๊ฐ€๋Š” ํƒ์ƒ‰ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

 

 

 

 

ํƒ์ƒ‰ํ•˜๋Š” ์ˆœ์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค.

 

์ถœ์ฒ˜: ์œ„ํ‚ค๋ฐฑ๊ณผ


์ฝ”๋“œ ๊ตฌํ˜„

 

์ด์ œ ์ฝ”๋“œ๋กœ ํ™•์ธํ•ด๋ณผ๊นŒ์š”?

 

๋จผ์ € ์ •์ ์˜ ๊ฐฏ์ˆ˜๋ฅผ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

 

 

let n:Int = 13

 

๊ฐ ์ •์ ์— ๋Œ€ํ•œ ์ด๋ฆ„์„ ์ˆœ์„œ๋Œ€๋กœ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

 

 

let world = ["์ง€๊ตฌ","ํ•œ๊ตญ","๋ฏธ๊ตญ","์˜๊ตญ","์„œ์šธ","๋ถ€์‚ฐ","๊ด‘์ฃผ","๋‰ด์š•","LA","์‹œ์นด๊ณ ","๋Ÿฐ๋˜","๋งจ์ฒด์Šคํ„ฐ","๋ฆฌ๋ฒ„ํ’€"]

 

์ง€๊ตฌ = 0 ,

ํ•œ๊ตญ = 1, ๋ฏธ๊ตญ = 2, ์˜๊ตญ = 3,

์„œ์šธ = 4, ๋ถ€์‚ฐ = 5, ๊ด‘์ฃผ = 6,

๋‰ด์š• = 7, LA = 8, ์‹œ์นด๊ณ  = 9,

๋Ÿฐ๋˜ = 10, ๋งจ์ฒด์Šคํ„ฐ = 11, ๋ฆฌ๋ฒ„ํ’€ = 12 

 

๋กœ ํ•ด๋‹น ์ด๋ฆ„ ์ธ๋ฑ์Šค๋ฅผ ์—ฐ๊ฒฐ๋กœ ์„ธํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

 

๋ฌด์ž‘์œ„๋กœ ์ •์ ์ด ์—ฐ๊ฒฐ๋œ ์ •๋ณด๋“ค์„ ์„ธํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

 

let allConnections:[[Int]] = [[0,1],[2,0],[0,3],[4,1],[5,1],[1,6],[2,7],[2,8],[9,2],[3,10],[3,11],[12,3]]

 

์ •์ ์— ๋Œ€ํ•œ ์—ฐ๊ฒฐ์„ ๋ฐฉ๋ฌธํ–ˆ๋Š”์ง€๋ฅผ ํ‘œ์‹œํ•  visited ์ด์ค‘๋ฐฐ์—ด๊ณผ

 

์ •์ ์˜ ์ˆœ์„œ๋“ค์„ ๋‹ด์„ ํ๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. (์‹œ์ž‘ ์ •์ ์ธ 0์„  ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.)

 

var visited:[[Bool]] = Array(repeating: Array(repeating: false, count: n), count: n)
var queue:[Int] = [0]

 

๋ณธ๊ฒฉ์ ์œผ๋กœ BFS๋ฅผ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ ๊ฐ€์žฅ ํ•ต์‹ฌ์€ while๋ฌธ์„ ์ด์šฉํ•ด์„œ ํ๊ฐ€ ์•„๋ฌด๊ฒƒ๋„ ์—†์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

๋จผ์ € ๊ฐ€์žฅ ํ์— ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ ์žˆ๋Š” ์ˆซ์ž๋ฅผ ๊บผ๋‚ด์˜ต๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณค ๋ชจ๋“  ์—ฐ๊ฒฐ ์ค‘์—์„œ ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž์™€ ์—ฐ๊ฒฐ๋œ ์ˆซ์ž๋ฅผ ํ์— ๋‹ด๋Š”๋ฐ 

 

๋งŒ์•ฝ ๋ถ€๋ชจ ๋…ธ๋“œ์ด๊ฑฐ๋‚˜ ์ธ์ ‘ํ•œ ๋…ธ๋“œ์ผ ๊ฒฝ์šฐ ๋ฌดํ•œ ๋ฃจํ”„๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

๊ณ ๋กœ visited์— ๊ผญ ์ด ํ˜„์žฌ first์™€ ์ •์  ๊ฐ„์— ์ด๋ฏธ ๋ฐฉ๋ฌธํ–ˆ๋Š”์ง€๋ฅผ ์ฒดํฌํ•˜๊ณ  ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ํ์— ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

 

func searchByBFS(queue:inout [Int],visited:inout [[Bool]]) {
    while !queue.isEmpty {
        let first = queue.removeFirst()
        print(world[first])
        allConnections.forEach{
            if $0[0] == first && !visited[$0[1]][first] {
               visited[$0[1]][first] = true
               visited[first][$0[1]] = true
                queue.append($0[1])
            }
            
            if $0[1] == first && !visited[$0[0]][first] {
               visited[$0[0]][first] = true
               visited[first][$0[0]] = true
                queue.append($0[0])
            }
        }
    }
}

 

์ด๋ ‡๊ฒŒ ์‹คํ–‰ํ•˜๊ณ  ์ถœ๋ ฅํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๋„ˆ๋น„ ์šฐ์„ ์œผ๋กœ ํƒ์ƒ‰๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

 

searchByBFS(queue: &queue, visited: &visited)

//์ง€๊ตฌ
//ํ•œ๊ตญ
//๋ฏธ๊ตญ
//์˜๊ตญ
//์„œ์šธ
//๋ถ€์‚ฐ
//๊ด‘์ฃผ
//๋‰ด์š•
//LA
//์‹œ์นด๊ณ 
//๋Ÿฐ๋˜
//๋งจ์ฒด์Šคํ„ฐ
//๋ฆฌ๋ฒ„ํ’€

์ „์ฒด ์†Œ์Šค ์ฝ”๋“œ

 

 

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€