๊ธ€ ์ž‘์„ฑ์ž: ๊ฐœ๋ฐœํ•˜๋Š” ํ›ˆ์ด

์ด์ œ ๋ณธ๊ฒฉ์ ์œผ๋กœ SSP๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ดํŽด๋ณด์ž. ์ฒ˜์Œ์œผ๋กœ ์†Œ๊ฐœํ•  ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ Bellman-Ford ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ์šฐ๋ฆฌ ๋ง๋กœ๋Š” ๋ฒจ๋งŒ-ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ๊ณ  ํ•œ๋‹ค.

Bellman-Ford Algorithm

๋ฒจ๋งŒ-ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํŠน์ง•์€ SSP๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ ‘๊ทผ ์ค‘ ์œ ์ผํ•˜๊ฒŒ negative weight ์ด ์กด์žฌํ•˜๋Š” ๊ทธ๋ž˜ํ”„์—์„œ๋„ ์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ negative weight cycle์ด ์กด์žฌํ•  ๋•Œ๋Š” ํ•ด๋‹ต์„ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค.

๋”ฐ๋ผ์„œ ๋ฒจ๋งŒํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ Negative Cycle์ด ์กด์žฌํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ด์„œ ์กด์žฌํ•  ๋•Œ๋Š” FALSE, Negative Cylcle์ด ์—†์„ ๋•Œ๋Š” TRUE๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

Algorithm Steps

๋ฒจ๋งŒ-ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋‹จ๊ณ„๋ณ„๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ง„ํ–‰ํ•ด๋ณด์ž.

Step 1 : Initialization - ๐›ฉ(# of vertice)

์ผ๋‹จ ํ˜„์žฌ ๊ทธ๋ž˜ํ”„์˜ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค. ์ด ๋•Œ ์‹œ์ž‘์ ์„ ์ œ์™ธํ•œ ๋ชจ๋“  ์ •์ ๋“ค์˜ ์ตœ๋‹จ๊ธธ์ด๋ฅผ ์–‘์˜ ๋ฌดํ•œ๋Œ€๋กœ ๋งŒ๋“ค๊ณ  ๋ชจ๋“  ์ •์ ๋“ค์˜ ๋ถ€๋ชจ๋…ธ๋“œ๋Š” NIL ๋กœ ๋งŒ๋“ ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‹œ์ž‘์ ์˜ ์ตœ๋‹จ๊ธธ์ด๋Š” 0์œผ๋กœ ๋งŒ๋“ ๋‹ค.

for each vertice in V
    do d[v] = INFINITE
        p[v] = NIL
d[s] = 0

Step 2 : Rexation - ๐›ฉ(# of edges)

๋ชจ๋“  ์ •์ ์— ๋Œ€ํ•ด์„œ relaxation์„ ์ง„ํ–‰ํ•œ๋‹ค. ๊ฐ ์ •์ ๋งˆ๋‹ค ์ด์–ด์ง„ ์ •์ ์œผ๋กœ์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ์™€ ๊ทธ ์ •์ ์„ ์ €์žฅํ•œ๋‹ค.

for i = 1 to N(number of edges)
    for each edge(u, v) in set of edges
        if (d[v] > d[u]+w)
            d[v] = d[u]+w
            p[v] = u

Step 3 : Test for Negative Weight Cycle - ๐›ฉ(# of edges)

์ด๋ฏธ Step 2์—์„œ Relaxation์ด ์ง„ํ–‰๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋งŒ์•ฝ์— ํ•œ๋ฒˆ ๋” ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ relaxation์„ ์ˆ˜ํ–‰ํ–ˆ์„ ๋•Œ, ์—…๋ฐ์ดํŠธ๊ฐ€ ๊ฐ€๋Šฅํ•œ ์ •์ ์ด ๋ฐœ๊ฒฌ๋œ๋‹ค๋ฉด, ํ•ด๋‹น ์ •์ ์€ Negative Weight Cycle์„ ๋งŒ๋“œ๋Š” ์ •์ ์ผ ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ false ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

for each edge in set of edges
    if d[v] > d[u] + w(u, b)
        return False

Example

์œ„ ๊ทธ๋ž˜ํ”„๋ฅผ ์˜ˆ์‹œ๋กœ ๋ฒจ๋งŒํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ ์šฉํ•ด๋ณด์ž.

Phase 1

๋จผ์ € ์‹œ์ž‘์ •์ ์„ ์ •ํ•˜๊ณ  ๊ฐ ์ •์ ๋“ค์˜ ๊ฐ’์„ ์ดˆ๊ธฐํ™” ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ์ด ์˜ˆ์‹œ์—์„œ๋Š” ์‹œ์ž‘์ ์„ A๋กœ ์ •ํ•˜๊ฒ ๋‹ค. ๊ทธ๋ฆฌ๊ณ  A๋งŒ ๊ธธ์ด ๋ณ€์ˆ˜๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™” ํ•˜๊ณ  ๋‚˜๋จธ์ง€ ์ •์ ๋“ค์— ๋Œ€ํ•ด์„œ๋Š” ์–‘์˜ ๋ฌดํ•œ๋Œ€๋ฅผ ์ง€์ •ํ•œ๋‹ค.

Phase 2

์‹œ์ž‘์ ์ธ A ์ •์ ์œผ๋กœ๋ถ€ํ„ฐ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๋…ธ๋“œ๋“ค์— ๋Œ€ํ•ด Relaxtion ์„ ์ง„ํ–‰ํ•œ๋‹ค.

  1. B ์ •์ ์— ์ €์žฅ๋œ ์ตœ๋‹จ๊ฑฐ๋ฆฌ ๊ฐ’์€ ์–‘์˜ ๋ฌดํ•œ๋Œ€์˜€๋Š”๋ฐ, A๋กœ๋ถ€ํ„ฐ B๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ์ธ 6์ด ๋” ์ž‘๊ธฐ ๋•Œ๋ฌธ์— d[B]๋Š” 6์ด ๋˜๊ณ , p[B]๋Š” A๊ฐ€ ๋œ๋‹ค. p[v]๋Š” ์ตœ๋‹จ๊ฒฝ๋กœ์—์„œ ์ •์  v ๋ฐ”๋กœ ์ง์ „์— ๋‚˜์˜จ ์ •์ ์„ ์˜๋ฏธํ•œ๋‹ค.
  2. ๊ฐ™์€ ๋งฅ๋ฝ์œผ๋กœ A๋กœ๋ถ€ํ„ฐ D๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•˜๊ฒŒ ๋˜๋ฉด d[D]๋Š” 7์ด ๋œ๋‹ค.

Phase 3

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

  1. B์™€ ์—ฐ๊ฒฐ๋œ C์ •์ ๊นŒ์ง€ ๋„๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” A์—์„œ B๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ์ธ 6์— B์—์„œ C๋กœ ์ด๋™ํ•˜๋Š” ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๋”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ํ˜„ ์‹œ์ ์—์„œ C๊นŒ์ง€ ๋„๋‹ฌํ•˜๋Š” ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋Š” ์–‘์˜ ๋ฌดํ•œ๋Œ€๋กœ ์„ค์ •๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ๊ฐ„์„  BC๋ฅผ ํ†ตํ•ด ์—ฐ๊ฒฐ๋˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ž‘์„ ์ˆ˜ ๋ฐ–์— ์—†๋‹ค. ๋”ฐ๋ผ์„œ d[C]๋Š” 6+5 ์ธ 11์ด ๋œ๋‹ค.
  2. B์™€ ์—ฐ๊ฒฐ๋œ ์ •์  E์— ๋„๋‹ฌํ•˜๋Š” ์ตœ๋‹จ๊ฑฐ๋ฆฌ ์—ญ์‹œ ์œ„์™€ ๊ฐ™์€ ๊ณผ์ •์„ ํ†ตํ•ด 6-2๋กœ 4๊ฐ€ ๋˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
  3. B์™€ ์—ฐ๊ฒฐ๋œ ๋งˆ์ง€๋ง‰ ์ •์  D์— ๋„๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฑฐ๋ฆฌ AB์™€ ๊ฑฐ๋ฆฌ BD๋ฅผ ๋”ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿผ ๊ฑฐ๋ฆฌ๊ฐ€ 6+8=14๊ฐ€ ๋˜๋Š”๋ฐ, ์ด ๊ฑฐ๋ฆฌ๋Š” A์—์„œ ๊ณง๋ฐ”๋กœ D๋กœ ์ด๋™ํ•˜๋Š” ๊ฑฐ๋ฆฌ๋ณด๋‹ค ํฌ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ์œ ์ง€ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— D๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋Š” A์—์„œ ๊ณง์žฅ ์ด๋™ํ•˜๋Š” 7๋กœ ์œ ์ง€ํ•œ๋‹ค.

Phase 4

์ด์ œ ์ •์  D์— ๋Œ€ํ•œ Relaxation์„ ์ง„ํ–‰ํ•ด๋ณด์ž. ์ •์  D๋Š” C์™€ E๋กœ ์—ฐ๊ฒฐ๋œ๋‹ค.

  1. ์ •์  D๋ฅผ ๊ฑฐ์ณ์„œ C๋กœ ์ด๋™ํ•˜๊ฒŒ ๋˜๋ฉด ์ด ๊ฑฐ๋ฆฌ๊ฐ€ 7 - 3 = 4๊ฐ€ ๋œ๋‹ค. ๊ธฐ์กด์— C๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋Š” B๋ฅผ ํ†ตํ•ด๊ฐ€๋Š” 11๋กœ ์„ค์ •๋˜์–ด์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋” ์งง์€ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ d[C]๋ฅผ ์ƒˆ๋กœ์šด ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ํ•ด์ค€๋‹ค.
  2. ์ •์  D๋ฅผ ๊ฑฐ์ณ์„œ ์ •์  E๋กœ ์ด๋™ํ•˜๊ฒŒ ๋˜๋ฉด ์ด ๊ฑฐ๋ฆฌ๊ฐ€ 7 + 9 + 11์ด ๋œ๋‹ค. ์ด ๊ฑฐ๋ฆฌ๋Š” ๊ธฐ์กด์— ์ตœ๋‹จ๊ฑฐ๋ฆฌ์˜€๋˜ ์ •์  B๋ฅผ ๊ฑฐ์ณ์„œ E๋กœ ์ด๋™ํ•˜๋Š” ๊ฑฐ๋ฆฌ๋ณด๋‹ค ์ž‘๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ๋กœ ๊ฐฑ์‹ ํ•˜์ง€ ์•Š๊ณ  ๋„˜์–ด๊ฐ„๋‹ค.

Phase 5

C์— ๋Œ€ํ•œ Relaxation์„ ์ง„ํ–‰ํ•ด๋ณด์ž. ํ˜„์žฌ ์ •์  C๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ ๊ฑฐ์ณ์„œ B๋กœ ์ด๋™ํ•˜๊ฒŒ ๋˜๋ฉด, A์—์„œ ๋ฐ”๋กœ B๋กœ ์ด๋™ํ•˜๋Š” ๊ฑฐ๋ฆฌ๋ณด๋‹ค ์งง๋‹ค. ๋”ฐ๋ผ์„œ B์˜ ์ƒˆ๋กœ์šด ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋Š” 4 - 2 = 2 ๊ฐ€ ๋œ๋‹ค.

Phase 6

E์ •์ ์— ๋Œ€ํ•œ Relaxation์„ ์ง„ํ–‰ํ•˜๋ฉด E๋ฅผ ๊ฑฐ์ณ C๋กœ ๊ฐ€๋Š” ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋Š” 9์ด๊ธฐ ๋•Œ๋ฌธ์— ํ˜„์žฌ C๊นŒ์ง€ ๊ฐ€๋Š” ์ตœ๋‹จ ๊ฒฝ๋กœ๋ณด๋‹ค ํฌ๋‹ค. ๋”ฐ๋ผ์„œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜์ง€ ์•Š๊ณ  ๋๋‚ธ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ•œ๋ฒˆ์˜ relaxation ์„ธํŠธ๊ฐ€ ๋๋‚˜๊ฒŒ ๋œ๋‹ค. ์ด ์ž‘์—…์„ ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ์ œ์™ธํ•œ ๋…ธ๋“œ์˜ ์ˆ˜ ๋งŒํผ ๋ฐ˜๋ณตํ•œ๋‹ค.

Phase 7

relaxation์„ V-1 ๋ฒˆ ๋ฐ˜๋ณตํ•˜๊ฒŒ ๋˜๋ฉด ๋‹ค๋ฅธ ๊ฐ„์„ ๋“ค์˜ ๊ฐ’์€ ๋ณ€ํ•˜์ง€ ์•Š๊ณ  B์—์„œ E๋กœ ๊ฐ€๋Š” ๊ฐ„์„ ์˜ ๊ฐ’์ด ์—…๋ฐ์ดํŠธ ๋œ๋‹ค. ์ด ์ดํ›„๋กœ ๋ชจ๋“  rexlation ๊ฐ’์€ ๋ณ€ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๊ทธ๋ž˜ํ”„ ๋„์‹์€ ์ƒ๋žตํ•˜๊ธฐ๋กœ ํ•˜๊ฒ ๋‹ค.

Phase 8

Relaxation์€ ๋๋‚ฌ์ง€๋งŒ, negative weight cycle์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ ์œ„ ์ž‘์—…์„ ํ•œ๋ฒˆ ๋” ๊ฑฐ์ณ์•ผ ํ•œ๋‹ค. ํ˜„์žฌ ๊ฐ ์ •์ ์— ๋Œ€ํ•œ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ€ ๊ธฐ๋ก๋˜์–ด ์žˆ๋Š”๋ฐ ๋งŒ์•ฝ ์ด ๊ฑฐ๋ฆฌ๊ฐ€ ์ƒˆ๋กœ์šด ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋กœ ๊ฐฑ์‹ ๋  ์ˆ˜ ์žˆ๋‹ค๋ฉด, negative weight cycle์ด ์กด์žฌํ•œ๋‹ค๋Š” ์˜๋ฏธ์ผ ๊ฒƒ์ด๋‹ค. ์ด๋•Œ๋Š” false ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์„œ ์ง€๊ธˆ๊นŒ์ง€ ๊ตฌํ•œ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ€ ์˜๋ฏธ๊ฐ€ ์—†์Œ์„ ์•Œ๋ฆฐ๋‹ค.

Algorithm Analysis

๋ฒจ๋งŒ ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ฐ ๋…ธ๋“œ ์ดˆ๊ธฐํ™”(๐›ฉ(V)) + Rexlaxation์„ v-1๋ฒˆ ๋ฐ˜๋ณต(๐›ฉ(E * (V-1))) + ์Œ์ˆ˜ ์‚ฌ์ดํด ์ฒดํฌ(๐›ฉ(E)) ์˜ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” ๐›ฉ(VE) ๊ฐ€ ๋œ๋‹ค.