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

[Algorithm] ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS,Depth-First-Search)์ด๋ž€? (feat.Swift)

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

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

 

์˜ค๋Š˜์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ์—์„œ ์—„์ฒญ ๋นˆ๋ฒˆํ•˜๊ฒŒ ์ถœ์ œ๋˜๊ณ  ์•Œ์•„๋‘๋ฉด ์ •๋ง ์œ ์šฉํ•œ DFS์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

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


DFS๋ž€?

 

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

 

์ด๊ฒŒ ๋ญ”๋ง์ด์•ผ....;;;

 

์‰ฝ๊ฒŒ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด์„œ ์„ค๋ช…๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

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

 

 

์–ด๋–ค ์‚ฌ๋žŒ์€ "๋‚œ ๋จผ์ € ์–ด๋–ค ๋‚˜๋ผ๋“ค์ด ์žˆ๋Š”์ง€ ๋ถ€ํ„ฐ ํ™•์ธํ•˜๊ณ  ๊ทธ ๋‚˜๋ผ ์•ˆ์— ์žˆ๋Š” ๋„์‹œ๋“ค์„ ํ™•์ธํ• ๋ž˜" ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๊ณ 

 

 

์–ด๋–ค ์‚ฌ๋žŒ์€ "๋‚œ ๋จผ์ € ํ•œ ๋‚˜๋ผ๋ถ€ํ„ฐ ๋ชจ๋“  ๋„์‹œ๋ฅผ ํ™•์ธํ•˜๊ณ  ๊ทธ ๋‹ค์Œ ๋‚˜๋ผ๋กœ ๋„˜์–ด๊ฐˆ๋ž˜" ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

์ด ๋‘˜ ์ค‘์— DFS๋Š” ์–ด๋–ค ๊ฒƒ ์ผ๊นŒ์š”?

 

๋ฐ”๋กœ ๋‘ ๋ฒˆ์งธ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. (์ฒซ ๋ฒˆ์งธ ๋ฐฉ์‹์€ BFS ์ž…๋‹ˆ๋‹ค.)

 

์ง€๊ตฌ -> ํ•œ๊ตญ -> ์„œ์šธ ์ด ์ˆœ์„œ๋Œ€๋กœ ๊ฐ ์ •์ ์˜ ๊นŠ์ด๋ฅผ ๋จผ์ € ํ™•์ธํ–ˆ์ฃ ?

 

๋ฐ”๋กœ ์ด๊ฒƒ์„ ๊นŠ์ด๋ฅผ ์šฐ์„  ํƒ์ƒ‰ํ•œ๋‹ค. ์ฆ‰, ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ DFS ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ ์„œ์šธ ์•ˆ์— ๊ตฌ๊นŒ์ง€ ์žˆ์—ˆ๋‹ค๋ฉด ์•„๋ž˜ ์‚ฌ์ง„์ฒ˜๋Ÿผ ์ ์  ๋” ๊นŠ๊ฒŒ ํ™•์ธํ•˜๋‹ค๊ฐ€ ๋” ์ด์ƒ ์ž์‹์ด ์—†๋Š” ๊ฒฝ์šฐ์—

 

๊ฐ•๋‚จ -> ๋งˆํฌ -> ์€ํ‰ ์˜†(๋„ˆ๋น„)์œผ๋กœ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด์ฃ .

 

 

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

 

 

๋ณดํ†ต ์žฌ๊ท€ํ˜ธ์ถœ๋กœ ๊ตฌํ˜„ํ•˜์ง€๋งŒ ๋‹จ์ˆœํ•˜๊ฒŒ ์Šคํƒ์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ๋„ ์žˆ์Šต๋‹ˆ๋‹ค.


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

 

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

 

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

 

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],
    [3,7],
    [3,8],
    [9,3],
    [2,10],
    [2,11],
    [12,2]
]

 

 

๊ฐ ์ธ๋ฑ์Šค์— ์ž์‹๋“ค์ด ๋‹ด๊ธฐ๋„๋ก n ํฌ๊ธฐ์˜ ์ด์ค‘๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

 

var parentChildren:[[String]] = Array(repeating: [], count: n)

 

์ตœ๋Œ€ ์—ฐ๊ฒฐ๋  ์ˆ˜ ์žˆ๋Š” n*n ํฌ๊ธฐ์˜ ์ด์ค‘ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

var connections[[Bool]] = Array(repeating: Array(repeating: false, count: n), count: n)

 

๊ทธ๋ฆฌ๊ณค ์œ„์˜ connections์—์„œ ์„œ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ๋‘ ์ •์ ์„ true๋กœ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์„œ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๊ฒƒ๋“ค๋งŒ true๋กœ ๋˜๊ฒ ์ฃ ?

 

func setConnections() {
    for connection in allConnections {
        let left = connection[0]
        let right = connection[1]
        connections[left][right] = true
        connections[right][left] = true
    }
}

 

