Single Source Shortest Path Algorithms

๐Ÿ’ก ๋‹จ์ผ์ถœ๋ฐœ์ง€ ์ตœ๋Œ€๊ฒฝ๋กœ(SSP) : ํ•˜๋‚˜์˜ ์ถœ๋ฐœ์ ์—์„œ ๊ฐ ์ •์ ๊นŒ์ง€ ๋„๋‹ฌํ•˜๋Š”๋ฐ ๋น„์šฉ์„ ๊ณ„์‚ฐํ•˜์—ฌ ์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š”๊ฒƒ

๐Ÿ”ป

Untitled

๐Ÿ‘‰v1์—์„œ v4๋กœ ๊ฐ€๋ ค๋ฉด ๋‘ ๊ฐ€์ง€์˜ ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌ

  • v1 -> v2 -> v4
    • v1 -> v2์˜ ๋น„์šฉ์€ย 5์ด๊ณ , v2 -> v4์˜ ๋น„์šฉ์€ 2์ด๋ฏ€๋กœย 5 + 2 = 7
  • v1 -> v3 -> v4
    • v1 -> v3์˜ ๋น„์šฉ์€ย 3์ด๊ณ , v3 -> v4์˜ ๋น„์šฉ์€ 9์ด๋ฏ€๋กœ 3 + 9 = 12

โ‡’ ๋‘ ๊ฒฝ๋กœ ์ค‘ ๋น„์šฉ์ด ์ ์€ 1๋ฒˆ์„ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ด v1์—์„œ v4์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ

โ‡’ ์ตœ๋‹จ ๊ฒฝ๋กœ๋Š”ย ์ง์ „ ์ •์ ๊นŒ์ง€์˜ ๊ฒฝ๋กœ ๋น„์šฉ์— ์˜ํ–ฅ์„ ๋ฐ›๋Š”๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ‘‰Negative-Weight Edges (์Œ์˜ ๊ฐ€์ค‘์น˜)

  • ์Œ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ํ—ˆ์šฉํ•˜์—ฌ SSP๋ฅผ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ย Bellman-Fordย ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • ์Œ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ณ  SSP๋ฅผ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ย Dijkstraโ€™sย ์•Œ๊ณ ๋ฆฌ์ฆ˜

Relaxation

  • d[v] ๋ฅผ ์ •์  v๊นŒ์ง€์˜ย ์ตœ๋‹จ ๊ฒฝ๋กœ ์ถ”์ • ๊ฐ’, ฮด(s, v)๋ฅผ ์ •์  s์—์„œ v๊นŒ์ง€์˜ย ์‹ค์ œ ์ตœ๋‹จ๊ฒฝ๋กœ ๋น„์šฉ ์ด๋ผ๊ณ  ํ•  ๋•Œ,

Relaxation์ด๋ž€ d[v]๊ฐ’์ด ฮด(s, v)์˜ ์ƒํ•œ์ด ๋˜๋„๋ก ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.

  • d[v]๋ฅผ ์กฐ์ •ํ•˜๋ฉด์„œ ๋„๋‹ฌํ•ด์•ผ ํ•  ๋ชฉ์ ์ง€ = ฮด(s, v)
  • d[v]๋ฅผ ์กฐ์ •ํ•˜๋Š” ํ•จ์ˆ˜ : Relax

๐Ÿ”ป

Untitled

โ†’ Original : d[v2]=9 (SSP๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ์–ป์€ ์ตœ๋‹จ๊ฒฝ๋กœ ์ถ”์ •๊ฐ’)

โ†’ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ด์ „ ์ •์ ( v1 )๊นŒ์ง€์˜ ๊ฒฝ๋กœ ๋น„์šฉ๊ณผ ์—ฐ๊ฒฐ๋œ ๊ฐ€์ค‘์น˜์˜ ๋น„์šฉ์„ ๋”ํ•˜๋Š” ๊ฒƒ

โ†’ ๊ทธ๋ž˜์„œ d[v1]์ดย 5์ด๊ณ , ์—ฐ๊ฒฐ๋œ ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ 2์ด๋ฏ€๋กœ d[v2]๋Š” 7.

โ†’ ๊ธฐ์กด์— ์ถ”์ • ๊ฐ’ 9๋ณด๋‹ค ๋” ์ž‘์€ ๋น„์šฉ์ด๋ฏ€๋กœ d[v2]๋ฅผย 7๋กœ ๋ณ€๊ฒฝ

