Skip to content

Latest commit

ย 

History

History
71 lines (39 loc) ยท 1.65 KB

Floyd.md

File metadata and controls

71 lines (39 loc) ยท 1.65 KB

Floyd ์ตœ๋‹จ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜

๋ชจ๋“  ์ •์ ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ์ •์ ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ฌธ์ œ

  • 2์ฐจ์› ๋ฐฐ์—ด์„ ์ด์šฉํ•œ 3์ค‘ ๋ฐ˜๋ณต๋ฌธ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค
  • ๊ฐ€์ค‘์น˜๊ฐ€ ์Œ์˜ ์ •์ˆ˜์ผ ๋•Œ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค (๋‹จ, ์Œ์˜ ์‚ฌ์ดํด์ด ์—†์–ด์•ผ ํ•œ๋‹ค)

Dijkstra๋Š” ํ•˜๋‚˜์˜ ์ •์ ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ์ •์ ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด์—ˆ๋‹ค๋ฉด

Floyd๋Š” ํ•œ ๋ฒˆ ์‹คํ–‰ํ•˜์—ฌ ๋ชจ๋“  ๋…ธ๋“œ ๊ฐ„ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค


  • distance_k[i][j]: 0๋ถ€ํ„ฐ k๊นŒ์ง€์˜ ์ •์ ๋งŒ์„ ์ด์šฉํ•œ ์ •์  i์—์„œ j๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ ๊ธธ์ด

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


Floyd ์ˆ˜ํ–‰๋‹จ๊ณ„


1. ๊ทธ๋ž˜ํ”„์˜ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ๋กœ ๋ฐฐ์—ด A๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค



2. ์ •์  0์„ ๊ฑฐ์ณ์„œ ๊ฐ€๋Š” ๊ฒฝ๋กœ์™€ ๋น„๊ตํ•˜์—ฌ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค


  • distance[i][j] = min(distance[i][j], distance[i][n] + distance[n][j])

3. ์ •์  1์„ ๊ฑฐ์ณ์„œ ๊ฐ€๋Š” ๊ฒฝ๋กœ์™€ ๋น„๊ตํ•˜์—ฌ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค


  • distance[i][j] = min(distance[i][j], distance[i][n] + distance[n][j])

์ •์  k๋ฅผ ๊ฑฐ์ณ์„œ ๊ฐ€๋Š” ๊ฒฝ๋กœ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค


pseudocode

for k <- 0 to n-1
    for i <- 0 to n-1
        for j <- 0 to n-1
            distance[i][j] = min(distacne[i][j], distance[i][n] + distance[n][j])