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

[Algorithm] ๋‹ค์ต์ŠคํŠธ๋ผ(Dijkstra) ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ž€? (feat.Swift)

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

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

 

์˜ค๋Š˜์€ ๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ์—์„œ ์•„์ฃผ ๋นˆ๋ฒˆํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜๋Š” ๋‹ค์ต์ŠคํŠธ๋ผ๋ผ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

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


๋‹ค์ต์ŠคํŠธ๋ผ๋ž€? ๐Ÿค”

 

๋‚˜๋ฌด ์œ„ํ‚ค์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

 

"์Œ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ์—†๋Š” ๊ทธ๋ž˜ํ”„์˜ ํ•œ ์ •์ ์—์„œ ๋ชจ๋“  ์ •์ ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐ๊ฐ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค."

 

์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด ํ•œ ์ •์ ์—์„œ ๋‹ค๋ฅธ ์ •์ ์œผ๋กœ ์ด๋™ํ•  ๋•Œ ๊ฐ€์žฅ ๋น ๋ฅธ ๊ธธ์„ ์ฐพ์•„์ฃผ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด์—์š”.


๊ทธ๋ž˜ํ”„ ์„ค๋ช…

 

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ทธ๋ž˜ํ”„๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์‹œ์ž‘ ์ •์ ์€ 1๋กœ ๋ชฉํ‘œ ์ •์ ์„ 4๋กœ ๋‘์—ˆ์„ ๋•Œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ฑฐ๋ฆฌ๋Š” ๋ช‡์ผ๊นŒ์š”?

 

1 -  5 - 4 ๋กœ ๊ฐ€๋Š” ๊ฒฝ์šฐ๊ฐ€ 3 + 1 = 4๋กœ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ธธ์ด๊ฒ ์ฃ ?

 

์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํžˆ ๋ณด์ด๋Š” ๊ฒฝ๋กœ๋Š” ๋ˆˆ์œผ๋กœ ์‰ฝ๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ์ง€๋งŒ ๋ณต์žกํ•œ ๊ฒฝ๋กœ์ผ์ˆ˜๋ก ์ฐพ๊ธฐ๊ฐ€ ์–ด๋ ค์›Œ์งˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

์ด ๋•Œ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ ์šฉํ•˜๋Š” ๊ฒƒ์ธ๋ฐ์š”.

 

์‹œ์ž‘ ์ •์ ์—์„œ ๋‹ค๋ฅธ ์ •์ ์œผ๋กœ ๊ฐ€๋Š” ๋ชจ๋“  ๊ฑฐ๋ฆฌ๋ฅผ ์ตœ๋Œ“๊ฐ’์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณค ์‹œ์ž‘ ์ •์ ๊ณผ ์—ฐ๊ฒฐ๋œ ์ •์ ์˜ ๊ฑฐ๋ฆฌ๋กœ ์—…๋ฐ์ดํŠธ ํ•ด์ค๋‹ˆ๋‹ค

.

์‹œ์ž‘ ์ •์ ๊ณผ ์—ฐ๊ฒฐ๋œ ์ •์ ๊ณผ ์—ฐ๊ฒฐ๋œ ์ •์ ๋“ค์„ ์ˆœํšŒํ•˜๋ฉด์„œ ๋” ์งง์€ ๊ฑฐ๋ฆฌ๋กœ ์—…๋ฐ์ดํŠธ ํ•ด์ค๋‹ˆ๋‹ค.

 

1๋ฒˆ๊ณผ ์—ฐ๊ฒฐ๋œ 2๋ฒˆ ์ •์ ๊ณผ ์—ฐ๊ฒฐ๋œ ์ •์ ์„ ์ˆœํšŒํ•˜๋ฉด์„œ 2๋ฅผ ๊ฑฐ์ณ๊ฐ€๋Š” ๊ฒƒ๊ณผ 1๋ฒˆ์—์„œ ์ง์ ‘ ๊ฐ€๋Š” ๊ฒƒ์„ ๋น„๊ตํ•˜์—ฌ

 