โ‡’ ์ด๋ ‡๊ฒŒ ๋น„๊ต ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์„ Relax ๋ผ๊ณ  ํ•œ๋‹ค.

  • Relax๋ž€ย ๋ชฉ์ ์ง€ ์ •์ ( v2 )์˜ ์ถ”์ •๊ฐ’๊ณผ ์ง์ „ ์ •์  ( v1 )์˜ ์ถ”์ •๊ฐ’ +ย ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๋น„๊ตํ•ด์„œ

๋ชฉ์ ์ง€ ์ •์ ์˜ ์ถ”์ • ๊ฐ’์„ ์กฐ์ •ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

  • ์ถ”์ • ๊ฐ’์„ ๋ณ€๊ฒฝ์‹œํ‚ฌ ์ˆ˜๋„ ์žˆ๊ณ , ์•ˆํ• ์ˆ˜๋„ ์žˆ๋‹ค.

Untitled

Bellman-Ford ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„, ์Œ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ–๋Š” ๊ทธ๋ž˜ํ”„์—์„œ SSP๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด ๋ชฉ์ 
  • ํ•˜์ง€๋งŒ ๊ฐ„์„ ์ด ์Œ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ–๋Š” ๊ฒฝ์šฐ, ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๊ฐ„์„ ์ด ์ˆœํ™˜๊ตฌ์กฐ๋ฅผ ๋„๋Š” ๊ฒฝ์šฐ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์—†๋‹ค.

Untitled

  • v2 โ†’ v3์˜ ๊ฐ€์ค‘์น˜ 2๋ณด๋‹ค v3 -> v2์˜ ๊ฐ€์ค‘์น˜ -4๊ฐ€ ์ ˆ๋Œ€๊ฐ’์ด ๋” ํฌ๋ฏ€๋กœ,

์ด ์ˆœํ™˜๊ตฌ์กฐ๋กœ d[v2], d[v3] = oo ๊ฐ€ ๋˜๊ณ , d[v4]๋„ oo ๊ฐ€ ๋˜์–ด ๊ฒฐ๊ตญ v1โ†’v4์˜ ์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์—†๊ฒŒ ๋œ๋‹ค.

  • ๋ฒจ๋งŒ ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ SSP๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Œ์€ ๋ฌผ๋ก , ์œ„์™€ ๊ฐ™์ด ์ตœ๋‹จ๊ฒฝ๋กœ์— ๋„๋‹ฌํ•  ์ˆ˜ ์—†์Œ์„ false๋ฅผ ๋ฐ˜ํ™˜ํ•จ์œผ๋กœ์จ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

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

๐Ÿ”ป

Untitled Untitled

Untitled Untitled

Untitled Untitled

Untitled Untitled

Untitled Untitled

Untitled

โ‡’ ์ •์  v1์— ๋Œ€ํ•ด ๋ชจ๋“  ๊ฐ„์„ ์„ Relax๋ฅผ ํ–ˆ์Œ. ์ด์ œ ๋‹ค์Œ ์ •์ ์„ ์„ ํƒํ•˜์—ฌ ๋˜ ๋ชจ๋“  ๊ฐ„์„ ์„ Relax๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.

  • Bellman-Ford ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ •์ ์˜ ๊ฐœ์ˆ˜๋งŒํผ ๋ชจ๋“  ๊ฐ„์„ ์„ Relaxํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์—„์ฒญ๋‚œ ์—ฐ์‚ฐ์ด ์ˆ˜ํ–‰๋จ
  • ๊ฐ™์€ SSP ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ Dijkstraโ€™s ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์Œ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ–๋Š” ๊ฐ„์„ ์— ๋Œ€ํ•ด SSP๋ฅผ ๊ตฌํ•  ์ˆ˜ ์—†๋‹ค.
  • ์‹œ๊ฐ„๋ณต์žก๋„
    • ์ดˆ๊ธฐํ™” ์ž‘์—… : ฮ˜(V)
    • ์Œ์˜ ์ˆœํ™˜๊ตฌ์กฐ๋ฅผ ๊ฒ€์‚ฌํ•˜๋Š” ๋ฐ˜๋ณต๋ฌธ : ฮ˜( E )
    • ๋”ฐ๋ผ์„œ ฮ˜(VE)

