2020-1ํ•™๊ธฐ, ๋Œ€ํ•™์—์„œ โ€˜์•Œ๊ณ ๋ฆฌ์ฆ˜โ€™ ์ˆ˜์—…์„ ๋“ฃ๊ณ  ๊ณต๋ถ€ํ•œ ๋ฐ”๋ฅผ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ์ง€์ ์€ ์–ธ์ œ๋‚˜ ํ™˜์˜์ž…๋‹ˆ๋‹ค :)

4 minute read

2020-1ํ•™๊ธฐ, ๋Œ€ํ•™์—์„œ โ€˜์•Œ๊ณ ๋ฆฌ์ฆ˜โ€™ ์ˆ˜์—…์„ ๋“ฃ๊ณ  ๊ณต๋ถ€ํ•œ ๋ฐ”๋ฅผ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ์ง€์ ์€ ์–ธ์ œ๋‚˜ ํ™˜์˜์ž…๋‹ˆ๋‹ค :)


์šฐ๋ฆฌ๊ฐ€ ์ง€๊ธˆ๊นŒ์ง€ ์‚ดํŽด๋ณธ โ€œShortest Pathโ€ ๋ฌธ์ œ๋Š” ๋ชจ๋‘ ์ถœ๋ฐœ ๋…ธ๋“œ $s$์™€ ๋„์ฐฉ ๋…ธ๋“œ $t$๊ฐ€ ๊ณ ์ •๋œ ๊ฒฝ์šฐ์˜€๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋•Œ๋กœ๋Š” ์ฃผ์–ด์ง€๋Š” ๋…ธ๋“œ ์ฟผ๋ฆฌ $(s, t)$์— ๋Œ€ํ•œ shortest path๋ฅผ ์ฐพ๊ฑฐ๋‚˜ ๋˜๋Š” ๊ทธ๋ž˜ํ”„์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๋…ธ๋“œ ์Œ $(u, v)$ ์‚ฌ์ด์˜ shortest path๋ฅผ ์ฐพ๊ณ  ์‹ถ์„ ์ˆ˜ ์žˆ๋‹ค.

๋งŒ์•ฝ ์ด๊ฒƒ์„ <Dijkstra Algorithm>์„ ์‚ฌ์šฉํ•ด $V$๊ฐœ์˜ ๋…ธ๋“œ์— ๋Œ€ํ•ด์„œ โ€œsingle-source shortest pathโ€๋ฅผ ์ฐพ๊ณ ์ž ํ•œ๋‹ค๋ฉด, ์ด $V \times O(VE) = O(V^2 E)$์˜ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ๋‹ค.

๊ทธ.๋Ÿฌ.๋‚˜. <DP>๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ์ข€๋” ๋น ๋ฅธ ์‹œ๊ฐ„ ์•ˆ์— ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค!! ๐Ÿ˜ ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ <Floyd-Warshall Aglrorithm>๋ผ๊ณ  ํ•œ๋‹ค.


์šฐ๋ฆฌ๋Š” ๋‘ ์ •์  $(i, j)$์„ ์ž‡๋Š” ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฒฝ์œ ์ง€ $k$๋ฅผ ๊ฑฐ์น˜๋Š” ๋ฌธ์ œ๋กœ ์ƒ๊ฐํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค!!

์ฆ‰, ๋‘ ์ •์ ์„ ์ž‡๋Š” ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ $k-1$๊นŒ์ง€์˜ ๋…ธ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์™€ $k$๊นŒ์ง€ ๋…ธ๋“œ๋ฅผ ํฌํ•จํ•ด ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ(pass through $k$)๋กœ ๋‚˜๋ˆ„์–ด ๋‘˜ ์ค‘ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์„ ํƒํ•œ๋‹ค.

์ด๋•Œ, ์ฃผ์˜ํ•  ์ ์€ ์ด๋ ‡๊ฒŒ ๊ฒฝ์œ ์ง€๋ฅผ ๊ฑฐ์ณ๊ฐ€๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„  ๊ฒฝ์œ ํ•˜๋Š” ๊ฒฝ๋กœ๋ฅผ ์ด๋ฃจ๋Š” sub-path ์—ญ์‹œ ์ตœ๋‹จ ๊ฒฝ๋กœ์ž„์ด ๋ณด์žฅ๋˜์–ด์•ผ ํ•œ๋‹ค!