๋” ์งง์€ ๊ฑฐ๋ฆฌ๋กœ ๊ฒฝ๋กœ์™€ ๊ฑฐ๋ฆฌ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•ด์ค๋‹ˆ๋‹ค.

์ด๋Ÿฐ์‹์œผ๋กœ ๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ์‹œ์ž‘ ์ •์ ์—์„œ ๋‹ค๋ฅธ ์ •์ ์œผ๋กœ ๊ฐ€๋Š” ๊ฐ€์žฅ ์งง์€ ๊ฑฐ๋ฆฌ๋ฅผ ๋ชจ๋‘ ์—…๋ฐ์ดํŠธ ํ•ด์ค๋‹ˆ๋‹ค.


์ฝ”๋“œ ์„ค๋ช…

 

์•„๋ž˜์™€ ๊ฐ™์ด ์ •์ ์˜ ๊ฐฏ์ˆ˜, ์‹œ์ž‘ ์ •์ ,์—ฐ๊ฒฐ ์ •๋ณด,๊ฒฝ๋กœ๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด,๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

//์ •์ ์˜ ๊ฐฏ์ˆ˜
var n = 5
//์‹œ์ž‘ ์ •์ 
var start = 1
//์—ฐ๊ฒฐ๋œ ์ •๋ณด
var connections = [[1,2,1],[1,3,19],[1,4,10],[1,5,3],[2,3,30],[2,4,2],[2,5,7],[3,4,26],[3,5,4],[4,5,1]]
//๊ฒฝ๋กœ๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด
var allRoutes = (0...n).map{[$0]}
//๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด(์ดˆ๊ธฐ ๊ฐ’์€ Int์˜ ์ตœ๋Œ“๊ฐ’์œผ๋กœ ์„ค์ •)
var allDistances = Array(repeating: Int.max, count: n+1)

 

๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‹ค์ต์ŠคํŠธ๋ผ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

๋จผ์ € ์‹œ์ž‘ ์ •์ ์— ์žˆ๋Š” ๊ฑฐ๋ฆฌ๋ฅผ 0์œผ๋กœ ์„ค์ •ํ•˜๊ณ 

 

ํ์™€ while๋ฌธ์„ ์ด์šฉํ•ด์„œ ๊ฐ ์ •์  ๊ทธ๋ฆฌ๊ณ  ์—ฐ๊ฒฐ๋œ ์ •์ ๋“ค์„ BFS ๋ฐฉ์‹์œผ๋กœ ์ˆœํšŒํ•ด์ค๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ ์ค‘๊ฐ„์— ๊ฑฐ์ณ์„œ ๊ฐ€๋Š” ๊ฒƒ์ด ์ง์ ‘ ๊ฐ€๋Š” ๊ฒƒ๋ณด๋‹ค ๊ฑฐ๋ฆฌ๊ฐ€ ์งง๋‹ค๋ฉด allDistances์˜ ๊ฑฐ๋ฆฌ์™€ allRoutes์˜ ๊ฒฝ๋กœ๋ฅผ ์ƒˆ๋กญ๊ฒŒ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค.

 

