[c++] ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐœ๋…๊ณต๋ถ€ :: ์ตœ๋‹จ๊ฒฝ๋กœ ์ฐพ๊ธฐ (Dijkstra ์•Œ๊ณ ๋ฆฌ์ฆ˜)

์ตœ๋‹จ ๊ฒฝ๋กœ

 ๊ทธ๋ž˜ํ”„์—์„œ ์œ ๋„๋˜๋Š” ๋ฌธ์ œ๋“ค ์ค‘ ์ตœ๋‹จ ๊ฒฝ๋กœ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค. ์ฃผ์–ด์ง„ ๊ทธ๋ž˜ํ”„์—์„œ ํŠน์ • ์ •์ ์—์„œ ๋‹ค๋ฅธ ์ •์ ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ(๊ฐ€์ค‘์น˜ ํ•ฉ์ด ์ตœ์†Œ์ธ ๊ฒฝ๋กœ)๊ฐ€ ๋ฌด์—‡์ธ์ง€๋ฅผ ์•Œ์•„๋‚ด๋Š” ๋ฌธ์ œ์ด๋‹ค.

 ์ด๋Š” ์ด์ „์˜ MST ๋ฌธ์ œ์™€ ๋‹ค๋ฅด๋‹ค. ๋ชจ๋“  ์ •์ ์„ ํฌํ•จํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

ํ•˜์ง€๋งŒ MST์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ Prim(Dijkstra) ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‘์šฉํ•ด์„œ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

[MST]

 

Dijkstra ์•Œ๊ณ ๋ฆฌ์ฆ˜

๊ตฌํ˜„

vertice(์ •์ )๋Š” 3๊ฐ€์ง€ ๋ถ„๋ฅ˜๋กœ ๋‚˜๋‰œ๋‹ค.

  • tree vertice(T) : tree์— ์†ํ•ด ์žˆ๋Š” ์ •์ 
  • fringe vertice (F) : tree์— ํฌํ•จ๋˜์ง€ ์•Š์œผ๋ฉด์„œ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š” ์ •์ 
  • unseen vertice (U) : ๋‚˜๋จธ์ง€

 MST์—์„œ T์™€ F์˜ ์ •์  ์‚ฌ์ด์˜ ์ตœ์†Œ ๊ฐ€์ค‘์น˜๋ฅผ ๊ณ„์† ๊ฐฑ์‹ ํ•ด์ฃผ์—ˆ๋˜ ๊ฒƒ ๋Œ€์‹ ์—, ์ตœ๋‹จ ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ๋Š” ์‹œ์ž‘ ์ •์ ๋ถ€ํ„ฐ F์˜ ์ •์ ๊นŒ์ง€ ๊ฒฝ๋กœ๋ฅผ ๊ฐฑ์‹ ํ•ด์ค€๋‹ค.

 F์˜ ํŠน์ • ์ •์  V๋ฅผ ๋ณด์•˜์„ ๋•Œ, T-F์˜ ๊ฐ„์„ ์„ ๋”ฐ๋ผ ์‹œ์ž‘์ •์ ๋ถ€ํ„ฐ V๊นŒ์ง€์˜ ๊ฒฝ๋กœ์˜ ์ตœ์†Œ๊ฐ’์ด ํ˜„์žฌ ์ €์žฅ๋˜์–ด์žˆ๋‹ค๊ณ  ํ•˜์ž. ์ด ๋•Œ, ๋‹ค๋ฅธ ์ •์  V'๊ฐ€ tree์— ํฌํ•จ๋˜๋ฉด V'-V ๊ฐ„์„ ์˜ ํฌํ•จ์œผ๋กœ ์ด ๊ฐ„์„ ์„ ํ†ตํ•œ ๊ฒฝ๋กœ๊ฐ€ ๊ธฐ์กด์— ์กด์žฌํ•˜๋˜ ๊ฒฝ๋กœ๋ณด๋‹ค ์งง์•„์งˆ ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋ฅผ ๊ฐฑ์‹ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

 

 ์œ„์˜ ์˜ˆ์ œ์—์„œ Tree set์— ์‹œ์ž‘ ์ •์ ์ธ A๋ฐ–์— ์—†์—ˆ์„ ๋•Œ, D๊นŒ์ง€์˜ ๊ฒฝ๋กœ๋Š” A-D ๊ฐ„์„ ์„ ๋”ฐ๋ผ ๊ฐ€์ค‘์น˜๊ฐ€ 3์ด๋‹ค. ํ•˜์ง€๋งŒ ์ดํ›„ C๊ฐ€ Tree set์— ํฌํ•จ๋˜๋ฉด D๊นŒ์ง€์˜ ๊ฒฝ๋กœ๋Š” A์—์„œ C, C์—์„œ D๋ฅผ ๊ฑฐ์น˜๋Š” 1+1 = 2์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒฝ๋กœ๊ฐ€ ์ตœ์†Œ๊ฐ€ ๋œ๋‹ค.

๋ฐ˜์‘ํ˜•