[Algorithm] 12. Shortest Path (3): APSP & Floyd-Warshall's Algorithm
최단경로 중 APSP 문제에 대한 플로이드 워셜 알고리즘에 대해 다룹니다.
APSP(All Pairs Shortest-Paths) Problem
Problem Definition
Directed graph G=(V, E)가 주어지고, 실수값의 가중치 함수 w가 주어졌을 때,
Vertex 개수가 총 n개이고, 1부터 n까지 각 vertices가 mapping 된다고 가정하고, nxn 행렬 W로 임의의 vertex
Naïve Approach
Floyd-Warshall’s Algorithm
1
2
3
4
5
6
7
8
9
FLOYD-WARSHALL(W)
n = W.rows
D⁽⁰⁾ = W
for k=1 to n
let D⁽ᵏ⁾ = (dᵢⱼ⁽ᵏ⁾) be a new n x n matrix
for i=1 to n
for j=1 to n
dᵢⱼ⁽ᵏ⁾ = min(dᵢⱼ⁽ᵏ⁻¹⁾, dᵢₖ⁽ᵏ⁻¹⁾+dₖⱼ⁽ᵏ⁻¹⁾) // -> O(1)
return D⁽ⁿ⁾
This post is licensed under CC BY 4.0 by the author.