Dykstraโ€™s ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ์ตœ์†Œ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์ด์šฉํ•ด์„œ ํ•˜๋‚˜์˜ ์ •์ ์œผ๋กœ๋ถ€ํ„ฐ ์ธ์ ‘ํ•œ ๊ฐ„์„ ๋“ค์„ ํ™•์žฅํ•ด ๋‚˜๊ฐ€๋Š” ๋ฐฉ์‹
  • Dijkstraโ€™s ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋จผ์ € ๋ชจ๋“  ์ •์ ๋“ค์„ ์ตœ์†Œ ์šฐ์„ ์ˆœ์œ„ ํ์— ์‚ฝ์ž…ํ•œ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ์ตœ๋‹จ ๊ฒฝ๋กœ ๊ฐ€์ค‘์น˜ ๊ฐ’( d[v] )์ด ๊ฐ€์žฅ ์ž‘์€ ์ •์ ์„ ์„ ํƒํ•˜์—ฌ ์ธ์ ‘ ๊ฐ„์„ ๋“ค์— ๋Œ€ํ•ด Relax๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ,

์‹œ์ž‘ ์ •์ ์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ ์ •์ ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ ๋น„์šฉ์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ”ป

Untitled

  1. ๋ชจ๋“  ์ •์ ๋“ค์„ ์ตœ์†Œ์šฐ์„ ์ˆœ์œ„ํ์— ์‚ฝ์ž…ํ•œ๋‹ค.

  2. ์ตœ๋‹จ ๊ฒฝ๋กœ ๊ฐ€์ค‘์น˜ ๊ฐ’ ( d[v] )์ด ๊ฐ€์žฅ ์ž‘์€ ์ •์ ์„ ์„ ํƒํ•ด ์ธ์ ‘๊ฐ„์„ ๋“ค์— ๋Œ€ํ•ด Relax๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ,

์‹œ์ž‘ ์ •์ ์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ ์ •์ ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ๋น„์šฉ์„ ๊ณ„์‚ฐํ•œ๋‹ค.

Dijkstraโ€™s Algorithm Example

Untitled

์‹œ์ž‘ ์ •์ ์„ 0์œผ๋กœ ์žก๊ณ , ๊ฐ ์ง€์ ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ํ‘œ์‹œ.

์ง์ ‘์ ์œผ๋กœ ๊ฐ€๋Š” ๊ฒฝ๋กœ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ๋ฌดํ•œ๋Œ€๋กœ ํ‘œ์‹œ.

ํ‘œ์‹œ ๋˜์–ด ์žˆ๋Š” ๊ฑฐ๋ฆฌ ์ค‘ ๊ฐ€์žฅ ์งง์€ ๊ฑฐ๋ฆฌ๋Š” ์ •์  4๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ์ธ 3์ด๋ฏ€๋กœ ์ •์  4๋ฅผ ์ง‘ํ•ฉ S์— ์ถ”๊ฐ€์‹œ์ผœ์ค€๋‹ค.

Untitled

์ƒˆ๋กœ์šด ์ •์ ์ด S์— ์ถ”๊ฐ€๋˜๋ฉด ๋‹ค๋ฅธ ์ •์ ๋“ค์˜ distance ๊ฐ’์ด ๋ณ€๊ฒฝ๋œ๋‹ค.

0๋ฒˆ ์ •์ ์—์„œ๋Š” ์ง์ ‘์ ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์—†๋˜ ์ •์ ์— ์ƒˆ๋กญ๊ฒŒ ๋“ค์–ด์˜จ ์ •์  4๋ฅผ ํ†ตํ•ด

์ง์ ‘์ ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌดํ•œ๋Œ€์˜ ๊ฐ’์—์„œ ๊ตฌ์ฒด์ ์ธ ์ •์ˆ˜๊ฑฐ๋ฆฌ๋กœ ์ •๋ณด๊ฐ€ ๊ฐฑ์‹ ๋œ๋‹ค.

๋˜ํ•œ ์ƒˆ๋กœ์šด ์ •์  4๋ฅผ ํ†ตํ•ด ๊ฐˆ ๋•Œ ๋” ์งง์€ ๊ฒฝ๋กœ๊ฐ€ ๋ฐœ๊ฒฌ ๋œ๋‹ค๋ฉด ๊ทธ ์ •๋ณด ๋˜ํ•œ ๊ฐฑ์‹ ์„ ํ•ด์ค€๋‹ค.

