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

[Algorithm] ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ(Floyd Warshall) ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ž€? (feat.Swift)

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

 

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

 

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

 

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


ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ์ด๋ž€? ๐Ÿค”

 

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

 

"ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋ณ€์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ์Œ์ด๊ฑฐ๋‚˜ ์–‘์ธ (์Œ์ˆ˜ ์‚ฌ์ดํด์€ ์—†๋Š”) ๊ฐ€์ค‘ ๊ทธ๋ž˜ํ”„์—์„œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋“ค์„ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค."

 

์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด ๋ชจ๋“  ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด์—์š”.


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

 

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

 

(ํŒŒ๋ž€์ƒ‰ ๋™๊ทธ๋ผ๋ฏธ๋Š” ์ •์ , ์‚ฌ์ด์— ์ˆซ์ž๋Š” ๊ฑฐ๋ฆฌ์ž…๋‹ˆ๋‹ค.)

๋งŒ์•ฝ 1๋ฒˆ ์ •์ ์—์„œ 3๋ฒˆ ์ •์ ์œผ๋กœ ๊ฐ€๋ ค๋ฉด ์–ด๋–ค ์ •์ ์„ ์ด์šฉํ•ด์•ผ ๊ฐ€์žฅ ์งง์„๊นŒ์š”?

 

์ •๋‹ต์„ ์•Œ๊ธฐ ์œ„ํ•ด์„  ์ค‘๊ฐ„์— ๋ชจ๋“  ์ •์ ์„ ๊ฑฐ์ณ์„œ ๊ฐ„ ๋‹ค์Œ ๊ฐ€์žฅ ์งง์€ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•ด์•ผ ํ•  ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

 

1๋ฒˆ -> 2๋ฒˆ -> 3๋ฒˆ 32

 

1๋ฒˆ -> 5๋ฒˆ -> 3๋ฒˆ 11

 

์ด๋ฏ€๋กœ 1๋ฒˆ์—์„œ 5๋ฒˆ์„ ๊ฑฐ์ณ 3๋ฒˆ์œผ๋กœ ๊ฐ€๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ๋น ๋ฅด๊ฒ ์ฃ ?

 

๋˜ํ•œ 2๋ฒˆ๊ณผ 3๋ฒˆ ๊ฐ™์ด ์ง์ ‘ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ 2๋ฒˆ์—์„œ 4๋ฒˆ์„ ๊ฑฐ์ณ 3๋ฒˆ์œผ๋กœ ๊ฐ€๋Š” ๊ฒฝ์šฐ๊ฐ€ ํ›จ์”ฌ ๋” ๊ฑฐ๋ฆฌ๊ฐ€ ์งง์€ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

(2๋ฒˆ -> 3๋ฒˆ 20 2๋ฒˆ -> 4๋ฒˆ -> 3๋ฒˆ 8)

 

์ด๋Ÿฐ ์‹์œผ๋กœ ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ์€ ์‹œ์ž‘ : 1...n  ์ค‘๊ฐ„: 1... n ๋„์ฐฉ: 1...n ๊นŒ์ง€ n์˜ 3์ œ๊ณฑ์œผ๋กœ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ ๋ชจ๋“  ๊ฐ’์„ ์ฐพ์•„๋†“์œผ๋ฉด ์–ด๋–ค ์ •์ ์ด๋“  ๊ฐ€์žฅ ์งง์€ ๊ฑฐ๋ฆฌ๋ฅผ ์•Œ ์ˆ˜ ์žˆ๊ฒ ์ฃ .

 

๋‹ค์Œ๊ณผ ๊ฐ™์ด 6๋ฒˆ์ฒ˜๋Ÿผ ์ ˆ๋Œ€๋กœ ๊ฐ€์ง€ ๋ชปํ•˜๋Š” ์ •์ ์— ๋Œ€ํ•œ ๊ฒƒ์€ ๋ฌดํ•œ๋Œ€๋กœ ๊ฐ’์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

 


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

 

๋ช‡๊ฐœ์˜ ์ •์ (n)์ด ์žˆ๋Š”์ง€์™€ ๋ชจ๋“  ์ •์ ์˜ ์—ฐ๊ฒฐ์— ๋Œ€ํ•œ ๊ฑฐ๋ฆฌ(allConnections)๋ฅผ ์ €์žฅํ•ด๋‘ก๋‹ˆ๋‹ค.

let n = 6
let allConnections = [[1,2,12],[2,3,20],[3,4,5],[4,5,7],[1,5,10],[2,4,3]]