for๋ฌธ์„ ์ด์šฉํ•ด์„œ ํ˜„์žฌ ์ •์ ๊ณผ ๋ชจ๋“  ์ธ๋ฑ์Šค๋ฅผ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค.

 

์ด ์ค‘ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์ •์  ์ค‘ ๋ถ€๋ชจ๊ฐ€ ์•„๋‹Œ ์ •์ ์€ ์ž์‹์ด๋ž€ ์˜๋ฏธ์ด๋ฏ€๋กœ

 

parentChildren์˜ ํ˜„์žฌ ์ •์  ์ธ๋ฑ์Šค ๋ฐฐ์—ด์— ์—ฐ๊ฒฐ๋œ ์ •์ ์„ ์ž์‹์œผ๋กœ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณค ๋ถ€๋ชจ๋ฅผ ํ˜„์žฌ ์ •์ ์œผ๋กœ ํ˜„์žฌ๋ฅผ ์—ฐ๊ฒฐ๋œ ์ •์ ์œผ๋กœ ์žฌ๊ท€ํ•ด์ค๋‹ˆ๋‹ค.

 

func searchByDFS(parent:Int,current:Int) {
    connections[current]
        .enumerated()
        .filter{$0.element == true && $0.offset != parent}
        .forEach {
            parentChildren[current].append(world[$0.offset])
            searchByDFS(parent: current, current: $0.offset)
        }
}

 

๋ชจ๋“  ์—ฐ๊ฒฐ์„ ํ•˜๊ณ  ์ •์  0๋ถ€ํ„ฐ DFS๋ฅผ ์‹คํ–‰ ์‹œ์ผœ์ค€ ๋’ค parentChildren์˜ ๊ฐ ์ธ๋ฑ์Šค ๋ณ„ ์ž์‹๋“ค์„ ํ™•์ธํ•ด๋ณด๋ฉด

 

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

 

์œ„์™€ ๊ฐ™์ด ๊ฐ ์ •์ ์— ๋Œ€ํ•œ ์ž์‹๋“ค์ด ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

setConnections()
searchByDFS(parent: 0, current: 0)
print(parentChildren)
for (i,children) in parentChildren.enumerated(){
    print("\(world[i])์˜ ์ž์‹์€ \(children)")
}

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



์ „์ฒด ์ฝ”๋“œ

 


๊ด€๋ จ ๋ฌธ์ œ

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์—ฌํ–‰ ๊ฒฝ๋กœ

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์—ฌํ–‰๊ฒฝ๋กœ

[["ICN", "SFO"], ["ICN", "ATL"], ["SFO", "ATL"], ["ATL", "ICN"], ["ATL","SFO"]] ["ICN", "ATL", "ICN", "SFO", "ATL", "SFO"]

programmers.co.kr

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋„คํŠธ์›Œํฌ

 

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋„คํŠธ์›Œํฌ Swift

Source Code Solution ํ•ด๋‹น ๋ฌธ์ œ๋Š” DFS์— ๊ด€๋ จ๋œ ๋ฌธ์ œ๋‹ค. 1. ์ปดํ“จํ„ฐ์™€ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š”์ง€ ์•„๋‹Œ์ง€๋ฅผ ํ™•์ธํ•œ๋‹ค. -> 2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ์—ฐ๊ฒฐ๋œ ๋„คํŠธ์›Œํฌ๋“ค๋งŒ ๋ชจ์•„์ค€๋‹ค. ์ฆ‰, ๊ฐ™์€ ๋ฐฐ์—ด์— ์žˆ๋Š” ์ปดํ“จํ„ฐ๋Š” ํ•˜๋‚˜์˜

fomaios.tistory.com

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ชจ๋‘ 0์œผ๋กœ ๋งŒ๋“ค๊ธฐ

 

 

[Swift] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์›”๊ฐ„ ์ฝ”๋“œ ์ฑŒ๋ฆฐ์ง€ 2 ๋ชจ๋‘ 0์œผ๋กœ ๋งŒ๋“ค๊ธฐ

Problem ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ชจ๋‘ 0์œผ๋กœ ๋งŒ๋“ค๊ธฐ ๊ฐ ์ ์— ๊ฐ€์ค‘์น˜๊ฐ€ ๋ถ€์—ฌ๋œ ํŠธ๋ฆฌ๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋‹น์‹ ์€ ๋‹ค์Œ ์—ฐ์‚ฐ์„ ํ†ตํ•˜์—ฌ, ์ด ํŠธ๋ฆฌ์˜ ๋ชจ๋“  ์ ๋“ค์˜ ๊ฐ€์ค‘์น˜๋ฅผ 0์œผ๋กœ ๋งŒ๋“ค๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ž„์˜์˜ ์—ฐ๊ฒฐ๋œ ๋‘

fomaios.tistory.com

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€