๊ฐฑ์‹ ๋œ ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ง‘ํ•ฉ S์— ์ถ”๊ฐ€ํ•  ๋‹ค์Œ ์ •์ ์„ ์„ ํƒ. ๋‚จ์€ ์ •์  ์ค‘ ๊ฐ€์ค‘์น˜๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์ •์ 1์„ S์— ์ถ”๊ฐ€ํ•˜๊ณ  ์ •๋ณด๋ฅผ ๊ฐฑ์‹ ํ•œ๋‹ค.

Untitled

1๋ฒˆ ์ •์ ์„ ์ถ”๊ฐ€ํ•จ์œผ๋กœ์จ 2๋ฒˆ ์ •์ ๊นŒ์ง€ ์ง์ ‘์ ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์œผ๋ฏ€๋กœ

๋ฌดํ•œ๋Œ€์˜ ๊ฐ’์—์„œ ๊ตฌ์ฒด์ ์ธ ๊ฐ€์ค‘์น˜์ธ 9๋กœ ์ˆ˜์ •ํ•ด์ค€๋‹ค. ํ˜„์žฌ๊นŒ์ง€์˜ distance ๋ฐฐ์—ด๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ

๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์€ 6๋ฒˆ ์ •์ ์˜ 8์ด๋ฏ€๋กœ, 6๋ฒˆ ์ •์ ์„ ํƒํ•œ๋‹ค.

Untitled

6๋ฒˆ ์ •์ ์„ ์ง‘ํ•ฉ S์— ์ถ”๊ฐ€ํ•จ์œผ๋กœ์จ ๊ฐฑ์‹ ํ•  ์ˆ˜ ์žˆ๋Š” ์ •๋ณด๋Š” ์ •์  3๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ.

๋‹ค์Œ ํƒํ•  ์ •์ ์€ ์ •์ 2

Untitled

์ •์  2๋ฅผ ์ง‘ํ•ฉ S์— ์ถ”๊ฐ€ํ•จ์œผ๋กœ์จ ์ •์  3๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐฑ์‹ ๋˜์—ˆ๋‹ค.

๊ฐ€์ค‘์น˜๊ฐ€ ๊ฐ€์žฅ ์ ์€ 5๋ฒˆ ์ •์ ์„ ์„ ํƒํ•˜๊ณ , ๊ฐฑ์‹ ํ•  ์ •๋ณด๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ฐฑ์‹ ํ•œ๋‹ค.

๊ทธ ๋‹ค์Œ์€ ๋งˆ์ง€๋ง‰ ์ •์ ์ธ 3๋ฒˆ ์ •์ ์„ ํƒํ•œ๋‹ค.

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ด๋Ÿฌํ•œ ์ˆœ์„œ์™€ ์›๋ฆฌ๋กœ ์ง„ํ–‰ ๋œ๋‹ค.

import heapq
import sys