//๋‹ค์ต์ŠคํŠธ๋ผ๋กœ ์‹œ์ž‘ ์ •์ ์—์„œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ฑฐ๋ฆฌ๋กœ ์—…๋ฐ์ดํŠธํ•ด์คŒ.
func dijkstra() {
    //์ž๊ธฐ ์ž์‹ ๊ณผ์˜ ๊ฑฐ๋ฆฌ๋Š” 0
    allDistances[start] = 0
    //์‹œ์ž‘ ์ธ๋ฑ์Šค๋ฅผ ๋‹ด์€ ํ๋ฅผ ๋งŒ๋“ค์–ด์คŒ.
    var queue:[Int] = [start]
    //ํ๊ฐ€ ๋ชจ๋‘ ์—†์–ด์งˆ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
    while !queue.isEmpty {
        //ํ์˜ ๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ ๊ฐ’์„ ์ €์žฅํ•˜๋ฉด์„œ ์‚ญ์ œํ•ด์คŒ.
        let first = queue.removeFirst()
        //์—ฐ๊ฒฐ ์ •๋ณด์—์„œ ํ์˜ ์ฒซ๋ฒˆ์งธ ๊ฐ’์ด ์žˆ๋Š” ๊ฒƒ๋“ค๋กœ ํ•„ํ„ฐ๋ง ํ•ด์คŒ.
        let filterNodes = connections.filter{$0[0] == first || $0[1] == first}
        //ํ•„ํ„ฐ๋งํ•œ ๊ฒƒ๋“ค์„ ์ˆœํšŒ
        for node in filterNodes {
            //์ฒซ๋ฒˆ์งธ ๊ฐ’์ด ์•„๋‹Œ ๊ฒƒ์„ other๋กœ ์„ค์ •
            let connectionNode = node[0] == first ? node[1] : node[0]
            //๋งŒ์•ฝ Int ์ตœ๋Œ“๊ฐ’์ด๋ผ๋ฉด continue
            if allDistances[first] == Int.max {continue}
            //์ฒซ๋ฒˆ์งธ ๊ฐ’์— ๊ฑฐ๋ฆฌ๋ฅผ ๋”ํ•œ ๊ฒƒ์ด ์›๋ž˜ ์žˆ๋Š” ๊ฐ’๋ณด๋‹ค ์ž‘๋‹ค๋ฉด
            if allDistances[first]+node[2] < allDistances[connectionNode] {
                //์ฒซ๋ฒˆ์งธ ๊ฐ’์— ๊ฑฐ๋ฆฌ๋ฅผ ๋”ํ•œ ๊ฒƒ์œผ๋กœ ์—…๋ฐ์ดํŠธ
                allDistances[connectionNode] = allDistances[first]+node[2]
                //์ฒซ๋ฒˆ์งธ ํ์— ์žˆ๋Š” ๋ฃจํŠธ๋ฅผ ์ €์žฅํ•จ.
                var newRoute = allRoutes[first]
                //ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์คŒ.
                newRoute.append(connectionNode)
                //ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ์ƒˆ๋กœ์šด ๊ฒฝ๋กœ๋กœ ๋ฐ”๊ฟ”์คŒ.
                allRoutes[connectionNode] = newRoute
                //ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ํ์— ๋„ฃ์–ด์คŒ.
                queue.append(connectionNode)
            }
        }
    }
}

 

์ด๋ ‡๊ฒŒ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‹คํ–‰ํ•ด์ฃผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด allDistances์—” 1์—์„œ ๊ฐ ์ •์ ๋ณ„ ๊ฐ€์žฅ ์งง์€ ๊ฑฐ๋ฆฌ๊ฐ€ ๋‹ด๊ฒจ์žˆ๊ณ 

 

allRoutes์—” ๊ฐ€์žฅ ์งง์€ ๊ฑฐ๋ฆฌ๋กœ ๊ฐ€๋Š” ๊ฒฝ๋กœ๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ๋‹ด๊ธฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค!

dijkstra()
print(allDistances) //[9223372036854775807, 0, 1, 7, 3, 3]
print(allRoutes) //[[0], [1], [1, 2], [1, 5, 3], [1, 2, 4], [1, 5]]

๊ด€๋ จ๋ฌธ์ œ

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฐฐ๋‹ฌ

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ฐฐ๋‹ฌ

5 [[1,2,1],[2,3,3],[5,2,2],[1,4,2],[5,3,1],[5,4,2]] 3 4 6 [[1,2,1],[1,3,2],[2,3,2],[3,4,3],[3,5,2],[3,5,3],[5,6,1]] 4 4

programmers.co.kr

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๊ฐ€์žฅ ๋จผ ๋…ธ๋“œ

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ฐ€์žฅ ๋จผ ๋…ธ๋“œ

6 [[3, 6], [4, 3], [3, 2], [1, 3], [1, 2], [2, 4], [5, 2]] 3

programmers.co.kr

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€