๋ชจ๋“  ๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅํ•  Int.max๋กœ ์ด๋ค„์ ธ์žˆ๋Š” ์ด์ฐจ์› ๋ฐฐ์—ด์„ n+1 ์ œ๊ณฑ๋งŒํผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.( 1๋ฒˆ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ 0๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ๋ฌด์‹œํ•˜๊ธฐ ์œ„ํ•ด)

var allDistances = Array(repeating: Array(repeating: Int.max, count: n+1), count: n+1)

 

์ž๊ธฐ ์ž์‹ ์˜ ๊ฑฐ๋ฆฌ๋Š” 0์œผ๋กœ ์„ธํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

for i in 1...n {
	allDistances[i][i] = 0
}

 

allConnections์— ์žˆ๋Š” ๋ชจ๋“  ๊ฑฐ๋ฆฌ๋“ค์„ allDistances์— ๋‹ด์•„์ค๋‹ˆ๋‹ค.

func setConnection() {
    for connection in allConnections {
        allDistances[connection[0]][connection[1]] = connection[2]
        allDistances[connection[1]][connection[0]] = connection[2]
    }
}

ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด j๊ฐ€ ์ง์ ‘ k๋กœ ๊ฐ€๋Š” ๊ฒƒ๊ณผ i๋ฅผ ๊ฑฐ์ณ k๋กœ ๊ฐ€๋Š” ๊ฒƒ์„ ๋น„๊ตํ•˜์—ฌ ๋” ์ž‘์€ ๊ฐ’์œผ๋กœ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค.

func floyd() {
    for i in 1...n {
        for j in 1...n {
            for k in 1...n {
                if allDistances[j][i] == Int.max || allDistances[i][k] == Int.max { continue }
                if allDistances[j][i] + allDistances[i][k] < allDistances[j][k] {
                    allDistances[j][k] = allDistances[j][i] + allDistances[i][k]
                }
            }
        }
    }
}

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ชจ๋“  ์ •์ ์— ๋Œ€ํ•œ ๊ฐ€์žฅ ๋น ๋ฅธ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

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

 

let n = 6

let allConnections = [[1,2,12],[2,3,20],[3,4,5],[4,5,7],[1,5,10],[2,4,3]]

var allDistances = Array(repeating: Array(repeating: Int.max, count: n+1), count: n+1)

func setConnection() {
    for connection in allConnections {
        allDistances[connection[0]][connection[1]] = connection[2]
        allDistances[connection[1]][connection[0]] = connection[2]
    }
}

func floyd() {
    for i in 1...n {
        for j in 1...n {
            for k in 1...n {
                if allDistances[j][i] == Int.max || allDistances[i][k] == Int.max { continue }
                if allDistances[j][i] + allDistances[i][k] < allDistances[j][k] {
                    allDistances[j][k] = allDistances[j][i] + allDistances[i][k]
                }
            }
        }
    }
}

setConnection()
floyd()
print(allDistances)

๊ด€๋ จ ๋ฌธ์ œ

 

2021 KAKAO BLIND RECRUITMENT ํ•ฉ์Šน ํƒ์‹œ ์š”๊ธˆ

 

 

[Swift] 2021 KAKO BLIND RECUITMENT ํ•ฉ์Šน ํƒ์‹œ ์š”๊ธˆ

Problem ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํ•ฉ์Šน ํƒ์‹œ ์š”๊ธˆ 6 4 6 2 [[4, 1, 10], [3, 5, 24], [5, 6, 2], [3, 1, 41], [5, 1, 24], [4, 6, 50], [2, 4, 66], [2, 3, 22], [1, 6, 25]] 82 7 3 4 1 [[5, 7, 9], [4, 6, 4], [3, 6,..

fomaios.tistory.com

 

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ˆœ์œ„

 

 

[Swift] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ˆœ์œ„

Problem ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ˆœ์œ„ 5 [[4, 3], [4, 2], [3, 2], [1, 2], [2, 5]] 2 programmers.co.kr Solution ์ด๋ฒˆ ๋ฌธ์ œ๋Š” ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ ์œผ๋กœ ํ’€์–ด์•ผ ํ•˜๋Š” ๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. 1. ๋ชจ๋“  ์Šน๋ถ€๋ฅผ ์ €์žฅํ•  ์ด์ค‘ ๋ฐฐ์—ด..

fomaios.tistory.com

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€