def dijkstra(start):
    # ์ดˆ๊ธฐ ๋ฐฐ์—ด ์„ค์ •
    distances = {node: sys.maxsize for node in graph}
    # ์‹œ์ž‘ ๋…ธ๋“œ์˜ ๊ฑฐ๋ฆฌ๋Š” 0์œผ๋กœ ์„ค์ •
    distances[start] = 0
    queue = []
    # ์‹œ์ž‘ ๋…ธ๋“œ๋ถ€ํ„ฐ ํƒ์ƒ‰ ์‹œ์ž‘ ํ•˜๊ธฐ ์œ„ํ•จ. (๊ฑฐ๋ฆฌ, ๋…ธ๋“œ) - ๊ฑฐ๋ฆฌ, ๋…ธ๋“œ ์ˆœ์œผ๋กœ ๋„ฃ์€ ์ด์œ ๋Š” heapq ๋ชจ๋“ˆ์— ์ฒซ ๋ฒˆ์งธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ์„ ์ง„ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ (๋…ธ๋“œ, ๊ฑฐ๋ฆฌ) ์ˆœ์œผ๋กœ ๋„ฃ์œผ๋ฉด ์ตœ์†Œ ํž™์ด ์˜ˆ์ƒํ•œ๋Œ€๋กœ ์ •๋ ฌ๋˜์ง€ ์•Š์Œ
    heapq.heappush(queue, (distances[start], start))

    # ์šฐ์„  ์ˆœ์œ„ ํ์— ๋ฐ์ดํ„ฐ๊ฐ€ ํ•˜๋‚˜๋„ ์—†์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
    while queue:
        # ๊ฐ€์žฅ ๋‚ฎ์€ ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐ€์ง„ ๋…ธ๋“œ์™€ ๊ฑฐ๋ฆฌ๋ฅผ ์ถ”์ถœ
        current_distance, node = heapq.heappop(queue)
        # ํŒŒ์ด์ฌ heapq์— (๊ฑฐ๋ฆฌ, ๋…ธ๋“œ) ์ˆœ์œผ๋กœ ๋„ฃ๋‹ค ๋ณด๋‹ˆ๊นŒ ๋™์ผํ•œ ๋…ธ๋“œ๋ผ๋„ ํ์— ์ €์žฅ์ด ๋œ๋‹ค ์˜ˆ์‹œ: queue[(7, 'B'), (10, 'B')]
        # ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ์•„๋ž˜ ์กฐ๊ฑด๋ฌธ์œผ๋กœ ์ด๋ฏธ ๊ณ„์‚ฐ๋˜์–ด ์ €์žฅํ•œ ๊ฑฐ๋ฆฌ์™€ ์ถ”์ถœ๋œ ๊ฑฐ๋ฆฌ์™€ ๋น„๊ตํ•˜์—ฌ ์ €์žฅ๋œ ๊ฑฐ๋ฆฌ๊ฐ€ ๋” ์ž‘๋‹ค๋ฉด ๋น„๊ตํ•˜์ง€ ์•Š๊ณ  ํ์˜ ๋‹ค์Œ ๋ฐ์ดํ„ฐ๋กœ ๋„˜์–ด๊ฐ„๋‹ค.
        if distances[node] < current_distance:
            continue

        # ๋Œ€์ƒ์ธ ๋…ธ๋“œ์—์„œ ์ธ์ ‘ํ•œ ๋…ธ๋“œ์™€ ๊ฑฐ๋ฆฌ๋ฅผ ์ˆœํšŒ
        for adjacency_node, distance in graph[node].items():
            # ํ˜„์žฌ ๋…ธ๋“œ์—์„œ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋ฅผ ์ง€๋‚˜๊ฐˆ ๋•Œ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋”ํ•จ
            weighted_distance = current_distance + distance
            # ๋ฐฐ์—ด์˜ ์ €์žฅ๋œ ๊ฑฐ๋ฆฌ๋ณด๋‹ค ์œ„์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ๋” ์ž‘์œผ๋ฉด ํ•ด๋‹น ๋…ธ๋“œ์˜ ๊ฑฐ๋ฆฌ ๋ณ€๊ฒฝ
            if weighted_distance < distances[adjacency_node]:
                # ๋ฐฐ์—ด์— ์ €์žฅ๋œ ๊ฑฐ๋ฆฌ๋ณด๋‹ค ๊ฐ€์ค‘์น˜๊ฐ€ ๋” ์ž‘์œผ๋ฏ€๋กœ ๋ณ€๊ฒฝ
                distances[adjacency_node] = weighted_distance
                # ๋‹ค์Œ ์ธ์ ‘ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐ ํ•˜๊ธฐ ์œ„ํ•ด ์šฐ์„  ์ˆœ์œ„ ํ์— ์‚ฝ์ž… (๋…ธ๋“œ๊ฐ€ ๋™์ผํ•ด๋„ ์ผ๋‹จ ๋‹ค ์ €์žฅํ•จ)
                heapq.heappush(queue, (weighted_distance, adjacency_node))

    return distances

graph = {
    'A': {'B': 10, 'C': 3},
    'B': {'C': 1, 'D': 2},
    'C': {'B': 4, 'D': 8, 'E': 2},
    'D': {'E': 7},
    'E': {'D': 9},
}

result = dijkstra('A')
print(result)

# {'A': 0, 'B': 7, 'C': 3, 'D': 9, 'E': 5}

#์ถœ์ฒ˜:https://brownbears.tistory.com/554

[์ถœ์ฒ˜]

https://victorydntmd.tistory.com/103

https://mattlee.tistory.com/50