์ด๋ฅผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

// initialize
for all $(i, j) \in E$
โ€ƒโ€ƒ$\text{dist}(i, j, 0) = \ell(i, j)$

for $k=1$ to $n$:
โ€ƒโ€ƒfor $i=1$ to $n$:
โ€ƒโ€ƒโ€ƒโ€ƒfor $j=1$ to $n$:
โ€ƒโ€ƒโ€ƒโ€ƒโ€ƒโ€ƒ$\text{dist}(i, j, k) = \min \{ \text{dist}(i, j, k-1), \; \text{dist}(i, k, k-1) + \text{dist}(k, j, k-1)\}$

์‚ฌ์‹ค ์‹ค์ œ๋กœ ๊ตฌํ˜„์„ ํ•ด๋ณด๋ฉด, 3์ฐจ์› ๋ฐฐ์—ด์ด ์•„๋‹ˆ๋ผ $k$๋ฅผ ์—†์•ค 2์ฐจ์› ๋ฐฐ์—ด๋กœ๋„ ์ถฉ๋ถ„ํžˆ ์ž˜ ๋™์ž‘ํ•œ๋‹ค ๐Ÿ˜‰

์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” ๊ธฐ์กด DFS ๊ธฐ๋ฐ˜์˜ $O(V^2 E)$์—์„œ $O(V^3)$๋กœ ์ค„์–ด๋“ค์—ˆ๋‹ค!! (์ผ๋ฐ˜์ ์œผ๋กœ sparse graph๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด $V < E$์ด๋‹ค.)


๊ตฌํ˜„

  • adjacency matrix๋กœ ์—ฐ๊ฒฐ์„ฑ์„ ํ‘œํ˜„ํ•˜๋Š” ๊ฑธ ์ถ”์ฒœ
  • adjacency matrix ์ดˆ๊ธฐํ™” ์žŠ์ง€ ๋ง๊ฒƒ; fill();
    • ์ดˆ๊ธฐํ™” ํ•  ๋•Œ, INT_MAX์™€ ๊ฐ™์ด ๋งค์šฐ ํฐ์ˆ˜๋กœ ํ•˜๊ฒŒ ๋˜๋ฉด overflow ๋•Œ๋ฌธ์— ์ž˜๋ชป๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ฑ‰์Œ;;
  • ๊ตฌํ˜„ ์ž์ฒด๋Š” ์ •๋ง ๊ฐ„๋‹จํ•จ! LIS, edit distance ๊ธ‰์œผ๋กœ ์‰ฌ์šด ๊ตฌํ˜„!
// initialization 1
fill(&dist[0][0], &dist[N+1][N+1], MAX2);

// initialization 2
for (int i = 0; i < M; i++) {
  int a, b;
  scanf("%d %d", &a, &b);

  dist[a][b] = 1;
  dist[b][a] = 1;
}

// Floyd-Warshall
for (int k = 1; k <= N; ++k) {
  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j <= N; ++j) {
      if(dist[i][k] == MAX2 || dist[k][j] == MAX2) continue;

      dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
    }
  }
}

์ฐธ๊ณ ๋กœ <Flyod-Warshall>์€ ๋…ธ๋“œ์˜ ์ˆ˜๊ฐ€ 10,000๊ฐ€ ๋˜๋ฉด ๋„ˆ๋ฌด ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์“ฐ๊ธฐ ๋•Œ๋ฌธ์— ์“ธ ์ˆ˜ ์—†๋‹ค! ๋ฐฑ์ค€ 1325๋ฒˆ: ํšจ์œจ์ ์ธ ํ•ดํ‚น์ด ๋ฉ”๋ชจ๋ฆฌ ๋•Œ๋ฌธ์— <Flyod-Warshall>์„ ์“ธ ์ˆ˜ ์—†๋Š” ์˜ˆ์ด๋‹ค!

์ถ”์ฒœ ๋ฌธ์ œ

Categories:

Updated: